Debian Codegraph @

src/font-viewer/MainWindow.vala

raw
1 /* MainWindow.vala
2 *
3 * Copyright (C) 2009-2025 Jerry Casiano
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.
17 *
18 * If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
19 */
20
21 namespace FontManager.FontViewer {
22
23 [GtkTemplate (ui = "/com/github/FontManager/FontManager/ui/font-viewer-main-window.ui")]
24 public class MainWindow : FontManager.ApplicationWindow {
25
26 public int predefined_size { get; set; }
27 public bool show_line_size { get; set; default = true; }
28
29 [GtkChild] unowned Gtk.Label title_label;
30 [GtkChild] unowned Gtk.DropDown title_widget;
31 [GtkChild] unowned Gtk.Stack stack;
32 [GtkChild] unowned Gtk.Button action_button;
33 [GtkChild] unowned Gtk.ToggleButton preference_toggle;
34 [GtkChild] unowned Gtk.ListBox preference_list;
35 [GtkChild] unowned PreviewPane preview_pane;
36
37 FileStatus file_status;
38 List <string> installed_files;
39 Gdk.Rectangle clicked_area;
40 Gtk.Switch prefer_dark_theme;
41 Gtk.Switch use_adwaita_stylesheet;
42 PreviewColors preview_colors;
43 WaterfallSettings waterfall_settings;
44
45 Font? font = null;
46 Family? family = null;
47 File? current_file = null;
48 File? current_target = null;
49
50 enum FileStatus {
51 NOT_INSTALLED,
52 INSTALLED,
53 SYSTEM_FONT,
54 WOULD_DOWNGRADE,
55 WOULD_UPGRADE;
56 }
57
58 static construct {
59 install_property_action("predefined-size", "predefined-size");
60 install_property_action("show-line-size", "show-line-size");
61 }
62
63 public class MainWindow (GLib.Settings? settings) {
64 // Settings instance used in base class
65 Object(settings: settings);
66 family = new Family();
67 var target = new Gtk.DropTarget(typeof(Gdk.FileList), Gdk.DragAction.COPY);
68 target.drop.connect(on_drop);
69 stack.add_controller(target);
70 stack.set_visible_child_name("PlaceHolder");
71 preview_pane.add_action_widget(action_button, Gtk.PackType.END);
72 preview_pane.changed.connect(update);
73 preview_pane.realize.connect_after(() => {
74 preview_pane.restore_state(get_gsettings(BUS_ID));
75 });
76 clicked_area = Gdk.Rectangle();
77 Gtk.Gesture right_click = new Gtk.GestureClick() {
78 button = Gdk.BUTTON_SECONDARY
79 };
80 ((Gtk.GestureClick) right_click).pressed.connect(on_show_context_menu);
81 preview_pane.add_controller(right_click);
82 update_action_button();
83 waterfall_settings = new WaterfallSettings(settings);
84 BindingFlags flags = BindingFlags.BIDIRECTIONAL | BindingFlags.SYNC_CREATE;
85 waterfall_settings.bind_property("predefined-size", this, "predefined-size", flags);
86 waterfall_settings.bind_property("show-line-size", this, "show-line-size", flags);
87 bind_property("show-line-size", preview_pane, "show-line-size", flags);
88 waterfall_settings.notify["predefined-size"].connect_after(() => {
89 waterfall_settings.on_selection_changed();
90 preview_pane.set_waterfall_size(waterfall_settings.minimum,
91 waterfall_settings.maximum,
92 waterfall_settings.ratio);
93 });
94 populate_preference_list();
95 title_widget.notify["selected"].connect(on_variation_selected);
96 var popup = (Gtk.Popover) title_widget.get_last_child();
97 popup.set_halign(Gtk.Align.CENTER);
98 popup.set_has_arrow(true);
99 #if HAVE_ADWAITA
100 map.connect_after(() => {
101 use_adwaita_stylesheet.notify["active"].connect(() => { on_restart_required(); });
102 });
103 #endif
104 }
105
106 public void show_uri (string? uri) {
107 if (uri == null) {
108 family = null;
109 font = null;
110 current_file = null;
111 current_target = null;
112 return;
113 }
114 installed_files = list_available_font_files();
115 File file = File.new_for_commandline_arg(uri);
116 string path = file.get_path();
117 add_application_font(path);
118 clear_pango_cache(get_pango_context());
119 Json.Object? source = null;
120 try {
121 source = get_attributes_from_filepath(path);
122 } catch (Error e) {
123 critical(e.message);
124 return;
125 }
126 assert(source != null);
127 family = new Family();
128 font = new Font();
129 family.source_object = source;
130 font.source_object = family.get_default_variant();
131 var model = new Gtk.StringList(null);
132 family.variations.foreach_element((array, index, element) => {
133 Json.Object json_object = element.get_object();
134 var font_desc = json_object.get_string_member("description");
135 if (family.n_variations == 1 || font_desc != family.family)
136 model.append(font_desc);
137 string sample = get_sample_string(json_object);
138 json_object.set_string_member("preview-text", sample);
139 });
140 title_widget.set_model(model);
141 title_widget.set_selected(family.get_default_index());
142 return;
143 }
144
145 public void open (File file) {
146 show_uri(file.get_uri());
147 return;
148 }
149
150 public void update () {
151 if (preview_pane.font != null) {
152 current_file = File.new_for_path(preview_pane.font.filepath);
153 } else {
154 current_file = null;
155 var model = new Gtk.StringList(null);
156 model.append(_("Font Viewer"));
157 title_widget.set_model(model);
158 }
159 stack.set_visible_child_name(preview_pane.font != null ? "Preview" : "PlaceHolder");
160 current_target = null;
161 file_status = get_file_status();
162 update_action_button();
163 update_title_widget();
164 return;
165 }
166
167 [GtkCallback]
168 public void on_preferences_toggled () {
169 if (preference_toggle.active)
170 stack.set_visible_child_name("Preferences");
171 else
172 stack.set_visible_child_name(preview_pane.font != null ? "Preview" : "PlaceHolder");
173 return;
174 }
175
176 [GtkCallback]
177 public void on_action_button_clicked () {
178 File font_dir = File.new_for_path(get_user_font_directory());
179 switch (file_status) {
180 case FileStatus.SYSTEM_FONT:
181 string directory = GLib.Path.get_dirname(current_file.get_path());
182 File file = File.new_for_path(directory);
183 var launcher = new Gtk.FileLauncher(file);
184 launcher.launch.begin(null, null);
185 break;
186 case FileStatus.INSTALLED:
187 try {
188 File parent = current_target.get_parent();
189 current_target.delete(null);
190 remove_directory_tree_if_empty(parent);
191 font = null;
192 preview_pane.font = null;
193 update();
194 } catch (Error e) {
195 critical("Failed to remove %s", current_target.get_path());
196 }
197 break;
198 default:
199 try {
200 install_file(current_file, font_dir);
201 update();
202 } catch (Error e) {
203 critical("Failed to install %s", current_file.get_path());
204 }
205 break;
206 }
207 return;
208 }
209
210 bool on_drop (Value val, double x, double y) {
211 unowned SList <File> files = (SList) val.get_boxed();
212 if (files.length() > 0)
213 show_uri(files.nth_data(0).get_uri());
214 return true;
215 }
216
217 void on_variation_selected () {
218 if (family == null || font == null)
219 return;
220 font = new Font() {
221 source_object = family.variations.get_object_element(title_widget.selected)
222 };
223 preview_pane.set_font(font);
224 return;
225 }
226
227 void update_title_widget () {
228 int64 n_items = (family != null && font != null) ? family.n_variations : 0;
229 title_widget.set_show_arrow(n_items > 1);
230 title_widget.set_can_target(n_items > 1);
231 title_widget.set_visible(n_items > 1);
232 title_label.set_visible(n_items <= 1);
233 if (n_items == 1 && font != null)
234 title_label.set_label(font.description);
235 else
236 title_label.set_label(_("Font Viewer"));
237 return;
238 }
239
240 FileStatus get_file_status () {
241 if (current_file == null)
242 return FileStatus.NOT_INSTALLED;
243 string current_path = current_file.get_path();
244 if (installed_files.find_custom(current_path, strcmp) != null && get_file_owner(current_path) != 0)
245 return FileStatus.SYSTEM_FONT;
246 File font_dir = File.new_for_path(get_user_font_directory());
247 if (current_file.get_path().contains(font_dir.get_path()))
248 current_target = current_file;
249 if (current_target == null) {
250 try {
251 current_target = get_installation_target(current_file, font_dir, false);
252 } catch (Error e) {
253 return FileStatus.NOT_INSTALLED;
254 }
255 }
256 if (current_target.query_exists()) {
257 float a = get_font_revision(current_target.get_path());
258 float b = get_font_revision(current_file.get_path());
259 if (a < b)
260 return FileStatus.WOULD_UPGRADE;
261 else if (a > b)
262 return FileStatus.WOULD_DOWNGRADE;
263 else
264 return FileStatus.INSTALLED;
265 } else
266 return FileStatus.NOT_INSTALLED;
267 }
268
269 void update_action_button () {
270 action_button.remove_css_class(STYLE_CLASS_DESTRUCTIVE_ACTION);
271 action_button.remove_css_class(STYLE_CLASS_SUGGESTED_ACTION);
272 action_button.remove_css_class(STYLE_CLASS_DIM_LABEL);
273 action_button.set_tooltip_text(null);
274 action_button.set_visible(preview_pane.font != null);
275 if (preview_pane.font == null)
276 return;
277 switch (file_status) {
278 case FileStatus.SYSTEM_FONT:
279 action_button.set_label(_("System Font"));
280 action_button.add_css_class(STYLE_CLASS_DIM_LABEL);
281 action_button.set_tooltip_text(_("Selected font file is either installed in a system directory or is not writable by the current user.\n\nIf you wish to remove this font from the list of available fonts use the system package manager to remove the package containing this font file, ask the system administrator to remove it or use a font management application to disable it."));
282 break;
283 case FileStatus.WOULD_DOWNGRADE:
284 action_button.set_label(_("Newer version already installed"));
285 action_button.add_css_class(STYLE_CLASS_DESTRUCTIVE_ACTION);
286 action_button.set_tooltip_text(_("Click to overwrite"));
287 break;
288 case FileStatus.WOULD_UPGRADE:
289 action_button.set_label(_("Update Font"));
290 action_button.add_css_class(STYLE_CLASS_SUGGESTED_ACTION);
291 action_button.set_tooltip_text(_("Click to overwrite"));
292 break;
293 case FileStatus.INSTALLED:
294 action_button.set_label(_("Remove Font"));
295 action_button.add_css_class(STYLE_CLASS_DESTRUCTIVE_ACTION);
296 break;
297 default:
298 action_button.set_label(_("Install Font"));
299 action_button.add_css_class(STYLE_CLASS_SUGGESTED_ACTION);
300 break;
301 }
302 return;
303 }
304
305 void bind_settings () {
306 return_if_fail(settings != null);
307 SettingsBindFlags flags = SettingsBindFlags.DEFAULT;
308 Gtk.Settings? gtk_settings = Gtk.Settings.get_default();
309 const string gtk_prefer_dark = "gtk-application-prefer-dark-theme";
310 if (gtk_settings != null) {
311 #if HAVE_ADWAITA
312 if (settings.get_boolean("use-adwaita-stylesheet")) {
313 prefer_dark_theme.notify["active"].connect(() => {
314 Adw.StyleManager style_manager = Adw.StyleManager.get_default();
315 Adw.ColorScheme color_scheme = prefer_dark_theme.active ?
316 Adw.ColorScheme.PREFER_DARK :
317 Adw.ColorScheme.PREFER_LIGHT;
318 style_manager.set_color_scheme(color_scheme);
319 });
320 } else
321 settings.bind("prefer-dark-theme", gtk_settings, gtk_prefer_dark, flags);
322 #else
323 settings.bind("prefer-dark-theme", gtk_settings, gtk_prefer_dark, flags);
324 #endif
325 }
326 warn_if_fail(gtk_settings != null);
327
328 settings.bind("prefer-dark-theme", prefer_dark_theme, "active", flags);
329 #if HAVE_ADWAITA
330 settings.bind("use-adwaita-stylesheet", use_adwaita_stylesheet, "active", flags);
331 #endif
332 return;
333 }
334
335 #if HAVE_ADWAITA
336 void on_restart_required () {
337 var title = _("Selected setting requires restart to apply");
338 var body = _("Changes will take effect next time the application is started");
339 var icon = new GLib.ThemedIcon(BUS_ID);
340 var notification = new GLib.Notification(title);
341 notification.set_body(body);
342 notification.set_icon(icon);
343 GLib.Application.get_default().send_notification("restart-required", notification);
344 return;
345 }
346 #endif
347
348 void populate_preference_list () {
349 preference_list.set_selection_mode(Gtk.SelectionMode.NONE);
350 prefer_dark_theme = new Gtk.Switch();
351 // The added margins here are due to PreviewColors widget alignment issues
352 var row = new Gtk.ListBoxRow() { activatable = false, selectable = false, margin_start = 6, margin_end = 6 };
353 row.set_child(new PreferenceRow(_("Prefer Dark Theme"), null, null, prefer_dark_theme));
354 preference_list.insert(row, -1);
355 #if HAVE_ADWAITA
356 use_adwaita_stylesheet = new Gtk.Switch();
357 row = new Gtk.ListBoxRow() { activatable = false, selectable = false, margin_start = 6, margin_end = 6 };
358 row.set_child(new PreferenceRow(_("Use Adwaita Stylesheet"), null, null, use_adwaita_stylesheet));
359 preference_list.insert(row, -1);
360 #endif
361 preview_colors = new PreviewColors();
362 preview_colors.restore_state(settings);
363 widget_set_margin(preview_colors, 0);
364 row = new Gtk.ListBoxRow() { activatable = false, selectable = false, margin_start = 6 };
365 row.set_child(new PreferenceRow(_("Preview Area Colors"), null, null, preview_colors));
366 preference_list.insert(row, -1);
367 var adjustment = new Gtk.Adjustment(0.0, 0.0, double.MAX, 1.0, 1.0, 1.0);
368 var spacing = new Gtk.SpinButton(adjustment, 1.0, 0);
369 spacing.set_value((double) waterfall_settings.line_spacing);
370 row = new Gtk.ListBoxRow() { activatable = false, selectable = false, margin_start = 6, margin_end = 6 };
371 row.set_child(new PreferenceRow(_("Waterfall Line Spacing"), null, null, spacing));
372 row.set_tooltip_text(_("Padding in pixels to insert above and below rows"));
373 preference_list.insert(row, -1);
374 row = new Gtk.ListBoxRow() { activatable = false, selectable = false, margin_start = 6, margin_end = 6 };
375 var show_line_size = new Gtk.Switch();
376 row.set_child(new PreferenceRow(_("Display line size in Waterfall Preview"), null, null, show_line_size));
377 BindingFlags flags = BindingFlags.BIDIRECTIONAL | BindingFlags.SYNC_CREATE;
378 bind_property("show-line-size", show_line_size, "active", flags);
379 preference_list.insert(row, -1);
380 spacing.value_changed.connect(() => {
381 waterfall_settings.line_spacing = (int) spacing.value;
382 });
383 row = new Gtk.ListBoxRow() { activatable = false, selectable = false, margin_start = 6, margin_end = 6 };
384 row.set_child(waterfall_settings.preference_row);
385 waterfall_settings.preference_row.margin_end = 12;
386 preference_list.insert(row, -1);
387 bind_settings();
388 return;
389 }
390
391 void on_show_context_menu (int n_press, double x, double y) {
392 if (waterfall_settings == null)
393 return;
394 if (preview_pane.page != PreviewPanePage.PREVIEW || preview_pane.preview_mode != PreviewPageMode.WATERFALL)
395 return;
396 clicked_area.x = (int) x;
397 clicked_area.y = (int) y;
398 clicked_area.width = 2;
399 clicked_area.height = 2;
400 if (waterfall_settings.context_menu.get_parent() != null)
401 waterfall_settings.context_menu.unparent();
402 waterfall_settings.context_menu.set_parent(preview_pane);
403 waterfall_settings.context_menu.set_pointing_to(clicked_area);
404 waterfall_settings.context_menu.popup();
405 return;
406 }
407
408 bool remove_directory_tree_if_empty (File dir) {
409 try {
410 var enumerator = dir.enumerate_children(FileAttribute.STANDARD_NAME,
411 FileQueryInfoFlags.NONE);
412 if (enumerator.next_file() != null)
413 return false;
414 File parent = dir.get_parent();
415 dir.delete();
416 if (parent != null)
417 remove_directory_tree_if_empty(parent);
418 return true;
419 } catch (Error e) {
420 warning(e.message);
421 }
422 return false;
423 }
424
425 }
426
427 }
428