[virt-tools-list] [PATCH 2/5] app: Use app-menu

Fabiano Fidêncio fidencio at redhat.com
Sat Dec 17 16:50:09 UTC 2016


From: Sagar Ghuge <ghugesss at gmail.com>

Add support for an app-menu containing "Preferences", "About" and "Quit"
options.

The decision of naming the file as "menus.ui" under
"$(srcdir)/resources/gtk/" and having the widget id as "app-menu" is
that it can get loaded automatically during GApplication startup.

The options presented in the app-menu got removed from the
virt-viewer interface.

Signed-off-by: Sagar Ghuge <ghugesss at gmail.com>
---
 po/POTFILES.in                          |  1 +
 src/Makefile.am                         |  1 +
 src/resources/gtk/menus.ui              | 25 +++++++++
 src/resources/ui/virt-viewer.ui         | 36 -------------
 src/resources/virt-viewer.gresource.xml |  1 +
 src/virt-viewer-app.c                   | 89 ++++++++++++++++++++++++++++-----
 src/virt-viewer-app.h                   |  1 -
 src/virt-viewer-window.c                | 49 +-----------------
 8 files changed, 106 insertions(+), 97 deletions(-)
 create mode 100644 src/resources/gtk/menus.ui

diff --git a/po/POTFILES.in b/po/POTFILES.in
index 371c242..6e17de9 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -19,6 +19,7 @@ src/virt-viewer-vm-connection.c
 src/virt-viewer-window.c
 src/virt-viewer-file.c
 src/virt-viewer.c
+[type: gettext/glade] src/resources/gtk/menus.ui
 [type: gettext/glade] src/resources/ui/virt-viewer.ui
 [type: gettext/glade] src/resources/ui/virt-viewer-guest-details.ui
 [type: gettext/glade] src/resources/ui/virt-viewer-vm-connection.ui
diff --git a/src/Makefile.am b/src/Makefile.am
index dbad60e..a4824fc 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -15,6 +15,7 @@ noinst_DATA = \
 	resources/ui/remote-viewer-connect.ui \
 	resources/ui/virt-viewer-file-transfer-dialog.ui \
 	resources/ui/remote-viewer-iso-list.ui \
+	resources/gtk/menus.ui \
 	$(NULL)
 
 EXTRA_DIST =					\
diff --git a/src/resources/gtk/menus.ui b/src/resources/gtk/menus.ui
new file mode 100644
index 0000000..298c730
--- /dev/null
+++ b/src/resources/gtk/menus.ui
@@ -0,0 +1,25 @@
+<?xml version="1.0"?>
+<interface>
+  <!-- interface-requires gtk+ 3.0 -->
+  <menu id="app-menu">
+    <section>
+      <attribute name="id">preferences-section</attribute>
+      <item>
+        <attribute name="label" translatable="yes">_Preferences</attribute>
+        <attribute name="action">app.preferences</attribute>
+      </item>
+    </section>
+    <section>
+      <attribute name="id">help-section</attribute>
+      <item>
+        <attribute name="label" translatable="yes">_About</attribute>
+        <attribute name="action">app.about</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">_Quit</attribute>
+        <attribute name="action">app.quit</attribute>
+        <attribute name="accel"><Primary><Shift>q</attribute>
+      </item>
+    </section>
+  </menu>
+</interface>
diff --git a/src/resources/ui/virt-viewer.ui b/src/resources/ui/virt-viewer.ui
index e9609ec..800bc0f 100644
--- a/src/resources/ui/virt-viewer.ui
+++ b/src/resources/ui/virt-viewer.ui
@@ -81,32 +81,6 @@
                             <signal name="activate" handler="virt_viewer_window_menu_change_cd_activate" swapped="no"/>
                           </object>
                         </child>
-                        <child>
-                          <object class="GtkMenuItem" id="menu-preferences">
-                            <property name="visible">True</property>
-                            <property name="can_focus">False</property>
-                            <property name="label" translatable="yes">_Preferences</property>
-                            <property name="use_underline">True</property>
-                            <signal name="activate" handler="virt_viewer_window_menu_preferences_cb" swapped="no"/>
-                          </object>
-                        </child>
-                        <child>
-                          <object class="GtkSeparatorMenuItem" id="separatormenuitem1">
-                            <property name="visible">True</property>
-                            <property name="can_focus">False</property>
-                          </object>
-                        </child>
-                        <child>
-                          <object class="GtkMenuItem" id="imagemenuitem5">
-                            <property name="label" translatable="yes">_Quit</property>
-                            <property name="visible">True</property>
-                            <property name="can_focus">False</property>
-                            <property name="use_action_appearance">False</property>
-                            <property name="use_underline">True</property>
-                            <accelerator key="q" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
-                            <signal name="activate" handler="virt_viewer_window_menu_file_quit" swapped="no"/>
-                          </object>
-                        </child>
                       </object>
                     </child>
                   </object>
