From 9e5a2a490cab3804d93b368a3c64d76a71e14c60 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 9 Oct 2024 10:50:41 +0300 Subject: [PATCH] use libadwaita for app & app window --- src/app.rs | 2 +- src/app/browser.rs | 34 ++---------- src/app/browser/widget.rs | 9 ++- src/app/browser/window.rs | 55 +++++++++++++++---- src/app/browser/{ => window}/header.rs | 0 src/app/browser/{ => window}/header/title.rs | 0 src/app/browser/{ => window}/header/tray.rs | 0 .../browser/{ => window}/header/tray/menu.rs | 0 .../browser/{ => window}/header/tray/tab.rs | 0 src/app/browser/{ => window}/header/widget.rs | 0 src/app/browser/window/widget.rs | 4 +- 11 files changed, 57 insertions(+), 47 deletions(-) rename src/app/browser/{ => window}/header.rs (100%) rename src/app/browser/{ => window}/header/title.rs (100%) rename src/app/browser/{ => window}/header/tray.rs (100%) rename src/app/browser/{ => window}/header/tray/menu.rs (100%) rename src/app/browser/{ => window}/header/tray/tab.rs (100%) rename src/app/browser/{ => window}/header/widget.rs (100%) diff --git a/src/app.rs b/src/app.rs index b4f0e40e..ed44bd5c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -6,10 +6,10 @@ use action::Action; use browser::Browser; use database::Database; +use adw::Application; use gtk::{ glib::ExitCode, prelude::{ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt}, - Application, }; use sqlite::{Connection, Transaction}; diff --git a/src/app/browser.rs b/src/app/browser.rs index 542ae126..d8a85c5c 100644 --- a/src/app/browser.rs +++ b/src/app/browser.rs @@ -1,18 +1,15 @@ mod database; -mod header; mod widget; mod window; use database::Database; -use header::Header; use widget::Widget; use window::Window; +use adw::ApplicationWindow; use gtk::{ gio::{AppInfo, AppLaunchContext, SimpleAction}, - glib::GString, prelude::{ActionMapExt, GtkWindowExt}, - ApplicationWindow, }; use sqlite::Transaction; use std::{path::PathBuf, sync::Arc}; @@ -43,11 +40,11 @@ impl Browser { action_tab_page_navigation_reload: Arc, action_tab_pin: Arc, ) -> Browser { - // Init components - let header = Arc::new(Header::new( + let window = Arc::new(Window::new( action_tool_debug.clone(), action_tool_profile_directory.clone(), action_quit.clone(), + action_update.clone(), action_tab_append.clone(), action_tab_close.clone(), action_tab_close_all.clone(), @@ -58,16 +55,10 @@ impl Browser { action_tab_pin.clone(), )); - let window = Arc::new(Window::new( - action_tab_page_navigation_base.clone(), - action_tab_page_navigation_history_back.clone(), - action_tab_page_navigation_history_forward.clone(), - action_tab_page_navigation_reload.clone(), - action_update.clone(), - )); + //window.gobject().append(header.gobject()); // Init widget - let widget = Arc::new(Widget::new(header.gobject(), window.gobject())); + let widget = Arc::new(Widget::new(window.gobject())); // Assign actions widget.gobject().add_action(action_tool_debug.as_ref()); @@ -119,24 +110,9 @@ impl Browser { }); action_update.connect_activate({ - let header = header.clone(); let window = window.clone(); move |_, _| { - // Update window first window.update(); - - // Update header - let title = match window.tab_page_title() { - Some(value) => value, - None => GString::new(), // @TODO - }; - - let subtitle = match window.tab_page_description() { - Some(value) => value, - None => GString::new(), // @TODO - }; - - header.update(title.as_str(), subtitle.as_str()); } }); diff --git a/src/app/browser/widget.rs b/src/app/browser/widget.rs index d79e6208..98e63e45 100644 --- a/src/app/browser/widget.rs +++ b/src/app/browser/widget.rs @@ -2,8 +2,8 @@ mod database; use database::Database; -use adw::HeaderBar; -use gtk::{prelude::GtkWindowExt, ApplicationWindow, Box}; +use adw::ApplicationWindow; +use gtk::{prelude::GtkWindowExt, Box}; use sqlite::Transaction; // Default options @@ -17,11 +17,10 @@ pub struct Widget { impl Widget { // Construct - pub fn new(titlebar: &HeaderBar, child: &Box) -> Self { + pub fn new(content: &Box) -> Self { // Init GTK let gobject = ApplicationWindow::builder() - .titlebar(titlebar) - .child(child) + .content(content) .default_height(DEFAULT_HEIGHT) .default_width(DEFAULT_WIDTH) .maximized(MAXIMIZED) diff --git a/src/app/browser/window.rs b/src/app/browser/window.rs index b72b882c..a0a8e742 100644 --- a/src/app/browser/window.rs +++ b/src/app/browser/window.rs @@ -1,8 +1,10 @@ mod database; +mod header; mod tab; mod widget; use database::Database; +use header::Header; use sqlite::Transaction; use tab::Tab; use widget::Widget; @@ -12,6 +14,7 @@ use std::sync::Arc; use gtk::{gio::SimpleAction, glib::GString, Box}; pub struct Window { + header: Arc
, tab: Arc, widget: Arc, } @@ -20,13 +23,34 @@ impl Window { // Construct pub fn new( // Actions + action_tool_debug: Arc, + action_tool_profile_directory: Arc, + action_quit: Arc, + action_update: Arc, + action_tab_append: Arc, + action_tab_close: Arc, + action_tab_close_all: Arc, action_tab_page_navigation_base: Arc, action_tab_page_navigation_history_back: Arc, action_tab_page_navigation_history_forward: Arc, action_tab_page_navigation_reload: Arc, - action_update: Arc, + action_tab_pin: Arc, ) -> Self { // Init components + let header = Arc::new(Header::new( + action_tool_debug.clone(), + action_tool_profile_directory.clone(), + action_quit.clone(), + action_tab_append.clone(), + action_tab_close.clone(), + action_tab_close_all.clone(), + action_tab_page_navigation_base.clone(), + action_tab_page_navigation_history_back.clone(), + action_tab_page_navigation_history_forward.clone(), + action_tab_page_navigation_reload.clone(), + action_tab_pin.clone(), + )); + let tab = Arc::new(Tab::new( action_tab_page_navigation_base, action_tab_page_navigation_history_back, @@ -37,10 +61,14 @@ impl Window { tab.activate(tab.clone()); // GTK - let widget = Arc::new(Widget::new(tab.gobject())); + let widget = Arc::new(Widget::new(header.gobject(), tab.gobject())); // Init struct - Self { tab, widget } + Self { + header, + tab, + widget, + } } // Actions @@ -77,6 +105,19 @@ impl Window { } pub fn update(&self) { + // Update header + let title = match self.tab.page_title() { + Some(value) => value, + None => GString::new(), // @TODO + }; + + let subtitle = match self.tab.page_description() { + Some(value) => value, + None => GString::new(), // @TODO + }; + + self.header.update(title.as_str(), subtitle.as_str()); + self.tab.update(); } @@ -131,14 +172,6 @@ impl Window { } // Getters - pub fn tab_page_title(&self) -> Option { - self.tab.page_title() - } - - pub fn tab_page_description(&self) -> Option { - self.tab.page_description() - } - pub fn gobject(&self) -> &Box { &self.widget.gobject() } diff --git a/src/app/browser/header.rs b/src/app/browser/window/header.rs similarity index 100% rename from src/app/browser/header.rs rename to src/app/browser/window/header.rs diff --git a/src/app/browser/header/title.rs b/src/app/browser/window/header/title.rs similarity index 100% rename from src/app/browser/header/title.rs rename to src/app/browser/window/header/title.rs diff --git a/src/app/browser/header/tray.rs b/src/app/browser/window/header/tray.rs similarity index 100% rename from src/app/browser/header/tray.rs rename to src/app/browser/window/header/tray.rs diff --git a/src/app/browser/header/tray/menu.rs b/src/app/browser/window/header/tray/menu.rs similarity index 100% rename from src/app/browser/header/tray/menu.rs rename to src/app/browser/window/header/tray/menu.rs diff --git a/src/app/browser/header/tray/tab.rs b/src/app/browser/window/header/tray/tab.rs similarity index 100% rename from src/app/browser/header/tray/tab.rs rename to src/app/browser/window/header/tray/tab.rs diff --git a/src/app/browser/header/widget.rs b/src/app/browser/window/header/widget.rs similarity index 100% rename from src/app/browser/header/widget.rs rename to src/app/browser/window/header/widget.rs diff --git a/src/app/browser/window/widget.rs b/src/app/browser/window/widget.rs index 6cc82eb3..f6e0bbdf 100644 --- a/src/app/browser/window/widget.rs +++ b/src/app/browser/window/widget.rs @@ -1,3 +1,4 @@ +use adw::HeaderBar; use gtk::{prelude::BoxExt, Box, Notebook, Orientation}; pub struct Widget { @@ -6,8 +7,9 @@ pub struct Widget { impl Widget { // Construct - pub fn new(tab: &Notebook) -> Self { + pub fn new(header: &HeaderBar, tab: &Notebook) -> Self { let gobject = Box::builder().orientation(Orientation::Vertical).build(); + gobject.append(header); gobject.append(tab); Self { gobject }