From 14141b5a9e44ecc01736ce9fc3e0970be236e79b Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Wed, 23 Jul 2014 21:39:44 +0200 Subject: [PATCH] Add some hackery to work around the problem of textedit losing focus on click --- ui/view.cpp | 20 ++++++++++++++++---- ui/view.h | 5 +++-- ui/viewgroup.cpp | 8 +++++--- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/ui/view.cpp b/ui/view.cpp index 01b6e5ec4f..0b63de5651 100644 --- a/ui/view.cpp +++ b/ui/view.cpp @@ -17,7 +17,7 @@ namespace UI { static View *focusedView; static bool focusMovementEnabled; - +bool focusForced; static recursive_mutex mutex_; const float ITEM_HEIGHT = 64.f; @@ -63,21 +63,25 @@ View *GetFocusedView() { return focusedView; } -void SetFocusedView(View *view) { +void SetFocusedView(View *view, bool force) { if (focusedView) { focusedView->FocusChanged(FF_LOSTFOCUS); } focusedView = view; if (focusedView) { focusedView->FocusChanged(FF_GOTFOCUS); + if (force) { + focusForced = true; + } } } void EnableFocusMovement(bool enable) { focusMovementEnabled = enable; if (!enable) { - if (focusedView) + if (focusedView) { focusedView->FocusChanged(FF_LOSTFOCUS); + } focusedView = 0; } } @@ -619,7 +623,7 @@ void TextEdit::GetContentDimensions(const UIContext &dc, float &w, float &h) con } // Handles both windows and unix line endings. -std::string FirstLine(const std::string &text) { +static std::string FirstLine(const std::string &text) { size_t pos = text.find("\r\n"); if (pos != std::string::npos) { return text.substr(0, pos); @@ -631,6 +635,14 @@ std::string FirstLine(const std::string &text) { return text; } +void TextEdit::Touch(const TouchInput &touch) { + if (touch.flags & TOUCH_DOWN) { + if (bounds_.Contains(touch.x, touch.y)) { + SetFocusedView(this, true); + } + } +} + void TextEdit::Key(const KeyInput &input) { if (!HasFocus()) return; diff --git a/ui/view.h b/ui/view.h index 4b006673e0..1010e99eef 100644 --- a/ui/view.h +++ b/ui/view.h @@ -43,7 +43,7 @@ class View; void EnableFocusMovement(bool enable); bool IsFocusMovementEnabled(); View *GetFocusedView(); -void SetFocusedView(View *view); +void SetFocusedView(View *view, bool force = false); enum DrawableType { DRAW_NOTHING, @@ -674,7 +674,8 @@ public: virtual void GetContentDimensions(const UIContext &dc, float &w, float &h) const override; virtual void Draw(UIContext &dc) override; - virtual void Key(const KeyInput &input) override; + virtual void Key(const KeyInput &key) override; + virtual void Touch(const TouchInput &touch) override; private: void InsertAtCaret(const char *text); diff --git a/ui/viewgroup.cpp b/ui/viewgroup.cpp index b19efb82f4..3d3068efa0 100644 --- a/ui/viewgroup.cpp +++ b/ui/viewgroup.cpp @@ -17,7 +17,7 @@ const float ITEM_HEIGHT = 64.f; static recursive_mutex focusLock; static std::vector focusMoves; - +extern bool focusForced; bool dragCaptured[MAX_POINTERS]; void CaptureDrag(int id) { @@ -1192,9 +1192,11 @@ static void ProcessHeldKeys(ViewGroup *root) { } bool TouchEvent(const TouchInput &touch, ViewGroup *root) { - EnableFocusMovement(false); - + focusForced = false; root->Touch(touch); + if ((touch.flags & TOUCH_DOWN) && !focusForced) { + EnableFocusMovement(false); + } return true; }