From de09688dd8b2c6b42c000e81526bd41970447573 Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 8 Oct 2024 07:09:41 +0300 Subject: [PATCH] fix tab items order --- src/app/browser/window/tab.rs | 34 +++++++++++---------- src/app/browser/window/tab/item.rs | 8 ++++- src/app/browser/window/tab/item/database.rs | 14 +++++++-- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/app/browser/window/tab.rs b/src/app/browser/window/tab.rs index 082b7047..a38257ce 100644 --- a/src/app/browser/window/tab.rs +++ b/src/app/browser/window/tab.rs @@ -250,22 +250,24 @@ impl Tab { // Delegate save action to childs let id = Database::last_insert_id(transaction); - // Read HashMap index collected - let mut page_number = 0; - - // @TODO incorrect order - for (_, item) in self.index.borrow().iter() { - item.save( - transaction, - &id, - &match self.widget.gobject().current_page() { - Some(number) => number == page_number, - None => false, - }, - )?; - - page_number += 1; - } + // At least one active page wanted to continue + if let Some(current_page) = self.widget.gobject().current_page() { + // Read collected HashMap index + for (_, item) in self.index.borrow().iter() { + // Get page number as HashMap does not keep order, no page_reorder listener also + match self.widget.gobject().page_num(item.page()) { + Some(page_number) => { + item.save( + transaction, + &id, + &page_number, + &(current_page == page_number), + )?; + } + None => panic!(), // page number expected at this point @TODO Err? + } + } + }; } Err(e) => return Err(e.to_string()), } diff --git a/src/app/browser/window/tab/item.rs b/src/app/browser/window/tab/item.rs index 38b3e961..b04cdb45 100644 --- a/src/app/browser/window/tab/item.rs +++ b/src/app/browser/window/tab/item.rs @@ -170,9 +170,15 @@ impl Item { &self, transaction: &Transaction, app_browser_window_tab_id: &i64, + page_number: &u32, is_initially_current: &bool, ) -> Result<(), String> { - match Database::add(transaction, app_browser_window_tab_id, is_initially_current) { + match Database::add( + transaction, + app_browser_window_tab_id, + page_number, + is_initially_current, + ) { Ok(_) => { let id = Database::last_insert_id(transaction); diff --git a/src/app/browser/window/tab/item/database.rs b/src/app/browser/window/tab/item/database.rs index 93e70be6..62ff9979 100644 --- a/src/app/browser/window/tab/item/database.rs +++ b/src/app/browser/window/tab/item/database.rs @@ -17,6 +17,7 @@ impl Database { ( `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `app_browser_window_tab_id` INTEGER NOT NULL, + `page_number` INTEGER NOT NULL, `is_initially_current` INTEGER NOT NULL )", [], @@ -26,14 +27,20 @@ impl Database { pub fn add( tx: &Transaction, app_browser_window_tab_id: &i64, + page_number: &u32, is_initially_current: &bool, ) -> Result { tx.execute( "INSERT INTO `app_browser_window_tab_item` ( `app_browser_window_tab_id`, + `page_number`, `is_initially_current` - ) VALUES (?, ?)", - [app_browser_window_tab_id, &(*is_initially_current as i64)], + ) VALUES (?, ?, ?)", + [ + app_browser_window_tab_id, + &(*page_number as i64), + &(*is_initially_current as i64), + ], ) } @@ -42,7 +49,8 @@ impl Database { "SELECT `id`, `app_browser_window_tab_id`, `is_initially_current` FROM `app_browser_window_tab_item` - WHERE `app_browser_window_tab_id` = ?", + WHERE `app_browser_window_tab_id` = ? + ORDER BY `page_number` ASC", // just order by, no store in struct wanted )?; let result = stmt.query_map([app_browser_window_tab_id], |row| {