Browse Source

use common TabItem struct for tabs HashMap

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

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

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

Loading…
Cancel
Save