mod database;
use crate::app::browser::window::action::Position;
use adw::{TabPage, TabView};
use gtk::prelude::IsA;
use sqlite::Transaction;
const DEFAULT_TITLE: &str = "New page";
pub struct Widget {
pub gobject: TabPage,
}
impl Widget {
pub fn new(
keyword: &str, tab_view: &TabView,
child: &impl IsA<gtk::Widget>,
title: Option<&str>,
position: Position,
state: (bool, bool, bool),
) -> Self {
let (is_pinned, is_selected, is_attention) = state;
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),
};
gobject.set_needs_attention(is_attention);
gobject.set_keyword(keyword);
gobject.set_title(match title {
Some(value) => value,
None => DEFAULT_TITLE,
});
tab_view.set_page_pinned(&gobject, is_pinned);
if is_selected {
tab_view.set_selected_page(&gobject);
}
Self { gobject }
}
pub fn clean(
&self,
transaction: &Transaction,
app_browser_window_tab_item_id: &i64,
) -> Result<(), String> {
match database::select(transaction, app_browser_window_tab_item_id) {
Ok(records) => {
for record in records {
match database::delete(transaction, &record.id) {
Ok(_) => {
}
Err(e) => return Err(e.to_string()),
}
}
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
pub fn restore(
&self,
transaction: &Transaction,
app_browser_window_tab_item_id: &i64,
) -> Result<(), String> {
match database::select(transaction, app_browser_window_tab_item_id) {
Ok(records) => {
for record in records {
if let Some(title) = record.title {
self.gobject.set_title(title.as_str());
}
}
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
pub fn save(
&self,
transaction: &Transaction,
app_browser_window_tab_item_id: &i64,
) -> Result<(), String> {
let title = self.gobject.title();
match database::insert(
transaction,
app_browser_window_tab_item_id,
match title.is_empty() {
true => None,
false => Some(title.as_str()),
},
) {
Ok(_) => {
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
}
pub fn migrate(tx: &Transaction) -> Result<(), String> {
if let Err(e) = database::init(tx) {
return Err(e.to_string());
}
Ok(())
}
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)
}
}