-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApplication.vala
More file actions
125 lines (100 loc) · 3.8 KB
/
Application.vala
File metadata and controls
125 lines (100 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: {{YEAR}} {{DEVELOPER_NAME}} <{{DEVELOPER_EMAIL}}>
*/
public class Inscriptions.Application : Gtk.Application {
internal static Settings settings;
internal static DeepL.Backend backend;
internal static MainWindow main_window;
public const string ACTION_PREFIX = "app.";
public const string ACTION_QUIT = "action_quit";
public static Gee.MultiMap<string, string> action_accelerators = new Gee.HashMultiMap<string, string> ();
private const GLib.ActionEntry[] ACTION_ENTRIES = {
{ ACTION_QUIT, quit}
};
public Application () {
Object (
application_id: RDNN,
flags: ApplicationFlags.HANDLES_OPEN
);
}
construct {
Intl.setlocale (LocaleCategory.ALL, "");
Intl.bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
Intl.textdomain (GETTEXT_PACKAGE);
}
static construct {
settings = new GLib.Settings (RDNN);
// Backend takes care of the async for us. We give it the text
// And it will emit a signal whenever finished, which we can connect to
backend = new DeepL.Backend ();
}
protected override void startup () {
base.startup ();
Gtk.init ();
Granite.init ();
add_action_entries (ACTION_ENTRIES, this);
set_accels_for_action ("app.action_quit", {"<Control>q"});
// Styling
var granite_settings = Granite.Settings.get_default ();
var gtk_settings = Gtk.Settings.get_default ();
// Force Slate on other DE, as else there is a risk whatever theme is running it breaks the app
unowned string desktop_environment = Environment.get_variable ("XDG_CURRENT_DESKTOP");
if (desktop_environment != "Pantheon") {
gtk_settings.gtk_theme_name = "io.elementary.stylesheet.slate";
}
gtk_settings.gtk_application_prefer_dark_theme = (
granite_settings.prefers_color_scheme == DARK
);
granite_settings.notify["prefers-color-scheme"].connect (() => {
gtk_settings.gtk_application_prefer_dark_theme = (
granite_settings.prefers_color_scheme == DARK
);
});
var provider = new Gtk.CssProvider ();
provider.load_from_resource ("/io/github/elly_code/inscriptions/Application.css");
Gtk.StyleContext.add_provider_for_display (
Gdk.Display.get_default (),
provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
);
}
protected override void activate () {
if (main_window != null) {
main_window.present ();
return;
}
main_window = new MainWindow () {
application = this
};
main_window.show ();
main_window.present ();
}
protected override void open (File[] files, string hint) {
if (main_window == null) {
activate ();
}
var file = files [0];
string content = "";
try {
FileUtils.get_contents (file.get_path (), out content);
} catch (Error e) {
warning ("Failed to open file: %s", e.message);
var dialog = new Granite.MessageDialog (
_("Couldn't open file"),
e.message,
new ThemedIcon ("document-open")
) {
badge_icon = new ThemedIcon ("dialog-error"),
modal = true,
transient_for = main_window
};
dialog.present ();
dialog.response.connect (dialog.destroy);
}
main_window.open (content);
}
public static int main (string[] args) {
return new Application ().run (args);
}
}