implement tab restore feature

This commit is contained in:
yggverse 2024-10-11 04:37:06 +03:00
parent d074ef17fe
commit 09c08b2b6f
7 changed files with 58 additions and 72 deletions

View file

@ -3,6 +3,7 @@ use sqlite::{Error, Transaction};
pub struct Table {
pub id: i64,
// pub app_browser_window_tab_id: i64, not in use
pub is_selected: bool,
}
pub struct Database {
@ -16,7 +17,8 @@ impl Database {
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_browser_window_tab_id` INTEGER NOT NULL,
`page_number` INTEGER NOT NULL
`page_position` INTEGER NOT NULL,
`is_selected` INTEGER NOT NULL
)",
[],
)
@ -25,29 +27,38 @@ impl Database {
pub fn add(
tx: &Transaction,
app_browser_window_tab_id: &i64,
page_number: &u32,
page_position: &i32,
is_selected: &bool,
) -> Result<usize, Error> {
tx.execute(
"INSERT INTO `app_browser_window_tab_item` (
`app_browser_window_tab_id`,
`page_number`
) VALUES (?, ?)",
[app_browser_window_tab_id, &(*page_number as i64)],
`page_position`,
`is_selected`
) VALUES (?, ?, ?)",
[
app_browser_window_tab_id,
&(*page_position as i64),
&(*is_selected as i64),
],
)
}
pub fn records(tx: &Transaction, app_browser_window_tab_id: &i64) -> Result<Vec<Table>, Error> {
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_window_tab_id` FROM `app_browser_window_tab_item`
WHERE `app_browser_window_tab_id` = ?
ORDER BY `page_number` ASC", // just order by, no store in struct wanted
`app_browser_window_tab_id`,
`is_selected`
FROM `app_browser_window_tab_item`
WHERE `app_browser_window_tab_id` = ?
ORDER BY `page_position` ASC", // just order by, no store in struct wanted
)?;
let result = stmt.query_map([app_browser_window_tab_id], |row| {
Ok(Table {
id: row.get(0)?,
// app_browser_window_tab_id: row.get(1)?, not in use
is_selected: row.get(2)?,
})
})?;

View file

@ -9,16 +9,13 @@ use navigation::Navigation;
use widget::Widget;
use gtk::{
gio::{
Cancellable, SimpleAction, SimpleActionGroup, SocketClient, SocketProtocol,
TlsCertificateFlags,
},
gio::{Cancellable, SimpleAction, SocketClient, SocketProtocol, TlsCertificateFlags},
glib::{gformat, GString, Priority, Regex, RegexCompileFlags, RegexMatchFlags, Uri, UriFlags},
prelude::{
ActionExt, ActionMapExt, BoxExt, IOStreamExt, InputStreamExtManual, OutputStreamExtManual,
SocketClientExt, StaticVariantType, ToVariant, WidgetExt,
ActionExt, IOStreamExt, InputStreamExtManual, OutputStreamExtManual, SocketClientExt,
StaticVariantType, ToVariant,
},
Box, Orientation,
Box,
};
use std::{cell::RefCell, path::Path, sync::Arc};
@ -466,14 +463,6 @@ impl Page {
}
// Getters
pub fn title(&self) -> Option<GString> {
self.meta.borrow().title.clone()
}
pub fn description(&self) -> Option<GString> {
self.meta.borrow().description.clone()
}
pub fn gobject(&self) -> &Box {
&self.widget.gobject()
}

View file

@ -2,6 +2,8 @@ use adw::{TabPage, TabView};
use gtk::Box;
use std::sync::Arc;
const DEFAULT_TITLE: &str = "New page";
pub struct Widget {
gobject: TabPage,
}
@ -12,15 +14,16 @@ impl Widget {
tab_view: &TabView,
page: &Box,
title: Option<&str>,
is_selected_page: bool,
is_selected: bool,
) -> Arc<Self> {
let gobject = tab_view.append(page);
if let Some(value) = title {
gobject.set_title(value);
}
gobject.set_title(match title {
Some(value) => value,
None => DEFAULT_TITLE,
});
if is_selected_page {
if is_selected {
tab_view.set_selected_page(&gobject);
}