Commit ba2f81ad authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui

subclass Window

parent 11723557
...@@ -16,7 +16,7 @@ mod imp { ...@@ -16,7 +16,7 @@ mod imp {
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Application { pub struct Application {
pub(super) window: OnceCell<Window>, pub(super) window: OnceCell<WeakRef<Window>>,
} }
#[glib::object_subclass] #[glib::object_subclass]
...@@ -30,11 +30,12 @@ mod imp { ...@@ -30,11 +30,12 @@ mod imp {
impl ApplicationImpl for Application { impl ApplicationImpl for Application {
fn activate(&self, application: &Self::Type) { fn activate(&self, application: &Self::Type) {
let window = Window::new(&application); let window = Window::new(&application);
application.add_window(&window.widget); application.add_window(&window);
window.widget.present(); window.present();
self.window.set(window); self.window.set(window.downgrade()).unwrap();
self.parent_activate(application); self.parent_activate(application);
} }
fn startup(&self, application: &Self::Type) { fn startup(&self, application: &Self::Type) {
// Quit // Quit
utils::action( utils::action(
...@@ -68,8 +69,8 @@ mod imp { ...@@ -68,8 +69,8 @@ mod imp {
"next-page", "next-page",
clone!(@weak application => move |_, _| { clone!(@weak application => move |_, _| {
let window = application.window(); let window = application.window();
if window.paginator.try_next().is_none() { if window.paginator().try_next().is_none() {
window.widget.close(); window.close();
} }
}), }),
); );
...@@ -79,7 +80,7 @@ mod imp { ...@@ -79,7 +80,7 @@ mod imp {
"previous-page", "previous-page",
clone!(@weak application => move |_, _| { clone!(@weak application => move |_, _| {
let window = application.window(); let window = application.window();
if window.paginator.try_previous().is_none() { if window.paginator().try_previous().is_none() {
window.reset_tour(); window.reset_tour();
} }
}), }),
...@@ -107,9 +108,8 @@ impl Application { ...@@ -107,9 +108,8 @@ impl Application {
.unwrap() .unwrap()
} }
fn window(&self) -> &Window { fn window(&self) -> Window {
// and_then(|w| w.upgrade()) self.imp().window.get().and_then(|w| w.upgrade()).unwrap()
self.imp().window.get().unwrap()
} }
pub fn run() { pub fn run() {
......
...@@ -212,3 +212,9 @@ impl PaginatorWidget { ...@@ -212,3 +212,9 @@ impl PaginatorWidget {
} }
} }
} }
impl Default for PaginatorWidget {
fn default() -> Self {
Self::new()
}
}
use adw::prelude::*; use adw::prelude::*;
use gettextrs::gettext; use gettextrs::gettext;
use gtk::subclass::prelude::*;
use gtk::{gio, glib};
use super::pages::{ImagePageWidget, WelcomePageWidget}; use super::pages::{ImagePageWidget, WelcomePageWidget};
use super::paginator::PaginatorWidget; use super::paginator::PaginatorWidget;
use crate::config::{APP_ID, PROFILE};
use crate::Application; use crate::Application;
#[derive(Debug)] mod imp {
pub struct Window { use super::*;
pub widget: adw::ApplicationWindow, use crate::config;
pub paginator: PaginatorWidget, use adw::subclass::prelude::*;
}
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 };
window_widget.init();
window_widget
}
pub fn start_tour(&self) { #[derive(Debug, Default)]
self.paginator.set_page(1); pub struct Window {
pub(super) paginator: PaginatorWidget,
} }
pub fn reset_tour(&self) { #[glib::object_subclass]
self.paginator.set_page(0); impl ObjectSubclass for Window {
const NAME: &'static str = "Window";
type Type = super::Window;
type ParentType = adw::ApplicationWindow;
} }
fn init(&mut self) { impl ObjectImpl for Window {
self.widget.set_default_size(960, 720); fn constructed(&self, widget: &Self::Type) {
self.widget.set_icon_name(Some(APP_ID)); widget.set_default_size(960, 720);
widget.set_icon_name(Some(config::APP_ID));
// Devel Profile // Devel Profile
if PROFILE == "Devel" { if config::PROFILE == "Devel" {
self.widget.add_css_class("devel"); widget.add_css_class("devel");
} }
self.paginator.add_page(WelcomePageWidget::new().widget); self.paginator.add_page(WelcomePageWidget::new().widget);
self.paginator.add_page(ImagePageWidget::new( self.paginator.add_page(ImagePageWidget::new(
"/org/gnome/Tour/overview.svg", "/org/gnome/Tour/overview.svg",
...@@ -79,6 +73,36 @@ impl Window { ...@@ -79,6 +73,36 @@ impl Window {
last_page.add_css_class("last-page"); last_page.add_css_class("last-page");
self.paginator.add_page(last_page); self.paginator.add_page(last_page);
self.widget.set_content(Some(&self.paginator)); 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 {
glib::Object::new(&[("application", app)]).unwrap()
}
pub fn paginator(&self) -> PaginatorWidget {
self.imp().paginator.clone()
}
pub fn start_tour(&self) {
self.imp().paginator.set_page(1);
}
pub fn reset_tour(&self) {
self.imp().paginator.set_page(0);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment