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 {
fn init(&self) {}
fn save(&self) {}
fn restore(&self) {}
}
// Construct new browser DB (connection)
pub fn new(connection: Arc<sqlite::Connection>) -> Browser {
let this = Browser { connection };
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) {}
}

View File

@ -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<gtk::ApplicationWindow>,
pub header: Arc<header::Header>,
pub main: Arc<main::Main>,
widget: widget::Browser,
}
impl Browser {
// Construct new browser
pub fn new(
app: &Application,
connection: Arc<sqlite::Connection>,
app: &gtk::Application,
connection: std::sync::Arc<sqlite::Connection>,
width: i32,
height: i32,
) -> Browser {
// Init components
let header = Arc::new(header::new());
let main = Arc::new(main::new());
// 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(),
let widget = widget::Browser::new(
app,
header::new().widget.as_ref(), // @TODO
main::new().widget.as_ref(), // @TODO
width,
height,
);
// Init actions
let action_tab_append = ActionEntry::builder("tab_append")
.activate({
let main = main.clone();
move |_, _, _| {
main.tab_append();
}
})
.build();
// Connect actions
widget
.gtk()
.add_action_entries([action::debug(), action::quit()]);
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),
// Return
Self {
db: db::Browser::new(connection),
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| {
browser::new(&this, db.clone(), 640, 480).widget.present();
browser::Browser::new(&this, db.clone(), 640, 480)
.widget()
.gtk()
.present();
}
});