From f6772ec526da177408eaba76eb722ede894ebcdd Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Fri, 22 Jan 2016 22:40:16 -0800 Subject: [PATCH] UI: Add a simple means of persisting data. --- ext/native/ui/ui_screen.cpp | 13 ++++++++++++- ext/native/ui/view.h | 12 ++++++++++-- ext/native/ui/viewgroup.cpp | 9 ++++++++- ext/native/ui/viewgroup.h | 1 + 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/ext/native/ui/ui_screen.cpp b/ext/native/ui/ui_screen.cpp index 88ba27eca2..57af678ddd 100644 --- a/ext/native/ui/ui_screen.cpp +++ b/ext/native/ui/ui_screen.cpp @@ -1,3 +1,4 @@ +#include #include "base/display.h" #include "input/input_state.h" #include "input/keycodes.h" @@ -19,13 +20,23 @@ UIScreen::~UIScreen() { void UIScreen::DoRecreateViews() { if (recreateViews_) { + UI::PersistMap persisted; + bool persisting = root_ != nullptr; + if (persisting) { + root_->PersistData(UI::PERSIST_SAVE, persisted); + } + delete root_; - root_ = 0; + root_ = nullptr; CreateViews(); if (root_ && root_->GetDefaultFocusView()) { root_->GetDefaultFocusView()->SetFocus(); } recreateViews_ = false; + + if (persisting) { + root_->PersistData(UI::PERSIST_RESTORE, persisted); + } } } diff --git a/ext/native/ui/view.h b/ext/native/ui/view.h index 952e3d9c29..4b4cbb32d4 100644 --- a/ext/native/ui/view.h +++ b/ext/native/ui/view.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -203,9 +204,15 @@ enum FocusFlags { FF_GOTFOCUS = 2 }; -class ViewGroup; +enum PersistStatus { + PERSIST_SAVE, + PERSIST_RESTORE, +}; -void Fill(UIContext &dc, const Bounds &bounds, const Drawable &drawable); +typedef std::vector PersistBuffer; +typedef std::map PersistMap; + +class ViewGroup; struct MeasureSpec { MeasureSpec(MeasureSpecType t, float s = 0.0f) : type(t), size(s) {} @@ -339,6 +346,7 @@ public: virtual std::string Describe() const; virtual void FocusChanged(int focusFlags) {} + virtual void PersistData(PersistStatus status, PersistMap &storage) {} void Move(Bounds bounds) { bounds_ = bounds; diff --git a/ext/native/ui/viewgroup.cpp b/ext/native/ui/viewgroup.cpp index 948852bd81..a7011cd5a3 100644 --- a/ext/native/ui/viewgroup.cpp +++ b/ext/native/ui/viewgroup.cpp @@ -70,11 +70,18 @@ void ViewGroup::Clear() { lock_guard guard(modifyLock_); for (size_t i = 0; i < views_.size(); i++) { delete views_[i]; - views_[i] = 0; + views_[i] = nullptr; } views_.clear(); } +void ViewGroup::PersistData(PersistStatus status, PersistMap &storage) { + lock_guard guard(modifyLock_); + for (size_t i = 0; i < views_.size(); i++) { + views_[i]->PersistData(status, storage); + } +} + void ViewGroup::Touch(const TouchInput &input) { lock_guard guard(modifyLock_); for (auto iter = views_.begin(); iter != views_.end(); ++iter) { diff --git a/ext/native/ui/viewgroup.h b/ext/native/ui/viewgroup.h index 833c937ef4..da85e8a1e7 100644 --- a/ext/native/ui/viewgroup.h +++ b/ext/native/ui/viewgroup.h @@ -64,6 +64,7 @@ public: virtual void SetBG(const Drawable &bg) { bg_ = bg; } virtual void Clear(); + void PersistData(PersistStatus status, PersistMap &storage) override; View *GetViewByIndex(int index) { return views_[index]; } int GetNumSubviews() const { return (int)views_.size(); } void SetHasDropShadow(bool has) { hasDropShadow_ = has; }