#196: Dropdown closes when it is displaced or lost focus 4.x-dev

Sat, 14 Nov 2020 17:38:36 +0200

author
cemkalyoncu
date
Sat, 14 Nov 2020 17:38:36 +0200
branch
4.x-dev
changeset 1487
66f8110078d5
parent 1486
cb932eb6b647
child 1488
82a11d6bd77c

#196: Dropdown closes when it is displaced or lost focus

Source/Gorgon/UI/ComponentStackWidget.h file | annotate | diff | comparison | revisions
Source/Gorgon/UI/Widget.h file | annotate | diff | comparison | revisions
Source/Gorgon/UI/WidgetContainer.cpp file | annotate | diff | comparison | revisions
Source/Gorgon/UI/WidgetContainer.h file | annotate | diff | comparison | revisions
Source/Gorgon/Widgets/Button.h file | annotate | diff | comparison | revisions
Source/Gorgon/Widgets/Checkbox.h file | annotate | diff | comparison | revisions
Source/Gorgon/Widgets/Dropdown.h file | annotate | diff | comparison | revisions
Source/Gorgon/Widgets/Listbox.h file | annotate | diff | comparison | revisions
Source/Gorgon/Widgets/Panel.cpp file | annotate | diff | comparison | revisions
Source/Gorgon/Widgets/Panel.h file | annotate | diff | comparison | revisions
--- a/Source/Gorgon/UI/ComponentStackWidget.h	Sat Nov 14 12:18:17 2020 +0200
+++ b/Source/Gorgon/UI/ComponentStackWidget.h	Sat Nov 14 17:38:36 2020 +0200
@@ -16,8 +16,6 @@
         ComponentStackWidget(const Template &temp, std::map<ComponentTemplate::Tag, std::function<Widget *(const Template &)>> generators = {}) : stack(*new ComponentStack(temp, temp.GetSize(), generators)) { }
         
         ComponentStackWidget(ComponentStackWidget &&) = default;
-        
-        ComponentStackWidget &operator =(ComponentStackWidget &&) = default;
 
         virtual ~ComponentStackWidget() {
             delete &stack;
--- a/Source/Gorgon/UI/Widget.h	Sat Nov 14 12:18:17 2020 +0200
+++ b/Source/Gorgon/UI/Widget.h	Sat Nov 14 17:38:36 2020 +0200
@@ -235,6 +235,11 @@
         
         /// Call this function when the widget bounds is changed
         virtual void boundschanged();
+        
+        /// Call this function when the widget container causes it to be 
+        /// displaced. This function will not be called when the widget is
+        /// moved on parent.
+        virtual void displaced() { }
 
         /// This function is called when the parent's enabled state changes.
         virtual void parentenabledchanged(bool state) { }
--- a/Source/Gorgon/UI/WidgetContainer.cpp	Sat Nov 14 12:18:17 2020 +0200
+++ b/Source/Gorgon/UI/WidgetContainer.cpp	Sat Nov 14 17:38:36 2020 +0200
@@ -467,4 +467,11 @@
         if(ownorganizer)
             delete organizer;
     }
+
+    void WidgetContainer::Displaced() {
+        for(auto &w : widgets) {
+            w.displaced();
+        }
+    }
+
 } }
--- a/Source/Gorgon/UI/WidgetContainer.h	Sat Nov 14 12:18:17 2020 +0200
+++ b/Source/Gorgon/UI/WidgetContainer.h	Sat Nov 14 17:38:36 2020 +0200
@@ -376,6 +376,9 @@
             
             return *organizer;
         }
+        
+        /// Call this function if the container or widgets in it is moved without move function is called.
+        void Displaced();
 
         /// This function should be called whenever a key is pressed or released.
         virtual bool KeyEvent(Input::Key key, float state) { return distributekeyevent(key, state, true); }
--- a/Source/Gorgon/Widgets/Button.h	Sat Nov 14 12:18:17 2020 +0200
+++ b/Source/Gorgon/Widgets/Button.h	Sat Nov 14 17:38:36 2020 +0200
@@ -62,8 +62,6 @@
         template<class F_>
         Button(const UI::Template &temp, const char *text, F_ clickfn) : Button(temp, std::string(text), clickfn) { }
         
