UI: Persist the last focused view on resize.

This commit is contained in:
Unknown W. Brackets 2016-01-23 01:59:25 -08:00
parent e866d83547
commit f26d682e14
5 changed files with 42 additions and 17 deletions

View file

@ -23,7 +23,7 @@ void UIScreen::DoRecreateViews() {
UI::PersistMap persisted;
bool persisting = root_ != nullptr;
if (persisting) {
root_->PersistData(UI::PERSIST_SAVE, persisted);
root_->PersistData(UI::PERSIST_SAVE, "root", persisted);
}
delete root_;
@ -34,8 +34,8 @@ void UIScreen::DoRecreateViews() {
}
recreateViews_ = false;
if (persisting) {
root_->PersistData(UI::PERSIST_RESTORE, persisted);
if (persisting && root_ != nullptr) {
root_->PersistData(UI::PERSIST_RESTORE, "root", persisted);
}
}
}

View file

@ -164,6 +164,23 @@ std::string View::Describe() const {
}
void View::PersistData(PersistStatus status, std::string anonId, PersistMap &storage) {
// Remember if this view was a focused view.
const std::string focusedKey = "ViewFocused::" + anonId;
switch (status) {
case UI::PERSIST_SAVE:
if (HasFocus()) {
storage[focusedKey].resize(1);
}
break;
case UI::PERSIST_RESTORE:
if (storage.find(focusedKey) != storage.end()) {
SetFocus();
}
break;
}
}
Point View::GetFocusPosition(FocusDirection dir) {
// The +2/-2 is some extra fudge factor to cover for views sitting right next to each other.
// Distance zero yields strange results otherwise.

View file

@ -346,7 +346,7 @@ public:
virtual std::string Describe() const;
virtual void FocusChanged(int focusFlags) {}
virtual void PersistData(PersistStatus status, PersistMap &storage) {}
virtual void PersistData(PersistStatus status, std::string anonId, PersistMap &storage);
void Move(Bounds bounds) {
bounds_ = bounds;

View file

@ -3,6 +3,7 @@
#include "base/functional.h"
#include "base/logging.h"
#include "base/mutex.h"
#include "base/stringutil.h"
#include "base/timeutil.h"
#include "input/keycodes.h"
#include "ui/ui_context.h"
@ -75,10 +76,17 @@ void ViewGroup::Clear() {
views_.clear();
}
void ViewGroup::PersistData(PersistStatus status, PersistMap &storage) {
void ViewGroup::PersistData(PersistStatus status, std::string anonId, PersistMap &storage) {
lock_guard guard(modifyLock_);
std::string tag = Tag();
if (tag.empty()) {
tag = anonId;
}
ITOA stringify;
for (size_t i = 0; i < views_.size(); i++) {
views_[i]->PersistData(status, storage);
views_[i]->PersistData(status, tag + "/" + stringify.p((int)i), storage);
}
}
@ -812,12 +820,12 @@ bool ScrollView::SubviewFocused(View *view) {
return true;
}
void ScrollView::PersistData(PersistStatus status, PersistMap &storage) {
ViewGroup::PersistData(status, storage);
void ScrollView::PersistData(PersistStatus status, std::string anonId, PersistMap &storage) {
ViewGroup::PersistData(status, anonId, storage);
const std::string &tag = Tag();
std::string tag = Tag();
if (tag.empty()) {
return;
tag = anonId;
}
PersistBuffer &buffer = storage["ScrollView::" + tag];
@ -1084,12 +1092,12 @@ EventReturn TabHolder::OnTabClick(EventParams &e) {
return EVENT_DONE;
}
void TabHolder::PersistData(PersistStatus status, PersistMap &storage) {
ViewGroup::PersistData(status, storage);
void TabHolder::PersistData(PersistStatus status, std::string anonId, PersistMap &storage) {
ViewGroup::PersistData(status, anonId, storage);
const std::string &tag = Tag();
std::string tag = Tag();
if (tag.empty()) {
return;
tag = anonId;
}
PersistBuffer &buffer = storage["TabHolder::" + tag];

View file

@ -64,7 +64,7 @@ public:
virtual void SetBG(const Drawable &bg) { bg_ = bg; }
virtual void Clear();
void PersistData(PersistStatus status, PersistMap &storage) override;
void PersistData(PersistStatus status, std::string anonId, PersistMap &storage) override;
View *GetViewByIndex(int index) { return views_[index]; }
int GetNumSubviews() const { return (int)views_.size(); }
void SetHasDropShadow(bool has) { hasDropShadow_ = has; }
@ -247,7 +247,7 @@ public:
// Override so that we can scroll to the active one after moving the focus.
bool SubviewFocused(View *view) override;
void PersistData(PersistStatus status, PersistMap &storage) override;
void PersistData(PersistStatus status, std::string anonId, PersistMap &storage) override;
// Quick hack to prevent scrolling to top in some lists
void SetScrollToTop(bool t) { scrollToTopOnSizeChange_ = t; }
@ -325,7 +325,7 @@ public:
int GetCurrentTab() const { return currentTab_; }
std::string Describe() const override { return "TabHolder: " + View::Describe(); }
void PersistData(PersistStatus status, PersistMap &storage) override;
void PersistData(PersistStatus status, std::string anonId, PersistMap &storage) override;
private:
EventReturn OnTabClick(EventParams &e);