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
// window.rs
//
// Copyright 2024 nee <nee-git@hidamari.blue>
//
// 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 <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later
use adw::prelude::*;
use adw::subclass::prelude::*;
use gtk::{gio, glib};
use crate::application::ExampleApplication;
use crate::browse_page::BrowsePage;
use crate::config::{APP_ID, PROFILE};
use crate::wiki_page::WikiPage;
mod imp {
use super::*;
#[derive(Debug, gtk::CompositeTemplate)]
#[template(resource = "/blue/hidamari/boorus/ui/window.ui")]
pub struct ExampleApplicationWindow {
#[template_child]
pub toast: TemplateChild<adw::ToastOverlay>,
#[template_child]
pub stack: TemplateChild<gtk::Stack>,
#[template_child]
pub browse: TemplateChild<BrowsePage>,
#[template_child]
pub wiki: TemplateChild<WikiPage>,
pub settings: gio::Settings,
}
impl Default for ExampleApplicationWindow {
fn default() -> Self {
Self {
browse: TemplateChild::default(),
wiki: TemplateChild::default(),
toast: TemplateChild::default(),
stack: TemplateChild::default(),
settings: gio::Settings::new(APP_ID),
}
}
}
#[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) {
BrowsePage::ensure_type();
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 {
glib::Object::builder().property("application", app).build()
}
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();
}
}
pub fn stack(&self) -> gtk::Stack {
self.imp().stack.get()
}
pub fn browse(&self) -> &BrowsePage {
&self.imp().browse
}
pub fn wiki(&self) -> &WikiPage {
&self.imp().wiki
}
pub fn make_toast(&self, toast: adw::Toast) {
self.imp().toast.add_toast(toast);
}
pub fn init(&self, state: &crate::data::State) {
self.browse().init(state);
self.wiki().init(state);
}
}