2024-10-11 04:37:06 +03:00
|
|
|
use adw::TabView;
|
2024-10-14 05:12:30 +03:00
|
|
|
use gtk::{
|
2024-11-04 04:52:33 +02:00
|
|
|
gio::{Icon, MenuModel},
|
|
|
|
glib::GString,
|
|
|
|
prelude::IsA,
|
2024-10-14 05:12:30 +03:00
|
|
|
};
|
2024-10-06 16:28:46 +03:00
|
|
|
|
2024-11-04 04:52:33 +02:00
|
|
|
/// Currently used as the indicator for pinned tabs
|
|
|
|
const DEFAULT_TAB_ICON: &str = "view-pin-symbolic";
|
|
|
|
|
|
|
|
/// Wrapper for [TabView](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.TabView.html) GObject
|
2024-10-06 16:28:46 +03:00
|
|
|
pub struct Widget {
|
2024-10-11 01:38:12 +03:00
|
|
|
gobject: TabView,
|
2024-10-06 16:28:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Widget {
|
|
|
|
// Construct
|
2024-11-04 04:52:33 +02:00
|
|
|
pub fn new(menu_model: &impl IsA<MenuModel>) -> Self {
|
2024-10-14 05:12:30 +03:00
|
|
|
// Init gobject
|
2024-11-04 04:52:33 +02:00
|
|
|
let gobject = TabView::builder().menu_model(menu_model).build();
|
2024-10-14 05:12:30 +03:00
|
|
|
|
2024-11-04 04:52:33 +02:00
|
|
|
// Change default icon (if available in the system icon set)
|
|
|
|
// * visible for pinned tabs only
|
|
|
|
// * @TODO not default GTK behavior, make this feature optional
|
|
|
|
if let Ok(default_icon) = Icon::for_string(DEFAULT_TAB_ICON) {
|
2024-10-14 05:24:24 +03:00
|
|
|
gobject.set_default_icon(&default_icon);
|
|
|
|
}
|
|
|
|
|
2024-11-04 04:52:33 +02:00
|
|
|
// Done
|
2024-10-14 05:12:30 +03:00
|
|
|
Self { gobject }
|
2024-10-06 16:28:46 +03:00
|
|
|
}
|
|
|
|
|
2024-10-06 17:12:08 +03:00
|
|
|
// Actions
|
2024-11-04 04:52:33 +02:00
|
|
|
|
2024-10-06 17:12:08 +03:00
|
|
|
pub fn close(&self) {
|
2024-10-11 01:38:12 +03:00
|
|
|
if let Some(selected_page) = self.gobject.selected_page() {
|
|
|
|
self.gobject.close_page(&selected_page);
|
|
|
|
}
|
2024-10-06 17:12:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close_all(&self) {
|
|
|
|
// @TODO skip pinned or make confirmation alert (GTK>=4.10)
|
2024-10-11 01:38:12 +03:00
|
|
|
if let Some(selected_page) = self.gobject.selected_page() {
|
|
|
|
self.gobject.close_other_pages(&selected_page);
|
|
|
|
self.close();
|
2024-10-06 17:12:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 16:28:46 +03:00
|
|
|
// Getters
|
2024-11-04 04:52:33 +02:00
|
|
|
|
2024-10-11 05:01:42 +03:00
|
|
|
pub fn current_page_keyword(&self) -> Option<GString> {
|
2024-10-11 01:38:12 +03:00
|
|
|
let page = self.gobject.selected_page()?;
|
2024-10-11 05:01:42 +03:00
|
|
|
let id = page.keyword()?;
|
|
|
|
Some(id)
|
2024-10-06 17:12:08 +03:00
|
|
|
}
|
|
|
|
|
2024-10-11 01:38:12 +03:00
|
|
|
pub fn gobject(&self) -> &TabView {
|
2024-10-06 16:28:46 +03:00
|
|
|
&self.gobject
|
|
|
|
}
|
|
|
|
}
|