implement activate action

This commit is contained in:
yggverse 2024-09-26 01:11:07 +03:00
parent 1db706aa5b
commit bdad712690
2 changed files with 16 additions and 22 deletions

View File

@ -1,11 +1,13 @@
mod tab; mod tab;
use std::sync::Arc;
use tab::Tab; use tab::Tab;
use gtk::{glib::GString, prelude::BoxExt, Box, Orientation}; use gtk::{glib::GString, prelude::BoxExt, Box, Orientation};
pub struct Main { pub struct Main {
tab: Tab, tab: Arc<Tab>,
widget: Box, widget: Box,
} }
@ -13,7 +15,9 @@ impl Main {
// Construct // Construct
pub fn new() -> Main { pub fn new() -> Main {
// Init components // Init components
let tab = Tab::new(); let tab = Arc::new(Tab::new());
tab.activate(tab.clone());
// Extras // Extras
let widget = Box::builder().orientation(Orientation::Vertical).build(); let widget = Box::builder().orientation(Orientation::Vertical).build();

View File

@ -14,7 +14,7 @@ use std::{cell::RefCell, collections::HashMap, sync::Arc};
pub struct Tab { pub struct Tab {
// GTK // GTK
widget: Arc<Notebook>, widget: Notebook,
// Dynamically allocated reference index // Dynamically allocated reference index
labels: RefCell<HashMap<GString, Arc<Label>>>, labels: RefCell<HashMap<GString, Arc<Label>>>,
pages: RefCell<HashMap<GString, Arc<Page>>>, pages: RefCell<HashMap<GString, Arc<Page>>>,
@ -22,37 +22,27 @@ pub struct Tab {
impl Tab { impl Tab {
// Construct // Construct
pub fn new() -> Tab { pub fn new() -> Self {
// Init GTK component Self {
let notebook = Arc::new(Notebook::builder().scrollable(true).build()); widget: Notebook::builder().scrollable(true).build(),
// Init new Tab struct
let tab = Self {
// Reference wanted for async events, create new smart pointer
widget: notebook.clone(),
// Init empty HashMap index as no tabs appended yet // Init empty HashMap index as no tabs appended yet
labels: RefCell::new(HashMap::new()), labels: RefCell::new(HashMap::new()),
pages: RefCell::new(HashMap::new()), pages: RefCell::new(HashMap::new()),
}; }
}
// Connect events // Actions
/* @TODO move outside pub fn activate(&self, tab: Arc<Self>) {
notebook.connect_page_removed({ self.widget.connect_page_removed({
// Make new local ref
let tab = tab.clone();
// Begin async action
move |_, widget: &Widget, _| { move |_, widget: &Widget, _| {
// Cleanup HashMap index // Cleanup HashMap index
let id = &widget.widget_name(); let id = &widget.widget_name();
tab.labels.borrow_mut().remove(id); tab.labels.borrow_mut().remove(id);
tab.pages.borrow_mut().remove(id); tab.pages.borrow_mut().remove(id);
} }
});*/ });
tab // return Arc pointer to the new Tab constructed
} }
// Actions
pub fn append(&self, is_current_page: bool) -> u32 { pub fn append(&self, is_current_page: bool) -> u32 {
// Generate unique ID for new page components // Generate unique ID for new page components
let id = uuid_string_random(); let id = uuid_string_random();