implement new tab position enum

This commit is contained in:
yggverse 2024-11-11 19:28:39 +02:00
parent 57cdc4cee9
commit 23e4e83e45
6 changed files with 89 additions and 44 deletions

View file

@ -2,6 +2,7 @@ mod database;
use database::Database;
use crate::app::browser::window::action::Position;
use adw::{TabPage, TabView};
use gtk::prelude::IsA;
use sqlite::Transaction;
@ -13,34 +14,32 @@ pub struct Widget {
}
impl Widget {
// Construct
// Constructors
pub fn new(
keyword: &str, // ID
tab_view: &TabView,
child: &impl IsA<gtk::Widget>,
title: Option<&str>,
position: Option<i32>,
position: Position,
state: (bool, bool, bool),
) -> Self {
let gobject = match position {
Some(value) => {
// If given `position` match pinned tab, GTK will panic with notice:
// adw_tab_view_insert: assertion 'position >= self->n_pinned_pages'
// as the solution, prepend new page after pinned tabs on this case
if value > tab_view.n_pinned_pages() {
tab_view.insert(child, value)
} else {
tab_view.prepend(child)
}
}
None => tab_view.append(child),
};
// Define state variables
let (is_pinned, is_selected, is_attention) = state;
// Create new `TabPage` GObject in given `TabView`
let gobject = match position {
Position::After => match tab_view.selected_page() {
Some(page) => add(tab_view, child, tab_view.page_position(&page) + 1),
None => tab_view.append(child),
},
Position::End => tab_view.append(child),
Position::Number(value) => add(tab_view, child, value),
};
// Setup `GObject`
gobject.set_needs_attention(is_attention);
gobject.set_keyword(keyword);
gobject.set_title(match title {
Some(value) => value,
None => DEFAULT_TITLE,
@ -52,10 +51,12 @@ impl Widget {
tab_view.set_selected_page(&gobject);
}
// Done
Self { gobject }
}
// Actions
pub fn clean(
&self,
transaction: &Transaction,
@ -131,12 +132,14 @@ impl Widget {
}
// Getters
pub fn gobject(&self) -> &TabPage {
&self.gobject
}
}
// Tools
pub fn migrate(tx: &Transaction) -> Result<(), String> {
// Migrate self components
if let Err(e) = Database::init(tx) {
@ -149,3 +152,17 @@ pub fn migrate(tx: &Transaction) -> Result<(), String> {
// Success
Ok(())
}
/// Create new [TabPage](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.TabPage.html)
/// in [TabView](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.TabView.html) at given position
///
/// * if given `position` match pinned tab, GTK will panic with notice:
/// adw_tab_view_insert: assertion 'position >= self->n_pinned_pages'\
/// as the solution, prepend new page after pinned tabs in this case
fn add(tab_view: &TabView, child: &impl IsA<gtk::Widget>, position: i32) -> TabPage {
if position > tab_view.n_pinned_pages() {
tab_view.insert(child, position)
} else {
tab_view.prepend(child)
}
}