From f26d682e14f450cf87b2acb4b094e93c2490c510 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 23 Jan 2016 01:59:25 -0800 Subject: [PATCH] UI: Persist the last focused view on resize. --- ext/native/ui/ui_screen.cpp | 6 +++--- ext/native/ui/view.cpp | 17 +++++++++++++++++ ext/native/ui/view.h | 2 +- ext/native/ui/viewgroup.cpp | 28 ++++++++++++++++++---------- ext/native/ui/viewgroup.h | 6 +++--- 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/ext/native/ui/ui_screen.cpp b/ext/native/ui/ui_screen.cpp index 57af678ddd..41fd375630 100644 --- a/ext/native/ui/ui_screen.cpp +++ b/ext/native/ui/ui_screen.cpp @@ -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); } } } diff --git a/ext/native/ui/view.cpp b/ext/native/ui/view.cpp index 4ec1c965f3..68a9304936 100644 --- a/ext/native/ui/view.cpp +++ b/ext/native/ui/view.cpp @@ -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. diff --git a/ext/native/ui/view.h b/ext/native/ui/view.h index 4b4cbb32d4..87de3297ea 100644 --- a/ext/native/ui/view.h +++ b/ext/native/ui/view.h @@ -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; diff --git a/ext/native/ui/viewgroup.cpp b/ext/native/ui/viewgroup.cpp index 3140141ac1..61f0e13d66 100644 --- a/ext/native/ui/viewgroup.cpp +++ b/ext/native/ui/viewgroup.cpp @@ -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]; diff --git a/ext/native/ui/viewgroup.h b/ext/native/ui/viewgroup.h index a178a94f55..b5591cd129 100644 --- a/ext/native/ui/viewgroup.h +++ b/ext/native/ui/viewgroup.h @@ -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);