From c0ebe95eb88f67168d216bd4b2cb5c7f4224662a Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 22 Sep 2024 02:00:54 +0300 Subject: [PATCH] define browser entities as structs --- src/browser/main/mod.rs | 36 ++++++++++++++++++++------ src/browser/main/tab/mod.rs | 36 ++++++++++++++------------ src/browser/mod.rs | 50 ++++++++++++++++++++++++++----------- src/main.rs | 2 +- 4 files changed, 84 insertions(+), 40 deletions(-) diff --git a/src/browser/main/mod.rs b/src/browser/main/mod.rs index bf690059..f6f6fcf0 100644 --- a/src/browser/main/mod.rs +++ b/src/browser/main/mod.rs @@ -1,14 +1,34 @@ mod tab; +use std::sync::Arc; + use gtk::prelude::BoxExt; use gtk::Box; -pub fn new() -> Box { - let main = Box::builder() - .orientation(gtk::Orientation::Vertical) - .build(); - - main.append(&tab::new()); - - main +pub struct Main { + pub widget: Arc, + pub tab: Arc, +} + +impl Main { + pub fn tab_append(&self) { + self.tab.append(true); + } +} + +pub fn new() -> Main { + // Init components + let tab = Arc::new(tab::new()); + + // Init widget + let widget = Arc::new( + Box::builder() + .orientation(gtk::Orientation::Vertical) + .build(), + ); + + widget.append(tab.widget.as_ref()); + + // Init struct + Main { widget, tab } } diff --git a/src/browser/main/tab/mod.rs b/src/browser/main/tab/mod.rs index 900920ac..2a1df04b 100644 --- a/src/browser/main/tab/mod.rs +++ b/src/browser/main/tab/mod.rs @@ -1,27 +1,31 @@ mod label; mod page; +use std::sync::Arc; + use gtk::Notebook; - -pub fn new() -> Notebook { - let tab = Notebook::builder().scrollable(true).build(); - - // Add test tab @TODO restore from session - append(&tab, true); - - tab +pub struct Tab { + pub widget: Arc, } -pub fn append(tab: &Notebook, current: bool) -> u32 { - let page = page::new(); +impl Tab { + pub fn append(&self, current: bool) -> u32 { + let page = page::new(); - let page_number = tab.append_page(&page, Some(&label::new())); + let page_number = self.widget.append_page(&page, Some(&label::new())); - tab.set_tab_reorderable(&page, true); + self.widget.set_tab_reorderable(&page, true); - if current { - tab.set_current_page(Some(page_number)); + if current { + self.widget.set_current_page(Some(page_number)); + } + + page_number } - - page_number +} + +pub fn new() -> Tab { + let widget = Arc::new(Notebook::builder().scrollable(true).build()); + + Tab { widget } } diff --git a/src/browser/mod.rs b/src/browser/mod.rs index cc467eaa..486b843f 100644 --- a/src/browser/mod.rs +++ b/src/browser/mod.rs @@ -1,6 +1,8 @@ mod header; mod main; +use std::sync::Arc; + use gtk::{ gio::ActionEntry, prelude::{ActionMapExtManual, GtkWindowExt}, @@ -8,32 +10,50 @@ use gtk::{ }; use sqlite::Connection; +pub struct Browser { + pub widget: Arc, + pub main: Arc, +} -pub fn new(app: &Application, db: &Connection, width: i32, height: i32) -> ApplicationWindow { - // Init browser window - let browser = ApplicationWindow::builder() - .default_width(width) - .default_height(height) - .application(app) - .titlebar(&header::new()) - .child(&main::new()) - .build(); +pub fn new(app: &Application, db: &Connection, width: i32, height: i32) -> Browser { + // Init components + let main = Arc::new(main::new()); + + // Init widget + let widget = Arc::new( + ApplicationWindow::builder() + .default_width(width) + .default_height(height) + .application(app) + .titlebar(&header::new()) + .child(main.widget.as_ref()) + .build(), + ); // Init actions + let action_tab_append = ActionEntry::builder("tab_append") + .activate({ + let main = main.clone(); + move |_, _, _| { + main.tab_append(); + } + }) + .build(); + let action_debug = ActionEntry::builder("debug") - .activate(|browser: &ApplicationWindow, _, _| { - browser.emit_enable_debugging(true); + .activate(|this: &ApplicationWindow, _, _| { + this.emit_enable_debugging(true); }) .build(); let action_quit = ActionEntry::builder("quit") - .activate(|browser: &ApplicationWindow, _, _| { - browser.close(); + .activate(|this: &ApplicationWindow, _, _| { + this.close(); }) .build(); - browser.add_action_entries([action_debug, action_quit]); + widget.add_action_entries([action_tab_append, action_debug, action_quit]); // Done - browser + Browser { widget, main } } diff --git a/src/main.rs b/src/main.rs index 83cd0e35..847c692d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,7 +40,7 @@ fn main() -> glib::ExitCode { }; move |this| { - browser::new(&this, &db, 640, 480).present(); + browser::new(&this, &db, 640, 480).widget.present(); } });