-        Button &operator =(Button &&) = default;
-
         virtual ~Button();
         
         /// Changes the text displayed on the button
--- a/Source/Gorgon/Widgets/Checkbox.h	Sat Nov 14 12:18:17 2020 +0200
+++ b/Source/Gorgon/Widgets/Checkbox.h	Sat Nov 14 17:38:36 2020 +0200
@@ -101,8 +101,6 @@
 
         Checkbox(const UI::Template &temp, const char *text, bool state) : Checkbox(temp, std::string(text), state) { }
         
-        Checkbox &operator =(Checkbox &&) = default;
-        
         Checkbox &operator =(bool value) { SetState(value); return *this; }
         
         operator bool() { return GetState(); }
--- a/Source/Gorgon/Widgets/Dropdown.h	Sat Nov 14 12:18:17 2020 +0200
+++ b/Source/Gorgon/Widgets/Dropdown.h	Sat Nov 14 17:38:36 2020 +0200
@@ -41,6 +41,7 @@
             List(list)
         { 
             stack.SetClickEvent([this](auto, auto, auto) {
+                Focus();
                 Toggle();
             });
             list.SetOverscroll(0.5);
@@ -75,6 +76,8 @@
         void Open() {
             if(!HasParent())
                 return;
+            if(opened)
+                return;
             
             auto res = GetParent().RequestExtender(stack);
             
@@ -89,7 +92,7 @@
             res.Extender->Add(list);
             
             stack.AddCondition(UI::ComponentCondition::Opened);
-            
+            stack.SetFrameEvent(std::bind(&DropdownBase::checkfocus, this));
             
             if(refresh) {
                 refresh = false;
@@ -99,8 +102,12 @@
         
         /// Closes the list
         void Close() {
+            if(!opened)
+                return;
+            
             opened = false;
             stack.RemoveCondition(UI::ComponentCondition::Opened);
+            stack.RemoveFrameEvent();
             list.Remove();
         }
         
@@ -122,6 +129,20 @@
         bool IsReversed() const {
             Utils::NotImplemented();
         }
+        
+    protected:
+        virtual void boundschanged() override {
+            Close();
+        }
+        
+        virtual void displaced() override {
+            Close();
+        }
+        
+        void checkfocus() {
+            if(!this->IsFocused())
+                Close();
+        }
     };
     
     /**
--- a/Source/Gorgon/Widgets/Listbox.h	Sat Nov 14 12:18:17 2020 +0200
+++ b/Source/Gorgon/Widgets/Listbox.h	Sat Nov 14 17:38:36 2020 +0200
@@ -1648,6 +1648,8 @@
                     stack.RemoveCondition(UI::ComponentCondition::VScroll);
                 }
             }
+            
+            contents.Displaced();
         }
         
         /// Scrolls the contents of the listbox so that the it will start displaying
--- a/Source/Gorgon/Widgets/Panel.cpp	Sat Nov 14 12:18:17 2020 +0200
+++ b/Source/Gorgon/Widgets/Panel.cpp	Sat Nov 14 17:38:36 2020 +0200
@@ -161,6 +161,12 @@
         childboundschanged(nullptr);
     }
     
+    void Panel::Move(const Geometry::Point &location) { 
+        ComponentStackWidget::Move(location);
+        
+        Displaced();
+    }
+    
     bool Panel::allowfocus() const {
         if(CurrentFocusStrategy() == Deny)
             return false;
@@ -277,6 +283,7 @@
         stack.SetTagLocation(UI::ComponentTemplate::ContentsTag, -cur);
         
         updatebars();
+        Displaced();
         
         if(done == 2) {
             isscrolling = false;
--- a/Source/Gorgon/Widgets/Panel.h	Sat Nov 14 12:18:17 2020 +0200
+++ b/Source/Gorgon/Widgets/Panel.h	Sat Nov 14 17:38:36 2020 +0200
@@ -26,6 +26,10 @@
         
         virtual void Resize(const Geometry::Size &size) override;
         
+        using Widget::Move;
+        
+        virtual void Move(const Geometry::Point &location) override;
+        
 
         virtual bool Activate() override;
 

mercurial