Yoda/src/app/browser/widget.rs

110 lines
3.0 KiB
Rust
Raw Normal View History

2024-10-05 03:27:09 +03:00
mod database;
use database::Database;
2024-10-09 10:50:41 +03:00
use adw::ApplicationWindow;
use gtk::{prelude::GtkWindowExt, Box};
use sqlite::Transaction;
2024-10-05 03:27:09 +03:00
// Default options
const DEFAULT_HEIGHT: i32 = 480;
const DEFAULT_WIDTH: i32 = 640;
const MAXIMIZED: bool = false;
pub struct Widget {
gobject: ApplicationWindow,
2024-10-05 03:27:09 +03:00
}
impl Widget {
// Construct
2024-10-09 10:50:41 +03:00
pub fn new(content: &Box) -> Self {
2024-10-05 03:27:09 +03:00
// Init GTK
let gobject = ApplicationWindow::builder()
2024-10-09 10:50:41 +03:00
.content(content)
2024-10-05 03:27:09 +03:00
.default_height(DEFAULT_HEIGHT)
.default_width(DEFAULT_WIDTH)
.maximized(MAXIMIZED)
.build();
// Return new struct
2024-10-07 20:34:48 +03:00
Self { gobject }
2024-10-05 03:27:09 +03:00
}
// Actions
pub fn clean(&self, transaction: &Transaction, app_browser_id: &i64) -> Result<(), String> {
match Database::records(transaction, app_browser_id) {
2024-10-05 03:27:09 +03:00
Ok(records) => {
for record in records {
match Database::delete(transaction, &record.id) {
2024-10-05 03:27:09 +03:00
Ok(_) => {
// Delegate clean action to childs
// nothing yet..
}
Err(e) => return Err(e.to_string()),
2024-10-05 03:27:09 +03:00
}
}
}
Err(e) => return Err(e.to_string()),
2024-10-05 03:27:09 +03:00
}
Ok(())
2024-10-05 03:27:09 +03:00
}
pub fn restore(&self, transaction: &Transaction, app_browser_id: &i64) -> Result<(), String> {
match Database::records(transaction, app_browser_id) {
2024-10-05 15:32:03 +03:00
Ok(records) => {
for record in records {
// Restore widget
self.gobject.set_maximized(record.is_maximized);
self.gobject
2024-10-05 15:32:03 +03:00
.set_default_size(record.default_width, record.default_height);
// Delegate restore action to childs
// nothing yet..
}
}
Err(e) => return Err(e.to_string()),
2024-10-05 15:32:03 +03:00
}
Ok(())
2024-10-05 03:27:09 +03:00
}
pub fn save(&self, transaction: &Transaction, app_browser_id: &i64) -> Result<(), String> {
2024-10-07 20:34:48 +03:00
match Database::add(
transaction,
2024-10-05 03:27:09 +03:00
app_browser_id,
&self.gobject.default_width(),
&self.gobject.default_height(),
&self.gobject.is_maximized(),
2024-10-05 03:27:09 +03:00
) {
Ok(_) => {
// Delegate save action to childs
// let id = self.database.last_insert_id(transaction);
2024-10-05 03:27:09 +03:00
// nothing yet..
}
Err(e) => return Err(e.to_string()),
2024-10-05 03:27:09 +03:00
}
Ok(())
2024-10-05 03:27:09 +03:00
}
// Getters
pub fn gobject(&self) -> &ApplicationWindow {
&self.gobject
2024-10-05 03:27:09 +03:00
}
// Tools
pub fn migrate(tx: &Transaction) -> Result<(), String> {
// Migrate self components
if let Err(e) = Database::init(&tx) {
return Err(e.to_string());
}
// Delegate migration to childs
// nothing yet..
// Success
Ok(())
}
2024-10-05 03:27:09 +03:00
}