Skip to content

Commit 3dbf6d5

Browse files
jeremypwdanirabbitzeebok
authored
Expose system document font in Preferences dialog (#1670)
* Bind document_font_name to application property and use where appropriate * Use correct switch --------- Co-authored-by: Danielle Foré <danielle@elementary.io> Co-authored-by: Ryan Kornheisl <ryan@skarva.tech>
1 parent f4d0359 commit 3dbf6d5

5 files changed

Lines changed: 56 additions & 40 deletions

File tree

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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,21 @@ 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+
121+
var application = ((Scratch.Application) (GLib.Application.get_default ()));
122+
var font_switch = new SettingSwitch (
123+
_("Use system font (%s)").printf (application.system_document_font),
124+
"use-system-font"
125+
);
126+
// We assume the system font will not change while dialog open
127+
120128
var select_font = new Gtk.FontButton ();
121129
Scratch.settings.bind ("font", select_font, "font-name", DEFAULT);
122130
Scratch.settings.bind ("use-system-font", select_font, "sensitive", INVERT_BOOLEAN);
123131

124132
var font_box = new Gtk.Box (VERTICAL, 12);
125133
font_box.add (new Granite.HeaderLabel (_("Font")));
126-
font_box.add (new SettingSwitch (_("Use system font"), "use-system-font"));
134+
font_box.add (font_switch);
127135
font_box.add (select_font);
128136

129137
var interface_box = new Gtk.Box (VERTICAL, 24);
@@ -191,7 +199,6 @@ public class Scratch.Dialogs.Preferences : Granite.Dialog {
191199
hexpand = true,
192200
mnemonic_widget = switch_widget
193201
};
194-
195202
column_spacing = 12;
196203
attach (label_widget, 0, 0);
197204
attach (switch_widget, 1, 0, 1, 2);

src/MainWindow.vala

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

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

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

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 ("elementary-highcontrast-light");
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) {
@@ -631,7 +644,7 @@ namespace Scratch.Widgets {
631644
// Use a default size of 10pt
632645
double px_per_line = 10 * PT_TO_PX;
633646

634-
var last_window = ((Scratch.Application) GLib.Application.get_default ()).get_last_window ();
647+
var last_window = application.get_last_window ();
635648
if (last_window != null) {
636649
// Get the actual font size
637650
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 ();
@@ -224,8 +217,8 @@ public class Code.Terminal : Gtk.Box {
224217
font_name = terminal_settings.get_string (TERMINAL_FONT_KEY);
225218
}
226219

227-
if (font_name == "" && gnome_interface_settings != null) {
228-
font_name = gnome_interface_settings.get_string (GNOME_FONT_KEY);
220+
if (font_name == "" ) {
221+
font_name = application.system_monospace_font;
229222
}
230223

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

0 commit comments

Comments
 (0)