[gnome-text-editor] sourceview: implement zoom in/out/one



commit ed44e9b08565421d9234e2d7e0844d35ce24d40c
Author: Christian Hergert <chergert redhat com>
Date:   Wed Jul 28 19:09:23 2021 -0700

    sourceview: implement zoom in/out/one
    
    Fixes #96

 src/editor-source-view.c | 103 ++++++++++++++++++++++++++++++++++++++---------
 src/help-overlay.ui      |  23 +++++++++++
 2 files changed, 106 insertions(+), 20 deletions(-)
---
diff --git a/src/editor-source-view.c b/src/editor-source-view.c
index 481305c..8187cf9 100644
--- a/src/editor-source-view.c
+++ b/src/editor-source-view.c
@@ -33,6 +33,7 @@ struct _EditorSourceView
   PangoFontDescription *font_desc;
   GMenuModel *spelling_menu;
   char *spelling_word;
+  int font_scale;
 };
 
 G_DEFINE_TYPE (EditorSourceView, editor_source_view, GTK_SOURCE_TYPE_VIEW)
@@ -45,6 +46,56 @@ enum {
 
 static GParamSpec *properties [N_PROPS];
 
+static void
+editor_source_view_update_css (EditorSourceView *self)
+{
+  const PangoFontDescription *font_desc;
+  PangoFontDescription *scaled = NULL;
+  g_autoptr(GString) str = NULL;
+  g_autofree char *font_css = NULL;
+  int size = 11; /* 11pt */
+
+  g_assert (EDITOR_IS_SOURCE_VIEW (self));
+
+  if (self->font_scale == 0 && self->font_desc == NULL)
+    {
+      gtk_css_provider_load_from_data (self->css_provider, "", -1);
+      return;
+    }
+
+  if (self->font_desc != NULL &&
+      pango_font_description_get_set_fields (self->font_desc) & PANGO_FONT_MASK_SIZE)
+    size = pango_font_description_get_size (self->font_desc) / PANGO_SCALE;
+
+  size = MAX (1, size + self->font_scale);
+
+  font_desc = self->font_desc;
+
+  if (size != 0)
+    {
+      if (font_desc)
+        scaled = pango_font_description_copy (font_desc);
+      else
+        scaled = pango_font_description_new ();
+      pango_font_description_set_size (scaled, size * PANGO_SCALE);
+      font_desc = scaled;
+    }
+
+  str = g_string_new ("textview {\n");
+  if (font_desc)
+    {
+
+      font_css = _editor_font_description_to_css (font_desc);
+      g_string_append (str, font_css);
+      g_string_append_c (str, '\n');
+    }
+  g_string_append (str, "}\n");
+
+  gtk_css_provider_load_from_data (self->css_provider, str->str, -1);
+
+  g_clear_pointer (&scaled, pango_font_description_free);
+}
+
 static gboolean
 on_key_pressed_cb (GtkEventControllerKey *key,
                    guint                  keyval,
@@ -276,6 +327,27 @@ editor_source_view_action_spelling_correct (GtkWidget  *widget,
   gtk_text_buffer_end_user_action (buffer);
 }
 
+static void
+editor_source_view_action_zoom (GtkWidget  *widget,
+                                const char *action_name,
+                                GVariant   *param)
+{
+  EditorSourceView *self = (EditorSourceView *)widget;
+
+  g_assert (EDITOR_IS_SOURCE_VIEW (self));
+
+  if (g_strcmp0 (action_name, "page.zoom-in") == 0)
+    self->font_scale++;
+  else if (g_strcmp0 (action_name, "page.zoom-out") == 0)
+    self->font_scale--;
+  else if (g_strcmp0 (action_name, "page.zoom-one") == 0)
+    self->font_scale = 0;
+  else
+    g_assert_not_reached ();
+
+  editor_source_view_update_css (self);
+}
+
 static void
 editor_source_view_finalize (GObject *object)
 {
@@ -345,9 +417,18 @@ editor_source_view_class_init (EditorSourceViewClass *klass)
 
   g_object_class_install_properties (object_class, N_PROPS, properties);
 
+  gtk_widget_class_install_action (widget_class, "page.zoom-in", NULL, editor_source_view_action_zoom);
+  gtk_widget_class_install_action (widget_class, "page.zoom-out", NULL, editor_source_view_action_zoom);
+  gtk_widget_class_install_action (widget_class, "page.zoom-one", NULL, editor_source_view_action_zoom);
   gtk_widget_class_install_action (widget_class, "spelling.add", NULL, 
editor_source_view_action_spelling_add);
   gtk_widget_class_install_action (widget_class, "spelling.ignore", NULL, 
editor_source_view_action_spelling_ignore);
   gtk_widget_class_install_action (widget_class, "spelling.correct", "s", 
editor_source_view_action_spelling_correct);
+
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_plus, GDK_CONTROL_MASK, "page.zoom-in", NULL);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_KP_Add, GDK_CONTROL_MASK, "page.zoom-in", NULL);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_minus, GDK_CONTROL_MASK, "page.zoom-out", NULL);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_KP_Subtract, GDK_CONTROL_MASK, "page.zoom-out", 
NULL);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_0, GDK_CONTROL_MASK, "page.zoom-one", NULL);
 }
 
 static void
