[gnome-builder/wip/plugins] source-view: add font zoom support



commit 217ab620caf80a639550331509ef4a5ffc713220
Author: Christian Hergert <christian hergert me>
Date:   Wed Jun 10 19:13:32 2015 -0700

    source-view: add font zoom support
    
     - zoom in: <ctrl><shift>plus
     - zoom out: <ctrl>minus
     - zoom 1: <ctrl>0

 data/keybindings/default.css |    4 ++
 data/keybindings/emacs.css   |    4 ++
 data/keybindings/vim.css     |    3 +
 libide/ide-source-view.c     |   93 +++++++++++++++++++++++++++++++++++++++++-
 libide/ide-source-view.h     |    3 +
 5 files changed, 105 insertions(+), 2 deletions(-)
---
diff --git a/data/keybindings/default.css b/data/keybindings/default.css
index 2d23efd..47301ca 100644
--- a/data/keybindings/default.css
+++ b/data/keybindings/default.css
@@ -16,6 +16,10 @@
   bind "<ctrl>k" { "action" ("view-stack", "show-list", "") };
   bind "<ctrl>d" { "delete-from-cursor" (paragraphs, 1) };
 
+  bind "<ctrl>minus" { "decrease-font-size" () };
+  bind "<ctrl>plus" { "increase-font-size" () };
+  bind "<ctrl>0" { "reset-font-size" () };
+
   /* cycle "tabs" */
   bind "<ctrl>Page_Up" { "action" ("view-stack", "previous-view", "") };
   bind "<ctrl>KP_Page_Up" { "action" ("view-stack", "previous-view", "") };
diff --git a/data/keybindings/emacs.css b/data/keybindings/emacs.css
index 396048b..7002cb5 100644
--- a/data/keybindings/emacs.css
+++ b/data/keybindings/emacs.css
@@ -67,6 +67,10 @@
               "clear-count" ()
               "clear-selection" () };
 
+  bind "<ctrl>minus" { "decrease-font-size" () };
+  bind "<ctrl>plus" { "increase-font-size" () };
+  bind "<ctrl>0" { "reset-font-size" () };
+
   /* swap between header/source */
   bind "<alt>o" { "action" ("view", "find-other-file", "") };
 
diff --git a/data/keybindings/vim.css b/data/keybindings/vim.css
index 53d5110..7c23d27 100644
--- a/data/keybindings/vim.css
+++ b/data/keybindings/vim.css
@@ -97,6 +97,9 @@
   bind "<ctrl><shift>s" { "action" ("view", "save-as", "") };
   bind "<ctrl><shift>o" { "action" ("workbench", "open", "") };
   bind "<ctrl>k" { "action" ("view-stack", "show-list", "") };
+  bind "<ctrl>minus" { "decrease-font-size" () };
+  bind "<ctrl>plus" { "increase-font-size" () };
+  bind "<ctrl>0" { "reset-font-size" () };
 
   bind "F4" { "action" ("view", "find-other-file", "") };
   bind "F6" { "action" ("view", "preview", "") };
diff --git a/libide/ide-source-view.c b/libide/ide-source-view.c
index 82cbd8c..f3be0fb 100644
--- a/libide/ide-source-view.c
+++ b/libide/ide-source-view.c
@@ -127,6 +127,8 @@ typedef struct
   GdkRGBA                      bubble_color1;
   GdkRGBA                      bubble_color2;
 
+  gdouble                      font_scale;
+
   guint                        auto_indent : 1;
   guint                        completion_blocked : 1;
   guint                        completion_visible : 1;
