Skip to content
Draft
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
7 changes: 5 additions & 2 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ namespace Scratch {

public class Application : Gtk.Application {
public string data_home_folder_unsaved { get { return _data_home_folder_unsaved; } }
public string default_font { get; set; }
public string system_monospace_font { get; set; }
public string system_document_font { get; set; }
public bool is_running_in_flatpak { get; construct; }

private static string _data_home_folder_unsaved;
Expand Down Expand Up @@ -66,11 +67,13 @@ namespace Scratch {
add_main_option_entries (ENTRIES);

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

location_jump_manager = new LocationJumpManager ();
Environment.set_variable ("GTK_USE_PORTAL", "1", true);
Expand Down
11 changes: 9 additions & 2 deletions src/Dialogs/PreferencesDialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,21 @@ public class Scratch.Dialogs.Preferences : Granite.Dialog {
editor_box.add (new SettingSwitch (_("Line width guide"), "show-right-margin"));
editor_box.add (right_margin_position);


var application = ((Scratch.Application) (GLib.Application.get_default ()));
var font_switch = new SettingSwitch (
_("Use system font (%s)").printf (application.system_document_font),
"use-system-font"
);
// We assume the system font will not change while dialog open

var select_font = new Gtk.FontButton ();
Scratch.settings.bind ("font", select_font, "font-name", DEFAULT);
Scratch.settings.bind ("use-system-font", select_font, "sensitive", INVERT_BOOLEAN);

var font_box = new Gtk.Box (VERTICAL, 12);
font_box.add (new Granite.HeaderLabel (_("Font")));
font_box.add (new SettingSwitch (_("Use system font"), "use-system-font"));
font_box.add (font_switch);
font_box.add (select_font);

var interface_box = new Gtk.Box (VERTICAL, 24);
Expand Down Expand Up @@ -191,7 +199,6 @@ public class Scratch.Dialogs.Preferences : Granite.Dialog {
hexpand = true,
mnemonic_widget = switch_widget
};

column_spacing = 12;
attach (label_widget, 0, 0);
attach (switch_widget, 1, 0, 1, 2);
Expand Down
4 changes: 2 additions & 2 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -934,13 +934,13 @@ namespace Scratch {
}

public string get_default_font () {
string font = app.default_font;
string font = app.system_document_font;
string font_family = font.substring (0, font.last_index_of (" "));
return font_family;
}

public double get_default_font_size () {
string font = app.default_font;
string font = app.system_document_font;
string font_size = font.substring (font.last_index_of (" ") + 1);
return double.parse (font_size);
}
Expand Down
47 changes: 30 additions & 17 deletions src/Widgets/SourceView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace Scratch.Widgets {
private double total_delta = 0;
private const double SCROLL_THRESHOLD = 1.0;

protected static Scratch.Application application;

public signal void style_changed (Gtk.SourceStyleScheme style);
// "selection_changed" signal now only emitted when the selected text changes (position ignored).
// Listened to by searchbar and highlight word selection plugin
Expand Down Expand Up @@ -84,6 +86,7 @@ namespace Scratch.Widgets {
}

construct {
application = (Scratch.Application) (GLib.Application.get_default ());
space_drawer.enable_matrix = true;

expand = true;
Expand Down Expand Up @@ -201,6 +204,12 @@ namespace Scratch.Widgets {
});
}
});

application.notify["system-document-font"].connect (() => {
if (Scratch.settings.get_boolean ("use-system-font")) {
update_font ();
}
});
}

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

update_font ();

if (settings.get_boolean ("follow-system-style")) {
var system_prefers_dark = Granite.Settings.get_default ().prefers_color_scheme == Granite.Settings.ColorScheme.DARK;
if (system_prefers_dark) {
source_buffer.style_scheme = style_scheme_manager.get_scheme ("elementary-dark");
} else {
source_buffer.style_scheme = style_scheme_manager.get_scheme ("elementary-light");
}
} else {
var scheme = style_scheme_manager.get_scheme (Scratch.settings.get_string ("style-scheme"));
source_buffer.style_scheme = scheme ?? style_scheme_manager.get_scheme ("classic");
}

git_diff_gutter_renderer.set_style_scheme (source_buffer.style_scheme);
style_changed (source_buffer.style_scheme);
}

private void update_font () {
if (Scratch.settings.get_boolean ("use-system-font")) {
font = ((Scratch.Application) GLib.Application.get_default ()).default_font;
font = application.system_document_font;
} else {
font = Scratch.settings.get_string ("font");
}
Expand All @@ -296,21 +324,6 @@ namespace Scratch.Widgets {
} catch (Error e) {
critical (e.message);
}

if (settings.get_boolean ("follow-system-style")) {
var system_prefers_dark = Granite.Settings.get_default ().prefers_color_scheme == Granite.Settings.ColorScheme.DARK;
if (system_prefers_dark) {
source_buffer.style_scheme = style_scheme_manager.get_scheme ("elementary-dark");
} else {
source_buffer.style_scheme = style_scheme_manager.get_scheme ("elementary-light");
}
} else {
var scheme = style_scheme_manager.get_scheme (Scratch.settings.get_string ("style-scheme"));
source_buffer.style_scheme = scheme ?? style_scheme_manager.get_scheme ("classic");
}

git_diff_gutter_renderer.set_style_scheme (source_buffer.style_scheme);
style_changed (source_buffer.style_scheme);
}

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

var last_window = ((Scratch.Application) GLib.Application.get_default ()).get_last_window ();
var last_window = application.get_last_window ();
if (last_window != null) {
// Get the actual font size
px_per_line = last_window.get_current_font_size () * PT_TO_PX;
Expand Down
27 changes: 10 additions & 17 deletions src/Widgets/Terminal.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class Code.Terminal : Gtk.Box {
private const string TERMINAL_FOREGROUND_KEY = "foreground";
private const string TERMINAL_BACKGROUND_KEY = "background";
private const string TERMINAL_PALETTE_KEY = "palette";
private const string GNOME_FONT_KEY = "monospace-font-name";
private const string GNOME_BELL_KEY = "audible-bell";

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

private Scratch.Application application;

construct {
application = (Scratch.Application) (GLib.Application.get_default ());
terminal = new Vte.Terminal () {
hexpand = true,
vexpand = true,
Expand Down Expand Up @@ -95,20 +97,11 @@ public class Code.Terminal : Gtk.Box {
// "org.gnome.desktop.interface.color-scheme"
}

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

update_font ();
update_audible_bell ();
Expand Down Expand Up @@ -221,8 +214,8 @@ public class Code.Terminal : Gtk.Box {
font_name = terminal_settings.get_string (TERMINAL_FONT_KEY);
}

if (font_name == "" && gnome_interface_settings != null) {
font_name = gnome_interface_settings.get_string (GNOME_FONT_KEY);
if (font_name == "" ) {
font_name = application.system_monospace_font;
}

var fd = Pango.FontDescription.from_string (font_name);
Expand Down