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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
// Furtherance - Track your time without being tracked
// Copyright (C) 2022 Ricky Kresslein <rk@lakoliu.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use glib::clone;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{gdk, gio, glib};
use crate::config;
use crate::ui::{FurtheranceWindow, FurPreferencesWindow};
use crate::database;
use crate::settings_manager;
mod imp {
use super::*;
#[derive(Debug, Default)]
pub struct FurtheranceApplication {
// pub settings: gio::Settings,
}
#[glib::object_subclass]
impl ObjectSubclass for FurtheranceApplication {
const NAME: &'static str = "FurtheranceApplication";
type Type = super::FurtheranceApplication;
type ParentType = gtk::Application;
}
impl ObjectImpl for FurtheranceApplication {
fn constructed(&self, obj: &Self::Type) {
self.parent_constructed(obj);
obj.setup_gactions();
obj.setup_application();
obj.set_accels_for_action("app.quit", &["<primary>Q", "<primary>W"]);
}
}
impl ApplicationImpl for FurtheranceApplication {
// We connect to the activate callback to create a window when the application
// has been launched. Additionally, this callback notifies us when the user
// tries to launch a "second instance" of the application. When they try
// to do that, we'll just present any existing window.
fn activate(&self, application: &Self::Type) {
// Initialize the database
let _ = database::db_init();
// Get the current window or create one if necessary
let window = if let Some(window) = application.active_window() {
window
} else {
let window = FurtheranceWindow::new(application);
window.set_default_size(400, 600);
window.set_title(Some("Furtherance"));
window.upcast()
};
// Load style.css
let css_file = gtk::CssProvider::new();
gtk::CssProvider::load_from_resource(&css_file, "/com/lakoliu/Furtherance/gtk/style.css");
gtk::StyleContext::add_provider_for_display(&gdk::Display::default().unwrap(), &css_file, 500);
// Ask the window manager/compositor to present the window
window.present();
}
}
impl GtkApplicationImpl for FurtheranceApplication {}
}
glib::wrapper! {
pub struct FurtheranceApplication(ObjectSubclass<imp::FurtheranceApplication>)
@extends gio::Application, gtk::Application,
@implements gio::ActionGroup, gio::ActionMap;
}
impl FurtheranceApplication {
pub fn new(application_id: &str, flags: &gio::ApplicationFlags) -> Self {
glib::Object::new(&[("application-id", &application_id), ("flags", flags)])
.expect("Failed to create FurtheranceApplication")
}
fn setup_gactions(&self) {
let quit_action = gio::SimpleAction::new("quit", None);
quit_action.connect_activate(clone!(@weak self as app => move |_, _| {
app.quit();
}));
self.add_action(&quit_action);
let preferences_action = gio::SimpleAction::new("preferences", None);
preferences_action.connect_activate(clone!(@weak self as app => move |_, _| {
FurPreferencesWindow::new().show();
}));
self.set_accels_for_action("app.preferences", &["<primary>comma"]);
self.add_action(&preferences_action);
let about_action = gio::SimpleAction::new("about", None);
about_action.connect_activate(clone!(@weak self as app => move |_, _| {
app.show_about();
}));
self.add_action(&about_action);
}
fn setup_application(&self) {
let app_id = config::APP_ID.trim_end_matches(".Devel");
let settings = gio::Settings::new(app_id);
settings.connect_changed(
Some("dark-mode"),
clone!(@weak self as app => move |_, _| {
app.update_light_dark();
}
),
);
self.update_light_dark()
}
fn show_about(&self) {
let window = self.active_window().unwrap();
let dialog = gtk::AboutDialog::builder()
.transient_for(&window)
.modal(true)
.program_name("Furtherance")
.logo_icon_name(config::APP_ID)
.version(config::VERSION)
.comments("Track your time without being tracked.")
.copyright("© 2022 Ricky Kresslein")
.authors(vec!["Ricky Kresslein <rk@lakoliu.com>".into()])
.website("https://furtherance.app")
.license_type(gtk::License::Gpl30)
.build();
dialog.present();
}
fn delete_history(&self) {
// Show dialog to delete all history
let window = FurtheranceWindow::default();
let dialog = gtk::MessageDialog::with_markup(
Some(&window),
gtk::DialogFlags::MODAL,
gtk::MessageType::Question,
gtk::ButtonsType::None,
Some("<span size='x-large' weight='bold'>Delete history?</span>"),
);
dialog.add_buttons(&[
("Cancel", gtk::ResponseType::Reject),
("Delete", gtk::ResponseType::Accept)
]);
let message_area = dialog.message_area().downcast::<gtk::Box>().unwrap();
let explanation = gtk::Label::new(Some("This will delete ALL of your task history."));
let instructions = gtk::Label::new(Some(
"Type DELETE in the box below then click Delete to proceed."));
let delete_entry = gtk::Entry::new();
message_area.append(&explanation);
message_area.append(&instructions);
message_area.append(&delete_entry);
dialog.connect_response(clone!(@weak dialog = > move |_, resp| {
if resp == gtk::ResponseType::Accept {
if delete_entry.text().to_uppercase() == "DELETE" {
let _ = database::delete_all();
window.reset_history_box();
dialog.close();
}
} else {
dialog.close();
}
}));
dialog.show();
}
pub fn delete_enabled(&self, enabled: bool) {
if enabled {
let delete_history_action = gio::SimpleAction::new("delete-history", None);
delete_history_action.connect_activate(clone!(@weak self as app => move |_, _| {
app.delete_history();
}));
self.add_action(&delete_history_action);
} else {
self.remove_action("delete-history");
}
}
fn update_light_dark(&self) {
let manager = adw::StyleManager::default();
if !manager.system_supports_color_schemes() {
let color_scheme = if settings_manager::get_bool("dark-mode") {
adw::ColorScheme::PreferDark
} else {
adw::ColorScheme::PreferLight
};
manager.set_color_scheme(color_scheme);
}
}
}
impl Default for FurtheranceApplication {
fn default() -> Self {
gio::Application::default()
.expect("Could not get default GApplication")
.downcast()
.unwrap()
}
}
|