@@ -407,26 +488,6 @@ editor_source_view_init (EditorSourceView *self)
   gtk_text_view_set_extra_menu (GTK_TEXT_VIEW (self), G_MENU_MODEL (joined));
 }
 
-static void
-editor_source_view_update_css (EditorSourceView *self)
-{
-  g_autoptr(GString) str = NULL;
-  g_autofree char *font_css = NULL;
-
-  g_assert (EDITOR_IS_SOURCE_VIEW (self));
-
-  str = g_string_new ("textview {\n");
-  if (self->font_desc)
-    {
-      font_css = _editor_font_description_to_css (self->font_desc);
-      g_string_append (str, font_css);
-      g_string_append_c (str, '\n');
-    }
-  g_string_append (str, "}\n");
-
-  gtk_css_provider_load_from_data (self->css_provider, str->str, -1);
-}
-
 const PangoFontDescription *
 editor_source_view_get_font_desc (EditorSourceView *self)
 {
@@ -451,6 +512,8 @@ editor_source_view_set_font_desc (EditorSourceView           *self,
   if (font_desc)
     self->font_desc = pango_font_description_copy (font_desc);
 
+  self->font_scale = 0;
+
   editor_source_view_update_css (self);
 
   g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_FONT_DESC]);
diff --git a/src/help-overlay.ui b/src/help-overlay.ui
index 86bc0dc..f57514d 100644
--- a/src/help-overlay.ui
+++ b/src/help-overlay.ui
@@ -47,6 +47,29 @@
             </child>
           </object>
         </child>
+        <child>
+          <object class="GtkShortcutsGroup">
+            <property name="title" translatable="yes" context="shortcut window">Zoom</property>
+            <child>
+              <object class="GtkShortcutsShortcut">
+                <property name="accelerator">&lt;ctrl&gt;plus</property>
+                <property name="title" translatable="yes" context="shortcut window">Zoom In</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkShortcutsShortcut">
+                <property name="accelerator">&lt;ctrl&gt;minus</property>
+                <property name="title" translatable="yes" context="shortcut window">Zoom Out</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkShortcutsShortcut">
+                <property name="accelerator">&lt;ctrl&gt;0</property>
+                <property name="title" translatable="yes" context="shortcut window">Reset Zoom</property>
+              </object>
+            </child>
+          </object>
+        </child>
         <child>
           <object class="GtkShortcutsGroup">
             <property name="title" translatable="yes" context="shortcut window">Documents</property>


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