Browse Source

begin gtk notebook to adwaita tab page features replacement

master
yggverse 2 months ago
parent
commit
ef5eb9b317
  1. 27
      src/app/browser/window/tab.rs
  2. 42
      src/app/browser/window/tab/widget.rs
  3. 6
      src/app/browser/window/widget.rs

27
src/app/browser/window/tab.rs

@ -4,16 +4,15 @@ mod widget;
use database::Database; use database::Database;
use item::Item; use item::Item;
use sqlite::Transaction;
use widget::Widget; use widget::Widget;
use adw::TabView;
use gtk::{ use gtk::{
gio::SimpleAction, gio::SimpleAction,
glib::GString, glib::{GString, Propagation},
prelude::{ActionExt, WidgetExt}, prelude::{ActionExt, WidgetExt},
Notebook,
}; };
use sqlite::Transaction;
use std::{cell::RefCell, collections::HashMap, sync::Arc}; use std::{cell::RefCell, collections::HashMap, sync::Arc};
// Main // Main
@ -65,24 +64,19 @@ impl Tab {
pub fn activate(&self, tab: Arc<Self>) { pub fn activate(&self, tab: Arc<Self>) {
self.widget self.widget
.gobject() .gobject()
.connect_page_removed(move |_, widget, _| { .connect_close_page(move |_, tab_page| {
/* @TODO
// Cleanup HashMap index // Cleanup HashMap index
let id = widget.widget_name(); let id = tab_page.widget_name();
// Check for required value as raw access to gobject @TODO // Check for required value as raw access to gobject @TODO
if id.is_empty() { if id.is_empty() {
panic!("Undefined tab index!") panic!("Undefined tab index!")
} }
tab.index.borrow_mut().remove(&id); tab.index.borrow_mut().remove(&id); */
Propagation::Proceed
}); });
// Switch page post-event (`connect_switch_page` activates before `page_number` get updated)
self.widget.gobject().connect_page_notify({
let action_update = self.action_update.clone();
// Update window header with current page title
move |_| action_update.activate(None)
});
} }
pub fn append( pub fn append(
@ -253,6 +247,7 @@ impl Tab {
let id = Database::last_insert_id(transaction); let id = Database::last_insert_id(transaction);
// At least one active page wanted to continue // At least one active page wanted to continue
/* @TODO
if let Some(current_page) = self.widget.gobject().current_page() { if let Some(current_page) = self.widget.gobject().current_page() {
// Read collected HashMap index // Read collected HashMap index
for (_, item) in self.index.borrow().iter() { for (_, item) in self.index.borrow().iter() {
@ -269,7 +264,7 @@ impl Tab {
None => panic!(), // page number expected at this point @TODO Err? None => panic!(), // page number expected at this point @TODO Err?
} }
} }
}; }; */
} }
Err(e) => return Err(e.to_string()), Err(e) => return Err(e.to_string()),
} }
@ -278,7 +273,7 @@ impl Tab {
} }
// Getters // Getters
pub fn gobject(&self) -> &Notebook { pub fn gobject(&self) -> &TabView {
self.widget.gobject() self.widget.gobject()
} }

42
src/app/browser/window/tab/widget.rs

@ -1,58 +1,52 @@
use gtk::{glib::GString, prelude::WidgetExt, Box, Notebook}; use adw::{TabPage, TabView};
use gtk::{glib::GString, Box};
pub struct Widget { pub struct Widget {
gobject: Notebook, gobject: TabView,
} }
impl Widget { impl Widget {
// Construct // Construct
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
gobject: Notebook::builder().scrollable(true).build(), gobject: TabView::builder().build(),
} }
} }
// Actions // Actions
pub fn append(&self, label: &Box, page: &Box, is_current_page: bool) -> u32 { pub fn append(&self, page: &Box) -> TabPage {
// Append new Notebook page self.gobject.append(page)
let page_number = self.gobject.append_page(page, Some(label));
// Additional setup for Notebook tab created
self.gobject.set_tab_reorderable(page, true);
if is_current_page {
self.gobject.set_current_page(Some(page_number));
}
// Result
page_number
} }
pub fn close(&self) { pub fn close(&self) {
self.gobject.remove_page(self.gobject().current_page()); if let Some(selected_page) = self.gobject.selected_page() {
self.gobject.close_page(&selected_page);
}
} }
pub fn close_all(&self) { pub fn close_all(&self) {
// @TODO skip pinned or make confirmation alert (GTK>=4.10) // @TODO skip pinned or make confirmation alert (GTK>=4.10)
while let Some(page_number) = self.gobject.current_page() { if let Some(selected_page) = self.gobject.selected_page() {
self.gobject.remove_page(Some(page_number)); self.gobject.close_other_pages(&selected_page);
self.close();
} }
} }
// Getters // Getters
pub fn current_name(&self) -> Option<GString> { pub fn current_name(&self) -> Option<GString> {
let page_number = self.gobject.current_page()?; let page = self.gobject.selected_page()?;
let nth_page = self.gobject.nth_page(Some(page_number))?;
let widget_name = nth_page.widget_name(); /* @TODO
let widget_name = page.widget_name();
if !widget_name.is_empty() { if !widget_name.is_empty() {
Some(widget_name) Some(widget_name)
} else { } else {
None None
} } */
None
} }
pub fn gobject(&self) -> &Notebook { pub fn gobject(&self) -> &TabView {
&self.gobject &self.gobject
} }
} }

6
src/app/browser/window/widget.rs

@ -1,5 +1,5 @@
use adw::ToolbarView; use adw::{TabView, ToolbarView};
use gtk::{prelude::BoxExt, Box, Notebook, Orientation}; use gtk::{prelude::BoxExt, Box, Orientation};
pub struct Widget { pub struct Widget {
gobject: Box, gobject: Box,
@ -7,7 +7,7 @@ pub struct Widget {
impl Widget { impl Widget {
// Construct // Construct
pub fn new(header: &ToolbarView, tab: &Notebook) -> Self { pub fn new(header: &ToolbarView, tab: &TabView) -> Self {
let gobject = Box::builder().orientation(Orientation::Vertical).build(); let gobject = Box::builder().orientation(Orientation::Vertical).build();
gobject.append(header); gobject.append(header);
gobject.append(tab); gobject.append(tab);

Loading…
Cancel
Save