define browser entities as structs

This commit is contained in:
yggverse 2024-09-22 02:00:54 +03:00
parent 3fbfe6a7e0
commit c0ebe95eb8
4 changed files with 84 additions and 40 deletions

View File

@ -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<gtk::Box>,
pub tab: Arc<tab::Tab>,
}
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 }
}

View File

@ -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<gtk::Notebook>,
}
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 }
}

View File

@ -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<ApplicationWindow>,
pub main: Arc<main::Main>,
}
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 }
}

View File

@ -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();
}
});