]> git.tdb.fi Git - libs/gltk.git/commitdiff
Refactor the traversal logic out of List::check_view_range
authorMikko Rasa <tdb@tdb.fi>
Wed, 31 Aug 2016 08:17:33 +0000 (11:17 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 31 Aug 2016 08:17:33 +0000 (11:17 +0300)
It'll be useful for other things as well.

source/list.cpp
source/list.h

index 0f887f3a6641316a6610da9f403a4bc5abefb7f4..b9713422f11236fa86bc23a87a64f59523365569 100644 (file)
@@ -254,29 +254,39 @@ void List::item_autosize_changed(Item *item)
        rebuild();
 }
 
-void List::check_view_range()
+unsigned List::last_to_first(unsigned last) const
 {
        if(!style)
-               return;
+               return last;
 
-       unsigned h = geom.h;
+       unsigned view_h = geom.h;
        if(const Part *items_part = style->get_part("items"))
        {
                const Sides &margin = items_part->get_margin();
-               h -= margin.top+margin.bottom;
+               view_h -= margin.top+margin.bottom;
        }
 
-       max_scroll = items.size();
-       for(unsigned i=items.size(); i-->0; )
+       unsigned items_h = 0;
+       for(unsigned i=last; i<items.size(); --i)
        {
-               unsigned ih = items[i]->get_geometry().h;
-               if(ih<=h)
-               {
-                       h -= ih;
-                       --max_scroll;
-               }
+               items_h += items[i]->get_geometry().h;
+               if(items_h>view_h)
+                       return min(i+1, last);
        }
 
+       return 0;
+}
+
+void List::check_view_range()
+{
+       if(!style)
+               return;
+
+       if(items.empty())
+               max_scroll = 0;
+       else
+               max_scroll = last_to_first(items.size()-1);
+
        if(first>max_scroll)
                first = max_scroll;
 
index 246a7294d6da99e457d81d6f0e529bb78e4fbf67..ad12786bc7d962f3c43f0d06258781b60ae1f129 100644 (file)
@@ -193,6 +193,7 @@ public:
 
 private:
        void item_autosize_changed(Item *);
+       unsigned last_to_first(unsigned) const;
        void check_view_range();
        void slider_value_changed(double);
 };