[shotwell/wip/dedeprecate: 34/38] wip: Fix InjectionGroups for GtkBuilder



commit 1c8376e7c2ef3d767704b026c444eafabc6d5ea8
Author: Jens Georg <mail jensge org>
Date:   Tue Nov 1 19:03:25 2016 +0100

    wip: Fix InjectionGroups for GtkBuilder
    
    Signed-off-by: Jens Georg <mail jensge org>

 src/AppWindow.vala              |    3 -
 src/CollectionPage.vala         |   12 ++--
 src/Page.vala                   |  123 ++++++++++++++++++++++++++++-----------
 src/PhotoPage.vala              |    8 +-
 src/direct/DirectPhotoPage.vala |    4 +-
 src/library/LibraryWindow.vala  |    4 -
 ui/media.ui                     |    4 +-
 7 files changed, 103 insertions(+), 55 deletions(-)
---
diff --git a/src/AppWindow.vala b/src/AppWindow.vala
index 0103300..19894e8 100644
--- a/src/AppWindow.vala
+++ b/src/AppWindow.vala
@@ -760,9 +760,6 @@ public abstract class AppWindow : PageWindow {
         return common_action_groups;
     }
     
-    public virtual void replace_common_placeholders(Gtk.UIManager ui) {
-    }
-    
     public void go_fullscreen(Page page) {
         // if already fullscreen, use that
         if (fullscreen_window != null) {
diff --git a/src/CollectionPage.vala b/src/CollectionPage.vala
index e95f022..77d0d6c 100644
--- a/src/CollectionPage.vala
+++ b/src/CollectionPage.vala
@@ -69,7 +69,7 @@ public abstract class CollectionPage : MediaPage {
     }
     
     private static InjectionGroup create_file_menu_injectables() {
-        InjectionGroup group = new InjectionGroup("/MenuBar/FileMenu/FileExtrasPlaceholder");
+        InjectionGroup group = new InjectionGroup("FileExtrasPlaceholder");
         
         group.add_menu_item("Print");
         group.add_separator();
@@ -81,7 +81,7 @@ public abstract class CollectionPage : MediaPage {
     }
     
     private static InjectionGroup create_edit_menu_injectables() {
-        InjectionGroup group = new InjectionGroup("/MenuBar/EditMenu/EditExtrasPlaceholder");
+        InjectionGroup group = new InjectionGroup("EditExtrasPlaceholder");
         
         group.add_menu_item("Duplicate");
 
@@ -89,7 +89,7 @@ public abstract class CollectionPage : MediaPage {
     }
 
     private static InjectionGroup create_view_menu_fullscreen_injectables() {
-        InjectionGroup group = new 
InjectionGroup("/MenuBar/ViewMenu/ViewExtrasFullscreenSlideshowPlaceholder");
+        InjectionGroup group = new InjectionGroup("ViewExtrasFullscreenSlideshowPlaceholder");
         
         group.add_menu_item("Fullscreen", "CommonFullscreen");
         group.add_separator();
@@ -99,7 +99,7 @@ public abstract class CollectionPage : MediaPage {
     }
 
     private static InjectionGroup create_photos_menu_edits_injectables() {
-        InjectionGroup group = new InjectionGroup("/MenuBar/PhotosMenu/PhotosExtrasEditsPlaceholder");
+        InjectionGroup group = new InjectionGroup("PhotosExtrasEditsPlaceholder");
         
         group.add_menu_item("RotateClockwise");
         group.add_menu_item("RotateCounterclockwise");
@@ -116,7 +116,7 @@ public abstract class CollectionPage : MediaPage {
     }
   
     private static InjectionGroup create_photos_menu_date_injectables() {
-        InjectionGroup group = new InjectionGroup("/MenuBar/PhotosMenu/PhotosExtrasDateTimePlaceholder");
+        InjectionGroup group = new InjectionGroup("PhotosExtrasDateTimePlaceholder");
         
         group.add_menu_item("AdjustDateTime");
         
@@ -124,7 +124,7 @@ public abstract class CollectionPage : MediaPage {
     }
 
     private static InjectionGroup create_photos_menu_externals_injectables() {
-        InjectionGroup group = new InjectionGroup("/MenuBar/PhotosMenu/PhotosExtrasExternalsPlaceholder");
+        InjectionGroup group = new InjectionGroup("PhotosExtrasExternalsPlaceholder");
         
         group.add_menu_item("ExternalEdit");
         group.add_menu_item("ExternalEditRAW");
diff --git a/src/Page.vala b/src/Page.vala
index ee54b65..a285a32 100644
--- a/src/Page.vala
+++ b/src/Page.vala
@@ -6,43 +6,49 @@
 
 public class InjectionGroup {
     public class Element {
+        public enum ItemType {
+            MENUITEM,
+            MENU,
+            SEPARATOR
+        }
         public string name;
         public string action;
-        public Gtk.UIManagerItemType kind;
-        
-        public Element(string name, string? action, Gtk.UIManagerItemType kind) {
+        public ItemType kind;
+
+        public Element(string name, string? action, ItemType kind) {
             this.name = name;
             this.action = action != null ? action : name;
             this.kind = kind;
         }
     }
-    
+
     private string path;
     private Gee.ArrayList<Element?> elements = new Gee.ArrayList<Element?>();
     private int separator_id = 0;
-    
+
     public InjectionGroup(string path) {
         this.path = path;
     }
-    
+
     public string get_path() {
         return path;
     }
-    
+
     public Gee.List<Element?> get_elements() {
         return elements;
     }
-    
+
     public void add_menu_item(string name, string? action = null) {
-        elements.add(new Element(name, action, Gtk.UIManagerItemType.MENUITEM));
+        elements.add(new Element(name, action, Element.ItemType.MENUITEM));
     }
-    
+
     public void add_menu(string name, string? action = null) {
-        elements.add(new Element(name, action, Gtk.UIManagerItemType.MENU));
+        elements.add(new Element(name, action, Element.ItemType.MENU));
     }
-    
+
     public void add_separator() {
-        elements.add(new Element("%d-separator".printf(separator_id++), null, 
Gtk.UIManagerItemType.SEPARATOR));
+        elements.add(new Element("%d-separator".printf(separator_id++), null,
+                    Element.ItemType.SEPARATOR));
     }
 }
 
@@ -201,11 +207,39 @@ public abstract class Page : Gtk.ScrolledWindow {
     }
 
     private Gtk.MenuBar menubar;
-    
-    public virtual Gtk.MenuBar get_menubar() {
+
+    public Gtk.MenuBar get_menubar() {
         if (this.menubar == null) {
-           var model = builder.get_object ("MenuBar") as GLib.MenuModel;
-           this.menubar = new Gtk.MenuBar.from_model (model);
+            var model = builder.get_object ("MenuBar") as GLib.Menu;
+
+            // Collect injected UI elements and add them to the UI manager
+            InjectionGroup[] injection_groups = init_collect_injection_groups();
+            foreach (InjectionGroup group in injection_groups) {
+                var items = model.get_n_items ();
+                for (int i = 0; i < items; i++) {
+                    var submenu = model.get_item_link (i, GLib.Menu.LINK_SUBMENU);
+
+                    var section = this.find_extension_point (submenu,
+                            group.get_path ());
+
+                    if (section == null) {
+                        continue;
+                    }
+
+                    foreach (var element in group.get_elements ()) {
+                        var menu = section as GLib.Menu;
+                        switch (element.kind) {
+                            case InjectionGroup.Element.ItemType.MENUITEM:
+                                menu.append (element.name, element.action);
+                                break;
+                            default:
+                                break;
+                        }
+                    }
+                }
+            }
+
+            this.menubar = new Gtk.MenuBar.from_model (model);
         }
 
         return this.menubar;
@@ -473,26 +507,12 @@ public abstract class Page : Gtk.ScrolledWindow {
         
         foreach (string ui_filename in ui_filenames)
             init_load_ui(ui_filename);
-            
+
         //ui.insert_action_group(action_group, 0);
+        critical ("==> %p", builder.get_object ("MenuBar") as GLib.Menu);
 
-        // Collect injected UI elements and add them to the UI manager
-        InjectionGroup[] injection_groups = init_collect_injection_groups();
-        foreach (InjectionGroup group in injection_groups) {
-            foreach (InjectionGroup.Element element in group.get_elements()) {
-                debug (element.name);
-/*                uint merge_id = ui.new_merge_id();
-                ui.add_ui(merge_id, group.get_path(), element.name, element.action,
-                    element.kind, false);
-                merge_ids += merge_id;*/
-            }
-        }
-        
-//        AppWindow.get_instance().replace_common_placeholders(ui);
-        
-//        ui.ensure_update();
     }
-    
+
     private void remove_ui() {
 /*        for (int i = merge_ids.length - 1 ; i >= 0 ; --i)
             ui.remove_ui(merge_ids[i]);
@@ -1168,6 +1188,39 @@ public abstract class Page : Gtk.ScrolledWindow {
 
         return false;
     }
+
+    private GLib.MenuModel? find_extension_point (GLib.MenuModel model,
+                                                  string extension_point) {
+        var items = model.get_n_items ();
+        GLib.MenuModel? section = null;
+
+        for (int i = 0; i < items && section == null; i++) {
+            string? name = null;
+            model.get_item_attribute (i, "id", "s", out name);
+            if (name == extension_point) {
+                section = model.get_item_link (i, GLib.Menu.LINK_SECTION);
+            } else {
+                var subsection = model.get_item_link (i, GLib.Menu.LINK_SECTION);
+
+                if (subsection == null)
+                    continue;
+
+                // Recurse into submenus
+                var sub_items = subsection.get_n_items ();
+                for (int j = 0; j < sub_items && section == null; j++) {
+                    var submenu = subsection.get_item_link
+                                                (j, GLib.Menu.LINK_SUBMENU);
+                    if (submenu != null) {
+                        section = this.find_extension_point (submenu,
+                                                             extension_point);
+                    }
+                }
+            }
+        }
+
+        return section;
+    }
+
 }
 
 public abstract class CheckerboardPage : Page {
@@ -2607,4 +2660,6 @@ public class DragAndDropHandler {
     private void on_export_completed() {
         exporter = null;
     }
+
+
 }
diff --git a/src/PhotoPage.vala b/src/PhotoPage.vala
index 603f5c9..48862ea 100644
--- a/src/PhotoPage.vala
+++ b/src/PhotoPage.vala
@@ -2669,18 +2669,18 @@ public class LibraryPhotoPage : EditingHostPage {
     
     protected override InjectionGroup[] init_collect_injection_groups() {
         InjectionGroup[] groups = base.init_collect_injection_groups();
-        
-        InjectionGroup print_group = new InjectionGroup("/MenuBar/FileMenu/PrintPlaceholder");
+
+        InjectionGroup print_group = new InjectionGroup("PrintPlaceholder");
         print_group.add_menu_item("Print");
         
         groups += print_group;
         
-        InjectionGroup publish_group = new InjectionGroup("/MenuBar/FileMenu/PublishPlaceholder");
+        InjectionGroup publish_group = new InjectionGroup("PublishPlaceholder");
         publish_group.add_menu_item("Publish");
         
         groups += publish_group;
         
-        InjectionGroup bg_group = new InjectionGroup("/MenuBar/FileMenu/SetBackgroundPlaceholder");
+        InjectionGroup bg_group = new InjectionGroup("SetBackgroundPlaceholder");
         bg_group.add_menu_item("SetBackground");
         
         groups += bg_group;
diff --git a/src/direct/DirectPhotoPage.vala b/src/direct/DirectPhotoPage.vala
index ffeab51..7717f77 100644
--- a/src/direct/DirectPhotoPage.vala
+++ b/src/direct/DirectPhotoPage.vala
@@ -209,12 +209,12 @@ public class DirectPhotoPage : EditingHostPage {
     protected override InjectionGroup[] init_collect_injection_groups() {
         InjectionGroup[] groups = base.init_collect_injection_groups();
         
-        InjectionGroup print_group = new InjectionGroup("/MenuBar/FileMenu/PrintPlaceholder");
+        InjectionGroup print_group = new InjectionGroup("PrintPlaceholder");
         print_group.add_menu_item("Print");
         
         groups += print_group;
         
-        InjectionGroup bg_group = new InjectionGroup("/MenuBar/FileMenu/SetBackgroundPlaceholder");
+        InjectionGroup bg_group = new InjectionGroup("SetBackgroundPlaceholder");
         bg_group.add_menu_item("SetBackground");
         
         groups += bg_group;
diff --git a/src/library/LibraryWindow.vala b/src/library/LibraryWindow.vala
index 5dad995..36c5c93 100644
--- a/src/library/LibraryWindow.vala
+++ b/src/library/LibraryWindow.vala
@@ -442,10 +442,6 @@ public class LibraryWindow : AppWindow {
         return groups;
     }
     
-    public override void replace_common_placeholders(Gtk.UIManager ui) {
-        base.replace_common_placeholders(ui);
-    }
-    
     protected override void switched_pages(Page? old_page, Page? new_page) {
         base.switched_pages(old_page, new_page);
         
diff --git a/ui/media.ui b/ui/media.ui
index bd3ba3c..1afe418 100644
--- a/ui/media.ui
+++ b/ui/media.ui
@@ -20,7 +20,7 @@
         </item>
       </section>
       <section>
-      <attribute name="id">FileExtrasPlaceholder</attribute>
+        <attribute name="id">FileExtrasPlaceholder</attribute>
       </section>
       <section>
         <item>
@@ -69,7 +69,7 @@
         </item>
       </section>
       <section>
-      <attribute name="id">EditExtrasPlaceholder</attribute>
+        <attribute name="id">EditExtrasPlaceholder</attribute>
         <item>
           <attribute name="label" translatable="yes">R_emove from Library</attribute>
           <attribute name="accel">&lt;Shift&gt;Delete</attribute>


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