From ac35fe9e678c622e5eb829c64670cfd243515351 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 7 Jul 2023 15:34:26 +0100 Subject: [PATCH 1/4] Add ScratchDrawSpaceState option CURRENT and implement --- data/io.elementary.code.gschema.xml | 1 + src/Services/Settings.vala | 3 ++- src/Widgets/SourceView.vala | 15 ++++++++++----- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/data/io.elementary.code.gschema.xml b/data/io.elementary.code.gschema.xml index 1794882fa9..7833d7835e 100644 --- a/data/io.elementary.code.gschema.xml +++ b/data/io.elementary.code.gschema.xml @@ -9,6 +9,7 @@ + diff --git a/src/Services/Settings.vala b/src/Services/Settings.vala index 248780de56..86b6edf14b 100644 --- a/src/Services/Settings.vala +++ b/src/Services/Settings.vala @@ -30,6 +30,7 @@ namespace Scratch { public enum ScratchDrawSpacesState { NEVER = 0, FOR_SELECTION = 1, - ALWAYS = 2 + ALWAYS = 2, + CURRENT = 3 } } diff --git a/src/Widgets/SourceView.vala b/src/Widgets/SourceView.vala index 349d3007c5..d85684d8c3 100644 --- a/src/Widgets/SourceView.vala +++ b/src/Widgets/SourceView.vala @@ -501,13 +501,18 @@ namespace Scratch.Widgets { Gtk.TextIter start, end; var selection = buffer.get_selection_bounds (out start, out end); - + var draw_spaces_state = (ScratchDrawSpacesState) Scratch.settings.get_enum ("draw-spaces"); /* Draw spaces in selection the same way if drawn at all */ - if (selection) { - var draw_spaces_state = (ScratchDrawSpacesState) Scratch.settings.get_enum ("draw-spaces"); - if (draw_spaces_state in (ScratchDrawSpacesState.FOR_SELECTION | ScratchDrawSpacesState.ALWAYS)) { + if (selection && + draw_spaces_state in (ScratchDrawSpacesState.FOR_SELECTION | ScratchDrawSpacesState.ALWAYS)) { + + buffer.apply_tag_by_name ("draw_spaces", start, end); + } + + if (draw_spaces_state == ScratchDrawSpacesState.CURRENT && + get_current_line (out start, out end)) { + buffer.apply_tag_by_name ("draw_spaces", start, end); - } } } From 86744ae8e1fda1c1427d5adbab0ec657d7406cfa Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 7 Jul 2023 16:49:51 +0100 Subject: [PATCH 2/4] Provide for choosing between 3 drawing space states --- src/Dialogs/PreferencesDialog.vala | 83 ++++++++++++++++++++---------- src/Services/Settings.vala | 2 +- src/Widgets/SourceView.vala | 6 +-- 3 files changed, 61 insertions(+), 30 deletions(-) diff --git a/src/Dialogs/PreferencesDialog.vala b/src/Dialogs/PreferencesDialog.vala index f5878982e6..fe4bd38de6 100644 --- a/src/Dialogs/PreferencesDialog.vala +++ b/src/Dialogs/PreferencesDialog.vala @@ -119,11 +119,45 @@ namespace Scratch.Dialogs { var line_wrap_label = new SettingsLabel (_("Line wrap:")); var line_wrap = new SettingsSwitch ("line-wrap"); - var draw_spaces_label = new SettingsLabel (_("Visible whitespace:")); - var draw_spaces_switch = new DrawSpacesSwitch () { - halign = Gtk.Align.START, - valign = Gtk.Align.CENTER - }; + var draw_spaces_label = new SettingsLabel (_("White space visible when not selected:")); + + var never_button = new Gtk.RadioButton.with_label (null, _("None")); + never_button.set_mode (false); + never_button.set_data ("id", 0); + + var current_button = new Gtk.RadioButton.with_label_from_widget (never_button, _("Current Line")); + current_button.set_mode (false); + current_button.set_data ("id", 1); + + var always_button = new Gtk.RadioButton.with_label_from_widget (never_button, _("All")); + always_button.set_mode (false); + always_button.set_data ("id", 2); + + + switch (Scratch.settings.get_enum ("draw-spaces")) { + case 0: + never_button.active = true; + break; + case 1: + current_button.active = true; + break; + case 2: + always_button.active = true; + break; + default: + warning ("unrecognized draw space state"); + never_button.active = true; + break; + } + + never_button.toggled.connect (on_whitespace_toggled); + current_button.toggled.connect (on_whitespace_toggled); + always_button.toggled.connect (on_whitespace_toggled); + + var draw_spaces_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0); + draw_spaces_box.add (never_button); + draw_spaces_box.add (current_button); + draw_spaces_box.add (always_button); var show_mini_map_label = new SettingsLabel (_("Show Mini Map:")); show_mini_map = new SettingsSwitch ("show-mini-map"); @@ -155,7 +189,7 @@ namespace Scratch.Dialogs { content.attach (line_wrap_label, 0, 3, 1, 1); content.attach (line_wrap, 1, 3, 1, 1); content.attach (draw_spaces_label, 0, 4, 1, 1); - content.attach (draw_spaces_switch, 1, 4, 2, 1); + content.attach (draw_spaces_box, 1, 4, 2, 1); content.attach (show_mini_map_label, 0, 5, 1, 1); content.attach (show_mini_map, 1, 5, 1, 1); content.attach (show_right_margin_label, 0, 6, 1, 1); @@ -169,6 +203,23 @@ namespace Scratch.Dialogs { return content; } + private void on_whitespace_toggled (GLib.Object source) { + var toggle_button = (Gtk.ToggleButton)source; + if (toggle_button.active) { + switch (source.get_data ("id")) { + case 0: + Scratch.settings.set_enum ("draw-spaces", (int)ScratchDrawSpacesState.NEVER); + break; + case 1: + Scratch.settings.set_enum ("draw-spaces", (int)ScratchDrawSpacesState.CURRENT); + break; + case 2: + Scratch.settings.set_enum ("draw-spaces", (int)ScratchDrawSpacesState.ALWAYS); + break; + } + } + } + private class SettingsLabel : Gtk.Label { public SettingsLabel (string text) { label = text; @@ -184,25 +235,5 @@ namespace Scratch.Dialogs { Scratch.settings.bind (setting, this, "active", SettingsBindFlags.DEFAULT); } } - - private class DrawSpacesSwitch : Gtk.Switch { - public string string_value { - get { - return active ? "Always" : "For Selection"; - } - set { - active = (value == "Always"); - } - } - - public DrawSpacesSwitch () { - halign = Gtk.Align.START; - valign = Gtk.Align.CENTER; - notify["active"].connect (() => { - string_value = active ? "Always" : "For Selection"; - }); - Scratch.settings.bind ("draw-spaces", this, "string_value", SettingsBindFlags.DEFAULT); - } - } } } diff --git a/src/Services/Settings.vala b/src/Services/Settings.vala index 86b6edf14b..be059dda74 100644 --- a/src/Services/Settings.vala +++ b/src/Services/Settings.vala @@ -31,6 +31,6 @@ namespace Scratch { NEVER = 0, FOR_SELECTION = 1, ALWAYS = 2, - CURRENT = 3 + CURRENT = 4 } } diff --git a/src/Widgets/SourceView.vala b/src/Widgets/SourceView.vala index d85684d8c3..adb2cd6e9a 100644 --- a/src/Widgets/SourceView.vala +++ b/src/Widgets/SourceView.vala @@ -117,8 +117,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 (); @@ -252,6 +250,7 @@ namespace Scratch.Widgets { ); break; case ScratchDrawSpacesState.FOR_SELECTION: + case ScratchDrawSpacesState.CURRENT: space_drawer.set_types_for_locations ( Gtk.SourceSpaceLocationFlags.ALL, Gtk.SourceSpaceTypeFlags.NONE @@ -504,9 +503,10 @@ namespace Scratch.Widgets { var draw_spaces_state = (ScratchDrawSpacesState) Scratch.settings.get_enum ("draw-spaces"); /* Draw spaces in selection the same way if drawn at all */ if (selection && - draw_spaces_state in (ScratchDrawSpacesState.FOR_SELECTION | ScratchDrawSpacesState.ALWAYS)) { + draw_spaces_state in (ScratchDrawSpacesState.FOR_SELECTION | ScratchDrawSpacesState.CURRENT | ScratchDrawSpacesState.ALWAYS)) { buffer.apply_tag_by_name ("draw_spaces", start, end); + return; } if (draw_spaces_state == ScratchDrawSpacesState.CURRENT && From b1ff9b53728c00d3b0aec7a3a57f679bc702f157 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 7 Jul 2023 16:57:38 +0100 Subject: [PATCH 3/4] Correct schema --- data/io.elementary.code.gschema.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/io.elementary.code.gschema.xml b/data/io.elementary.code.gschema.xml index 7833d7835e..293f6913a3 100644 --- a/data/io.elementary.code.gschema.xml +++ b/data/io.elementary.code.gschema.xml @@ -9,7 +9,7 @@ - + From 759738575ef828b6c95715cbd4f416ba49a2dcf2 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 11 Feb 2024 18:19:22 +0000 Subject: [PATCH 4/4] Use ComboBox, simplify --- src/Dialogs/PreferencesDialog.vala | 77 ++++++++---------------------- 1 file changed, 21 insertions(+), 56 deletions(-) diff --git a/src/Dialogs/PreferencesDialog.vala b/src/Dialogs/PreferencesDialog.vala index 480cc5a5de..c3e20ead6c 100644 --- a/src/Dialogs/PreferencesDialog.vala +++ b/src/Dialogs/PreferencesDialog.vala @@ -134,44 +134,26 @@ namespace Scratch.Dialogs { var draw_spaces_label = new SettingsLabel (_("White space visible when not selected:")); - var never_button = new Gtk.RadioButton.with_label (null, _("None")); - never_button.set_mode (false); - never_button.set_data ("id", 0); - - var current_button = new Gtk.RadioButton.with_label_from_widget (never_button, _("Current Line")); - current_button.set_mode (false); - current_button.set_data ("id", 1); - - var always_button = new Gtk.RadioButton.with_label_from_widget (never_button, _("All")); - always_button.set_mode (false); - always_button.set_data ("id", 2); - - - switch (Scratch.settings.get_enum ("draw-spaces")) { - case 0: - never_button.active = true; - break; - case 1: - current_button.active = true; - break; - case 2: - always_button.active = true; - break; - default: - warning ("unrecognized draw space state"); - never_button.active = true; - break; - } - - never_button.toggled.connect (on_whitespace_toggled); - current_button.toggled.connect (on_whitespace_toggled); - always_button.toggled.connect (on_whitespace_toggled); - - var draw_spaces_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0); - draw_spaces_box.add (never_button); - draw_spaces_box.add (current_button); - draw_spaces_box.add (always_button); - + var drawspaces_combobox = new Gtk.ComboBoxText () { + hexpand = true + }; + drawspaces_combobox.append_text (_("None")); + drawspaces_combobox.append_text (_("Current Line")); + drawspaces_combobox.append_text (_("All")); + drawspaces_combobox.active = Scratch.settings.get_enum ("draw-spaces").clamp (0, 2); + drawspaces_combobox.changed.connect (() => { + switch (drawspaces_combobox.active) { + case 0: + Scratch.settings.set_enum ("draw-spaces", (int)ScratchDrawSpacesState.NEVER); + break; + case 1: + Scratch.settings.set_enum ("draw-spaces", (int)ScratchDrawSpacesState.CURRENT); + break; + case 2: + Scratch.settings.set_enum ("draw-spaces", (int)ScratchDrawSpacesState.ALWAYS); + break; + } + }); var show_mini_map_label = new SettingsLabel (_("Show Mini Map:")); show_mini_map = new SettingsSwitch ("show-mini-map"); @@ -202,7 +184,7 @@ namespace Scratch.Dialogs { content.attach (line_wrap_label, 0, 3, 1, 1); content.attach (line_wrap, 1, 3, 1, 1); content.attach (draw_spaces_label, 0, 4, 1, 1); - content.attach (draw_spaces_box, 1, 4, 2, 1); + content.attach (drawspaces_combobox, 1, 4, 2, 1); content.attach (show_mini_map_label, 0, 5, 1, 1); content.attach (show_mini_map, 1, 5, 1, 1); content.attach (show_right_margin_label, 0, 6, 1, 1); @@ -216,23 +198,6 @@ namespace Scratch.Dialogs { return content; } - private void on_whitespace_toggled (GLib.Object source) { - var toggle_button = (Gtk.ToggleButton)source; - if (toggle_button.active) { - switch (source.get_data ("id")) { - case 0: - Scratch.settings.set_enum ("draw-spaces", (int)ScratchDrawSpacesState.NEVER); - break; - case 1: - Scratch.settings.set_enum ("draw-spaces", (int)ScratchDrawSpacesState.CURRENT); - break; - case 2: - Scratch.settings.set_enum ("draw-spaces", (int)ScratchDrawSpacesState.ALWAYS); - break; - } - } - } - private class SettingsLabel : Gtk.Label { public SettingsLabel (string text) { label = text;