Support ScrollView for focus position

This commit is contained in:
iota97 2021-09-10 02:06:51 +02:00
parent fb5b528b97
commit c94e9ad73c
3 changed files with 29 additions and 2 deletions

View file

@ -4,6 +4,7 @@
#include "Common/UI/Screen.h"
#include "Common/UI/UI.h"
#include "Common/UI/View.h"
#include "Common/UI/ViewGroup.h"
#include "Common/Log.h"
#include "Common/TimeUtil.h"
@ -192,9 +193,11 @@ void ScreenManager::render() {
}
void ScreenManager::getFocusPosition(float &x, float &y, float &z) {
UI::ScrollView::GetLastScrollPosition(x, y);
UI::View *v = UI::GetFocusedView();
x = v ? v->GetBounds().x : 0;
y = v ? v->GetBounds().y : 0;
x += v ? v->GetBounds().x : 0;
y += v ? v->GetBounds().y : 0;
z = stack_.size();
}

View file

@ -1091,11 +1091,25 @@ bool ScrollView::CanScroll() const {
}
}
float ScrollView::lastScrollPosX = 0;
float ScrollView::lastScrollPosY = 0;
ScrollView::~ScrollView() {
lastScrollPosX = 0;
lastScrollPosY = 0;
}
void ScrollView::GetLastScrollPosition(float &x, float &y) {
x = lastScrollPosX;
y = lastScrollPosY;
}
void ScrollView::Update() {
if (visibility_ != V_VISIBLE) {
inertia_ = 0.0f;
}
ViewGroup::Update();
float oldPos = scrollPos_;
Gesture gesture = orientation_ == ORIENT_VERTICAL ? GESTURE_DRAG_VERTICAL : GESTURE_DRAG_HORIZONTAL;
gesture_.UpdateFrame();
@ -1124,6 +1138,9 @@ void ScrollView::Update() {
pull_ = 0.0f;
}
}
if (oldPos != scrollPos_)
orientation_ == ORIENT_HORIZONTAL ? lastScrollPosX = scrollPos_ : lastScrollPosY = scrollPos_;
}
void AnchorLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) {

View file

@ -264,6 +264,7 @@ class ScrollView : public ViewGroup {
public:
ScrollView(Orientation orientation, LayoutParams *layoutParams = 0, bool rememberPosition = false)
: ViewGroup(layoutParams), orientation_(orientation), rememberPosition_(rememberPosition) {}
~ScrollView();
void Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) override;
void Layout() override;
@ -280,6 +281,9 @@ public:
bool CanScroll() const;
void Update() override;
// Get the last moved scroll view position
static void GetLastScrollPosition(float &x, float &y);
// Override so that we can scroll to the active one after moving the focus.
bool SubviewFocused(View *view) override;
void PersistData(PersistStatus status, std::string anonId, PersistMap &storage) override;
@ -306,6 +310,9 @@ private:
float lastViewSize_ = 0.0f;
bool scrollToTopOnSizeChange_ = false;
bool rememberPosition_;
static float lastScrollPosX;
static float lastScrollPosY;
};
class ViewPager : public ScrollView {