128 lines
3.7 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::{
glib::{uuid_string_random, GString},
prelude::WidgetExt,
GestureClick, Notebook, Widget,
};
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-24 21:29:05 +03:00
widget: Arc<Notebook>,
// 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-24 21:29:05 +03:00
pub fn new() -> Arc<Tab> {
// Init GTK component
let notebook = Arc::new(Notebook::builder().scrollable(true).build());
// Init new Tab struct
let tab = Arc::new(Self {
// Reference wanted for async events, create new smart pointer
widget: notebook.clone(),
// Init empty HashMap index as no tabs appended yet
labels: RefCell::new(HashMap::new()),
pages: RefCell::new(HashMap::new()),
2024-09-24 21:29:05 +03:00
});
// Connect events
notebook.connect_page_removed({
// Make new local ref
let tab = tab.clone();
// Begin async action
move |_, widget: &Widget, _| {
// 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
}
});
tab // return Arc pointer to the new Tab constructed
2024-09-18 22:47:53 +03:00
}
2024-09-22 02:00:54 +03:00
2024-09-22 22:23:44 +03:00
// Actions
2024-09-23 22:40:23 +03:00
pub fn append(&self, 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
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
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-22 22:23:44 +03:00
// Getters
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
}