[banshee/better-now-playing: 70/70] [NowPlaying] Make context buttons add/remove on extension load/unload.



commit 0a1253a4878db00c5ac4e7ce73d0701c2e743352
Author: Alex Launi <alex launi gmail com>
Date:   Tue Jun 15 18:30:17 2010 -0400

    [NowPlaying] Make context buttons add/remove on extension load/unload.
    
    Context page source switcher buttons are now dynamic. Button appears when
    a source is added, and disappears when a source is removed.

 .../Banshee.ContextPane/ContextPageManager.cs      |    4 ++-
 .../Banshee.NowPlaying/Actions.cs                  |   22 ++++++++++++-
 .../Banshee.NowPlaying/NowPlayingSource.cs         |   32 ++++++++++++++++++--
 3 files changed, 52 insertions(+), 6 deletions(-)
---
diff --git a/src/Core/Banshee.ThickClient/Banshee.ContextPane/ContextPageManager.cs b/src/Core/Banshee.ThickClient/Banshee.ContextPane/ContextPageManager.cs
index dffaaf6..f0e5207 100644
--- a/src/Core/Banshee.ThickClient/Banshee.ContextPane/ContextPageManager.cs
+++ b/src/Core/Banshee.ThickClient/Banshee.ContextPane/ContextPageManager.cs
@@ -74,12 +74,14 @@ namespace Banshee.ContextPane
 
                 var page = pages[node.Id];
                 var handler = PageRemoved;
+
+                pages.Remove (node.Id);
+
                 if (handler != null) {
                     PageRemoved (page);
                 }
 
                 page.Dispose ();
-                pages.Remove (node.Id);
             }
         }
     }
diff --git a/src/Extensions/Banshee.NowPlaying/Banshee.NowPlaying/Actions.cs b/src/Extensions/Banshee.NowPlaying/Banshee.NowPlaying/Actions.cs
index 6f10261..981a152 100644
--- a/src/Extensions/Banshee.NowPlaying/Banshee.NowPlaying/Actions.cs
+++ b/src/Extensions/Banshee.NowPlaying/Banshee.NowPlaying/Actions.cs
@@ -48,15 +48,25 @@ namespace Banshee.NowPlaying
         private NowPlayingSource now_playing_source;
         private Dictionary<int, BaseContextPage> pages;
 
+        public event Action<EventArgs> Changed;
+
         public Actions (NowPlayingSource nowPlayingSource) : base ("NowPlaying")
         {
             now_playing_source = nowPlayingSource;
             pages = new Dictionary<int, BaseContextPage> ();
 
+            Register ();
+
             ContextView = new ContextView ();
+            ContextView.Manager.PageAdded += OnManagerPageAddedOrRemoved;
+            ContextView.Manager.PageRemoved += OnManagerPageAddedOrRemoved;
 
             LoadActions ();
-            Register ();
+        }
+
+        void OnManagerPageAddedOrRemoved (BaseContextPage obj)
+        {
+            LoadActions ();
         }
 
         // We've got 1 hard coded action available and the rest come from the context pane.
@@ -76,13 +86,16 @@ namespace Banshee.NowPlaying
 
         private void LoadActions ()
         {
+            int i = 0;
             // remove all of the existing actions
             foreach (Gtk.Action action in ListActions ()) {
                 Remove (action);
+                pages.Remove (i);
+                i++;
             }
 
             // then add them all.
-            int i = 0;
+            i = 0;
             List<RadioActionEntry> actions = new List<RadioActionEntry> ();
             actions.Add (new RadioActionEntry (TrackInfoId, null, null, null, "Track Information", i));
 
@@ -103,6 +116,11 @@ namespace Banshee.NowPlaying
                     }
                 }
             }
+
+            Action<EventArgs> handler = Changed;
+            if (handler != null) {
+                handler (new EventArgs ());
+            }
         }
 
         private void OnChanged (System.Object o, ChangedArgs args)
diff --git a/src/Extensions/Banshee.NowPlaying/Banshee.NowPlaying/NowPlayingSource.cs b/src/Extensions/Banshee.NowPlaying/Banshee.NowPlaying/NowPlayingSource.cs
index 8b9c2a2..09f4127 100644
--- a/src/Extensions/Banshee.NowPlaying/Banshee.NowPlaying/NowPlayingSource.cs
+++ b/src/Extensions/Banshee.NowPlaying/Banshee.NowPlaying/NowPlayingSource.cs
@@ -48,16 +48,19 @@ namespace Banshee.NowPlaying
         private UIManager ui_manager;
         private Widget substitute_audio_display;
 
+        uint? mergeId;
+
         private const string button_xml = @"
             <ui>
                 <toolbar name=""HeaderToolbar"">
                     <placeholder name=""SourceActions"">
                         <placeholder name=""ContextActions"">
-                            <toolitem action=""{0}"" />
+                            {0}
                         </placeholder>
                     </placeholder>
                 </toolbar>
             </ui>";
+        private const string toolitem_element = @"<toolitem action=""{0}"" />";
 
         public NowPlayingSource () : base ("now-playing", Catalog.GetString ("Now Playing"), 10, "now-playing")
         {
@@ -73,12 +76,35 @@ namespace Banshee.NowPlaying
             ServiceManager.PlayerEngine.ConnectEvent (OnTrackInfoUpdated, PlayerEvent.TrackInfoUpdated);
             ServiceManager.PlayerEngine.ConnectEvent (OnCreateVideoWindow, PlayerEvent.PrepareVideoWindow);
 
+            mergeId = null;
+            ui_manager = ServiceManager.Get<InterfaceActionService> ().UIManager;
+
             Actions = new Actions (this);
             Actions.Visible = ServiceManager.SourceManager.ActiveSource == this;
+            Actions.Changed += HandleActionsChanged;
 
-            ui_manager = ServiceManager.Get<InterfaceActionService> ().UIManager;
+            LoadContentButtons ();
+        }
+
+        void HandleActionsChanged (EventArgs obj)
+        {
+            LoadContentButtons ();
+        }
+        
+        private void LoadContentButtons ()
+        {
+            if (mergeId.HasValue) {
+                ui_manager.RemoveUi (mergeId.Value);
+            }
+
+            string toolitem = "";
             foreach (string pageId in Actions.PageIds) {
-                ui_manager.AddUiFromString (String.Format (button_xml, pageId));
+                string item = String.Format (toolitem_element, pageId);
+                toolitem += item;
+            }
+
+            if (toolitem != "") {
+                mergeId = ui_manager.AddUiFromString (String.Format (button_xml, toolitem));
             }
         }
 



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