@@ -242,16 +216,6 @@
                             <signal name="activate" handler="virt_viewer_window_menu_help_guest_details" swapped="no"/>
                           </object>
                         </child>
-                        <child>
-                          <object class="GtkMenuItem" id="imagemenuitem10">
-                            <property name="label" translatable="yes">_About</property>
-                            <property name="visible">True</property>
-                            <property name="can_focus">False</property>
-                            <property name="use_action_appearance">False</property>
-                            <property name="use_underline">True</property>
-                            <signal name="activate" handler="virt_viewer_window_menu_help_about" swapped="no"/>
-                          </object>
-                        </child>
                       </object>
                     </child>
                   </object>
diff --git a/src/resources/virt-viewer.gresource.xml b/src/resources/virt-viewer.gresource.xml
index 334fa47..f4fbab9 100644
--- a/src/resources/virt-viewer.gresource.xml
+++ b/src/resources/virt-viewer.gresource.xml
@@ -10,6 +10,7 @@
     <file>ui/virt-viewer-vm-connection.ui</file>
     <file>ui/virt-viewer.ui</file>
     <file>ui/virt-viewer-file-transfer-dialog.ui</file>
+    <file>gtk/menus.ui</file>
     <file alias="icons/16x16/virt-viewer.png">../../icons/16x16/virt-viewer.png</file>
     <file alias="icons/22x22/virt-viewer.png">../../icons/22x22/virt-viewer.png</file>
     <file alias="icons/24x24/virt-viewer.png">../../icons/24x24/virt-viewer.png</file>
diff --git a/src/virt-viewer-app.c b/src/virt-viewer-app.c
index 28b4f60..dd1f3df 100644
--- a/src/virt-viewer-app.c
+++ b/src/virt-viewer-app.c
@@ -104,7 +104,8 @@ static void virt_viewer_app_set_fullscreen(VirtViewerApp *self, gboolean fullscr
 static void virt_viewer_app_update_menu_displays(VirtViewerApp *self);
 static void virt_viewer_update_smartcard_accels(VirtViewerApp *self);
 static void virt_viewer_app_add_option_entries(VirtViewerApp *self, GOptionContext *context, GOptionGroup *group);
-
+static void virt_viewer_app_show_preferences(VirtViewerApp *self, GtkWidget *parent);
+static GtkWidget *virt_viewer_app_get_preferences(VirtViewerApp *self);
 
 struct _VirtViewerAppPrivate {
     VirtViewerWindow *main_window;
@@ -1787,6 +1788,77 @@ virt_viewer_update_smartcard_accels(VirtViewerApp *self)
 }
 
 static void
+virt_viewer_app_show_preferences(VirtViewerApp *self, GtkWidget *parent)
+{
+    GtkWidget *preferences = virt_viewer_app_get_preferences(self);
+
+    gtk_window_set_transient_for(GTK_WINDOW(preferences), GTK_WINDOW(parent));
+
+    gtk_window_present(GTK_WINDOW(preferences));
+}
+
+static void
+preferences_activated (GSimpleAction *action G_GNUC_UNUSED,
+                       GVariant *parameter G_GNUC_UNUSED,
+                       gpointer app)
+{
+    VirtViewerApp *self = VIRT_VIEWER_APP(app);
+    GtkWidget *window;
+
+    window = GTK_WIDGET(virt_viewer_window_get_window(self->priv->main_window));
+
+    virt_viewer_app_show_preferences(self, window);
+}
+
+static void
+about_activated (GSimpleAction *action G_GNUC_UNUSED,
+                 GVariant *parameter G_GNUC_UNUSED,
+                 gpointer app)
+{
+    VirtViewerApp *self = VIRT_VIEWER_APP(app);
+    GtkBuilder *about;
+    GtkWidget *dialog;
+    GdkPixbuf *icon;
+    GtkWidget *window;
+
+    about = virt_viewer_util_load_ui("virt-viewer-about.ui");
+    dialog = GTK_WIDGET(gtk_builder_get_object(about, "about"));
+    window = GTK_WIDGET(virt_viewer_window_get_window(self->priv->main_window));
+
+    gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(dialog), VERSION BUILDID);
+
+    icon = gdk_pixbuf_new_from_resource(VIRT_VIEWER_RESOURCE_PREFIX"/icons/48x48/virt-viewer.png", NULL);
+    if (icon != NULL) {
+        gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(dialog), icon);
+        g_object_unref(icon);
+    } else {
+        gtk_about_dialog_set_logo_icon_name(GTK_ABOUT_DIALOG(dialog), "virt-viewer");
+    }
+
+    gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(window));
+    gtk_builder_connect_signals(about, window);
+
+    gtk_widget_show_all(dialog);
+
+    g_object_unref(G_OBJECT(about));
+}
+
+static void
+quit_activated(GSimpleAction *action G_GNUC_UNUSED,
+               GVariant *parameter G_GNUC_UNUSED,
+               gpointer app)
+{
+    g_application_quit (G_APPLICATION (app));
+}
+
+static GActionEntry app_entries[] =
+{
+    {"preferences", preferences_activated, NULL, NULL, NULL, {0,0,0} },
+    {"about", about_activated, NULL, NULL, NULL, {0,0,0} },
+    {"quit", quit_activated, NULL, NULL, NULL, {0,0,0} }
+};
+
+static void
 virt_viewer_app_on_application_startup(GApplication *app)
 {
     VirtViewerApp *self = VIRT_VIEWER_APP(app);
@@ -1796,6 +1868,10 @@ virt_viewer_app_on_application_startup(GApplication *app)
 
     G_APPLICATION_CLASS(virt_viewer_app_parent_class)->startup(app);
 
+    g_action_map_add_action_entries(G_ACTION_MAP(app),
+                                    app_entries, G_N_ELEMENTS(app_entries),
+                                    app);
+
     self->priv->resource = virt_viewer_get_resource();
 
     virt_viewer_app_set_debug(opt_debug);
@@ -2530,17 +2606,6 @@ end:
     return preferences;
 }
 
