[blam] Make 'j' go to the next item



commit de656b20bc926460d33286ef48646792480ceb7b
Author: Carlos MartÃn Nieto <cmn dwim me>
Date:   Tue Jan 15 22:57:00 2013 +0100

    Make 'j' go to the next item

 src/Application.cs |    6 +++-
 src/ChannelList.cs |    2 +-
 src/ItemList.cs    |   82 ++++++++++++++++++++++++++-------------------------
 3 files changed, 48 insertions(+), 42 deletions(-)
---
diff --git a/src/Application.cs b/src/Application.cs
index 8124614..96f5042 100644
--- a/src/Application.cs
+++ b/src/Application.cs
@@ -411,7 +411,7 @@ namespace Imendio.Blam {
         public void NextUnreadActivated(object obj, EventArgs args)
         {
             if (channelList.NextUnread()) {
-                itemList.NextUnread();
+                itemList.Next(true);
             }
         }
 
@@ -423,6 +423,10 @@ namespace Imendio.Blam {
                 // Couldn't figure out how to get this to the menu item itself.
                 nextUnreadMenuItem.Activate ();
                 break;
+                case Gdk.Key.j:
+                case Gdk.Key.J:
+                    itemList.Next(false);
+                    break;
             case (Gdk.Key.Escape):
                 mainWindow.Hide ();
                 trayIcon.Show();
diff --git a/src/ChannelList.cs b/src/ChannelList.cs
index 31c9493..2a9a51e 100644
--- a/src/ChannelList.cs
+++ b/src/ChannelList.cs
@@ -15,7 +15,7 @@ using Mono.Unix;
 
 namespace Imendio.Blam {
 
-    public class ChannelList : Gtk.TreeView {
+	public class ChannelList : Gtk.TreeView {
 
         // Events
         public event ChannelEventHandler ChannelSelectedEvent;
diff --git a/src/ItemList.cs b/src/ItemList.cs
index bb82a46..84bf841 100644
--- a/src/ItemList.cs
+++ b/src/ItemList.cs
@@ -16,7 +16,7 @@ using System.ServiceModel.Syndication;
 
 namespace Imendio.Blam {
     
-    public class ItemList : Gtk.TreeView {
+	public class ItemList : Gtk.TreeView {
 
 	public delegate void ItemSelectedHandler(Imendio.Blam.Item item);
 	public event ItemSelectedHandler ItemSelected;
@@ -74,52 +74,54 @@ namespace Imendio.Blam {
 	    Conf.AddNotify (Conf.GetFullKey(Preference.REVERSE_ENTRIES), new NotifyEventHandler (ConfNotifyHandler));
 
 	}
-	
-	public void ItemAdded (Imendio.Blam.Item item)
-	{/*
-	    if (channel != null && channel == item.Channel) {
-		((ListStore)this.Model).AppendValues(item);
-	    }*/
-	}
 
-	public bool NextUnread()
-	{
-	    TreeModel model;
-	    TreeIter  iter;
-	    
-	    if (this.Selection.GetSelected(out model, out iter)) { 
-		if (!this.Model.IterNext(ref iter)) {
-		    if (!((ListStore)this.Model).GetIterFirst(out iter)) {
-			return false;
-		    }
+		bool Selected(out TreeModel model, out TreeIter iter)
+		{
+			return Selection.GetSelected(out model, out iter);
 		}
-	    } else {
-		if (!((ListStore)this.Model).GetIterFirst(out iter)) {
-		    return false;
+
+		void ScrollTo(TreeIter iter)
+		{
+			GrabFocus();
+			ScrollToCell(this.Model.GetPath(iter), titleColumn, false, 0, 0);
+			SetCursor(this.Model.GetPath(iter), titleColumn, false);
 		}
-	    }
 
-	    TreeIter startIter = iter;
+		bool LoopItems(TreeModel model, out TreeIter iter, Func<Item, bool> fn)
+		{
+			var start = iter;
+			do {
+				Item item = (Item)model.GetValue(iter, 0);
+				if (fn(item))
+					return true;
 
-	    do {
-		Imendio.Blam.Item item = (Imendio.Blam.Item)model.GetValue(iter, 0);
-		if (item.Unread) {
-		    this.GrabFocus();
-		    ScrollToCell(this.Model.GetPath(iter), titleColumn,
-				 false, 0, 0);
-		    
-		    this.SetCursor(this.Model.GetPath(iter), 
-				   titleColumn, false);
-		    return true;
-		}
-		
-		if (!this.Model.IterNext(ref iter)) {
-		    this.Model.GetIterFirst(out iter);
+				if (!Model.IterNext(ref iter))
+					Model.GetIterFirst(out iter);
+			} while (!iter.Equals(start));
+
+			return false;
 		}
-	    } while (!iter.Equals(startIter));
 
-	    return false;
-	}
+		public void Next(bool unread = true)
+		{
+			TreeModel model;
+			TreeIter  iter;
+
+			// If there's something selected (and it's not the last one),
+			// start looking from there. Otherwise from the start.
+			if (!Selected(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, out iter, check))
+				ScrollTo(iter);
+
+		}
 
 	public bool IdleScrollCb ()
 	{



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