From cc828c417af2d93fae9335bf81b81b45c4a3fcf1 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 24 Aug 2023 21:56:03 -0400 Subject: [PATCH] Change the layout/app_window to an ordinary object with necessary objects --- kifu/gtk/src/main.rs | 23 +++++-------- kifu/gtk/src/ui/layout.rs | 71 --------------------------------------- kifu/gtk/src/ui/mod.rs | 58 ++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 86 deletions(-) delete mode 100644 kifu/gtk/src/ui/layout.rs diff --git a/kifu/gtk/src/main.rs b/kifu/gtk/src/main.rs index 3217134..614b1dc 100644 --- a/kifu/gtk/src/main.rs +++ b/kifu/gtk/src/main.rs @@ -2,12 +2,12 @@ use adw::prelude::*; use kifu_core::{CoreApp, CoreRequest, CoreResponse}; use kifu_gtk::{ perftrace, - ui::{ConfigurationPage, Home, Layout, PlayingField}, + ui::{AppWindow, ConfigurationPage, Home, PlayingField}, CoreApi, }; use std::sync::{Arc, RwLock}; -fn handle_response(api: CoreApi, layout: Layout, message: CoreResponse) { +fn handle_response(api: CoreApi, app_window: &AppWindow, message: CoreResponse) { let playing_field = Arc::new(RwLock::new(None)); match message { CoreResponse::ConfigurationView(view) => perftrace("ConfigurationView", || { @@ -22,7 +22,7 @@ fn handle_response(api: CoreApi, layout: Layout, message: CoreResponse) { let api = api.clone(); let home = Home::new(api, view); - layout.set_content(&home); + app_window.set_content(&home); }), CoreResponse::PlayingFieldView(view) => perftrace("PlayingFieldView", || { let api = api.clone(); @@ -31,7 +31,7 @@ fn handle_response(api: CoreApi, layout: Layout, message: CoreResponse) { if playing_field.is_none() { perftrace("creating a new playing field", || { let field = PlayingField::new(api, view); - layout.set_content(&field); + app_window.set_content(&field); *playing_field = Some(field); }) } else { @@ -87,6 +87,8 @@ fn main() { let (gtk_tx, gtk_rx) = gtk::glib::MainContext::channel::(gtk::glib::PRIORITY_DEFAULT); + let app_window = AppWindow::new(&app); + let api = CoreApi { gtk_tx, rt: runtime.clone(), @@ -102,22 +104,13 @@ fn main() { }); app.add_action(&action_config); - let window = adw::ApplicationWindow::builder() - .application(app) - .width_request(800) - .height_request(500) - .build(); - - let layout = Layout::new(); - window.set_content(Some(&layout)); - - window.present(); + app_window.window.present(); gtk_rx.attach(None, { let api = api.clone(); move |message| { perftrace("handle_response", || { - handle_response(api.clone(), layout.clone(), message) + handle_response(api.clone(), &app_window, message) }); Continue(true) } diff --git a/kifu/gtk/src/ui/layout.rs b/kifu/gtk/src/ui/layout.rs deleted file mode 100644 index 5057bfc..0000000 --- a/kifu/gtk/src/ui/layout.rs +++ /dev/null @@ -1,71 +0,0 @@ -use glib::Object; -use gtk::{prelude::*, subclass::prelude::*}; -use std::{cell::RefCell, rc::Rc}; - -// Deprecated even from day 1. We want to use ToolbarView as soon as it's available in the versions -// of Libadwaita available in NixOS. -pub struct LayoutPrivate { - pub header: adw::HeaderBar, - pub content: Rc>, -} - -impl Default for LayoutPrivate { - fn default() -> Self { - let header = adw::HeaderBar::builder() - .title_widget(>k::Label::new(Some("Kifu"))) - .build(); - - let app_menu = gio::Menu::new(); - let menu_item = gio::MenuItem::new(Some("Configuration"), Some("app.show-config")); - app_menu.append_item(&menu_item); - - let hamburger = gtk::MenuButton::builder() - .icon_name("open-menu-symbolic") - .build(); - hamburger.set_menu_model(Some(&app_menu)); - - header.pack_end(&hamburger); - - let content = adw::StatusPage::builder().title("Nothing here").build(); - - Self { - header, - content: Rc::new(RefCell::new(content.into())), - } - } -} - -#[glib::object_subclass] -impl ObjectSubclass for LayoutPrivate { - const NAME: &'static str = "Layout"; - type Type = Layout; - type ParentType = gtk::Box; -} - -impl ObjectImpl for LayoutPrivate {} -impl WidgetImpl for LayoutPrivate {} -impl BoxImpl for LayoutPrivate {} - -glib::wrapper! { - pub struct Layout(ObjectSubclass) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable; -} - -impl Layout { - pub fn new() -> Self { - let s: Self = Object::builder().build(); - s.set_orientation(gtk::Orientation::Vertical); - s.set_homogeneous(false); - s.append(&s.imp().header); - s.append(&*s.imp().content.borrow()); - - s - } - - pub fn set_content(&self, content: &impl IsA) { - let mut widget = self.imp().content.borrow_mut(); - - self.remove(&*widget); - *widget = content.clone().upcast::(); - self.append(&*widget); - } -} diff --git a/kifu/gtk/src/ui/mod.rs b/kifu/gtk/src/ui/mod.rs index de1c5fe..30cd24d 100644 --- a/kifu/gtk/src/ui/mod.rs +++ b/kifu/gtk/src/ui/mod.rs @@ -1,3 +1,7 @@ +use adw::prelude::*; +use glib::IsA; +use gtk::prelude::*; + mod chat; pub use chat::Chat; @@ -27,3 +31,57 @@ pub use board::Board; #[cfg(feature = "screenplay")] pub use playing_field::playing_field_view; + +pub struct AppWindow { + pub window: adw::ApplicationWindow, + pub header: adw::HeaderBar, + pub content: adw::Bin, +} + +impl AppWindow { + pub fn new(app: &adw::Application) -> Self { + let header = adw::HeaderBar::builder() + .title_widget(>k::Label::new(Some("Kifu"))) + .build(); + + let app_menu = gio::Menu::new(); + let menu_item = gio::MenuItem::new(Some("Configuration"), Some("app.show-config")); + app_menu.append_item(&menu_item); + + let hamburger = gtk::MenuButton::builder() + .icon_name("open-menu-symbolic") + .build(); + hamburger.set_menu_model(Some(&app_menu)); + + header.pack_end(&hamburger); + + let content = adw::Bin::new(); + content.set_child(Some( + &adw::StatusPage::builder().title("Nothing here").build(), + )); + + let layout = gtk::Box::builder() + .orientation(gtk::Orientation::Vertical) + .build(); + layout.append(&header); + layout.append(&content); + + let window = adw::ApplicationWindow::builder() + .application(app) + .width_request(800) + .height_request(500) + .build(); + + window.set_content(Some(&layout)); + + Self { + window, + header, + content, + } + } + + pub fn set_content(&self, content: &impl IsA) { + self.content.set_child(Some(content)); + } +}