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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use adw::prelude::*;
use adw::subclass::prelude::*;
use chrono::prelude::*;
use glib::clone;
use gtk::{gio, glib};
use std::cell::Cell;
use sun_times::sun_times;
use tracing::*;
use crate::application::ExampleApplication;
use crate::config::{APP_ID, PROFILE};
use crate::locations::*;
mod imp {
use super::*;
#[derive(Debug, gtk::CompositeTemplate)]
#[template(resource = "/garden/patchouli/sunrise/ui/window.ui")]
pub struct ExampleApplicationWindow {
#[template_child]
pub headerbar: TemplateChild<adw::HeaderBar>,
#[template_child]
pub location: TemplateChild<gtk::DropDown>,
#[template_child]
pub date: TemplateChild<adw::SpinRow>,
#[template_child]
pub sunrise: TemplateChild<gtk::Label>,
#[template_child]
pub sundown: TemplateChild<gtk::Label>,
pub settings: gio::Settings,
pub location_city: Cell<Option<&'static Location>>,
pub now: Cell<DateTime<Local>>,
}
impl Default for ExampleApplicationWindow {
fn default() -> Self {
Self {
headerbar: TemplateChild::default(),
location: TemplateChild::default(),
date: TemplateChild::default(),
sunrise: TemplateChild::default(),
sundown: TemplateChild::default(),
settings: gio::Settings::new(APP_ID),
location_city: Cell::new(None),
now: Cell::new(chrono::Local::now()),
}
}
}
#[glib::object_subclass]
impl ObjectSubclass for ExampleApplicationWindow {
const NAME: &'static str = "ExampleApplicationWindow";
type Type = super::ExampleApplicationWindow;
type ParentType = adw::ApplicationWindow;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
// You must call `Widget`'s `init_template()` within `instance_init()`.
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for ExampleApplicationWindow {
fn constructed(&self) {
self.parent_constructed();
let obj = self.obj();
// Devel Profile
if PROFILE == "Devel" {
obj.add_css_class("devel");
}
// Load latest window state
obj.load_window_size();
}
}
impl WidgetImpl for ExampleApplicationWindow {}
impl WindowImpl for ExampleApplicationWindow {
// Save window state on delete event
fn close_request(&self) -> glib::Propagation {
if let Err(err) = self.obj().save_window_size() {
tracing::warn!("Failed to save window state, {}", &err);
}
// Pass close request on to the parent
self.parent_close_request()
}
}
impl ApplicationWindowImpl for ExampleApplicationWindow {}
impl AdwApplicationWindowImpl for ExampleApplicationWindow {}
}
glib::wrapper! {
pub struct ExampleApplicationWindow(ObjectSubclass<imp::ExampleApplicationWindow>)
@extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, adw::ApplicationWindow,
@implements gio::ActionMap, gio::ActionGroup, gtk::ConstraintTarget, gtk::Accessible, gtk::Buildable, gtk::ShortcutManager, gtk::Native, gtk::Root;
}
impl ExampleApplicationWindow {
pub fn new(app: &ExampleApplication) -> Self {
let this: Self = glib::Object::builder().property("application", app).build();
this.init();
this
}
fn init(&self) {
let labels: Vec<_> = LOCATIONS.iter().map(|l| l.label).collect();
let model = gtk::StringList::new(&labels);
self.imp().location.set_model(Some(&model));
match self.restore_city() {
Ok(id) => self.imp().location.set_selected(id as u32),
Err(_) => self.imp().location.set_selected(FALLBACK_ID),
}
self.set_suntimes();
self.imp().location.connect_selected_notify(clone!(
#[weak(rename_to = this)]
self,
move |dropdown| {
let id = dropdown.selected();
let _ = this.set_city(id);
this.set_suntimes();
}
));
self.imp().date.connect_changed(clone!(
#[weak(rename_to = this)]
self,
move |spin| {
let value = spin.value();
let new_date;
if value < 0.0 {
new_date =
chrono::Local::now().checked_sub_days(chrono::Days::new(-value as u64));
} else {
new_date =
chrono::Local::now().checked_add_days(chrono::Days::new(value as u64));
}
if let Some(now) = new_date {
this.imp().now.replace(now);
this.set_suntimes();
}
}
));
}
fn set_suntimes(&self) {
let now = self.imp().now.get();
let city = self.city();
let times = sun_times(
now.date_naive(),
city.coords.1,
city.coords.0,
city.elevation as f64,
);
info!("{times:#?}");
self.imp()
.date
.set_subtitle(format!("{}", now.format("%B %e")).as_str());
if let Some((rise, down)) = times {
let rise = DateTime::<Local>::from(rise).format("%H:%M");
let down = DateTime::<Local>::from(down).format("%H:%M");
self.imp().sunrise.set_text(format!("{rise}").as_str());
self.imp().sundown.set_text(format!("{down}").as_str());
}
}
fn city(&self) -> &'static Location {
self.imp()
.location_city
.get()
.unwrap_or_else(|| LOCATIONS.get(FALLBACK_ID as usize).unwrap())
}
fn set_city(&self, id: u32) -> Result<(), glib::BoolError> {
let imp = self.imp();
let city = LOCATIONS.get(id as usize);
if let Some(city) = city.as_ref() {
// TODO store key id instead of int offset
imp.settings.set_int("city", id as i32)?;
imp.settings.set_string("city-id", city.id)?;
}
imp.location_city.set(city);
Ok(())
}
fn restore_city(&self) -> Result<usize, glib::BoolError> {
let imp = self.imp();
let id = imp.settings.int("city") as usize;
let id_key = imp.settings.string("city-id");
// get city form raw index
let city = LOCATIONS.get(id);
if let Some(city) = city {
// check that it matches wikidata-key
if city.id == id_key {
info!("restored {} {}", id, city.label);
imp.location_city.set(Some(city));
Ok(id)
} else {
// get city from wikidata-key
let city_by_key = LOCATIONS.iter().enumerate().find_map(|(i, c)| {
if c.id == id_key {
Some((i, c))
} else {
None
}
});
if let Some((i, city)) = city_by_key {
info!("restored by key {} {}", id_key, city.label);
imp.location_city.set(Some(city));
Ok(i)
} else {
Err(glib::bool_error!("no city for id key"))
}
}
} else {
Err(glib::bool_error!("city out of bounds"))
}
}
fn save_window_size(&self) -> Result<(), glib::BoolError> {
let imp = self.imp();
let (width, height) = self.default_size();
imp.settings.set_int("window-width", width)?;
imp.settings.set_int("window-height", height)?;
imp.settings
.set_boolean("is-maximized", self.is_maximized())?;
Ok(())
}
fn load_window_size(&self) {
let imp = self.imp();
let width = imp.settings.int("window-width");
let height = imp.settings.int("window-height");
let is_maximized = imp.settings.boolean("is-maximized");
self.set_default_size(width, height);
if is_maximized {
self.maximize();
}
}
}