[blam/gtk3] ItemList: go to next unread on 'N'



commit 531cfea9fc1c43fb4caf038c15efa5695d017c5e
Author: Carlos Martín Nieto <cmn dwim me>
Date:   Sun Oct 13 16:44:40 2013 +0200

    ItemList: go to next unread on 'N'

 src/Blam.cs     |    5 +++++
 src/ItemList.cs |   44 ++++++++++++++++++++++++++++++++++++++++++++
 src/blam.ui     |   16 ++++++++++++++++
 3 files changed, 65 insertions(+), 0 deletions(-)
---
diff --git a/src/Blam.cs b/src/Blam.cs
index c46d435..9363bab 100644
--- a/src/Blam.cs
+++ b/src/Blam.cs
@@ -117,6 +117,11 @@ namespace Blam
                                ItemListSw.Visible = !ItemListSw.Visible;
                        });
 
+                       item = bld.GetObject<MenuItem>("menu-next-unread");
+                       item.ObserveActivated().Subscribe(obj => {
+                               ItemList.Next();
+                       });
+
                        item = bld.GetObject<MenuItem>("menu-about");
                        Observable.Merge(
                                item.ObserveActivated().Select(_ => Unit.Default),
diff --git a/src/ItemList.cs b/src/ItemList.cs
index 4ecf1f9..370e726 100644
--- a/src/ItemList.cs
+++ b/src/ItemList.cs
@@ -123,7 +123,51 @@ namespace Blam
                        }
                }
 
+               bool loopItems(ITreeModel model, ref TreeIter iter, Func<Item, bool> fn)
+               {
+                       var start = iter;
+                       do {
+                               Item item = (Item)model.GetValue(iter, 0);
+                               if (item != null && fn(item))
+                                       return true;
+
+                               if (!Model.IterNext(ref iter))
+                                       Model.GetIterFirst(out iter);
+                       } while (!iter.Equals(start));
 
+                       return false;
+               }
+
+               public bool Next(bool unread = true)
+               {
+                       ITreeModel model;
+                       TreeIter  iter;
+
+                       // If there's something selected (and it's not the last one),
+                       // start looking from there. Otherwise from the start.
+                       if (!Selection.GetSelected(out model, out iter) || !Model.IterNext(ref iter))
+                               ((ListStore)Model).GetIterFirst(out iter);
+
+                       Func<Item, bool> check;
+                       if (unread)
+                               check = item => item.Unread;
+                       else
+                               check = (_) => true;
+
+                       if (loopItems(model, ref iter, check)) {
+                               scrollTo(iter);
+                               return true;
+                       }
+
+                       return false;
+               }
+
+               void scrollTo(TreeIter iter)
+               {
+                       GrabFocus();
+                       ScrollToCell(this.Model.GetPath(iter), column, false, 0, 0);
+                       SetCursor(this.Model.GetPath(iter), column, false);
+               }
        }
 }
 
diff --git a/src/blam.ui b/src/blam.ui
index a74580c..e840f54 100644
--- a/src/blam.ui
+++ b/src/blam.ui
@@ -6,6 +6,11 @@
     <property name="can_focus">False</property>
     <property name="stock">gtk-missing-image</property>
   </object>
+  <object class="GtkImage" id="image2">
+    <property name="visible">True</property>
+    <property name="can_focus">False</property>
+    <property name="stock">gtk-missing-image</property>
+  </object>
   <object class="GtkWindow" id="main-window">
     <property name="can_focus">False</property>
     <child>
@@ -74,6 +79,17 @@
                         <accelerator key="i" signal="activate"/>
                       </object>
                     </child>
+                    <child>
+                      <object class="GtkImageMenuItem" id="menu-next-unread">
+                        <property name="label" translatable="yes">_Next Unread</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="use_underline">True</property>
+                        <property name="image">image2</property>
+                        <property name="use_stock">False</property>
+                        <accelerator key="n" signal="activate"/>
+                      </object>
+                    </child>
                   </object>
                 </child>
               </object>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]