Browse Source

use common TabItem struct for tabs HashMap

master
yggverse 2 months ago
parent
commit
c60ab38c2a
  1. 74
      src/app/browser/window/tab.rs

74
src/app/browser/window/tab.rs

@ -15,6 +15,11 @@ use gtk::{ @@ -15,6 +15,11 @@ use gtk::{
use std::{cell::RefCell, collections::HashMap, sync::Arc};
struct TabItem {
label: Arc<Label>,
page: Arc<Page>,
}
pub struct Tab {
// Keep action links in memory to not require them on every tab append
action_tab_page_navigation_base: Arc<SimpleAction>,
@ -23,8 +28,7 @@ pub struct Tab { @@ -23,8 +28,7 @@ pub struct Tab {
action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>,
// Dynamically allocated reference index
labels: RefCell<HashMap<GString, Arc<Label>>>,
pages: RefCell<HashMap<GString, Arc<Page>>>,
index: RefCell<HashMap<GString, TabItem>>,
// GTK
widget: Arc<Widget>,
}
@ -47,8 +51,7 @@ impl Tab { @@ -47,8 +51,7 @@ impl Tab {
action_tab_page_navigation_reload,
action_update,
// Init empty HashMap index as no tabs appended yet
labels: RefCell::new(HashMap::new()),
pages: RefCell::new(HashMap::new()),
index: RefCell::new(HashMap::new()),
// GTK
widget: Arc::new(Widget::new()),
}
@ -60,9 +63,14 @@ impl Tab { @@ -60,9 +63,14 @@ impl Tab {
.gobject()
.connect_page_removed(move |_, widget, _| {
// Cleanup HashMap index
let id = &widget.widget_name();
tab.labels.borrow_mut().remove(id);
tab.pages.borrow_mut().remove(id);
let id = widget.widget_name();
// Check for required value as raw access to gobject @TODO
if id.is_empty() {
panic!("Undefined tab index!")
}
tab.index.borrow_mut().remove(&id);
});
// Switch page post-event (`connect_switch_page` activates before `page_number` get updated)
@ -94,8 +102,13 @@ impl Tab { @@ -94,8 +102,13 @@ impl Tab {
));
// Register dynamically created tab components in the HashMap index
self.labels.borrow_mut().insert(id.clone(), label.clone());
self.pages.borrow_mut().insert(id.clone(), page.clone());
self.index.borrow_mut().insert(
id.clone(),
TabItem {
label: label.clone(),
page: page.clone(),
},
);
// Init additional label actions
let controller = GestureClick::new();
@ -148,55 +161,52 @@ impl Tab { @@ -148,55 +161,52 @@ impl Tab {
// Toggle pin status for active tab
pub fn pin(&self) {
if let Some(id) = self.widget.current_name() {
if let Some(label) = self.labels.borrow().get(&id) {
label.pin(!label.is_pinned()); // toggle
if let Some(item) = self.index.borrow().get(&id) {
item.label.pin(!item.label.is_pinned()); // toggle
}
}
}
pub fn page_navigation_base(&self) {
if let Some(id) = self.widget.current_name() {
if let Some(page) = self.pages.borrow().get(&id) {
page.navigation_base();
if let Some(item) = self.index.borrow().get(&id) {
item.page.navigation_base();
}
}
}
pub fn page_navigation_history_back(&self) {
if let Some(id) = self.widget.current_name() {
if let Some(page) = self.pages.borrow().get(&id) {
page.navigation_history_back();
if let Some(item) = self.index.borrow().get(&id) {
item.page.navigation_history_back();
}
}
}
pub fn page_navigation_history_forward(&self) {
if let Some(id) = self.widget.current_name() {
if let Some(page) = self.pages.borrow().get(&id) {
page.navigation_history_forward();
if let Some(item) = self.index.borrow().get(&id) {
item.page.navigation_history_forward();
}
}
}
pub fn page_navigation_reload(&self) {
if let Some(id) = self.widget.current_name() {
if let Some(page) = self.pages.borrow().get(&id) {
page.navigation_reload();
if let Some(item) = self.index.borrow().get(&id) {
item.page.navigation_reload();
}
}
}
pub fn update(&self) {
if let Some(id) = self.widget.current_name() {
if let Some(page) = self.pages.borrow().get(&id) {
page.update();
if let Some(label) = self.labels.borrow().get(&id) {
if let Some(title) = page.title() {
label.update(Some(&title));
} else {
label.update(None);
}
if let Some(item) = self.index.borrow().get(&id) {
item.page.update();
if let Some(title) = item.page.title() {
item.label.update(Some(&title));
} else {
item.label.update(None);
}
}
}
@ -205,8 +215,8 @@ impl Tab { @@ -205,8 +215,8 @@ impl Tab {
// Getters
pub fn page_title(&self) -> Option<GString> {
if let Some(id) = self.widget.current_name() {
if let Some(page) = self.pages.borrow().get(&id) {
return page.title();
if let Some(item) = self.index.borrow().get(&id) {
return item.page.title();
}
}
@ -216,8 +226,8 @@ impl Tab { @@ -216,8 +226,8 @@ impl Tab {
pub fn page_description(&self) -> Option<GString> {
if let Some(id) = self.widget.current_name() {
// Get page by widget ID
if let Some(page) = self.pages.borrow().get(&id) {
return page.description();
if let Some(item) = self.index.borrow().get(&id) {
return item.page.description();
}
}

Loading…
Cancel
Save