[gnome-builder] terminal: move styling into IdeTerminal



commit 23ec6837794f2840b5b7b236ea5188843d30054e
Author: Christian Hergert <chergert redhat com>
Date:   Thu Nov 23 00:03:42 2017 -0800

    terminal: move styling into IdeTerminal
    
    This brings the styling out of GbTerminalView and moves it into
    IdeTerminal for easier re-use across the application.

 src/libide/terminal/ide-terminal.c      |  105 +++++++++++++++++++++++++++++++
 src/plugins/terminal/gb-terminal-view.c |   77 +----------------------
 2 files changed, 108 insertions(+), 74 deletions(-)
---
diff --git a/src/libide/terminal/ide-terminal.c b/src/libide/terminal/ide-terminal.c
index 4e3696f..90a2b28 100644
--- a/src/libide/terminal/ide-terminal.c
+++ b/src/libide/terminal/ide-terminal.c
@@ -27,6 +27,7 @@
 typedef struct
 {
   GtkWidget *popup_menu;
+  GSettings *settings;
   gchar     *url;
 } IdeTerminalPrivate;
 
@@ -53,6 +54,53 @@ enum {
 
 static guint signals[N_SIGNALS];
 static const gchar *url_regexes[] = { DINGUS1, DINGUS2 };
+static const GdkRGBA solarized_palette[] = {
+  /*
+   * Solarized palette (1.0.0beta2):
+   * http://ethanschoonover.com/solarized
+   */
+  { 0.02745,  0.211764, 0.258823, 1 },
+  { 0.862745, 0.196078, 0.184313, 1 },
+  { 0.521568, 0.6,      0,        1 },
+  { 0.709803, 0.537254, 0,        1 },
+  { 0.149019, 0.545098, 0.823529, 1 },
+  { 0.82745,  0.211764, 0.509803, 1 },
+  { 0.164705, 0.631372, 0.596078, 1 },
+  { 0.933333, 0.909803, 0.835294, 1 },
+  { 0,        0.168627, 0.211764, 1 },
+  { 0.796078, 0.294117, 0.086274, 1 },
+  { 0.345098, 0.431372, 0.458823, 1 },
+  { 0.396078, 0.482352, 0.513725, 1 },
+  { 0.513725, 0.580392, 0.588235, 1 },
+  { 0.423529, 0.443137, 0.768627, 1 },
+  { 0.57647,  0.631372, 0.631372, 1 },
+  { 0.992156, 0.964705, 0.890196, 1 },
+};
+
+static void
+style_context_changed (IdeTerminal *self,
+                       GtkStyleContext *style_context)
+{
+  GtkStateFlags state;
+  GdkRGBA fg;
+  GdkRGBA bg;
+
+  g_assert (GTK_IS_STYLE_CONTEXT (style_context));
+  g_assert (IDE_IS_TERMINAL (self));
+
+  state = gtk_style_context_get_state (style_context);
+
+  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
+  gtk_style_context_get_color (style_context, state, &fg);
+  gtk_style_context_get_background_color (style_context, state, &bg);
+  G_GNUC_END_IGNORE_DEPRECATIONS;
+
+  if (bg.alpha == 0.0)
+    gdk_rgba_parse (&bg, "#f6f7f8");
+
+  vte_terminal_set_colors (VTE_TERMINAL (self), &fg, &bg,
+                           solarized_palette, G_N_ELEMENTS (solarized_palette));
+}
 
 static void
 popup_menu_detach (GtkWidget *attach_widget,
@@ -271,11 +319,46 @@ ide_terminal_real_search_reveal (IdeTerminal *self)
 }
 
 static void
+ide_terminal_font_changed (IdeTerminal *self,
+                           const gchar *key,
+                           GSettings   *settings)
+{
+  PangoFontDescription *font_desc = NULL;
+  g_autofree gchar *font_name = NULL;
+
+  g_assert (IDE_IS_TERMINAL (self));
+  g_assert (G_IS_SETTINGS (settings));
+
+  font_name = g_settings_get_string (settings, "font-name");
+
+  if (font_name != NULL)
+    font_desc = pango_font_description_from_string (font_name);
+
+  vte_terminal_set_font (VTE_TERMINAL (self), font_desc);
+  g_clear_pointer (&font_desc, pango_font_description_free);
+}
+
+static void
+ide_terminal_destroy (GtkWidget *widget)
+{
+  IdeTerminal *self = (IdeTerminal *)widget;
+  IdeTerminalPrivate *priv = ide_terminal_get_instance_private (self);
+
+  g_assert (IDE_IS_TERMINAL (self));
+
+  g_clear_object (&priv->settings);
+  g_clear_pointer (&priv->url, g_free);
+
+  GTK_WIDGET_CLASS (ide_terminal_parent_class)->destroy (widget);
+}
+
+static void
 ide_terminal_class_init (IdeTerminalClass *klass)
 {
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
   GtkBindingSet *binding_set;
 
+  widget_class->destroy = ide_terminal_destroy;
   widget_class->button_press_event = ide_terminal_button_press_event;
   widget_class->popup_menu = ide_terminal_popup_menu;
 
@@ -355,6 +438,9 @@ ide_terminal_class_init (IdeTerminalClass *klass)
 static void
 ide_terminal_init (IdeTerminal *self)
 {
+  IdeTerminalPrivate *priv = ide_terminal_get_instance_private (self);
+  GtkStyleContext *style_context;
+
   dzl_widget_action_group_attach (self, "terminal");
 
   for (guint i = 0; i < G_N_ELEMENTS (url_regexes); i++)
@@ -369,6 +455,25 @@ ide_terminal_init (IdeTerminal *self)
       tag = vte_terminal_match_add_regex (VTE_TERMINAL (self), regex, 0);
       vte_terminal_match_set_cursor_type (VTE_TERMINAL (self), tag, GDK_HAND2);
     }
+
+  priv->settings = g_settings_new ("org.gnome.builder.terminal");
+  g_signal_connect_object (priv->settings,
+                           "changed::font-name",
+                           G_CALLBACK (ide_terminal_font_changed),
+                           self,
+                           G_CONNECT_SWAPPED);
+  ide_terminal_font_changed (self, NULL, priv->settings);
+
+  style_context = gtk_widget_get_style_context (GTK_WIDGET (self));
+  gtk_style_context_add_class (style_context, "terminal");
+  g_signal_connect_object (style_context,
+                           "changed",
+                           G_CALLBACK (style_context_changed),
+                           self,
+                           G_CONNECT_SWAPPED);
+  style_context_changed (self, style_context);
+
+  gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
 }
 
 GtkWidget *
diff --git a/src/plugins/terminal/gb-terminal-view.c b/src/plugins/terminal/gb-terminal-view.c
index 3b13d88..c49da74 100644
--- a/src/plugins/terminal/gb-terminal-view.c
+++ b/src/plugins/terminal/gb-terminal-view.c
@@ -37,7 +37,6 @@ G_DEFINE_TYPE (GbTerminalView, gb_terminal_view, IDE_TYPE_LAYOUT_VIEW)
 
 enum {
   PROP_0,
-  PROP_FONT_NAME,
   PROP_MANAGE_SPAWN,
   PROP_PTY,
   PROP_RUNTIME,
@@ -48,31 +47,6 @@ enum {
 static GParamSpec *properties[LAST_PROP];
 static gchar *cached_shell;
 
-/* TODO: allow palette to come from gnome-terminal. */
-static const GdkRGBA solarized_palette[] =
-{
-  /*
-   * Solarized palette (1.0.0beta2):
-   * http://ethanschoonover.com/solarized
-   */
-  { 0.02745,  0.211764, 0.258823, 1 },
-  { 0.862745, 0.196078, 0.184313, 1 },
-  { 0.521568, 0.6,      0,        1 },
-  { 0.709803, 0.537254, 0,        1 },
-  { 0.149019, 0.545098, 0.823529, 1 },
-  { 0.82745,  0.211764, 0.509803, 1 },
-  { 0.164705, 0.631372, 0.596078, 1 },
-  { 0.933333, 0.909803, 0.835294, 1 },
-  { 0,        0.168627, 0.211764, 1 },
-  { 0.796078, 0.294117, 0.086274, 1 },
-  { 0.345098, 0.431372, 0.458823, 1 },
-  { 0.396078, 0.482352, 0.513725, 1 },
-  { 0.513725, 0.580392, 0.588235, 1 },
-  { 0.423529, 0.443137, 0.768627, 1 },
-  { 0.57647,  0.631372, 0.631372, 1 },
-  { 0.992156, 0.964705, 0.890196, 1 },
-};
-
 static void gb_terminal_view_connect_terminal (GbTerminalView *self,
                                                VteTerminal    *terminal);
 static void gb_terminal_respawn               (GbTerminalView *self,
@@ -494,10 +468,6 @@ style_context_changed (GtkStyleContext *style_context,
   if (bg.alpha == 0.0)
     gdk_rgba_parse (&bg, "#f6f7f8");
 
-  vte_terminal_set_colors (self->terminal_top, &fg, &bg,
-                           solarized_palette,
-                           G_N_ELEMENTS (solarized_palette));
-
   ide_layout_view_set_primary_color_fg (IDE_LAYOUT_VIEW (self), &fg);
   ide_layout_view_set_primary_color_bg (IDE_LAYOUT_VIEW (self), &bg);
 }
@@ -505,35 +475,11 @@ style_context_changed (GtkStyleContext *style_context,
 static IdeLayoutView *
 gb_terminal_create_split_view (IdeLayoutView *view)
 {
-  IdeLayoutView *new_view;
-
   g_assert (GB_IS_TERMINAL_VIEW (view));
 
-  new_view = g_object_new (GB_TYPE_TERMINAL_VIEW,
-                          "visible", TRUE,
-                          NULL);
-
-  return new_view;
-}
-
-static void
-gb_terminal_view_set_font_name (GbTerminalView *self,
-                                const gchar    *font_name)
-{
-  g_assert (GB_IS_TERMINAL_VIEW (self));
-
-  if (font_name != NULL)
-    {
-      PangoFontDescription *font_desc = NULL;
-
-      font_desc = pango_font_description_from_string (font_name);
-
-      if (font_desc != NULL)
-        {
-          vte_terminal_set_font (self->terminal_top, font_desc);
-          pango_font_description_free (font_desc);
-        }
-    }
+  return g_object_new (GB_TYPE_TERMINAL_VIEW,
+                       "visible", TRUE,
+                       NULL);
 }
 
 static void
@@ -638,10 +584,6 @@ gb_terminal_view_set_property (GObject      *object,
 
   switch (prop_id)
     {
-    case PROP_FONT_NAME:
-      gb_terminal_view_set_font_name (self, g_value_get_string (value));
-      break;
-
     case PROP_MANAGE_SPAWN:
       self->manage_spawn = g_value_get_boolean (value);
       break;
@@ -686,15 +628,6 @@ gb_terminal_view_class_init (GbTerminalViewClass *klass)
   gtk_widget_class_bind_template_child (widget_class, GbTerminalView, top_scrollbar);
   gtk_widget_class_bind_template_child (widget_class, GbTerminalView, terminal_overlay_top);
 
-  g_type_ensure (VTE_TYPE_TERMINAL);
-
-  properties [PROP_FONT_NAME] =
-    g_param_spec_string ("font-name",
-                         "Font Name",
-                         "Font Name",
-                         NULL,
-                         (G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
-
   properties [PROP_MANAGE_SPAWN] =
     g_param_spec_boolean ("manage-spawn",
                           "Manage Spawn",
@@ -730,7 +663,6 @@ static void
 gb_terminal_view_init (GbTerminalView *self)
 {
   GtkStyleContext *style_context;
-  g_autoptr(GSettings) settings = NULL;
 
   self->run_on_host = TRUE;
   self->manage_spawn = TRUE;
@@ -753,9 +685,6 @@ gb_terminal_view_init (GbTerminalView *self)
 
   gb_terminal_view_actions_init (self);
 
-  settings = g_settings_new ("org.gnome.builder.terminal");
-  g_settings_bind (settings, "font-name", self, "font-name", G_SETTINGS_BIND_GET);
-
   style_context = gtk_widget_get_style_context (GTK_WIDGET (self));
   gtk_style_context_add_class (style_context, "terminal");
   g_signal_connect_object (style_context,


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