From f407117470b1e757530692a00f6b9828439acfa9 Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 24 Sep 2024 20:48:31 +0300 Subject: [PATCH] generate unique HashMap keys for children widgets --- src/browser/main/tab/label/mod.rs | 11 +++++-- src/browser/main/tab/mod.rs | 55 ++++++++++++++++++------------- src/browser/main/tab/page/mod.rs | 10 +++--- 3 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src/browser/main/tab/label/mod.rs b/src/browser/main/tab/label/mod.rs index e3f88705..a5c95a03 100644 --- a/src/browser/main/tab/label/mod.rs +++ b/src/browser/main/tab/label/mod.rs @@ -1,11 +1,15 @@ mod pin; mod title; -use gtk::prelude::{BoxExt, WidgetExt}; -use gtk::{Align, Box, Orientation}; use pin::Pin; use title::Title; +use gtk::{ + glib::GString, + prelude::{BoxExt, WidgetExt}, + Align, Box, Orientation, +}; + pub struct Label { // Components pin: Pin, @@ -17,7 +21,7 @@ pub struct Label { impl Label { // Construct - pub fn new(is_pinned: bool) -> Label { + pub fn new(name: GString, is_pinned: bool) -> Label { // Components let pin = Pin::new(is_pinned); let title = Title::new(); @@ -26,6 +30,7 @@ impl Label { let widget = Box::builder() .orientation(Orientation::Horizontal) .halign(Align::Center) + .name(name) .build(); widget.append(pin.widget()); diff --git a/src/browser/main/tab/mod.rs b/src/browser/main/tab/mod.rs index 47bc094a..7f7b89eb 100644 --- a/src/browser/main/tab/mod.rs +++ b/src/browser/main/tab/mod.rs @@ -4,22 +4,31 @@ mod page; use label::Label; use page::Page; -use gtk::{prelude::WidgetExt, GestureClick, Notebook}; +use gtk::{ + glib::{uuid_string_random, GString}, + prelude::WidgetExt, + GestureClick, Notebook, Widget, +}; use std::{cell::RefCell, collections::HashMap, sync::Arc}; pub struct Tab { widget: Notebook, // Dynamically allocated reference index - labels: RefCell>>, - pages: RefCell>>, + labels: RefCell>>, + pages: RefCell>>, } impl Tab { // Construct pub fn new() -> Tab { + // Init widget + let widget = Notebook::builder().scrollable(true).build(); + + // Connect actions + widget.connect_page_reordered(|this: &Notebook, widget: &Widget, page_number: u32| todo!()); + Self { - // Init widget - widget: Notebook::builder().scrollable(true).build(), + widget, // Init empty hashmap as no tabs yet labels: RefCell::new(HashMap::new()), pages: RefCell::new(HashMap::new()), @@ -28,9 +37,17 @@ impl Tab { // Actions pub fn append(&self, is_current_page: bool) -> u32 { + // Generate unique ID for new page components + let id = uuid_string_random(); + // Init new tab components - let label = Arc::new(Label::new(false)); - let page = Arc::new(Page::new()); + let label = Arc::new(Label::new(id.clone(), false)); + let page = Arc::new(Page::new(id.clone())); + + // Register dynamically created tab components in the HashMap index + // @TODO cleanup on tab remove + self.labels.borrow_mut().insert(id.clone(), label.clone()); + self.pages.borrow_mut().insert(id.clone(), page.clone()); // Init additional label actions let controller = GestureClick::new(); @@ -57,17 +74,6 @@ impl Tab { self.widget.set_current_page(Some(page_number)); } - // Register dynamically created tab components in the HashMap index - // @TODO static key on tab reorder - // @TODO cleanup on tab remove - self.labels - .borrow_mut() - .insert(page_number.try_into().unwrap(), label); - - self.pages - .borrow_mut() - .insert(page_number.try_into().unwrap(), page); - // Result page_number } @@ -87,12 +93,15 @@ impl Tab { // Toggle pin status for active tab pub fn pin(&self) { + // Get current page if let Some(page_number) = self.widget.current_page() { - let label = self.labels.borrow(); - label - .get(&page_number) - .unwrap() - .pin(!label.get(&page_number).unwrap().is_pinned()); // toggle + // Get default widget to extract it name as the ID for childs + if let Some(widget) = self.widget.nth_page(Some(page_number)) { + // Get label by ID + if let Some(label) = self.labels.borrow().get(&widget.widget_name()) { + label.pin(!label.is_pinned()); // toggle + } + } } } diff --git a/src/browser/main/tab/page/mod.rs b/src/browser/main/tab/page/mod.rs index 321703e8..d2665131 100644 --- a/src/browser/main/tab/page/mod.rs +++ b/src/browser/main/tab/page/mod.rs @@ -1,21 +1,23 @@ mod content; mod navigation; -use gtk::prelude::BoxExt; -use gtk::{Box, Orientation}; +use gtk::{glib::GString, prelude::BoxExt, Box, Orientation}; pub struct Page { widget: Box, } impl Page { - pub fn new() -> Page { + pub fn new(name: GString) -> Page { // Init components let navigation = navigation::Navigation::new(); let content = content::Content::new(); // Init widget - let widget = Box::builder().orientation(Orientation::Vertical).build(); + let widget = Box::builder() + .orientation(Orientation::Vertical) + .name(name) + .build(); widget.append(navigation.widget()); widget.append(content.widget());