use libadwaita for app & app window

This commit is contained in:
yggverse 2024-10-09 10:50:41 +03:00
parent e2ab831d57
commit 9e5a2a490c
11 changed files with 57 additions and 47 deletions

View file

@ -0,0 +1,67 @@
mod title;
mod tray;
mod widget;
use title::Title;
use tray::Tray;
use widget::Widget;
use adw::HeaderBar;
use gtk::gio::SimpleAction;
use std::sync::Arc;
pub struct Header {
title: Arc<Title>,
// tray: Arc<Subject>,
widget: Arc<Widget>,
}
impl Header {
// Construct
pub fn new(
action_tool_debug: Arc<SimpleAction>,
action_tool_profile_directory: Arc<SimpleAction>,
action_quit: Arc<SimpleAction>,
action_tab_append: Arc<SimpleAction>,
action_tab_close: Arc<SimpleAction>,
action_tab_close_all: Arc<SimpleAction>,
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_history_back: Arc<SimpleAction>,
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
action_tab_page_navigation_reload: Arc<SimpleAction>,
action_tab_pin: Arc<SimpleAction>,
) -> Self {
// Init components
let tray = Tray::new(
action_tool_debug,
action_tool_profile_directory,
action_quit,
action_tab_append,
action_tab_close,
action_tab_close_all,
action_tab_page_navigation_base,
action_tab_page_navigation_history_back,
action_tab_page_navigation_history_forward,
action_tab_page_navigation_reload,
action_tab_pin,
);
let title = Arc::new(Title::new());
// Init widget
let widget = Arc::new(Widget::new(tray.gobject(), Some(title.gobject())));
// Return new struct
Self { title, widget }
}
// Actions
pub fn update(&self, title: &str, description: &str) {
self.title.update(title, description);
}
// Getters
pub fn gobject(&self) -> &HeaderBar {
&self.widget.gobject()
}
}

View file

@ -0,0 +1,39 @@
use adw::WindowTitle;
const DEFAULT_TITLE: &str = "Yoda"; // @TODO
const DEFAULT_SUBTITLE: &str = "";
pub struct Title {
gobject: WindowTitle,
}
impl Title {
// Construct
pub fn new() -> Self {
Self {
gobject: WindowTitle::new(DEFAULT_TITLE, DEFAULT_SUBTITLE),
}
}
// Actions
pub fn update(&self, title: &str, subtitle: &str) {
// Update title
let mut parts = Vec::new();
if !title.is_empty() {
parts.push(title);
}
parts.push(DEFAULT_TITLE);
self.gobject.set_title(&parts.join(" - "));
// Update subtitle
self.gobject.set_subtitle(subtitle);
}
// Getters
pub fn gobject(&self) -> &WindowTitle {
&self.gobject
}
}

View file

@ -0,0 +1,67 @@
mod menu;
mod tab;
use menu::Menu;
use tab::Tab;
use gtk::{
gio::SimpleAction,
prelude::BoxExt,
{Box, Orientation},
};
use std::sync::Arc;
pub struct Tray {
gobject: Box,
}
impl Tray {
pub fn new(
action_tool_debug: Arc<SimpleAction>,
action_tool_profile_directory: Arc<SimpleAction>,
action_quit: Arc<SimpleAction>,
action_tab_append: Arc<SimpleAction>,
action_tab_close: Arc<SimpleAction>,
action_tab_close_all: Arc<SimpleAction>,
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_history_back: Arc<SimpleAction>,
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
action_tab_page_navigation_reload: Arc<SimpleAction>,
action_tab_pin: Arc<SimpleAction>,
) -> Self {
// Init components
let tab = Tab::new(action_tab_append.clone());
let menu = Menu::new(
action_tool_debug,
action_tool_profile_directory,
action_quit,
action_tab_append,
action_tab_close,
action_tab_close_all,
action_tab_page_navigation_base,
action_tab_page_navigation_history_back,
action_tab_page_navigation_history_forward,
action_tab_page_navigation_reload,
action_tab_pin,
);
// Init widget
let gobject = Box::builder()
.orientation(Orientation::Horizontal)
.spacing(8)
.build();
gobject.append(menu.gobject());
gobject.append(tab.gobject());
// Return new struct
Self { gobject }
}
// Getters
pub fn gobject(&self) -> &Box {
&self.gobject
}
}

View file

