Browse Source

generate unique HashMap keys for children widgets

master
yggverse 2 months ago
parent
commit
f407117470
  1. 11
      src/browser/main/tab/label/mod.rs
  2. 55
      src/browser/main/tab/mod.rs
  3. 10
      src/browser/main/tab/page/mod.rs

11
src/browser/main/tab/label/mod.rs

@ -1,11 +1,15 @@
mod pin; mod pin;
mod title; mod title;
use gtk::prelude::{BoxExt, WidgetExt};
use gtk::{Align, Box, Orientation};
use pin::Pin; use pin::Pin;
use title::Title; use title::Title;
use gtk::{
glib::GString,
prelude::{BoxExt, WidgetExt},
Align, Box, Orientation,
};
pub struct Label { pub struct Label {
// Components // Components
pin: Pin, pin: Pin,
@ -17,7 +21,7 @@ pub struct Label {
impl Label { impl Label {
// Construct // Construct
pub fn new(is_pinned: bool) -> Label { pub fn new(name: GString, is_pinned: bool) -> Label {
// Components // Components
let pin = Pin::new(is_pinned); let pin = Pin::new(is_pinned);
let title = Title::new(); let title = Title::new();
@ -26,6 +30,7 @@ impl Label {
let widget = Box::builder() let widget = Box::builder()
.orientation(Orientation::Horizontal) .orientation(Orientation::Horizontal)
.halign(Align::Center) .halign(Align::Center)
.name(name)
.build(); .build();
widget.append(pin.widget()); widget.append(pin.widget());

55
src/browser/main/tab/mod.rs

@ -4,22 +4,31 @@ mod page;
use label::Label; use label::Label;
use page::Page; 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}; use std::{cell::RefCell, collections::HashMap, sync::Arc};
pub struct Tab { pub struct Tab {
widget: Notebook, widget: Notebook,
// Dynamically allocated reference index // Dynamically allocated reference index
labels: RefCell<HashMap<u32, Arc<Label>>>, labels: RefCell<HashMap<GString, Arc<Label>>>,
pages: RefCell<HashMap<u32, Arc<Page>>>, pages: RefCell<HashMap<GString, Arc<Page>>>,
} }
impl Tab { impl Tab {
// Construct // Construct
pub fn new() -> Tab { pub fn new() -> Tab {
Self {
// Init widget // Init widget
widget: Notebook::builder().scrollable(true).build(), let widget = Notebook::builder().scrollable(true).build();
// Connect actions
widget.connect_page_reordered(|this: &Notebook, widget: &Widget, page_number: u32| todo!());
Self {
widget,
// Init empty hashmap as no tabs yet // Init empty hashmap as no tabs yet
labels: RefCell::new(HashMap::new()), labels: RefCell::new(HashMap::new()),
pages: RefCell::new(HashMap::new()), pages: RefCell::new(HashMap::new()),
@ -28,9 +37,17 @@ impl Tab {
// Actions // 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
let id = uuid_string_random();
// Init new tab components // Init new tab components
let label = Arc::new(Label::new(false)); let label = Arc::new(Label::new(id.clone(), false));
let page = Arc::new(Page::new()); 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 // Init additional label actions
let controller = GestureClick::new(); let controller = GestureClick::new();
@ -57,17 +74,6 @@ impl Tab {
self.widget.set_current_page(Some(page_number)); 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 // Result
page_number page_number
} }
@ -87,12 +93,15 @@ impl Tab {
// Toggle pin status for active tab // Toggle pin status for active tab
pub fn pin(&self) { pub fn pin(&self) {
// Get current page
if let Some(page_number) = self.widget.current_page() { if let Some(page_number) = self.widget.current_page() {
let label = self.labels.borrow(); // Get default widget to extract it name as the ID for childs
label if let Some(widget) = self.widget.nth_page(Some(page_number)) {
.get(&page_number) // Get label by ID
.unwrap() if let Some(label) = self.labels.borrow().get(&widget.widget_name()) {
.pin(!label.get(&page_number).unwrap().is_pinned()); // toggle label.pin(!label.is_pinned()); // toggle
}
}
} }
} }

10
src/browser/main/tab/page/mod.rs

@ -1,21 +1,23 @@
mod content; mod content;
mod navigation; mod navigation;
use gtk::prelude::BoxExt; use gtk::{glib::GString, prelude::BoxExt, Box, Orientation};
use gtk::{Box, Orientation};
pub struct Page { pub struct Page {
widget: Box, widget: Box,
} }
impl Page { impl Page {
pub fn new() -> Page { pub fn new(name: GString) -> Page {
// Init components // Init components
let navigation = navigation::Navigation::new(); let navigation = navigation::Navigation::new();
let content = content::Content::new(); let content = content::Content::new();
// Init widget // 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(navigation.widget());
widget.append(content.widget()); widget.append(content.widget());

Loading…
Cancel
Save