Yoda/src/browser/main/tab.rs

215 lines
6.6 KiB
Rust
Raw Normal View History

2024-09-20 18:02:10 +03:00
mod label;
mod page;
2024-09-18 22:38:18 +03:00
2024-09-23 18:51:48 +03:00
use label::Label;
use page::Page;
2024-09-23 15:56:09 +03:00
use gtk::{
2024-09-28 03:10:07 +03:00
gio::SimpleAction,
glib::{uuid_string_random, GString},
prelude::WidgetExt,
2024-09-28 02:31:32 +03:00
GestureClick, Notebook,
};
2024-09-24 21:29:05 +03:00
use std::{cell::RefCell, collections::HashMap, sync::Arc};
2024-09-22 02:00:54 +03:00
pub struct Tab {
2024-09-24 21:31:12 +03:00
// GTK
2024-09-26 01:11:07 +03:00
widget: Notebook,
2024-09-28 03:10:07 +03:00
// Keep action links in memory to not require them on every tab append
action_tab_page_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>,
// Dynamically allocated reference index
labels: RefCell<HashMap<GString, Arc<Label>>>,
pages: RefCell<HashMap<GString, Arc<Page>>>,
2024-09-18 22:38:18 +03:00
}
2024-09-22 02:00:54 +03:00
impl Tab {
2024-09-22 22:23:44 +03:00
// Construct
2024-09-28 03:10:07 +03:00
pub fn new(
action_tab_page_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>,
) -> Self {
// Init widget
let widget = Notebook::builder().scrollable(true).build();
// Return non activated struct
2024-09-26 01:11:07 +03:00
Self {
2024-09-28 03:10:07 +03:00
// GTK
widget,
// Define action links
action_tab_page_reload,
action_update,
2024-09-24 21:29:05 +03:00
// Init empty HashMap index as no tabs appended yet
labels: RefCell::new(HashMap::new()),
pages: RefCell::new(HashMap::new()),
2024-09-26 01:11:07 +03:00
}
}
2024-09-24 21:29:05 +03:00
2024-09-26 01:11:07 +03:00
// Actions
pub fn activate(&self, tab: Arc<Self>) {
self.widget.connect_page_removed({
2024-09-28 02:29:14 +03:00
move |_, widget, _| {
2024-09-24 21:29:05 +03:00
// Cleanup HashMap index
2024-09-24 21:36:29 +03:00
let id = &widget.widget_name();
tab.labels.borrow_mut().remove(id);
tab.pages.borrow_mut().remove(id);
2024-09-24 21:29:05 +03:00
}
2024-09-26 01:11:07 +03:00
});
2024-09-18 22:47:53 +03:00
}
2024-09-22 02:00:54 +03:00
pub fn append(
&self,
page_navigation_request_text: Option<GString>,
is_current_page: bool,
) -> u32 {
// Generate unique ID for new page components
let id = uuid_string_random();
2024-09-23 21:51:32 +03:00
// Init new tab components
2024-09-26 00:59:44 +03:00
let label = Arc::new(Label::new(id.clone(), false));
2024-09-28 03:10:07 +03:00
let page = Arc::new(Page::new(
id.clone(),
page_navigation_request_text,
self.action_tab_page_reload.clone(),
self.action_update.clone(),
));
// 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());
2024-09-23 18:51:48 +03:00
2024-09-23 21:51:32 +03:00
// Init additional label actions
let controller = GestureClick::new();
controller.connect_pressed({
let label = label.clone();
move |_, n: i32, _, _| {
// double click
if n == 2 {
2024-09-23 22:01:48 +03:00
label.pin(!label.is_pinned()); // toggle
2024-09-23 21:51:32 +03:00
}
}
});
label.widget().add_controller(controller);
// Append new Notebook page
2024-09-23 18:51:48 +03:00
let page_number = self.widget.append_page(page.widget(), Some(label.widget()));
// Additional setup for Notebook tab created
2024-09-23 18:51:48 +03:00
self.widget.set_tab_reorderable(page.widget(), true);
2024-09-23 22:40:23 +03:00
if is_current_page {
2024-09-23 18:51:48 +03:00
self.widget.set_current_page(Some(page_number));
}
2024-09-23 21:51:32 +03:00
// Result
2024-09-23 18:51:48 +03:00
page_number
2024-09-22 22:23:44 +03:00
}
2024-09-18 22:47:53 +03:00
// Close active tab
2024-09-23 16:03:39 +03:00
pub fn close(&self) {
2024-09-24 00:17:53 +03:00
self.widget.remove_page(self.widget.current_page());
2024-09-23 16:03:39 +03:00
}
// Close all tabs
2024-09-23 16:03:39 +03:00
pub fn close_all(&self) {
2024-09-24 01:00:52 +03:00
// @TODO skip pinned or make confirmation alert (GTK>=4.10)
2024-09-24 00:56:46 +03:00
while let Some(page_number) = self.widget.current_page() {
self.widget.remove_page(Some(page_number));
}
}
2024-09-23 16:03:39 +03:00
// Toggle pin status for active tab
pub fn pin(&self) {
// Get current page
if let Some(page_number) = self.widget.current_page() {
// 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
}
}
}
2024-09-23 15:44:33 +03:00
}
2024-09-25 01:14:45 +03:00
pub fn page_reload(&self) {
// Get current page
if let Some(page_number) = self.widget.current_page() {
// Get default widget to extract it name as the ID for childs
if let Some(widget) = self.widget.nth_page(Some(page_number)) {
// Get page by widget ID
if let Some(page) = self.pages.borrow().get(&widget.widget_name()) {
page.reload();
}
}
}
}
2024-09-24 23:08:40 +03:00
pub fn update(&self) {
// Get current page
if let Some(page_number) = self.widget.current_page() {
// Get default widget to extract it name as the ID for childs
if let Some(widget) = self.widget.nth_page(Some(page_number)) {
// Get widget ID
let id = &widget.widget_name();
// Get page by widget ID
if let Some(page) = self.pages.borrow().get(id) {
page.update();
2024-09-27 18:49:18 +03:00
// Get label by widget ID
if let Some(label) = self.labels.borrow().get(id) {
2024-09-27 20:38:55 +03:00
if let Some(title) = page.title() {
label.update(Some(&title));
} else {
label.update(None);
}
2024-09-27 18:49:18 +03:00
}
2024-09-24 23:08:40 +03:00
}
}
}
}
2024-09-22 22:23:44 +03:00
// Getters
2024-09-27 16:23:59 +03:00
pub fn page_title(&self) -> Option<GString> {
2024-09-25 22:19:48 +03:00
// Get current page
if let Some(page_number) = self.widget.current_page() {
// Get default widget to extract it name as the ID for childs
if let Some(widget) = self.widget.nth_page(Some(page_number)) {
// Get widget ID
let id = &widget.widget_name();
// Get page by widget ID
if let Some(page) = self.pages.borrow().get(id) {
2024-09-27 20:38:55 +03:00
return page.title();
2024-09-25 22:19:48 +03:00
}
}
}
2024-09-27 16:23:59 +03:00
None
2024-09-25 22:19:48 +03:00
}
2024-09-27 16:23:59 +03:00
pub fn page_description(&self) -> Option<GString> {
2024-09-25 22:19:48 +03:00
// Get current page
if let Some(page_number) = self.widget.current_page() {
// Get default widget to extract it name as the ID for childs
if let Some(widget) = self.widget.nth_page(Some(page_number)) {
// Get widget ID
let id = &widget.widget_name();
// Get page by widget ID
if let Some(page) = self.pages.borrow().get(id) {
2024-09-27 20:38:55 +03:00
return page.description();
2024-09-25 22:19:48 +03:00
}
}
}
2024-09-27 16:23:59 +03:00
None
2024-09-25 22:19:48 +03:00
}
2024-09-23 18:51:48 +03:00
pub fn widget(&self) -> &Notebook {
2024-09-24 21:29:05 +03:00
self.widget.as_ref()
2024-09-22 22:23:44 +03:00
}
2024-09-20 18:02:10 +03:00
}