@ -0,0 +1,87 @@
use gtk::{
gio::{self, SimpleAction},
glib::{gformat, GString},
prelude::ActionExt,
MenuButton,
};
use std::sync::Arc;
pub struct Menu {
gobject: MenuButton,
}
#[rustfmt::skip] // @TODO template builder?
impl Menu {
pub fn new(
action_tool_debug: Arc<SimpleAction>,
action_tool_profile_directory: Arc<SimpleAction>,
action_quit: Arc<SimpleAction>,
action_tab_append: Arc<SimpleAction>,
action_tab_close: Arc<SimpleAction>,
action_tab_close_all: Arc<SimpleAction>,
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_history_back: Arc<SimpleAction>,
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
action_tab_page_navigation_reload: Arc<SimpleAction>,
action_tab_pin: Arc<SimpleAction>,
) -> Self {
// Init model
let model = gio::Menu::new();
let model_tab = gio::Menu::new();
model_tab.append(Some("New"), Some(&detailed_action_name(action_tab_append)));
model_tab.append(Some("Pin"), Some(&detailed_action_name(action_tab_pin)));
let model_tab_page = gio::Menu::new();
let model_tab_page_navigation = gio::Menu::new();
model_tab_page_navigation.append(Some("Base"), Some(&detailed_action_name(action_tab_page_navigation_base)));
let model_tab_page_navigation_history = gio::Menu::new();
model_tab_page_navigation_history.append(Some("Back"), Some(&detailed_action_name(action_tab_page_navigation_history_back)));
model_tab_page_navigation_history.append(Some("Forward"), Some(&detailed_action_name(action_tab_page_navigation_history_forward)));
model_tab_page_navigation.append_submenu(Some("History"), &model_tab_page_navigation_history);
model_tab_page_navigation.append(Some("Reload"), Some(&detailed_action_name(action_tab_page_navigation_reload)));
// @TODO model_tab_page_navigation.append(Some("Bookmark"), Some("win.tab_page_bookmark"));
model_tab_page.append_submenu(Some("Navigation"), &model_tab_page_navigation);
model_tab.append_submenu(Some("Page"), &model_tab_page);
let model_tab_close = gio::Menu::new();
model_tab_close.append(Some("Current"), Some(&detailed_action_name(action_tab_close)));
model_tab_close.append(Some("All"), Some(&detailed_action_name(action_tab_close_all)));
model_tab.append_submenu(Some("Close"), &model_tab_close);
model.append_submenu(Some("Tab"), &model_tab);
let model_tool = gio::Menu::new();
model_tool.append(Some("Debug"), Some(&detailed_action_name(action_tool_debug)));
model_tool.append(Some("Profile directory"), Some(&detailed_action_name(action_tool_profile_directory)));
model.append_submenu(Some("Tool"), &model_tool);
model.append(Some("Quit"), Some(&detailed_action_name(action_quit)));
// Init widget
let gobject = MenuButton::builder().tooltip_text("Menu").build();
gobject.set_menu_model(Some(&model));
// Result
Self { gobject }
}
// Getters
pub fn gobject(&self) -> &MenuButton {
&self.gobject
}
}
// Private helpers
fn detailed_action_name(action: Arc<SimpleAction>) -> GString {
gformat!("win.{}", action.name()) // @TODO find the way to ident parent group
// without application-wide dependencies import
// see also src/app/action.rs
}

View file

@ -0,0 +1,30 @@
use gtk::{gio::SimpleAction, prelude::ActionExt, prelude::ButtonExt, Button};
use std::sync::Arc;
pub struct Tab {
pub gobject: Button,
}
impl Tab {
// Construct
pub fn new(action_tab_append: Arc<SimpleAction>) -> Self {
// Init widget
let gobject = Button::builder()
.icon_name("tab-new-symbolic")
.tooltip_text("New tab")
.build();
// Init events
gobject.connect_clicked(move |_| {
action_tab_append.activate(None);
});
// Return activated struct
Self { gobject }
}
// Getters
pub fn gobject(&self) -> &Button {
&self.gobject
}
}

View file

@ -0,0 +1,23 @@
use adw::{HeaderBar, WindowTitle};
use gtk::Box;
pub struct Widget {
gobject: HeaderBar,
}
impl Widget {
// Construct
pub fn new(pack_start: &Box, title_widget: Option<&WindowTitle>) -> Self {
let gobject = HeaderBar::builder().build();
gobject.pack_start(pack_start);
gobject.set_title_widget(title_widget);
Self { gobject }
}
// Getters
pub fn gobject(&self) -> &HeaderBar {
&self.gobject
}
}

View file

@ -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 }