diff --git a/src/browser/action.rs b/src/browser/action.rs new file mode 100644 index 00000000..26600c09 --- /dev/null +++ b/src/browser/action.rs @@ -0,0 +1,31 @@ +use gtk::gio::ActionEntry; +use gtk::prelude::GtkWindowExt; +use gtk::ApplicationWindow; + +pub fn debug() -> ActionEntry { + ActionEntry::builder("debug") + .activate(|this: &ApplicationWindow, _, _| { + this.emit_enable_debugging(true); + }) + .build() +} + +pub fn quit() -> ActionEntry { + ActionEntry::builder("quit") + .activate(|this: &ApplicationWindow, _, _| { + this.close(); + }) + .build() +} + +/* @TODO +pub fn tab_append(main) -> ActionEntry { + let action_tab_append = ActionEntry::builder("tab_append") + .activate({ + let main = main.clone(); + move |_, _, _| { + main.tab_append(); + } + }) + .build(); +}*/ diff --git a/src/browser/db.rs b/src/browser/db.rs index d92df147..f3bb5fb4 100644 --- a/src/browser/db.rs +++ b/src/browser/db.rs @@ -5,13 +5,19 @@ pub struct Browser { } impl Browser { + // Construct new browser DB (connection) + pub fn new(connection: Arc) -> Browser { + let this = Self { connection }; + this.init(); + 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) {} } - -pub fn new(connection: Arc) -> Browser { - let this = Browser { connection }; - this.init(); - this -} diff --git a/src/browser/mod.rs b/src/browser/mod.rs index 10c27fb4..9c7c5632 100644 --- a/src/browser/mod.rs +++ b/src/browser/mod.rs @@ -1,72 +1,47 @@ +mod action; mod db; mod header; mod main; +mod widget; -use std::sync::Arc; - -use gtk::{ - gio::ActionEntry, - prelude::{ActionMapExtManual, GtkWindowExt}, - Application, ApplicationWindow, -}; +use gtk::prelude::ActionMapExtManual; pub struct Browser { db: db::Browser, - pub widget: Arc, - pub header: Arc, - pub main: Arc, + widget: widget::Browser, } -pub fn new( - app: &Application, - connection: Arc, - width: i32, - height: i32, -) -> Browser { - // Init components - let header = Arc::new(header::new()); - let main = Arc::new(main::new()); +impl Browser { + // Construct new browser + pub fn new( + app: >k::Application, + connection: std::sync::Arc, + width: i32, + height: i32, + ) -> Browser { + // Init widget + let widget = widget::Browser::new( + app, + header::new().widget.as_ref(), // @TODO + main::new().widget.as_ref(), // @TODO + width, + height, + ); - // Init widget - let widget = Arc::new( - ApplicationWindow::builder() - .default_width(width) - .default_height(height) - .application(app) - .titlebar(header.widget.as_ref()) - .child(main.widget.as_ref()) - .build(), - ); + // Connect actions + widget + .gtk() + .add_action_entries([action::debug(), action::quit()]); - // Init actions - let action_tab_append = ActionEntry::builder("tab_append") - .activate({ - let main = main.clone(); - move |_, _, _| { - main.tab_append(); - } - }) - .build(); + // Return + Self { + db: db::Browser::new(connection), + widget, + } + } - let action_debug = ActionEntry::builder("debug") - .activate(|this: &ApplicationWindow, _, _| { - this.emit_enable_debugging(true); - }) - .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, - header, - main, + // Getters + pub fn widget(&self) -> &widget::Browser { + &self.widget } } diff --git a/src/browser/widget.rs b/src/browser/widget.rs new file mode 100644 index 00000000..c992a240 --- /dev/null +++ b/src/browser/widget.rs @@ -0,0 +1,27 @@ +pub struct Browser { + gtk: gtk::ApplicationWindow, +} + +impl Browser { + pub fn new( + application: >k::Application, + titlebar: >k::HeaderBar, + child: >k::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) -> >k::ApplicationWindow { + &self.gtk + } +} diff --git a/src/main.rs b/src/main.rs index 9e4fed54..80f79ae2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,10 @@ fn main() -> glib::ExitCode { }; move |this| { - browser::new(&this, db.clone(), 640, 480).widget.present(); + browser::Browser::new(&this, db.clone(), 640, 480) + .widget() + .gtk() + .present(); } });