Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw spaces on current line option #1348

Merged
merged 7 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/io.elementary.code.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<value nick="Never" value="0" />
<value nick="For Selection" value="1" />
<value nick="Always" value="2" />
<value nick="Current" value="4" />
</enum>
<enum id="io.elementary.code.case-sensitive-mode">
<value nick="never" value="0" />
Expand Down
48 changes: 22 additions & 26 deletions src/Dialogs/PreferencesDialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
}
3 changes: 2 additions & 1 deletion src/Services/Settings.vala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace Scratch {
public enum ScratchDrawSpacesState {
NEVER = 0,
FOR_SELECTION = 1,
ALWAYS = 2
ALWAYS = 2,
CURRENT = 4
}
}
19 changes: 12 additions & 7 deletions src/Widgets/SourceView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}

Expand Down