From 78aae01cd4bfdfce5e3a74b34afe0f70cca36859 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Fri, 22 Jan 2016 23:02:44 -0800 Subject: [PATCH] UI: Allow scroll views to persist their position. It'll be clamped as necessary. --- ext/native/ui/viewgroup.cpp | 34 +++++++++++++++++++++++++++++++++- ext/native/ui/viewgroup.h | 4 +++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/ext/native/ui/viewgroup.cpp b/ext/native/ui/viewgroup.cpp index 5ae3114432..3140141ac1 100644 --- a/ext/native/ui/viewgroup.cpp +++ b/ext/native/ui/viewgroup.cpp @@ -812,6 +812,37 @@ bool ScrollView::SubviewFocused(View *view) { return true; } +void ScrollView::PersistData(PersistStatus status, PersistMap &storage) { + ViewGroup::PersistData(status, storage); + + const std::string &tag = Tag(); + if (tag.empty()) { + return; + } + + PersistBuffer &buffer = storage["ScrollView::" + tag]; + switch (status) { + case PERSIST_SAVE: + { + buffer.resize(1); + float pos = scrollToTarget_ ? scrollTarget_ : scrollPos_; + // Hmm, ugly... better buffer? + buffer[0] = *(int *)&pos; + } + break; + + case PERSIST_RESTORE: + if (buffer.size() == 1) { + float pos = *(float *)&buffer[0]; + ClampScrollPos(pos); + scrollPos_ = pos; + scrollTarget_ = pos; + scrollToTarget_ = false; + } + break; + } +} + void ScrollView::ScrollTo(float newScrollPos) { scrollTarget_ = newScrollPos; scrollToTarget_ = true; @@ -1054,8 +1085,9 @@ EventReturn TabHolder::OnTabClick(EventParams &e) { } void TabHolder::PersistData(PersistStatus status, PersistMap &storage) { - const std::string &tag = Tag(); + ViewGroup::PersistData(status, storage); + const std::string &tag = Tag(); if (tag.empty()) { return; } diff --git a/ext/native/ui/viewgroup.h b/ext/native/ui/viewgroup.h index 3b432901cf..a178a94f55 100644 --- a/ext/native/ui/viewgroup.h +++ b/ext/native/ui/viewgroup.h @@ -245,7 +245,9 @@ public: void Update(const InputState &input_state) override; // Override so that we can scroll to the active one after moving the focus. - virtual bool SubviewFocused(View *view) override; + bool SubviewFocused(View *view) override; + + void PersistData(PersistStatus status, PersistMap &storage) override; // Quick hack to prevent scrolling to top in some lists void SetScrollToTop(bool t) { scrollToTopOnSizeChange_ = t; }