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 @@ @@ -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 { @@ -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 { @@ -26,6 +30,7 @@ impl Label {
let widget = Box::builder()
.orientation(Orientation::Horizontal)
.halign(Align::Center)
.name(name)
.build();
widget.append(pin.widget());

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

@ -4,22 +4,31 @@ mod page; @@ -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<HashMap<u32, Arc<Label>>>,
pages: RefCell<HashMap<u32, Arc<Page>>>,
labels: RefCell<HashMap<GString, Arc<Label>>>,
pages: RefCell<HashMap<GString, Arc<Page>>>,
}
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 { @@ -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 { @@ -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 { @@ -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
}
}
}
}

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

@ -1,21 +1,23 @@ @@ -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());

Loading…
Cancel
Save