[gnome-tour/bilelmoussaoui/fixes: 9/11] subclass Window




commit ba2f81ad49fd07cfcfa6250663898f352cab6d00
Author: Bilal Elmoussaoui <bil elmoussaoui gmail com>
Date:   Fri Dec 31 19:33:32 2021 +0100

    subclass Window

 src/application.rs       |  20 +++----
 src/widgets/paginator.rs |   6 ++
 src/widgets/window.rs    | 152 +++++++++++++++++++++++++++--------------------
 3 files changed, 104 insertions(+), 74 deletions(-)
---
diff --git a/src/application.rs b/src/application.rs
index b808f70..1f1bd04 100644
--- a/src/application.rs
+++ b/src/application.rs
@@ -16,7 +16,7 @@ mod imp {
 
     #[derive(Debug, Default)]
     pub struct Application {
-        pub(super) window: OnceCell<Window>,
+        pub(super) window: OnceCell<WeakRef<Window>>,
     }
 
     #[glib::object_subclass]
@@ -30,11 +30,12 @@ mod imp {
     impl ApplicationImpl for Application {
         fn activate(&self, application: &Self::Type) {
             let window = Window::new(&application);
-            application.add_window(&window.widget);
-            window.widget.present();
-            self.window.set(window);
+            application.add_window(&window);
+            window.present();
+            self.window.set(window.downgrade()).unwrap();
             self.parent_activate(application);
         }
+
         fn startup(&self, application: &Self::Type) {
             // Quit
             utils::action(
@@ -68,8 +69,8 @@ mod imp {
                 "next-page",
                 clone!(@weak application => move |_, _| {
                     let window = application.window();
-                    if window.paginator.try_next().is_none() {
-                        window.widget.close();
+                    if window.paginator().try_next().is_none() {
+                        window.close();
                     }
                 }),
             );
@@ -79,7 +80,7 @@ mod imp {
                 "previous-page",
                 clone!(@weak application => move |_, _| {
                     let window = application.window();
-                    if window.paginator.try_previous().is_none() {
+                    if window.paginator().try_previous().is_none() {
                         window.reset_tour();
                     }
                 }),
@@ -107,9 +108,8 @@ impl Application {
         .unwrap()
     }
 
-    fn window(&self) -> &Window {
-        // and_then(|w| w.upgrade())
-        self.imp().window.get().unwrap()
+    fn window(&self) -> Window {
+        self.imp().window.get().and_then(|w| w.upgrade()).unwrap()
     }
 
     pub fn run() {
diff --git a/src/widgets/paginator.rs b/src/widgets/paginator.rs
index 0e0865e..55c5d20 100644
--- a/src/widgets/paginator.rs
+++ b/src/widgets/paginator.rs
@@ -212,3 +212,9 @@ impl PaginatorWidget {
         }
     }
 }
+
+impl Default for PaginatorWidget {
+    fn default() -> Self {
+        Self::new()
+    }
+}
diff --git a/src/widgets/window.rs b/src/widgets/window.rs
index b6c2540..81019d2 100644
--- a/src/widgets/window.rs
+++ b/src/widgets/window.rs
@@ -1,84 +1,108 @@
 use adw::prelude::*;
 use gettextrs::gettext;
+use gtk::subclass::prelude::*;
+use gtk::{gio, glib};
 
 use super::pages::{ImagePageWidget, WelcomePageWidget};
 use super::paginator::PaginatorWidget;
-use crate::config::{APP_ID, PROFILE};
 use crate::Application;
 
-#[derive(Debug)]
-pub struct Window {
-    pub widget: adw::ApplicationWindow,
-    pub paginator: PaginatorWidget,
+mod imp {
+    use super::*;
+    use crate::config;
+    use adw::subclass::prelude::*;
+
+    #[derive(Debug, Default)]
+    pub struct Window {
+        pub(super) paginator: PaginatorWidget,
+    }
+
+    #[glib::object_subclass]
+    impl ObjectSubclass for Window {
+        const NAME: &'static str = "Window";
+        type Type = super::Window;
+        type ParentType = adw::ApplicationWindow;
+    }
+
+    impl ObjectImpl for Window {
+        fn constructed(&self, widget: &Self::Type) {
+            widget.set_default_size(960, 720);
+            widget.set_icon_name(Some(config::APP_ID));
+
+            // Devel Profile
+            if config::PROFILE == "Devel" {
+                widget.add_css_class("devel");
+            }
+
+            self.paginator.add_page(WelcomePageWidget::new().widget);
+            self.paginator.add_page(ImagePageWidget::new(
+                "/org/gnome/Tour/overview.svg",
+                gettext("Get an Overview"),
+                gettext("Press the Super key to see open windows and apps."),
+            ));
+
+            self.paginator.add_page(ImagePageWidget::new(
+                "/org/gnome/Tour/search.svg",
+                gettext("Just Type to Search"),
+                gettext("Type in the overview to search. Launch apps, find things."),
+            ));
+
+            self.paginator.add_page(ImagePageWidget::new(
+                "/org/gnome/Tour/workspaces.svg",
+                gettext("Keep on Top with Workspaces"),
+                gettext("Easily organize windows with the workspaces view."),
+            ));
+
+            self.paginator.add_page(ImagePageWidget::new(
+                "/org/gnome/Tour/blank.svg",
+                gettext("Up/Down for the Overview"),
+                gettext("On a touchpad, use three-finger vertical swipes. Try it!"),
+            ));
+
+            self.paginator.add_page(ImagePageWidget::new(
+                "/org/gnome/Tour/blank.svg",
+                gettext("Left/Right for Workspaces"),
+                gettext("On a touchpad, use three-finger horizontal swipes. Try it!"),
+            ));
+
+            let last_page = ImagePageWidget::new(
+                "/org/gnome/Tour/ready-to-go.svg",
+                gettext("That's it. Have a nice day!"),
+                gettext("To get more advice and tips, see the Help app."),
+            );
+            last_page.add_css_class("last-page");
+            self.paginator.add_page(last_page);
+
+            widget.set_content(Some(&self.paginator));
+            self.parent_constructed(widget);
+        }
+    }
+    impl WidgetImpl for Window {}
+    impl WindowImpl for Window {}
+    impl ApplicationWindowImpl for Window {}
+    impl AdwApplicationWindowImpl for Window {}
+}
+
+glib::wrapper! {
+    pub struct Window(ObjectSubclass<imp::Window>)
+        @extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, adw::ApplicationWindow,
+        @implements gio::ActionMap, gio::ActionGroup;
 }
 
 impl Window {
     pub fn new(app: &Application) -> Self {
-        let widget = adw::ApplicationWindow::new(app);
-
-        let paginator = PaginatorWidget::new();
-
-        let mut window_widget = Window { widget, paginator };
+        glib::Object::new(&[("application", app)]).unwrap()
+    }
 
-        window_widget.init();
-        window_widget
+    pub fn paginator(&self) -> PaginatorWidget {
+        self.imp().paginator.clone()
     }
 
     pub fn start_tour(&self) {
-        self.paginator.set_page(1);
+        self.imp().paginator.set_page(1);
     }
 
     pub fn reset_tour(&self) {
-        self.paginator.set_page(0);
-    }
-
-    fn init(&mut self) {
-        self.widget.set_default_size(960, 720);
-        self.widget.set_icon_name(Some(APP_ID));
-
-        // Devel Profile
-        if PROFILE == "Devel" {
-            self.widget.add_css_class("devel");
-        }
-        self.paginator.add_page(WelcomePageWidget::new().widget);
-        self.paginator.add_page(ImagePageWidget::new(
-            "/org/gnome/Tour/overview.svg",
-            gettext("Get an Overview"),
-            gettext("Press the Super key to see open windows and apps."),
-        ));
-
-        self.paginator.add_page(ImagePageWidget::new(
-            "/org/gnome/Tour/search.svg",
-            gettext("Just Type to Search"),
-            gettext("Type in the overview to search. Launch apps, find things."),
-        ));
-
-        self.paginator.add_page(ImagePageWidget::new(
-            "/org/gnome/Tour/workspaces.svg",
-            gettext("Keep on Top with Workspaces"),
-            gettext("Easily organize windows with the workspaces view."),
-        ));
-
-        self.paginator.add_page(ImagePageWidget::new(
-            "/org/gnome/Tour/blank.svg",
-            gettext("Up/Down for the Overview"),
-            gettext("On a touchpad, use three-finger vertical swipes. Try it!"),
-        ));
-
-        self.paginator.add_page(ImagePageWidget::new(
-            "/org/gnome/Tour/blank.svg",
-            gettext("Left/Right for Workspaces"),
-            gettext("On a touchpad, use three-finger horizontal swipes. Try it!"),
-        ));
-
-        let last_page = ImagePageWidget::new(
-            "/org/gnome/Tour/ready-to-go.svg",
-            gettext("That's it. Have a nice day!"),
-            gettext("To get more advice and tips, see the Help app."),
-        );
-        last_page.add_css_class("last-page");
-        self.paginator.add_page(last_page);
-
-        self.widget.set_content(Some(&self.paginator));
+        self.imp().paginator.set_page(0);
     }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]