From 0dde04d6123c204e2d53af3293d94c09a6450a5f Mon Sep 17 00:00:00 2001 From: oowoosh0 Date: Thu, 5 May 2022 17:36:05 +0200 Subject: [PATCH 1/6] Add follow system style toggle to menu --- src/Widgets/HeaderBar.vala | 56 ++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/src/Widgets/HeaderBar.vala b/src/Widgets/HeaderBar.vala index 2ca4efef59..e559ebe117 100644 --- a/src/Widgets/HeaderBar.vala +++ b/src/Widgets/HeaderBar.vala @@ -129,6 +129,24 @@ namespace Scratch.Widgets { font_size_grid.add (zoom_default_button); font_size_grid.add (zoom_in_button); + var follow_system_label = new Gtk.Label (_("Follow System Style")) { + halign = Gtk.Align.START, + hexpand = true, + vexpand = false + }; + + var follow_system_switch = new Gtk.Switch () { + valign = Gtk.Align.START + }; + + var follow_system_grid = new Gtk.Grid (); + follow_system_grid.add (follow_system_label); + follow_system_grid.add (follow_system_switch); + + var follow_system_button = new Gtk.ModelButton (); + follow_system_button.get_child ().destroy (); + follow_system_button.add (follow_system_grid); + // Intentionally never attached so we can have a non-selected state var color_button_none = new Gtk.RadioButton (null); @@ -144,6 +162,21 @@ namespace Scratch.Widgets { color_button_dark.halign = Gtk.Align.CENTER; style_color_button (color_button_dark, STYLE_SCHEME_DARK); + var color_grid = new Gtk.Grid () { + column_homogeneous = true, + margin_start = 12, + margin_end = 12, + margin_top = 6, + margin_bottom = 6 + }; + + color_grid.add (color_button_white); + color_grid.add (color_button_light); + color_grid.add (color_button_dark); + + var color_revealer = new Gtk.Revealer (); + color_revealer.add (color_grid); + var menu_separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL); menu_separator.margin_top = 12; @@ -166,12 +199,11 @@ namespace Scratch.Widgets { menu_grid.orientation = Gtk.Orientation.VERTICAL; menu_grid.width_request = 200; menu_grid.attach (font_size_grid, 0, 0, 3, 1); - menu_grid.attach (color_button_white, 0, 1, 1, 1); - menu_grid.attach (color_button_light, 1, 1, 1, 1); - menu_grid.attach (color_button_dark, 2, 1, 1, 1); - menu_grid.attach (menu_separator, 0, 2, 3, 1); - menu_grid.attach (toggle_sidebar_menuitem, 0, 3, 3, 1); - menu_grid.attach (preferences_menuitem, 0, 6, 3); + menu_grid.attach (follow_system_button, 0, 1, 3, 1); + menu_grid.attach (color_revealer, 0, 2, 3, 1); + menu_grid.attach (menu_separator, 0, 3, 3, 1); + menu_grid.attach (toggle_sidebar_menuitem, 0, 4, 3, 1); + menu_grid.attach (preferences_menuitem, 0, 5, 3, 1); menu_grid.show_all (); var menu = new Gtk.Popover (null); @@ -217,6 +249,18 @@ namespace Scratch.Widgets { } }); + follow_system_button.button_release_event.connect (() => { + follow_system_switch.activate (); + return Gdk.EVENT_STOP; + }); + + follow_system_switch.bind_property ( + "active", + color_revealer, + "reveal-child", + GLib.BindingFlags.SYNC_CREATE | BindingFlags.INVERT_BOOLEAN + ); + var gtk_settings = Gtk.Settings.get_default (); switch (Scratch.settings.get_string ("style-scheme")) { From 51957f555380a18899a01e07d4bc56fd4b782215 Mon Sep 17 00:00:00 2001 From: oowoosh0 Date: Fri, 6 May 2022 14:43:14 +0200 Subject: [PATCH 2/6] Add follow-system-style entry to settings --- data/io.elementary.code.gschema.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/data/io.elementary.code.gschema.xml b/data/io.elementary.code.gschema.xml index 6ec10220c8..5236ea1c64 100644 --- a/data/io.elementary.code.gschema.xml +++ b/data/io.elementary.code.gschema.xml @@ -135,6 +135,11 @@ Request dark Gtk stylesheet variant Switches between dark and light style + + false + Use the Gtk stylesheet variant set by the system + Follow the system setting for dark or light Gtk style + false Whether search is cyclic From 80bb176f1000d2ab2b66b87ab41a2aaea5befee6 Mon Sep 17 00:00:00 2001 From: oowoosh0 Date: Wed, 11 May 2022 13:38:53 +0200 Subject: [PATCH 3/6] Implements logic behind follow system style toggle --- src/MainWindow.vala | 26 ++++++++++++++++++-- src/Widgets/HeaderBar.vala | 50 ++++++++++++++++++++++++-------------- 2 files changed, 56 insertions(+), 20 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index fef536c599..844de64e01 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -228,8 +228,9 @@ namespace Scratch { default_width = rect.width; default_height = rect.height; - var gtk_settings = Gtk.Settings.get_default (); - gtk_settings.gtk_application_prefer_dark_theme = Scratch.settings.get_boolean ("prefer-dark-style"); + set_color_scheme (); + Granite.Settings.get_default ().notify["prefers-color-scheme"].connect (set_color_scheme); + Scratch.settings.changed["follow-system-style"].connect (set_color_scheme); clipboard = Gtk.Clipboard.get_for_display (get_display (), Gdk.SELECTION_CLIPBOARD); @@ -450,6 +451,27 @@ namespace Scratch { set_widgets_sensitive (false); } + private void set_color_scheme () { + var gtk_settings = Gtk.Settings.get_default (); + var granite_settings = Granite.Settings.get_default (); + + if (Scratch.settings.get_boolean ("follow-system-style")) { + switch (granite_settings.prefers_color_scheme) { + case Granite.Settings.ColorScheme.DARK: + Scratch.settings.set_boolean ("prefer-dark-style", true); + Scratch.settings.set_string ("style-scheme", "solarized-dark"); + break; + default: + Scratch.settings.set_boolean ("prefer-dark-style", false); + Scratch.settings.set_string ("style-scheme", "solarized-light"); + break; + } + } + + gtk_settings.gtk_application_prefer_dark_theme = Scratch.settings.get_boolean ("prefer-dark-style"); + toolbar.activate_color_button (); + } + private void open_binary (File file) { if (!file.query_exists ()) { return; diff --git a/src/Widgets/HeaderBar.vala b/src/Widgets/HeaderBar.vala index e559ebe117..56a79776d7 100644 --- a/src/Widgets/HeaderBar.vala +++ b/src/Widgets/HeaderBar.vala @@ -30,6 +30,10 @@ namespace Scratch.Widgets { public Code.ChooseProjectButton choose_project_button; public Gtk.Revealer choose_project_revealer; + private Gtk.RadioButton color_button_none; + private Gtk.RadioButton color_button_white; + private Gtk.RadioButton color_button_light; + private Gtk.RadioButton color_button_dark; private const string STYLE_SCHEME_HIGH_CONTRAST = "classic"; private const string STYLE_SCHEME_LIGHT = "solarized-light"; private const string STYLE_SCHEME_DARK = "solarized-dark"; @@ -148,17 +152,17 @@ namespace Scratch.Widgets { follow_system_button.add (follow_system_grid); // Intentionally never attached so we can have a non-selected state - var color_button_none = new Gtk.RadioButton (null); + color_button_none = new Gtk.RadioButton (null); - var color_button_white = new Gtk.RadioButton.from_widget (color_button_none); + color_button_white = new Gtk.RadioButton.from_widget (color_button_none); color_button_white.halign = Gtk.Align.CENTER; style_color_button (color_button_white, STYLE_SCHEME_HIGH_CONTRAST); - var color_button_light = new Gtk.RadioButton.from_widget (color_button_none); + color_button_light = new Gtk.RadioButton.from_widget (color_button_none); color_button_light.halign = Gtk.Align.CENTER; style_color_button (color_button_light, STYLE_SCHEME_LIGHT); - var color_button_dark = new Gtk.RadioButton.from_widget (color_button_none); + color_button_dark = new Gtk.RadioButton.from_widget (color_button_none); color_button_dark.halign = Gtk.Align.CENTER; style_color_button (color_button_dark, STYLE_SCHEME_DARK); @@ -249,6 +253,13 @@ namespace Scratch.Widgets { } }); + Scratch.settings.bind ( + "follow-system-style", + follow_system_switch, + "active", + SettingsBindFlags.DEFAULT + ); + follow_system_button.button_release_event.connect (() => { follow_system_switch.activate (); return Gdk.EVENT_STOP; @@ -263,20 +274,6 @@ namespace Scratch.Widgets { var gtk_settings = Gtk.Settings.get_default (); - switch (Scratch.settings.get_string ("style-scheme")) { - case STYLE_SCHEME_HIGH_CONTRAST: - color_button_white.active = true; - break; - case STYLE_SCHEME_LIGHT: - color_button_light.active = true; - break; - case STYLE_SCHEME_DARK: - color_button_dark.active = true; - break; - default: - color_button_none.active = true; - } - color_button_dark.clicked.connect (() => { Scratch.settings.set_boolean ("prefer-dark-style", true); Scratch.settings.set_string ("style-scheme", STYLE_SCHEME_DARK); @@ -296,6 +293,23 @@ namespace Scratch.Widgets { }); } + public void activate_color_button () { + switch (Scratch.settings.get_string ("style-scheme")) { + case STYLE_SCHEME_HIGH_CONTRAST: + color_button_white.active = true; + break; + case STYLE_SCHEME_LIGHT: + color_button_light.active = true; + break; + case STYLE_SCHEME_DARK: + color_button_dark.active = true; + break; + default: + color_button_none.active = true; + break; + } + } + private void style_color_button (Gtk.Widget color_button, string style_id) { string background = "#FFF"; string foreground = "#333"; From 6c78f0ac3772e91d46763c825429a04d5809cdd5 Mon Sep 17 00:00:00 2001 From: oowoosh0 Date: Wed, 11 May 2022 13:50:15 +0200 Subject: [PATCH 4/6] Gets rid of critical runtime warning about header_bar being null --- src/MainWindow.vala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 844de64e01..6f1993525f 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -228,10 +228,6 @@ namespace Scratch { default_width = rect.width; default_height = rect.height; - set_color_scheme (); - Granite.Settings.get_default ().notify["prefers-color-scheme"].connect (set_color_scheme); - Scratch.settings.changed["follow-system-style"].connect (set_color_scheme); - clipboard = Gtk.Clipboard.get_for_display (get_display (), Gdk.SELECTION_CLIPBOARD); plugins = new Scratch.Services.PluginsManager (this, app.app_cmd_name.down ()); @@ -241,6 +237,10 @@ namespace Scratch { // Set up layout init_layout (); + set_color_scheme (); + Granite.Settings.get_default ().notify["prefers-color-scheme"].connect (set_color_scheme); + Scratch.settings.changed["follow-system-style"].connect (set_color_scheme); + var window_state = Scratch.saved_state.get_enum ("window-state"); switch (window_state) { case ScratchWindowState.MAXIMIZED: From 6fac7ccf6434929f3f151ca69fca719429ade5c4 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 8 Jul 2022 14:48:57 +0100 Subject: [PATCH 5/6] Add some column spacing --- src/Widgets/HeaderBar.vala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Widgets/HeaderBar.vala b/src/Widgets/HeaderBar.vala index 56a79776d7..6c3ed64297 100644 --- a/src/Widgets/HeaderBar.vala +++ b/src/Widgets/HeaderBar.vala @@ -143,7 +143,9 @@ namespace Scratch.Widgets { valign = Gtk.Align.START }; - var follow_system_grid = new Gtk.Grid (); + var follow_system_grid = new Gtk.Grid () { + column_spacing = 12 + }; follow_system_grid.add (follow_system_label); follow_system_grid.add (follow_system_switch); From 85a9cc6e148db5fa2c2d0849f4b350e47ae8b657 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 8 Jul 2022 16:22:53 +0100 Subject: [PATCH 6/6] Do not overwrite user style-scheme --- src/MainWindow.vala | 20 ++++++------------ src/Widgets/DocumentView.vala | 40 ++++++++++++++++++----------------- src/Widgets/HeaderBar.vala | 7 +++--- src/Widgets/SourceView.vala | 21 ++++++++++++------ 4 files changed, 47 insertions(+), 41 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 6f1993525f..4104647c6e 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -456,20 +456,14 @@ namespace Scratch { var granite_settings = Granite.Settings.get_default (); if (Scratch.settings.get_boolean ("follow-system-style")) { - switch (granite_settings.prefers_color_scheme) { - case Granite.Settings.ColorScheme.DARK: - Scratch.settings.set_boolean ("prefer-dark-style", true); - Scratch.settings.set_string ("style-scheme", "solarized-dark"); - break; - default: - Scratch.settings.set_boolean ("prefer-dark-style", false); - Scratch.settings.set_string ("style-scheme", "solarized-light"); - break; - } - } + gtk_settings.gtk_application_prefer_dark_theme = + granite_settings.prefers_color_scheme == Granite.Settings.ColorScheme.DARK; + Scratch.settings.changed ("style-scheme"); + } else { - gtk_settings.gtk_application_prefer_dark_theme = Scratch.settings.get_boolean ("prefer-dark-style"); - toolbar.activate_color_button (); + gtk_settings.gtk_application_prefer_dark_theme = Scratch.settings.get_boolean ("prefer-dark-style"); + toolbar.activate_color_button (); + } } private void open_binary (File file) { diff --git a/src/Widgets/DocumentView.vala b/src/Widgets/DocumentView.vala index e0299bdfb5..335a4c8919 100644 --- a/src/Widgets/DocumentView.vala +++ b/src/Widgets/DocumentView.vala @@ -98,7 +98,11 @@ public class Scratch.Widgets.DocumentView : Granite.Widgets.DynamicNotebook { ); update_inline_tab_colors (); - Scratch.settings.changed["style-scheme"].connect (update_inline_tab_colors); + Scratch.settings.changed.connect ((key) => { + if (key == "style-scheme" || key == "follow-system-style") { + update_inline_tab_colors (); + } + }); // Handle Drag-and-drop of files onto add-tab button to create document Gtk.TargetEntry uris = {"text/uri-list", 0, TargetType.URI_LIST}; @@ -107,25 +111,23 @@ public class Scratch.Widgets.DocumentView : Granite.Widgets.DynamicNotebook { } private void update_inline_tab_colors () { - var sssm = Gtk.SourceStyleSchemeManager.get_default (); - var style_scheme = Scratch.settings.get_string ("style-scheme"); - if (style_scheme in sssm.scheme_ids) { - var theme = sssm.get_scheme (style_scheme); - var text_color_data = theme.get_style ("text"); - - // Default gtksourceview background color is white - var color = "#FFFFFF"; - if (text_color_data != null) { - // If the current style has a background color, use that - color = text_color_data.background; - } + var theme = SourceView.get_style_scheme_from_settings (); + var text_color_data = theme.get_style ("text"); + + // Default gtksourceview background color is white + var color = "#FFFFFF"; + if (text_color_data != null) { + // If the current style has a background color, use that + color = text_color_data.background; + } else { + warning ("No text color in scheme"); + } - var define = "@define-color tab_base_color %s;".printf (color); - try { - style_provider.load_from_data (define); - } catch (Error e) { - critical ("Unable to set inline tab styling, going back to classic notebook tabs"); - } + var define = "@define-color tab_base_color %s;".printf (color); + try { + style_provider.load_from_data (define); + } catch (Error e) { + critical ("Unable to set inline tab styling, going back to classic notebook tabs"); } } diff --git a/src/Widgets/HeaderBar.vala b/src/Widgets/HeaderBar.vala index 6c3ed64297..ee190bd248 100644 --- a/src/Widgets/HeaderBar.vala +++ b/src/Widgets/HeaderBar.vala @@ -20,6 +20,9 @@ */ namespace Scratch.Widgets { + public const string STYLE_SCHEME_HIGH_CONTRAST = "classic"; + public const string STYLE_SCHEME_LIGHT = "solarized-light"; + public const string STYLE_SCHEME_DARK = "solarized-dark"; public class HeaderBar : Hdy.HeaderBar { public Gtk.Menu share_menu; public Gtk.MenuButton share_app_menu; @@ -34,9 +37,7 @@ namespace Scratch.Widgets { private Gtk.RadioButton color_button_white; private Gtk.RadioButton color_button_light; private Gtk.RadioButton color_button_dark; - private const string STYLE_SCHEME_HIGH_CONTRAST = "classic"; - private const string STYLE_SCHEME_LIGHT = "solarized-light"; - private const string STYLE_SCHEME_DARK = "solarized-dark"; + public HeaderBar () { Object ( diff --git a/src/Widgets/SourceView.vala b/src/Widgets/SourceView.vala index d14724d371..75a45ff186 100644 --- a/src/Widgets/SourceView.vala +++ b/src/Widgets/SourceView.vala @@ -22,7 +22,6 @@ namespace Scratch.Widgets { public class SourceView : Gtk.SourceView { public Gtk.SourceLanguageManager manager; - public Gtk.SourceStyleSchemeManager style_scheme_manager; public Gtk.CssProvider font_css_provider; public Gtk.TextTag warning_tag; public Gtk.TextTag error_tag; @@ -87,7 +86,6 @@ namespace Scratch.Widgets { expand = true; manager = Gtk.SourceLanguageManager.get_default (); - style_scheme_manager = new Gtk.SourceStyleSchemeManager (); font_css_provider = new Gtk.CssProvider (); get_style_context ().add_provider (font_css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION); @@ -115,8 +113,6 @@ namespace Scratch.Widgets { source_buffer.tag_table.add (error_tag); source_buffer.tag_table.add (warning_tag); - restore_settings (); - Gtk.drag_dest_add_uri_targets (this); restore_settings (); @@ -292,12 +288,25 @@ namespace Scratch.Widgets { critical (e.message); } - var scheme = style_scheme_manager.get_scheme (Scratch.settings.get_string ("style-scheme")); - source_buffer.style_scheme = scheme ?? style_scheme_manager.get_scheme ("classic"); + source_buffer.style_scheme = SourceView.get_style_scheme_from_settings (); git_diff_gutter_renderer.set_style_scheme (source_buffer.style_scheme); style_changed (source_buffer.style_scheme); } + public static Gtk.SourceStyleScheme get_style_scheme_from_settings () { + var sssm = Gtk.SourceStyleSchemeManager.get_default (); + string style_scheme = STYLE_SCHEME_LIGHT; + if (Scratch.settings.get_boolean ("follow-system-style")) { + if (Granite.Settings.get_default ().prefers_color_scheme == Granite.Settings.ColorScheme.DARK) { + style_scheme = STYLE_SCHEME_DARK; + } + } else { + style_scheme = Scratch.settings.get_string ("style-scheme"); + } + + return sssm.get_scheme (style_scheme) ?? sssm.get_scheme ("classic"); + } + private void update_settings () { var source_buffer = (Gtk.SourceBuffer) buffer; Scratch.settings.set_boolean ("show-right-margin", show_right_margin);