Skip to content

Commit 4a1801a

Browse files
committed
Bind document_font_name to application property and use where appropriate
1 parent ce4b5af commit 4a1801a

File tree

5 files changed

+54
-38
lines changed

5 files changed

+54
-38
lines changed

src/Application.vala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ namespace Scratch {
2828

2929
public class Application : Gtk.Application {
3030
public string data_home_folder_unsaved { get { return _data_home_folder_unsaved; } }
31-
public string default_font { get; set; }
31+
public string system_monospace_font { get; set; }
32+
public string system_document_font { get; set; }
3233
public bool is_running_in_flatpak { get; construct; }
3334

3435
private static string _data_home_folder_unsaved;
@@ -66,11 +67,13 @@ namespace Scratch {
6667
add_main_option_entries (ENTRIES);
6768

6869
// Init settings
69-
default_font = new GLib.Settings ("org.gnome.desktop.interface").get_string ("monospace-font-name");
7070
saved_state = new GLib.Settings (Constants.PROJECT_NAME + ".saved-state");
7171
settings = new GLib.Settings (Constants.PROJECT_NAME + ".settings");
7272
service_settings = new GLib.Settings (Constants.PROJECT_NAME + ".services");
7373
privacy_settings = new GLib.Settings ("org.gnome.desktop.privacy");
74+
var desktop_interface_settings = new GLib.Settings ("org.gnome.desktop.interface");
75+
desktop_interface_settings.bind ("document-font-name", this, "system-document-font", GET);
76+
desktop_interface_settings.bind ("monospace-font-name", this, "system-monospace-font", GET);
7477

7578
location_jump_manager = new LocationJumpManager ();
7679
Environment.set_variable ("GTK_USE_PORTAL", "1", true);

src/Dialogs/PreferencesDialog.vala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ public class Scratch.Dialogs.Preferences : Granite.Dialog {
117117
editor_box.add (new SettingSwitch (_("Line width guide"), "show-right-margin"));
118118
editor_box.add (right_margin_position);
119119

120+
var application = ((Scratch.Application) (GLib.Application.get_default ()));
121+
var font_switch = new SettingSwitch (
122+
_("Use system font (%s)").printf (application.system_document_font),
123+
"use-system-font"
124+
);
125+
// We assume the system font will not change while dialog open
126+
120127
var select_font = new Gtk.FontButton ();
121128
Scratch.settings.bind ("font", select_font, "font-name", DEFAULT);
122129
Scratch.settings.bind ("use-system-font", select_font, "sensitive", INVERT_BOOLEAN);

src/MainWindow.vala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,13 +934,13 @@ namespace Scratch {
934934
}
935935

936936
public string get_default_font () {
937-
string font = app.default_font;
937+
string font = app.system_document_font;
938938
string font_family = font.substring (0, font.last_index_of (" "));
939939
return font_family;
940940
}
941941

942942
public double get_default_font_size () {
943-
string font = app.default_font;
943+
string font = app.system_document_font;
944944
string font_size = font.substring (font.last_index_of (" ") + 1);
945945
return double.parse (font_size);
946946
}

src/Widgets/SourceView.vala

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ namespace Scratch.Widgets {
4343
private double total_delta = 0;
4444
private const double SCROLL_THRESHOLD = 1.0;
4545

46+
protected static Scratch.Application application;
47+
4648
public signal void style_changed (Gtk.SourceStyleScheme style);
4749
// "selection_changed" signal now only emitted when the selected text changes (position ignored).
4850
// Listened to by searchbar and highlight word selection plugin
@@ -84,6 +86,7 @@ namespace Scratch.Widgets {
8486
}
8587

8688
construct {
89+
application = (Scratch.Application) (GLib.Application.get_default ());
8790
space_drawer.enable_matrix = true;
8891

8992
expand = true;
@@ -201,6 +204,12 @@ namespace Scratch.Widgets {
201204
});
202205
}
203206
});
207+
208+
application.notify["system-document-font"].connect (() => {
209+
if (Scratch.settings.get_boolean ("use-system-font")) {
210+
update_font ();
211+
}
212+
});
204213
}
205214

206215
private bool get_current_line (out Gtk.TextIter start, out Gtk.TextIter end) {
@@ -278,8 +287,27 @@ namespace Scratch.Widgets {
278287
set_wrap_mode (Gtk.WrapMode.NONE);
279288
}
280289

290+
update_font ();
291+
292+
if (settings.get_boolean ("follow-system-style")) {
293+
var system_prefers_dark = Granite.Settings.get_default ().prefers_color_scheme == Granite.Settings.ColorScheme.DARK;
294+
if (system_prefers_dark) {
295+
source_buffer.style_scheme = style_scheme_manager.get_scheme ("elementary-dark");
296+
} else {
297+
source_buffer.style_scheme = style_scheme_manager.get_scheme ("elementary-light");
298+
}
299+
} else {
300+
var scheme = style_scheme_manager.get_scheme (Scratch.settings.get_string ("style-scheme"));
301+
source_buffer.style_scheme = scheme ?? style_scheme_manager.get_scheme ("classic");
302+
}
303+
304+
git_diff_gutter_renderer.set_style_scheme (source_buffer.style_scheme);
305+
style_changed (source_buffer.style_scheme);
306+
}
307+
308+
private void update_font () {
281309
if (Scratch.settings.get_boolean ("use-system-font")) {
282-
font = ((Scratch.Application) GLib.Application.get_default ()).default_font;
310+
font = application.system_document_font;
283311
} else {
284312
font = Scratch.settings.get_string ("font");
285313
}
@@ -296,21 +324,6 @@ namespace Scratch.Widgets {
296324
} catch (Error e) {
297325
critical (e.message);
298326
}
299-
300-
if (settings.get_boolean ("follow-system-style")) {
301-
var system_prefers_dark = Granite.Settings.get_default ().prefers_color_scheme == Granite.Settings.ColorScheme.DARK;
302-
if (system_prefers_dark) {
303-
source_buffer.style_scheme = style_scheme_manager.get_scheme ("elementary-dark");
304-
} else {
305-
source_buffer.style_scheme = style_scheme_manager.get_scheme ("elementary-light");
306-
}
307-
} else {
308-
var scheme = style_scheme_manager.get_scheme (Scratch.settings.get_string ("style-scheme"));
309-
source_buffer.style_scheme = scheme ?? style_scheme_manager.get_scheme ("classic");
310-
}
311-
312-
git_diff_gutter_renderer.set_style_scheme (source_buffer.style_scheme);
313-
style_changed (source_buffer.style_scheme);
314327
}
315328

316329
public void go_to_line (int line, int offset = 0) {
@@ -653,7 +666,7 @@ namespace Scratch.Widgets {
653666
// Use a default size of 10pt
654667
double px_per_line = 10 * PT_TO_PX;
655668

656-
var last_window = ((Scratch.Application) GLib.Application.get_default ()).get_last_window ();
669+
var last_window = application.get_last_window ();
657670
if (last_window != null) {
658671
// Get the actual font size
659672
px_per_line = last_window.get_current_font_size () * PT_TO_PX;

src/Widgets/Terminal.vala

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class Code.Terminal : Gtk.Box {
2222
private const string TERMINAL_FOREGROUND_KEY = "foreground";
2323
private const string TERMINAL_BACKGROUND_KEY = "background";
2424
private const string TERMINAL_PALETTE_KEY = "palette";
25-
private const string GNOME_FONT_KEY = "monospace-font-name";
2625
private const string GNOME_BELL_KEY = "audible-bell";
2726

2827
public Vte.Terminal terminal { get; construct; }
@@ -37,7 +36,10 @@ public class Code.Terminal : Gtk.Box {
3736
private GLib.Pid child_pid;
3837
private Gtk.Clipboard current_clipboard;
3938

39+
private Scratch.Application application;
40+
4041
construct {
42+
application = (Scratch.Application) (GLib.Application.get_default ());
4143
terminal = new Vte.Terminal () {
4244
hexpand = true,
4345
vexpand = true,
@@ -95,20 +97,11 @@ public class Code.Terminal : Gtk.Box {
9597
// "org.gnome.desktop.interface.color-scheme"
9698
}
9799

98-
// Always monitor changes in default font as that is what Terminal usually follows
99-
var gnome_interface_settings_schema = schema_source.lookup (GNOME_DESKTOP_INTERFACE_SCHEMA, true);
100-
if (gnome_interface_settings_schema != null) {
101-
gnome_interface_settings = new Settings.full (gnome_interface_settings_schema, null, null);
102-
gnome_interface_settings.changed.connect ((key) => {
103-
switch (key) {
104-
case GNOME_FONT_KEY:
105-
update_font ();
106-
break;
107-
default:
108-
break;
109-
}
110-
});
111-
}
100+
// Always monitor changes in systen font as that is what Terminal usually follows
101+
// The terminal font key is by default "" and can only be changed by editing the settings externally
102+
application.notify["system-monospace-font"].connect (() => {
103+
update_font ();
104+
});
112105

113106
update_font ();
114107
update_audible_bell ();
@@ -221,8 +214,8 @@ public class Code.Terminal : Gtk.Box {
221214
font_name = terminal_settings.get_string (TERMINAL_FONT_KEY);
222215
}
223216

224-
if (font_name == "" && gnome_interface_settings != null) {
225-
font_name = gnome_interface_settings.get_string (GNOME_FONT_KEY);
217+
if (font_name == "" ) {
218+
font_name = application.system_monospace_font;
226219
}
227220

228221
var fd = Pango.FontDescription.from_string (font_name);

0 commit comments

Comments
 (0)