#196 Dropdown can now open above 4.x-dev

Sun, 15 Nov 2020 18:08:43 +0200

author
cemkalyoncu
date
Sun, 15 Nov 2020 18:08:43 +0200
branch
4.x-dev
changeset 1488
82a11d6bd77c
parent 1487
66f8110078d5
child 1489
74f95fea7d11

#196 Dropdown can now open above
* List organizer ignores floating widgets

Source/Gorgon/UI/Widget.h file | annotate | diff | comparison | revisions
Source/Gorgon/Widgets/Dropdown.h file | annotate | diff | comparison | revisions
Testing/Source/Manual/UI_Generate.cpp file | annotate | diff | comparison | revisions
--- a/Source/Gorgon/UI/Widget.h	Sat Nov 14 17:38:36 2020 +0200
+++ b/Source/Gorgon/UI/Widget.h	Sun Nov 15 18:08:43 2020 +0200
@@ -145,7 +145,7 @@
         
         /// Sets floating status of this widget. Floating widgets will not
         /// be moved or resized by organizers.
-        virtual void SetIsFloating(bool value) {
+        virtual void SetFloating(bool value) {
             if(floating != value)
                 boundschanged();
                 
--- a/Source/Gorgon/Widgets/Dropdown.h	Sat Nov 14 17:38:36 2020 +0200
+++ b/Source/Gorgon/Widgets/Dropdown.h	Sun Nov 15 18:08:43 2020 +0200
@@ -29,6 +29,15 @@
             DropdownBase(Registry::Active()[type])
         { }
         
+        template <class I_>
+        DropdownBase(const I_ &begin, const I_ &end, Registry::TemplateType type = Registry::Dropdown_Regular) :
+            DropdownBase(Registry::Active()[type])
+        {
+            for(auto it = begin; it != end; ++it) {
+                this->list.Add(*it);
+            }
+        }
+        
         explicit DropdownBase(const UI::Template &temp) :
             ComponentStackWidget(temp, {
                 {UI::ComponentTemplate::ListTag, {}}
@@ -45,6 +54,7 @@
                 Toggle();
             });
             list.SetOverscroll(0.5);
+            defaultheight = list.GetHeight();
         }
         
         template <class ...A_>
@@ -86,9 +96,27 @@
             
             opened = true;
             
-            list.Move(res.CoordinatesInExtender.X, res.CoordinatesInExtender.Y + GetHeight());
+            int below = res.TotalSize.Height-res.CoordinatesInExtender.Y-GetHeight();
+            int above = res.CoordinatesInExtender.Y;
+            reversed  = false;
+            
+            if(below < defaultheight && above > below) {
+                bool fit = list.FitHeight(below);
+                if(!fit) {
+                    list.FitHeight(above);
+                    reversed = true;
+                }
+            }
+            
+            if(reversed) {
+                list.Move(res.CoordinatesInExtender.X, res.CoordinatesInExtender.Y - list.GetHeight());
+            }
+            else {
+                list.Move(res.CoordinatesInExtender.X, res.CoordinatesInExtender.Y + GetHeight());
+            }
+            
             list.SetWidth(GetWidth());
-            list.FitHeight(res.TotalSize.Height-list.GetLocation().Y); 
+            list.SetFloating(true);
             res.Extender->Add(list);
             
             stack.AddCondition(UI::ComponentCondition::Opened);
@@ -125,9 +153,10 @@
         }
         
         /// Retuns if the list will be opened above the dropdown instead of
-        /// below. This can happen if there is not enough space below.
+        /// below. This can happen if there is not enough space below. Currently
+        /// this function will return false if the dropdown is not open.
         bool IsReversed() const {
-            Utils::NotImplemented();
+            return opened && reversed;
         }
         
     protected:
@@ -143,6 +172,9 @@
             if(!this->IsFocused())
                 Close();
         }
+        
+        bool reversed = false;
+        int defaultheight;
     };
     
     /**
@@ -187,6 +219,15 @@
             });
         }
         
+        template <class I_>
+        SingleSelectionDropdown(const I_ &begin, const I_ &end, Registry::TemplateType type = Registry::Dropdown_Regular) :
+            SingleSelectionDropdown(Registry::Active()[type])
+        {
+            for(auto it = begin; it != end; ++it) {
+                this->list.Add(*it);
+            }
+        }
+        
         template <class ...A_>
         explicit SingleSelectionDropdown(const UI::Template &temp, A_&& ... elms) : SingleSelectionDropdown(temp)
         {
--- a/Testing/Source/Manual/UI_Generate.cpp	Sat Nov 14 17:38:36 2020 +0200
+++ b/Testing/Source/Manual/UI_Generate.cpp	Sun Nov 15 18:08:43 2020 +0200
@@ -22,6 +22,26 @@
 #include <Gorgon/Graphics/TintedObject.h>
 #include <Gorgon/Widgets/Dropdown.h>
 
+enum DaysOfWeek {
+    Monday,
+    Tuesday,
+    Wednesday,
+    Thursday,
+    Friday,
+    Saturday,
+    Sunday
+};
+
+DefineEnumStrings(DaysOfWeek, {
+    {Monday, "Monday"},
+    {Tuesday, "Tuesday"},
+    {Wednesday, "Wednesday"},
+    {Thursday, "Thursday"},
+    {Friday, "Friday"},
+    {Saturday, "Saturday"},
+    {Sunday, "Sunday"}
+});
+
 
 std::string helptext =
     "Key list:\n"
@@ -64,7 +84,7 @@
     generator.Activate();*/
 
     Widgets::Panel blank/*(Gorgon::Widgets::Registry::Panel_Blank)*/;
-    blank.Move(5, 10);
+    blank.Move(5, 200);
     blank.SetHeight(300);
     Gorgon::Widgets::Button btn("Save Âj",Gorgon::Widgets::Registry::Button_Regular);
     Gorgon::Widgets::Button icnbtn("+", Gorgon::Widgets::Registry::Button_Icon);
@@ -199,9 +219,7 @@
     list.EnsureVisible(11);
     list.EnsureVisible(1);
     
-    Widgets::DropdownList<std::string> dlist;
-    dlist.List.Add("Hello");
-    dlist.List.Add("Welcome", "to", "the", "wonderful", "Gorgon", "Library", "!");
+    Widgets::DropdownList<DaysOfWeek> dlist(begin(Enumerate<DaysOfWeek>()), end(Enumerate<DaysOfWeek>()));
     dlist.List.SetSelectedIndex(1);
     dlist.SelectionChanged.Register([](long index) {
         std::cout << "Dropdown index: " << index << std::endl;
@@ -211,8 +229,8 @@
     addme(blank, btn);
     addme(blank, icnbtn);
     addme(blank, icnbtn2);
+    addme(blank, list);
     addme(blank, dlist);
-    addme(blank, list);
     addme(blank, icnbtn3);
     addme(blank, l);
     addme(blank, radio);

mercurial