Skip to content

Commit c643e85

Browse files
authored
Text: all font selection (#483)
1 parent 0e141c8 commit c643e85

1 file changed

Lines changed: 80 additions & 43 deletions

File tree

src/Views/Text.vala

Lines changed: 80 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@
1919
*/
2020

2121
public class PantheonShell.Text : Switchboard.SettingsPage {
22-
private const string DYSLEXIA_KEY = "dyslexia-friendly-support";
23-
private const string FONT_KEY = "font-name";
24-
private const string DOCUMENT_FONT_KEY = "document-font-name";
25-
private const string MONOSPACE_FONT_KEY = "monospace-font-name";
26-
27-
private const string OD_REG_FONT = "OpenDyslexic Regular 9";
28-
private const string OD_DOC_FONT = "OpenDyslexic Regular 10";
29-
private const string OD_MON_FONT = "OpenDyslexicMono Regular 10";
30-
3122
private uint scale_timeout;
3223

3324
public Text () {
@@ -39,6 +30,53 @@ public class PantheonShell.Text : Switchboard.SettingsPage {
3930
}
4031

4132
construct {
33+
var mono_filter = new Gtk.BoolFilter (
34+
new Gtk.PropertyExpression (typeof (Pango.FontFamily), null, "is-monospace")
35+
);
36+
37+
var noto_filter = new Gtk.CustomFilter (noto_filter_func);
38+
39+
var mono_font_filter = new Gtk.EveryFilter ();
40+
mono_font_filter.append (noto_filter);
41+
mono_font_filter.append (mono_filter);
42+
43+
var default_font_dialog = new Gtk.FontDialog () {
44+
filter = noto_filter,
45+
language = Pango.Language.get_default ()
46+
};
47+
48+
var default_font_button = new Gtk.FontDialogButton (default_font_dialog) {
49+
use_font = true,
50+
use_size = true,
51+
level = FAMILY
52+
};
53+
54+
var default_font_label = new Granite.HeaderLabel (_("Interface Font")) {
55+
secondary_text = _("The default font used throughout the operating system."),
56+
mnemonic_widget = default_font_button
57+
};
58+
59+
var mono_font_dialog = new Gtk.FontDialog () {
60+
filter = mono_font_filter
61+
};
62+
63+
var mono_font_button = new Gtk.FontDialogButton (mono_font_dialog) {
64+
use_font = true,
65+
use_size = true,
66+
level = FAMILY
67+
};
68+
69+
var mono_font_label = new Granite.HeaderLabel (_("Monospace Font")) {
70+
secondary_text = _("Used in Code and Terminal for example."),
71+
mnemonic_widget = mono_font_button
72+
};
73+
74+
var font_box = new Granite.Box (VERTICAL, HALF);
75+
font_box.append (default_font_label);
76+
font_box.append (default_font_button);
77+
font_box.append (mono_font_label);
78+
font_box.append (mono_font_button);
79+
4280
var size_label = new Granite.HeaderLabel (_("Size"));
4381

4482
var size_adjustment = new Gtk.Adjustment (-1, 0.75, 1.5, 0.05, 0, 0);
@@ -61,23 +99,9 @@ public class PantheonShell.Text : Switchboard.SettingsPage {
6199
size_grid.attach (size_scale, 0, 1);
62100
size_grid.attach (size_spinbutton, 1, 1);
63101

64-
var dyslexia_font_switch = new Gtk.Switch () {
65-
valign = Gtk.Align.CENTER
66-
};
67-
68-
var dyslexia_font_label = new Granite.HeaderLabel (_("Dyslexia-friendly")) {
69-
hexpand = true,
70-
mnemonic_widget = dyslexia_font_switch,
71-
secondary_text = _("Bottom-heavy shapes and increased character spacing can help improve legibility and reading speed.")
72-
};
73-
74-
var dyslexia_box = new Gtk.Box (HORIZONTAL, 12);
75-
dyslexia_box.append (dyslexia_font_label);
76-
dyslexia_box.append (dyslexia_font_switch);
77-
78-
var box = new Gtk.Box (VERTICAL, 24);
102+
var box = new Granite.Box (VERTICAL, DOUBLE);
103+
box.append (font_box);
79104
box.append (size_grid);
80-
box.append (dyslexia_box);
81105

82106
child = box;
83107

@@ -97,23 +121,36 @@ public class PantheonShell.Text : Switchboard.SettingsPage {
97121
});
98122
});
99123

100-
var interface_font = interface_settings.get_string (FONT_KEY);
101-
var document_font = interface_settings.get_string (DOCUMENT_FONT_KEY);
102-
var monospace_font = interface_settings.get_string (MONOSPACE_FONT_KEY);
103-
104-
dyslexia_font_switch.active = interface_font == OD_REG_FONT || document_font == OD_DOC_FONT || monospace_font == OD_MON_FONT;
105-
106-
dyslexia_font_switch.state_set.connect (() => {
107-
if (dyslexia_font_switch.active) {
108-
interface_settings.set_string (FONT_KEY, OD_REG_FONT);
109-
interface_settings.set_string (DOCUMENT_FONT_KEY, OD_DOC_FONT);
110-
interface_settings.set_string (MONOSPACE_FONT_KEY, OD_MON_FONT);
111-
} else {
112-
interface_settings.reset (FONT_KEY);
113-
interface_settings.reset (DOCUMENT_FONT_KEY);
114-
interface_settings.reset (MONOSPACE_FONT_KEY);
115-
}
116-
return Gdk.EVENT_PROPAGATE;
117-
});
124+
interface_settings.bind_with_mapping (
125+
"font-name", default_font_button, "font-desc", DEFAULT,
126+
(SettingsBindGetMappingShared) to_fontbutton_fontdesc,
127+
(SettingsBindSetMappingShared) from_fontbutton_fontdesc,
128+
new Variant.int32 (9), null
129+
);
130+
131+
interface_settings.bind_with_mapping (
132+
"monospace-font-name", mono_font_button, "font-desc", DEFAULT,
133+
(SettingsBindGetMappingShared) to_fontbutton_fontdesc,
134+
(SettingsBindSetMappingShared) from_fontbutton_fontdesc,
135+
new Variant.int32 (10), null
136+
);
137+
}
138+
139+
private static bool to_fontbutton_fontdesc (Value font_desc, Variant settings_value, void* user_data) {
140+
string font = settings_value.get_string ();
141+
var desc = Pango.FontDescription.from_string (font);
142+
font_desc.set_boxed (desc);
143+
return true;
144+
}
145+
146+
private static Variant from_fontbutton_fontdesc (Value font_desc, VariantType expected_type, void* user_data) {
147+
var desc = (Pango.FontDescription) font_desc.get_boxed ();
148+
var font_string = "%s %i".printf (desc.to_string (), ((Variant) user_data).get_int32 ());
149+
return new Variant.string (font_string);
150+
}
151+
152+
private static bool noto_filter_func (Object item) {
153+
var font_family = ((Pango.FontFamily) item);
154+
return !font_family.get_name ().contains ("Noto");
118155
}
119156
}

0 commit comments

Comments
 (0)