separate features to submodules

This commit is contained in:
yggverse 2024-09-22 17:34:22 +03:00
parent 80fecec656
commit c38363b8cc
5 changed files with 107 additions and 65 deletions

31
src/browser/action.rs Normal file
View file

@ -0,0 +1,31 @@
use gtk::gio::ActionEntry;
use gtk::prelude::GtkWindowExt;
use gtk::ApplicationWindow;
pub fn debug() -> ActionEntry<ApplicationWindow> {
ActionEntry::builder("debug")
.activate(|this: &ApplicationWindow, _, _| {
this.emit_enable_debugging(true);
})
.build()
}
pub fn quit() -> ActionEntry<ApplicationWindow> {
ActionEntry::builder("quit")
.activate(|this: &ApplicationWindow, _, _| {
this.close();
})
.build()
}
/* @TODO
pub fn tab_append(main) -> ActionEntry<ApplicationWindow> {
let action_tab_append = ActionEntry::builder("tab_append")
.activate({
let main = main.clone();
move |_, _, _| {
main.tab_append();
}
})
.build();
}*/

View file

@ -5,13 +5,19 @@ pub struct Browser {
} }
impl Browser { impl Browser {
fn init(&self) {} // Construct new browser DB (connection)
fn save(&self) {} pub fn new(connection: Arc<sqlite::Connection>) -> Browser {
fn restore(&self) {} let this = Self { connection };
}
pub fn new(connection: Arc<sqlite::Connection>) -> Browser {
let this = Browser { connection };
this.init(); this.init();
this this
}
// Create browser table in DB if not exist yet
fn init(&self) {}
// Save active browser session to DB
fn save(&self) {}
// Restore previous browser session from DB
fn restore(&self) {}
} }

View file

@ -1,72 +1,47 @@
mod action;
mod db; mod db;
mod header; mod header;
mod main; mod main;
mod widget;
use std::sync::Arc; use gtk::prelude::ActionMapExtManual;
use gtk::{
gio::ActionEntry,
prelude::{ActionMapExtManual, GtkWindowExt},
Application, ApplicationWindow,
};
pub struct Browser { pub struct Browser {
db: db::Browser, db: db::Browser,
pub widget: Arc<gtk::ApplicationWindow>, widget: widget::Browser,
pub header: Arc<header::Header>,
pub main: Arc<main::Main>,
} }
pub fn new( impl Browser {
app: &Application, // Construct new browser
connection: Arc<sqlite::Connection>, pub fn new(
app: &gtk::Application,
connection: std::sync::Arc<sqlite::Connection>,
width: i32, width: i32,
height: i32, height: i32,
) -> Browser { ) -> Browser {
// Init components
let header = Arc::new(header::new());
let main = Arc::new(main::new());
// Init widget // Init widget
let widget = Arc::new( let widget = widget::Browser::new(
ApplicationWindow::builder() app,
.default_width(width) header::new().widget.as_ref(), // @TODO
.default_height(height) main::new().widget.as_ref(), // @TODO
.application(app) width,
.titlebar(header.widget.as_ref()) height,
.child(main.widget.as_ref())
.build(),
); );
// Init actions // Connect actions
let action_tab_append = ActionEntry::builder("tab_append") widget
.activate({ .gtk()
let main = main.clone(); .add_action_entries([action::debug(), action::quit()]);
move |_, _, _| {
main.tab_append();
}
})
.build();
let action_debug = ActionEntry::builder("debug") // Return
.activate(|this: &ApplicationWindow, _, _| { Self {
this.emit_enable_debugging(true); db: db::Browser::new(connection),
})
.build();
let action_quit = ActionEntry::builder("quit")
.activate(|this: &ApplicationWindow, _, _| {
this.close();
})
.build();
widget.add_action_entries([action_tab_append, action_debug, action_quit]);
// Done
Browser {
db: db::new(connection),
widget, widget,
header, }
main, }
// Getters
pub fn widget(&self) -> &widget::Browser {
&self.widget
} }
} }

27
src/browser/widget.rs Normal file
View file

@ -0,0 +1,27 @@
pub struct Browser {
gtk: gtk::ApplicationWindow,
}
impl Browser {
pub fn new(
application: &gtk::Application,
titlebar: &gtk::HeaderBar,
child: &gtk::Box,
default_width: i32,
default_height: i32,
) -> Browser {
Self {
gtk: gtk::ApplicationWindow::builder()
.application(application)
.default_width(default_width)
.default_height(default_height)
.titlebar(titlebar)
.child(child)
.build(),
}
}
pub fn gtk(&self) -> &gtk::ApplicationWindow {
&self.gtk
}
}

View file

@ -41,7 +41,10 @@ fn main() -> glib::ExitCode {
}; };
move |this| { move |this| {
browser::new(&this, db.clone(), 640, 480).widget.present(); browser::Browser::new(&this, db.clone(), 640, 480)
.widget()
.gtk()
.present();
} }
}); });