Skip to content

Commit

Permalink
Ensure tab toggle label is always correct (#1145)
Browse files Browse the repository at this point in the history
* Make settings source of truth for indent-width etc

* Do not allow individual documents to change global settings
* Update document when settings change

* Code style
  • Loading branch information
Jeremy Wootten authored Jul 13, 2022
1 parent fa17876 commit 95f037b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
13 changes: 7 additions & 6 deletions src/Widgets/FormatBar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -173,23 +173,24 @@ public class Code.FormatBar : Gtk.Grid {
tab_popover.add (tab_grid);

tab_toggle.bind_property ("active", tab_popover, "visible", GLib.BindingFlags.BIDIRECTIONAL);
Scratch.settings.changed["indent-width"].connect (() => format_tab_header ());
Scratch.settings.changed["spaces-instead-of-tabs"].connect (() => format_tab_header ());
Scratch.settings.changed["indent-width"].connect (format_tab_header);
Scratch.settings.changed["spaces-instead-of-tabs"].connect (format_tab_header);
}

private void format_tab_header () {
var indent_width = Scratch.settings.get_int ("indent-width");
var spaces_instead_of_tabs = Scratch.settings.get_boolean ("spaces-instead-of-tabs");
if (doc != null) {
indent_width = (int)doc.source_view.tab_width;
spaces_instead_of_tabs = doc.source_view.insert_spaces_instead_of_tabs;
}

if (spaces_instead_of_tabs) {
tab_toggle.text = ngettext ("%d Space", "%d Spaces", indent_width).printf (indent_width);
} else {
tab_toggle.text = ngettext ("%d Tab", "%d Tabs", indent_width).printf (indent_width);
}

if (doc != null) {
doc.source_view.tab_width = (uint)indent_width;
doc.source_view.insert_spaces_instead_of_tabs = spaces_instead_of_tabs;
}
}

private void format_line_header () {
Expand Down
17 changes: 0 additions & 17 deletions src/Widgets/SourceView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,6 @@ namespace Scratch.Widgets {
return !start.equal (end);
}

~SourceView () {
// Update settings when an instance is deleted
update_settings ();
}

public void change_syntax_highlight_from_file (File file) {
try {
var info = file.query_info ("standard::*", FileQueryInfoFlags.NONE, null);
Expand Down Expand Up @@ -298,18 +293,6 @@ namespace Scratch.Widgets {
style_changed (source_buffer.style_scheme);
}

private void update_settings () {
var source_buffer = (Gtk.SourceBuffer) buffer;
Scratch.settings.set_boolean ("show-right-margin", show_right_margin);
Scratch.settings.set_int ("right-margin-position", (int) right_margin_position);
Scratch.settings.set_boolean ("highlight-matching-brackets", source_buffer.highlight_matching_brackets);
Scratch.settings.set_boolean ("spaces-instead-of-tabs", insert_spaces_instead_of_tabs);
Scratch.settings.set_int ("indent-width", (int) tab_width);
Scratch.settings.set_string ("font", font);
Scratch.settings.set_string ("style-scheme", source_buffer.style_scheme.id);
style_changed (source_buffer.style_scheme);
}

public void go_to_line (int line, int offset = 0) {
Gtk.TextIter it;
buffer.get_iter_at_line (out it, line - 1);
Expand Down

0 comments on commit 95f037b

Please sign in to comment.