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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use gettextrs::gettext;
use tracing::{debug, info};
use adw::prelude::*;
use adw::subclass::prelude::*;
use gtk::{gdk, gio, glib};
use crate::config::{APP_ID, PKGDATADIR, PROFILE, VERSION};
use crate::window::ExampleApplicationWindow;
mod imp {
use super::*;
use glib::WeakRef;
use std::cell::OnceCell;
#[derive(Debug, Default)]
pub struct ExampleApplication {
pub window: OnceCell<WeakRef<ExampleApplicationWindow>>,
}
#[glib::object_subclass]
impl ObjectSubclass for ExampleApplication {
const NAME: &'static str = "ExampleApplication";
type Type = super::ExampleApplication;
type ParentType = adw::Application;
}
impl ObjectImpl for ExampleApplication {}
impl ApplicationImpl for ExampleApplication {
fn activate(&self) {
debug!("AdwApplication<ExampleApplication>::activate");
self.parent_activate();
let app = self.obj();
if let Some(window) = self.window.get() {
let window = window.upgrade().unwrap();
window.present();
return;
}
let window = ExampleApplicationWindow::new(&app);
self.window
.set(window.downgrade())
.expect("Window already set.");
app.main_window().present();
}
fn startup(&self) {
debug!("AdwApplication<ExampleApplication>::startup");
self.parent_startup();
let app = self.obj();
// Set icons for shell
// adw::Window::set_default_icon_name(APP_ID);
app.setup_css();
app.setup_gactions();
app.setup_accels();
}
}
impl GtkApplicationImpl for ExampleApplication {}
impl AdwApplicationImpl for ExampleApplication {}
}
glib::wrapper! {
pub struct ExampleApplication(ObjectSubclass<imp::ExampleApplication>)
@extends gio::Application, gtk::Application, adw::Application,
@implements gio::ActionMap, gio::ActionGroup;
}
impl ExampleApplication {
fn main_window(&self) -> ExampleApplicationWindow {
self.imp().window.get().unwrap().upgrade().unwrap()
}
fn setup_gactions(&self) {
// Quit
let action_quit = gio::ActionEntry::builder("quit")
.activate(move |app: &Self, _, _| {
// This is needed to trigger the delete event and saving the window state
app.main_window().close();
app.quit();
})
.build();
// About
let action_about = gio::ActionEntry::builder("about")
.activate(|app: &Self, _, _| {
app.show_about_dialog();
})
.build();
self.add_action_entries([action_quit, action_about]);
}
// Sets up keyboard shortcuts
fn setup_accels(&self) {
self.set_accels_for_action("app.quit", &["<Control>q"]);
self.set_accels_for_action("window.close", &["<Control>w"]);
// remember to add new shortcuts to shortcuts.ui !!!
}
fn setup_css(&self) {
let provider = gtk::CssProvider::new();
provider.load_from_resource("/garden/patchouli/sunrise/style.css");
if let Some(display) = gdk::Display::default() {
gtk::style_context_add_provider_for_display(
&display,
&provider,
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
}
}
fn show_about_dialog(&self) {
let dialog = adw::AboutDialog::builder()
.application_icon(APP_ID)
.version(VERSION)
.translator_credits(gettext("translator-credits"))
.application_name("Sunrise")
.license_type(gtk::License::Gpl30)
.website("https://patchouli.garden/git/sunrise/")
.build();
dialog.present(Some(&self.main_window()));
}
pub fn run(&self) -> glib::ExitCode {
info!("Sunrise ({})", APP_ID);
info!("Version: {} ({})", VERSION, PROFILE);
info!("Datadir: {}", PKGDATADIR);
ApplicationExtManual::run(self)
}
}
impl Default for ExampleApplication {
fn default() -> Self {
glib::Object::builder()
.property("application-id", APP_ID)
.property("resource-base-path", "/garden/patchouli/sunrise/")
.build()
}
}