-void
-virt_viewer_app_show_preferences(VirtViewerApp *self, GtkWidget *parent)
-{
-    GtkWidget *preferences = virt_viewer_app_get_preferences(self);
-
-    gtk_window_set_transient_for(GTK_WINDOW(preferences),
-                                 GTK_WINDOW(parent));
-
-    gtk_window_present(GTK_WINDOW(preferences));
-}
-
 static gboolean
 option_kiosk_quit(G_GNUC_UNUSED const gchar *option_name,
                   const gchar *value,
diff --git a/src/virt-viewer-app.h b/src/virt-viewer-app.h
index 8381631..573e67e 100644
--- a/src/virt-viewer-app.h
+++ b/src/virt-viewer-app.h
@@ -96,7 +96,6 @@ void virt_viewer_app_clear_hotkeys(VirtViewerApp *app);
 GList* virt_viewer_app_get_initial_displays(VirtViewerApp* self);
 gint virt_viewer_app_get_initial_monitor_for_display(VirtViewerApp* self, gint display);
 void virt_viewer_app_set_enable_accel(VirtViewerApp *app, gboolean enable);
-void virt_viewer_app_show_preferences(VirtViewerApp *app, GtkWidget *parent);
 void virt_viewer_app_set_menus_sensitive(VirtViewerApp *self, gboolean sensitive);
 gboolean virt_viewer_app_get_session_cancelled(VirtViewerApp *self);
 
diff --git a/src/virt-viewer-window.c b/src/virt-viewer-window.c
index a4f531e..27c0be0 100644
--- a/src/virt-viewer-window.c
+++ b/src/virt-viewer-window.c
@@ -52,9 +52,7 @@ void virt_viewer_window_menu_view_zoom_out(GtkWidget *menu, VirtViewerWindow *se
 void virt_viewer_window_menu_view_zoom_in(GtkWidget *menu, VirtViewerWindow *self);
 void virt_viewer_window_menu_view_zoom_reset(GtkWidget *menu, VirtViewerWindow *self);
 gboolean virt_viewer_window_delete(GtkWidget *src, void *dummy, VirtViewerWindow *self);
-void virt_viewer_window_menu_file_quit(GtkWidget *src, VirtViewerWindow *self);
 void virt_viewer_window_guest_details_response(GtkDialog *dialog, gint response_id, gpointer user_data);
-void virt_viewer_window_menu_help_about(GtkWidget *menu, VirtViewerWindow *self);
 void virt_viewer_window_menu_help_guest_details(GtkWidget *menu, VirtViewerWindow *self);
 void virt_viewer_window_menu_view_fullscreen(GtkWidget *menu, VirtViewerWindow *self);
 void virt_viewer_window_menu_send(GtkWidget *menu, VirtViewerWindow *self);
@@ -66,7 +64,6 @@ void virt_viewer_window_menu_view_release_cursor(GtkWidget *menu, VirtViewerWind
 void virt_viewer_window_menu_preferences_cb(GtkWidget *menu, VirtViewerWindow *self);
 void virt_viewer_window_menu_change_cd_activate(GtkWidget *menu, VirtViewerWindow *self);
 
-
 /* Internal methods */
 static void virt_viewer_window_enable_modifiers(VirtViewerWindow *self);
 static void virt_viewer_window_disable_modifiers(VirtViewerWindow *self);
@@ -316,7 +313,6 @@ virt_viewer_window_init (VirtViewerWindow *self)
     gtk_widget_set_sensitive(GTK_WIDGET(gtk_builder_get_object(self->priv->builder, "menu-send")), FALSE);
     gtk_widget_set_sensitive(GTK_WIDGET(gtk_builder_get_object(self->priv->builder, "menu-view-zoom")), FALSE);
     gtk_widget_set_sensitive(GTK_WIDGET(gtk_builder_get_object(self->priv->builder, "menu-file-screenshot")), FALSE);
-    gtk_widget_set_sensitive(GTK_WIDGET(gtk_builder_get_object(self->priv->builder, "menu-preferences")), FALSE);
 
     gtk_builder_connect_signals(priv->builder, self);
 
@@ -770,7 +766,7 @@ virt_viewer_window_delete(GtkWidget *src G_GNUC_UNUSED,
 }
 
 
-G_MODULE_EXPORT void
+static void
 virt_viewer_window_menu_file_quit(GtkWidget *src G_GNUC_UNUSED,
                                   VirtViewerWindow *self)
 {
@@ -967,13 +963,6 @@ virt_viewer_window_menu_file_smartcard_remove(GtkWidget *menu G_GNUC_UNUSED,
 }
 
 G_MODULE_EXPORT void
-virt_viewer_window_menu_preferences_cb(GtkWidget *menu G_GNUC_UNUSED,
-                                       VirtViewerWindow *self)
-{
-    virt_viewer_app_show_preferences(self->priv->app, self->priv->window);
-}
-
-G_MODULE_EXPORT void
 virt_viewer_window_menu_view_release_cursor(GtkWidget *menu G_GNUC_UNUSED,
                                             VirtViewerWindow *self)
 {
@@ -1027,38 +1016,6 @@ virt_viewer_window_guest_details_response(GtkDialog *dialog,
         gtk_widget_hide(GTK_WIDGET(dialog));
 }
 
-G_MODULE_EXPORT void
-virt_viewer_window_menu_help_about(GtkWidget *menu G_GNUC_UNUSED,
-                                   VirtViewerWindow *self)
-{
-    GtkBuilder *about;
-    GtkWidget *dialog;
-    GdkPixbuf *icon;
-
-    about = virt_viewer_util_load_ui("virt-viewer-about.ui");
-
-    dialog = GTK_WIDGET(gtk_builder_get_object(about, "about"));
-
-    gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(dialog), VERSION BUILDID);
-
-    icon = gdk_pixbuf_new_from_resource(VIRT_VIEWER_RESOURCE_PREFIX"/icons/48x48/virt-viewer.png", NULL);
-    if (icon != NULL) {
-        gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(dialog), icon);
-        g_object_unref(icon);
-    } else {
-        gtk_about_dialog_set_logo_icon_name(GTK_ABOUT_DIALOG(dialog), "virt-viewer");
-    }
-
-    gtk_window_set_transient_for(GTK_WINDOW(dialog),
-                                 GTK_WINDOW(self->priv->window));
-
-    gtk_builder_connect_signals(about, self);
-
-    gtk_widget_show_all(dialog);
-
-    g_object_unref(G_OBJECT(about));
-}
-
 static void
 iso_dialog_response(GtkDialog *dialog,
                     gint response_id,
@@ -1278,9 +1235,6 @@ virt_viewer_window_set_menus_sensitive(VirtViewerWindow *self, gboolean sensitiv
 
     priv = self->priv;
 
-    menu = GTK_WIDGET(gtk_builder_get_object(priv->builder, "menu-preferences"));
-    gtk_widget_set_sensitive(menu, sensitive);
-
     menu = GTK_WIDGET(gtk_builder_get_object(priv->builder, "menu-file-screenshot"));
     gtk_widget_set_sensitive(menu, sensitive);
 
@@ -1369,7 +1323,6 @@ virt_viewer_window_set_display(VirtViewerWindow *self, VirtViewerDisplay *displa
         if (virt_viewer_display_get_enabled(display))
             virt_viewer_window_desktop_resize(display, self);
 
-        gtk_widget_set_sensitive(GTK_WIDGET(gtk_builder_get_object(self->priv->builder, "menu-preferences")), TRUE);
         gtk_widget_set_sensitive(GTK_WIDGET(gtk_builder_get_object(self->priv->builder, "menu-view-zoom")), TRUE);
         gtk_widget_set_sensitive(GTK_WIDGET(gtk_builder_get_object(self->priv->builder, "menu-send")), TRUE);
         gtk_widget_set_sensitive(self->priv->toolbar_send_key, TRUE);
-- 
2.9.3




More information about the virt-tools-list mailing list