mod database;
use adw::ApplicationWindow;
use gtk::{
gio::SimpleActionGroup,
glib::GString,
prelude::{GtkWindowExt, IsA, WidgetExt},
};
use sqlite::Transaction;
const DEFAULT_HEIGHT: i32 = 480;
const DEFAULT_WIDTH: i32 = 640;
const MAXIMIZED: bool = false;
pub struct Widget {
gobject: ApplicationWindow,
}
impl Widget {
pub fn new(
content: &impl IsA<gtk::Widget>,
action_groups: &[(&GString, SimpleActionGroup)],
) -> Self {
let gobject = ApplicationWindow::builder()
.content(content)
.default_height(DEFAULT_HEIGHT)
.default_width(DEFAULT_WIDTH)
.maximized(MAXIMIZED)
.build();
for (name, group) in action_groups {
gobject.insert_action_group(name, Some(group));
}
Self { gobject }
}
pub fn clean(&self, transaction: &Transaction, app_browser_id: &i64) -> Result<(), String> {
match database::select(transaction, app_browser_id) {
Ok(records) => {
for record in records {
match database::delete(transaction, &record.id) {
Ok(_) => {
}
Err(e) => return Err(e.to_string()),
}
}
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
pub fn restore(&self, transaction: &Transaction, app_browser_id: &i64) -> Result<(), String> {
match database::select(transaction, app_browser_id) {
Ok(records) => {
for record in records {
self.gobject.set_maximized(record.is_maximized);
self.gobject
.set_default_size(record.default_width, record.default_height);
}
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
pub fn save(&self, transaction: &Transaction, app_browser_id: &i64) -> Result<(), String> {
match database::insert(
transaction,
app_browser_id,
&self.gobject.default_width(),
&self.gobject.default_height(),
&self.gobject.is_maximized(),
) {
Ok(_) => {
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
pub fn gobject(&self) -> &ApplicationWindow {
&self.gobject
}
}
pub fn migrate(tx: &Transaction) -> Result<(), String> {
if let Err(e) = database::init(tx) {
return Err(e.to_string());
}
Ok(())
}