[gtk/new-style-menu] widget factory: Add a context menu example



commit 40fa9f6a0da915a0405da86d22091aae0ae9d511
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Jun 7 01:25:59 2019 +0000

    widget factory: Add a context menu example
    
    This is also taken from
    https://gitlab.gnome.org/Teams/Design/os-mockups/blob/master/menus/menu-design-patterns.png

 demos/widget-factory/widget-factory.c  | 49 +++++++++++++++++++++++++++-
 demos/widget-factory/widget-factory.ui | 59 +++++++++++++++++++++++++++++++++-
 2 files changed, 106 insertions(+), 2 deletions(-)
---
diff --git a/demos/widget-factory/widget-factory.c b/demos/widget-factory/widget-factory.c
index 2ccc659c1b..cd7e7cc20b 100644
--- a/demos/widget-factory/widget-factory.c
+++ b/demos/widget-factory/widget-factory.c
@@ -1606,6 +1606,37 @@ adjustment3_value_changed (GtkAdjustment *adj, GtkProgressBar *pbar)
   gtk_progress_bar_set_fraction (pbar, fraction);
 }
 
+static void
+clicked_cb (GtkGesture *gesture,
+            int         n_press,
+            double      x,
+            double      y,
+            GtkPopover *popover)
+{
+  GdkRectangle rect;
+
+  rect.x = x;
+  rect.y = y;
+  rect.width = 1;
+  rect.height = 1;
+  gtk_popover_set_pointing_to (popover, &rect);
+  gtk_popover_popup (popover);
+}
+
+static void
+set_up_context_popover (GtkWidget *widget,
+                        GMenuModel *model)
+{
+  GtkWidget *popover = gtk_popover_new_from_model (widget, model);
+  GtkGesture *gesture;
+
+  g_object_set (popover, "has-arrow", FALSE, NULL);
+  gesture = gtk_gesture_click_new ();
+  gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (gesture), GDK_BUTTON_SECONDARY);
+  g_signal_connect (gesture, "pressed", G_CALLBACK (clicked_cb), popover);
+  gtk_widget_add_controller (widget, GTK_EVENT_CONTROLLER (gesture));
+}
+
 static void
 activate (GApplication *app)
 {
@@ -1619,6 +1650,7 @@ activate (GApplication *app)
   GtkWidget *dialog;
   GtkAdjustment *adj;
   GtkCssProvider *provider;
+  GMenuModel *model;
   static GActionEntry win_entries[] = {
     { "dark", NULL, NULL, "false", change_theme_state },
     { "transition", NULL, NULL, "false", change_transition_state },
@@ -1636,7 +1668,11 @@ activate (GApplication *app)
   } accels[] = {
     { "app.about", { "F1", NULL } },
     { "app.quit", { "<Primary>q", NULL } },
+    { "app.open", { "Return", NULL } },
     { "app.open-in", { "<Primary>n", NULL } },
+    { "app.cut", { "<Primary>x", NULL } },
+    { "app.copy", { "<Primary>c", NULL } },
+    { "app.paste", { "<Primary>v", NULL } },
     { "win.dark", { "<Primary>d", NULL } },
     { "win.search", { "<Primary>s", NULL } },
     { "win.delete", { "Delete", NULL } },
@@ -1892,6 +1928,10 @@ activate (GApplication *app)
   widget = (GtkWidget *)gtk_builder_get_object (builder, "extra_info_entry");
   g_timeout_add (100, (GSourceFunc)pulse_it, widget);
 
+  widget = (GtkWidget *)gtk_builder_get_object (builder, "box_for_context");
+  model = (GMenuModel *)gtk_builder_get_object (builder, "new_style_context_menu_model");
+  set_up_context_popover (widget, model);
+
   gtk_widget_show (GTK_WIDGET (window));
 
   g_object_unref (builder);
@@ -1937,7 +1977,7 @@ select_action (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       user_data)
 {
-  g_print ("Set action %s to %s\n",
+  g_print ("Select action %s value %s\n",
            g_action_get_name (G_ACTION (action)),
            g_variant_get_string (parameter, NULL));
 
@@ -1977,7 +2017,10 @@ main (int argc, char *argv[])
     { "print", activate_action, NULL, NULL, NULL },
     { "share", activate_action, NULL, NULL, NULL },
     { "labels", activate_action, NULL, NULL, NULL },
+    { "open", activate_action, NULL, NULL, NULL },
     { "open-in", activate_action, NULL, NULL, NULL },
+    { "open-tab", activate_action, NULL, NULL, NULL },
+    { "open-window", activate_action, NULL, NULL, NULL },
     { "cut", activate_action, NULL, NULL, NULL },
     { "copy", activate_action, NULL, NULL, NULL },
     { "paste", activate_action, NULL, NULL, NULL },
@@ -1987,6 +2030,10 @@ main (int argc, char *argv[])
     { "broni", toggle_action, NULL, "true", NULL },
     { "drutt", toggle_action, NULL, "true", NULL },
     { "upstairs", toggle_action, NULL, "true", NULL },
+    { "option-a", activate_action, NULL, NULL, NULL },
+    { "option-b", activate_action, NULL, NULL, NULL },
+    { "option-c", activate_action, NULL, NULL, NULL },
+    { "option-d", activate_action, NULL, NULL, NULL },
   };
   gint status;
 
diff --git a/demos/widget-factory/widget-factory.ui b/demos/widget-factory/widget-factory.ui
index 0b6c2db8f1..93ba817f3f 100644
--- a/demos/widget-factory/widget-factory.ui
+++ b/demos/widget-factory/widget-factory.ui
@@ -3033,7 +3033,7 @@ microphone-sensitivity-medium-symbolic</property>
                               <object class="GtkNotebookPage">
                                 <property name="tab-expand">1</property>
                                 <property name="child">
-                                  <object class="GtkBox">
+                                  <object class="GtkBox" id="box_for_context">
                                     <property name="height-request">120</property>
                                   </object>
                                 </property>
@@ -3830,4 +3830,61 @@ bad things might happen.</property>
         </item>
     </section>
   </menu>
+  <menu id="new_style_context_menu_model">
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">Open</attribute>
+        <attribute name="action">app.open</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">Open in New Tab</attribute>
+        <attribute name="action">app.open-tab</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">Open in New Window</attribute>
+        <attribute name="action">app.open-window</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">Cut</attribute>
+        <attribute name="action">app.cut</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">Copy</attribute>
+        <attribute name="action">app.copy</attribute>
+      </item>
+    </section>
+    <section>
+      <submenu>
+        <attribute name="label" translatable="yes">Options</attribute>
+        <section>
+          <item>
+            <attribute name="label" translatable="yes">Option A</attribute>
+            <attribute name="action">app.option-a</attribute>
+          </item>
+          <item>
+            <attribute name="label" translatable="yes">Option B</attribute>
+            <attribute name="action">app.option-b</attribute>
+          </item>
+          <item>
+            <attribute name="label" translatable="yes">Option C</attribute>
+            <attribute name="action">app.option-c</attribute>
+          </item>
+          <item>
+            <attribute name="label" translatable="yes">Option D</attribute>
+            <attribute name="action">app.option-d</attribute>
+          </item>
+        </section>
+      </submenu>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">Move to Trash</attribute>
+        <attribute name="action">win.delete</attribute>
+      </item>
+    </section>
+  </menu>
 </interface>


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