]> git.tdb.fi Git - libs/gltk.git/commitdiff
Improve list focus management
authorMikko Rasa <tdb@tdb.fi>
Thu, 1 Sep 2016 16:42:41 +0000 (19:42 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 1 Sep 2016 16:42:41 +0000 (19:42 +0300)
Now that containers no longer get an implicit navigation event when
focused, list must take care of focusing the correct item itself.

source/list.cpp
source/list.h

index 6496018380ffbb12411d4a24eb26caa50c9995cd..a5928ec91a76793d453ef67cba2bd062d89165be 100644 (file)
@@ -167,7 +167,8 @@ void List::set_selected_index(int i)
                sel_index = i;
                focus_index = i;
                items[sel_index]->set_active(true);
-               set_input_focus(items[focus_index]);
+               if(state&FOCUS)
+                       set_input_focus(items[focus_index]);
                signal_item_selected.emit(sel_index);
        }
 }
@@ -256,41 +257,28 @@ void List::button_press(int x, int y, unsigned btn)
 
 void List::focus_in()
 {
-       if(focus_index<0)
-               focus_index = sel_index;
-
-       if(focus_index>=0)
-       {
-               scroll_to_focus();
+       if(focus_index>=0 && items[focus_index]->is_visible())
                set_input_focus(items[focus_index]);
+       else
+       {
+               if(sel_index>=0 && items[sel_index]->is_visible())
+                       set_focus_index(sel_index);
+               else if(!items.empty())
+                       set_focus_index(first);
        }
 }
 
 bool List::navigate(Navigation nav)
 {
-       if(nav==NAV_UP)
+       if(nav==NAV_UP && !items.empty())
        {
-               if(focus_index<0 && !items.empty())
-                       focus_index = items.size()-1;
-               else if(focus_index>0)
-                       --focus_index;
-               else
-                       return false;
-
-               scroll_to_focus();
-               set_input_focus(items[focus_index]);
+               if(focus_index>0)
+                       set_focus_index(focus_index-1);
        }
-       else if(nav==NAV_DOWN)
+       else if(nav==NAV_DOWN && !items.empty())
        {
-               if(focus_index<0 && !items.empty())
-                       focus_index = 0;
-               else if(static_cast<unsigned>(focus_index+1)<items.size())
-                       ++focus_index;
-               else
-                       return false;
-
-               scroll_to_focus();
-               set_input_focus(items[focus_index]);
+               if(static_cast<unsigned>(focus_index+1)<items.size())
+                       set_focus_index(focus_index+1);
        }
        else if(nav==NAV_ACTIVATE)
                set_selected_index(focus_index);
@@ -300,6 +288,17 @@ bool List::navigate(Navigation nav)
        return true;
 }
 
+void List::set_focus_index(int i)
+{
+       focus_index = i;
+       if(focus_index>=0)
+       {
+               scroll_to_focus();
+               if(state&FOCUS)
+                       set_input_focus(items[focus_index]);
+       }
+}
+
 void List::item_autosize_changed(Item *item)
 {
        item->autosize();
index 78954fc0fcc60ebf2fa2d2ab68783f7f61ef03bc..5a95b085ae64c9d22837fea608d65af988135578 100644 (file)
@@ -193,8 +193,9 @@ public:
        virtual void button_press(int, int, unsigned);
        virtual void focus_in();
        virtual bool navigate(Navigation);
-
 private:
+       void set_focus_index(int);
+
        void item_autosize_changed(Item *);
        unsigned last_to_first(unsigned) const;
        void check_view_range();