From 1359d0944478c36ed64ce00054ab2c5d152e0459 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 18 Feb 2024 11:45:14 +0000 Subject: [PATCH] Draw spaces on current line option (#1348) * Add ScratchDrawSpaceState option CURRENT and implement * Provide for choosing between 3 drawing space states * Correct schema * Use ComboBox, simplify --------- Co-authored-by: Ryan Kornheisl --- data/io.elementary.code.gschema.xml | 1 + src/Dialogs/PreferencesDialog.vala | 48 +++++++++++++---------------- src/Services/Settings.vala | 3 +- src/Widgets/SourceView.vala | 19 +++++++----- 4 files changed, 37 insertions(+), 34 deletions(-) diff --git a/data/io.elementary.code.gschema.xml b/data/io.elementary.code.gschema.xml index f51bd221ea..400fe64e48 100644 --- a/data/io.elementary.code.gschema.xml +++ b/data/io.elementary.code.gschema.xml @@ -9,6 +9,7 @@ + diff --git a/src/Dialogs/PreferencesDialog.vala b/src/Dialogs/PreferencesDialog.vala index eaacfe9226..c3e20ead6c 100644 --- a/src/Dialogs/PreferencesDialog.vala +++ b/src/Dialogs/PreferencesDialog.vala @@ -132,12 +132,28 @@ 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 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"); @@ -168,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_switch, 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); @@ -197,25 +213,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 248780de56..be059dda74 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 = 4 } } diff --git a/src/Widgets/SourceView.vala b/src/Widgets/SourceView.vala index 5589de04f5..a9fecd3ed4 100644 --- a/src/Widgets/SourceView.vala +++ b/src/Widgets/SourceView.vala @@ -119,8 +119,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 (); @@ -255,6 +253,7 @@ namespace Scratch.Widgets { ); break; case ScratchDrawSpacesState.FOR_SELECTION: + case ScratchDrawSpacesState.CURRENT: space_drawer.set_types_for_locations ( Gtk.SourceSpaceLocationFlags.ALL, Gtk.SourceSpaceTypeFlags.NONE @@ -534,13 +533,19 @@ 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.CURRENT | ScratchDrawSpacesState.ALWAYS)) { + + buffer.apply_tag_by_name ("draw_spaces", start, end); + return; + } + + if (draw_spaces_state == ScratchDrawSpacesState.CURRENT && + get_current_line (out start, out end)) { + buffer.apply_tag_by_name ("draw_spaces", start, end); - } } }