@@ -203,12 +205,14 @@ enum {
   CLEAR_SELECTION,
   CLEAR_SNIPPETS,
   CYCLE_COMPLETION,
+  DECREASE_FONT_SIZE,
   DELETE_SELECTION,
   END_MACRO,
   END_USER_ACTION,
   FOCUS_LOCATION,
   GOTO_DEFINITION,
   HIDE_COMPLETION,
+  INCREASE_FONT_SIZE,
   INDENT_SELECTION,
   INSERT_AT_CURSOR_AND_INDENT,
   INSERT_MODIFIER,
@@ -224,6 +228,7 @@ enum {
   REBUILD_HIGHLIGHT,
   REPLAY_MACRO,
   REQUEST_DOCUMENTATION,
+  RESET_FONT_SIZE,
   RESTORE_INSERT_MARK,
   SAVE_INSERT_MARK,
   SELECTION_THEATRIC,
@@ -1119,10 +1124,25 @@ ide_source_view_rebuild_css (IdeSourceView *self)
     {
       g_autofree gchar *str = NULL;
       g_autofree gchar *css = NULL;
+      const PangoFontDescription *font_desc = priv->font_desc;
+      PangoFontDescription *copy = NULL;
+
+      if (priv->font_scale != 1.0)
+        {
+          guint font_size;
+
+          copy = pango_font_description_copy (priv->font_desc);
+          font_size = pango_font_description_get_size (priv->font_desc);
+          pango_font_description_set_size (copy, font_size * priv->font_scale);
 
-      str = ide_pango_font_description_to_css (priv->font_desc);
+          font_desc = copy;
+        }
+
+      str = ide_pango_font_description_to_css (font_desc);
       css = g_strdup_printf ("IdeSourceView { %s }", str ?: "");
       gtk_css_provider_load_from_data (priv->css_provider, css, -1, NULL);
+
+      g_clear_pointer (&copy, pango_font_description_free);
     }
 }
 
@@ -4808,6 +4828,42 @@ ide_source_view_scroll_event (GtkWidget      *widget,
 }
 
 static void
+ide_source_view_real_reset_font_size (IdeSourceView *self)
+{
+  IdeSourceViewPrivate *priv = ide_source_view_get_instance_private (self);
+
+  g_assert (IDE_IS_SOURCE_VIEW (self));
+
+  priv->font_scale = 1.0;
+
+  ide_source_view_rebuild_css (self);
+}
+
+static void
+ide_source_view_real_increase_font_size (IdeSourceView *self)
+{
+  IdeSourceViewPrivate *priv = ide_source_view_get_instance_private (self);
+
+  g_assert (IDE_IS_SOURCE_VIEW (self));
+
+  priv->font_scale *= PANGO_SCALE_LARGE;
+
+  ide_source_view_rebuild_css (self);
+}
+
+static void
+ide_source_view_real_decrease_font_size (IdeSourceView *self)
+{
+  IdeSourceViewPrivate *priv = ide_source_view_get_instance_private (self);
+
+  g_assert (IDE_IS_SOURCE_VIEW (self));
+
+  priv->font_scale *= PANGO_SCALE_SMALL;
+
+  ide_source_view_rebuild_css (self);
+}
+
+static void
 ide_source_view_dispose (GObject *object)
 {
   IdeSourceView *self = (IdeSourceView *)object;
@@ -5096,13 +5152,15 @@ ide_source_view_class_init (IdeSourceViewClass *klass)
   klass->capture_modifier = ide_source_view_real_capture_modifier;
   klass->clear_count = ide_source_view_real_clear_count;
   klass->clear_modifier = ide_source_view_real_clear_modifier;
-  klass->clear_snippets = ide_source_view_clear_snippets;
   klass->clear_selection = ide_source_view_real_clear_selection;
+  klass->clear_snippets = ide_source_view_clear_snippets;
   klass->cycle_completion = ide_source_view_real_cycle_completion;
+  klass->decrease_font_size = ide_source_view_real_decrease_font_size;
   klass->delete_selection = ide_source_view_real_delete_selection;
   klass->end_macro = ide_source_view_real_end_macro;
   klass->goto_definition = ide_source_view_real_goto_definition;
   klass->hide_completion = ide_source_view_real_hide_completion;
+  klass->increase_font_size = ide_source_view_real_increase_font_size;
   klass->indent_selection = ide_source_view_real_indent_selection;
   klass->insert_at_cursor_and_indent = ide_source_view_real_insert_at_cursor_and_indent;
   klass->insert_modifier = ide_source_view_real_insert_modifier;
@@ -5116,6 +5174,7 @@ ide_source_view_class_init (IdeSourceViewClass *klass)
   klass->push_snippet = ide_source_view_real_push_snippet;
   klass->rebuild_highlight = ide_source_view_real_rebuild_highlight;
   klass->replay_macro = ide_source_view_real_replay_macro;
+  klass->reset_font_size = ide_source_view_real_reset_font_size;
   klass->restore_insert_mark = ide_source_view_real_restore_insert_mark;
   klass->save_insert_mark = ide_source_view_real_save_insert_mark;
   klass->selection_theatric = ide_source_view_real_selection_theatric;
@@ -5425,6 +5484,15 @@ ide_source_view_class_init (IdeSourceViewClass *klass)
                   1,
                   GTK_TYPE_DIRECTION_TYPE);
 
+  gSignals [DECREASE_FONT_SIZE] =
+    g_signal_new ("decrease-font-size",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+                  G_STRUCT_OFFSET (IdeSourceViewClass, decrease_font_size),
+                  NULL, NULL, NULL,
+                  G_TYPE_NONE,
+                  0);
+
   gSignals [DELETE_SELECTION] =
     g_signal_new ("delete-selection",
                   G_TYPE_FROM_CLASS (klass),
@@ -5489,6 +5557,15 @@ ide_source_view_class_init (IdeSourceViewClass *klass)
                   G_TYPE_NONE,
                   0);
 
+  gSignals [INCREASE_FONT_SIZE] =
+    g_signal_new ("increase-font-size",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+                  G_STRUCT_OFFSET (IdeSourceViewClass, increase_font_size),
+                  NULL, NULL, NULL,
+                  G_TYPE_NONE,
+                  0);
+
   gSignals [INDENT_SELECTION] =
     g_signal_new ("indent-selection",
                   G_TYPE_FROM_CLASS (klass),
@@ -5687,6 +5764,15 @@ ide_source_view_class_init (IdeSourceViewClass *klass)
                   G_TYPE_NONE,
                   0);
 
+  gSignals [RESET_FONT_SIZE] =
+    g_signal_new ("reset-font-size",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+                  G_STRUCT_OFFSET (IdeSourceViewClass, reset_font_size),
+                  NULL, NULL, NULL,
+                  G_TYPE_NONE,
+                  0);
+
   gSignals [RESTORE_INSERT_MARK] =
     g_signal_new ("restore-insert-mark",
                   G_TYPE_FROM_CLASS (klass),
@@ -5799,6 +5885,7 @@ ide_source_view_init (IdeSourceView *self)
   priv->snippets = g_queue_new ();
   priv->selections = g_queue_new ();
   priv->show_line_diagnostics = TRUE;
+  priv->font_scale = 1.0;
 
   priv->file_setting_bindings = egg_binding_group_new ();
   egg_binding_group_bind (priv->file_setting_bindings, "indent-width",
@@ -5931,6 +6018,8 @@ ide_source_view_set_font_desc (IdeSourceView              *self,
       else
         priv->font_desc = pango_font_description_from_string (DEFAULT_FONT_DESC);
 
+      priv->font_scale = 1.0;
+
       ide_source_view_rebuild_css (self);
     }
 }
diff --git a/libide/ide-source-view.h b/libide/ide-source-view.h
index f71f348..708d061 100644
--- a/libide/ide-source-view.h
+++ b/libide/ide-source-view.h
@@ -262,6 +262,9 @@ struct _IdeSourceViewClass
                                        gboolean                 ignore_case,
                                        gboolean                 reverse);
   void (*swap_selection_bounds)       (IdeSourceView           *self);
+  void (*increase_font_size)          (IdeSourceView           *self);
+  void (*decrease_font_size)          (IdeSourceView           *self);
+  void (*reset_font_size)             (IdeSourceView           *self);
 };
 
 void                        ide_source_view_clear_snippets            (IdeSourceView              *self);


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