Преглед на файлове

Use action to preview HTML with C-m and button

Matthias Vogelgesang преди 7 години
родител
ревизия
dfa317c824
променени са 3 файла, в които са добавени 29 реда и са изтрити 11 реда
  1. 25 10
      src/iridium-window.c
  2. 2 1
      src/iridium-window.ui
  3. 2 0
      src/main.c

+ 25 - 10
src/iridium-window.c

@@ -43,7 +43,6 @@ struct _IridiumWindow
   GtkWidget         *main_pane;
   GtkSearchBar      *search_bar;
   GtkSearchEntry    *search_entry;
-  GtkToggleButton   *toggle_html_view;
   GtkRevealer       *notification_revealer;
   WebKitWebView     *html_view;
   GBinding          *title_binding;
@@ -116,11 +115,29 @@ on_note_selected (IridiumWindow *self, IridiumNoteRow *row, gpointer user_data)
 }
 
 static void
-search_activated (GAction *action, GVariant *param, IridiumWindow *self)
+search_activated (GSimpleAction *action, GVariant *param, gpointer user_data)
 {
+  IridiumWindow *self;
+
+  self = IRIDIUM_WINDOW (user_data);
   gtk_search_bar_set_search_mode (self->search_bar, !gtk_search_bar_get_search_mode (self->search_bar));
 }
 
+static void
+preview_toggled (GSimpleAction *action, GVariant *param, gpointer user_data)
+{
+  IridiumWindow *self;
+  GVariant *state;
+  gboolean visible;
+
+  self = IRIDIUM_WINDOW (user_data);
+  state = g_action_get_state (G_ACTION (action));
+  visible = !g_variant_get_boolean (state);
+  g_simple_action_set_state (action, g_variant_new_boolean (visible));
+  gtk_widget_set_visible (GTK_WIDGET (self->html_view), visible);
+  g_variant_unref (state);
+}
+
 static gboolean
 note_visible (IridiumNoteRow *row, IridiumWindow *window)
 {
@@ -310,7 +327,6 @@ iridium_window_class_init (IridiumWindowClass *klass)
   gtk_widget_class_bind_template_child (widget_class, IridiumWindow, search_bar);
   gtk_widget_class_bind_template_child (widget_class, IridiumWindow, search_entry);
   gtk_widget_class_bind_template_child (widget_class, IridiumWindow, html_view);
-  gtk_widget_class_bind_template_child (widget_class, IridiumWindow, toggle_html_view);
   gtk_widget_class_bind_template_child (widget_class, IridiumWindow, notification_revealer);
 
   gtk_widget_class_bind_template_callback (widget_class, on_tag_selected);
@@ -326,12 +342,16 @@ iridium_window_init (IridiumWindow *self)
   GtkSourceLanguageManager *manager;
   GtkStyleProvider *provider;
   GtkSourceBuffer *buffer;
-  GSimpleAction *action;
   GBytes *style_data;
   WebKitUserContentManager *content_manager;
   WebKitUserStyleSheet *stylesheet;
   GError *error = NULL;
 
+  static GActionEntry entries[] = {
+    { "search",     search_activated,   NULL, NULL,     NULL },
+    { "preview",    NULL,               NULL, "false",  preview_toggled },
+  };
+
   gtk_widget_init_template (GTK_WIDGET (self));
 
   self->settings = g_settings_new ("net.bloerg.Iridium");
@@ -348,13 +368,8 @@ iridium_window_init (IridiumWindow *self)
   webkit_user_content_manager_add_style_sheet (content_manager, stylesheet);
   g_bytes_unref (style_data);
 
-  action = g_simple_action_new ("search", NULL);
-  g_action_map_add_action (G_ACTION_MAP (self), G_ACTION (action));
-  g_signal_connect (action, "activate", G_CALLBACK (search_activated), self);
-  g_object_unref (action);
+  g_action_map_add_action_entries (G_ACTION_MAP (self), entries, G_N_ELEMENTS (entries), self);
 
-  g_object_bind_property (self->toggle_html_view, "active",
-                          self->html_view, "visible", G_BINDING_DEFAULT);
   gtk_search_bar_connect_entry (self->search_bar, GTK_ENTRY (self->search_entry));
 
   manager = gtk_source_language_manager_get_default ();

+ 2 - 1
src/iridium-window.ui

@@ -10,8 +10,9 @@
         <property name="show-close-button">True</property>
         <property name="title">Iridium</property>
         <child>
-          <object class="GtkToggleButton" id="toggle_html_view">
+          <object class="GtkToggleButton">
             <property name="visible">True</property>
+            <property name="action_name">win.preview</property>
             <child>
               <object class="GtkImage">
                 <property name="visible">True</property>

+ 2 - 0
src/main.c

@@ -39,12 +39,14 @@ static void
 on_startup (GtkApplication *app)
 {
   const gchar *search_accels[] = { "<Ctrl>F", NULL };
+  const gchar *preview_accels[] = { "<Ctrl>M", NULL };
   const gchar *quit_accels[] = { "<Ctrl>Q", NULL };
 
   g_action_map_add_action_entries (G_ACTION_MAP (app), action_entries, G_N_ELEMENTS (action_entries), app);
 
   gtk_application_set_accels_for_action (app, "app.quit", quit_accels);
   gtk_application_set_accels_for_action (app, "win.search", search_accels);
+  gtk_application_set_accels_for_action (app, "win.preview", preview_accels);
 }
 
 static void