Commit 8f181ddc authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui

subclass adw::Application

parent b44f77e5
use crate::config; use crate::config;
use crate::utils; use crate::utils;
use crate::widgets::Window; use crate::widgets::Window;
use gtk::gio::{self, prelude::*}; use adw::prelude::*;
use gtk::glib::{self, clone}; use gtk::{
use gtk::prelude::*; gio,
glib::{self, clone},
subclass::prelude::*,
};
use log::info; use log::info;
use std::{cell::RefCell, rc::Rc};
pub struct Application { mod imp {
app: adw::Application, use super::*;
window: RefCell<Rc<Option<Window>>>, use adw::subclass::prelude::*;
} use gtk::glib::{once_cell::sync::OnceCell, WeakRef};
impl Application {
pub fn new() -> Rc<Self> {
let app = adw::Application::new(Some(config::APP_ID), gio::ApplicationFlags::FLAGS_NONE);
app.set_resource_base_path(Some("/org/gnome/Tour"));
let application = Rc::new(Self { #[derive(Debug, Default)]
app, pub struct Application {
window: RefCell::new(Rc::new(None)), pub(super) window: OnceCell<Window>,
}); }
application.setup_signals(application.clone()); #[glib::object_subclass]
application impl ObjectSubclass for Application {
const NAME: &'static str = "Application";
type ParentType = adw::Application;
type Type = super::Application;
} }
fn setup_gactions(&self, application: Rc<Self>) { impl ObjectImpl for Application {}
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);
self.parent_activate(application);
}
fn startup(&self, application: &Self::Type) {
// Quit // Quit
utils::action( utils::action(
&self.app, application,
"quit", "quit",
clone!(@strong self.app as app => move |_, _| { clone!(@weak application => move |_, _| {
app.quit(); application.quit();
}), }),
); );
// Start Tour // Start Tour
utils::action( utils::action(
&self.app, application,
"start-tour", "start-tour",
clone!(@strong application => move |_, _| { clone!(@weak application => move |_, _| {
if let Some(window) = &*application.window.borrow().clone() { application.window().start_tour();
window.start_tour();
}
}), }),
); );
// Skip Tour // Skip Tour
utils::action( utils::action(
&self.app, application,
"skip-tour", "skip-tour",
clone!(@strong self.app as app => move |_, _| { clone!(@weak application => move |_, _| {
app.quit(); application.quit();
}), }),
); );
utils::action( utils::action(
&self.app, application,
"next-page", "next-page",
clone!(@strong application => move |_, _| { clone!(@weak application => move |_, _| {
if let Some(window) = &*application.window.borrow().clone() { let window = application.window();
if window.paginator.borrow_mut().try_next().is_none() { if window.paginator.borrow_mut().try_next().is_none() {
window.widget.close(); window.widget.close();
} }
}
}), }),
); );
utils::action( utils::action(
&self.app, application,
"previous-page", "previous-page",
clone!(@strong application => move |_, _| { clone!(@weak application => move |_, _| {
if let Some(window) = &*application.window.borrow().clone() { let window = application.window();
if window.paginator.borrow_mut().try_previous().is_none() { if window.paginator.borrow_mut().try_previous().is_none() {
window.reset_tour(); window.reset_tour();
} }
}
}), }),
); );
application.set_accels_for_action("app.quit", &["<Control>q"]);
self.parent_startup(application);
}
}
impl GtkApplicationImpl for Application {}
impl AdwApplicationImpl for Application {}
}
glib::wrapper! {
pub struct Application(ObjectSubclass<imp::Application>)
@extends gio::Application, gtk::Application, adw::Application,
@implements gio::ActionMap, gio::ActionGroup;
}
self.app.set_accels_for_action("app.quit", &["<primary>q"]); impl Application {
pub fn new() -> Self {
glib::Object::new(&[
("application-id", &config::APP_ID),
("resource-base-path", &Some("/org/gnome/Tour")),
])
.unwrap()
} }
fn setup_signals(&self, app: Rc<Self>) { fn window(&self) -> &Window {
self.app.connect_startup(clone!(@weak app => move |_| { // and_then(|w| w.upgrade())
app.setup_gactions(app.clone()); self.imp().window.get().unwrap()
}));
self.app
.connect_activate(clone!(@weak app => move |gtk_app| {
let window = Window::new(&gtk_app);
gtk_app.add_window(&window.widget);
window.widget.present();
window.widget.show();
app.window.replace(Rc::new(Some(window)));
}));
} }
pub fn run(&self) { pub fn run() {
info!("GNOME Tour ({})", config::APP_ID); info!("GNOME Tour ({})", config::APP_ID);
info!("Version: {} ({})", config::VERSION, config::PROFILE); info!("Version: {} ({})", config::VERSION, config::PROFILE);
info!("Datadir: {}", config::PKGDATADIR); info!("Datadir: {}", config::PKGDATADIR);
let app = Self::new();
self.app.run(); gtk::prelude::ApplicationExtManual::run(&app);
} }
} }
...@@ -32,6 +32,5 @@ fn main() { ...@@ -32,6 +32,5 @@ fn main() {
static_resources::init().expect("Failed to initialize the resource file."); static_resources::init().expect("Failed to initialize the resource file.");
let app = Application::new(); Application::run()
app.run();
} }
...@@ -4,6 +4,7 @@ use gtk::prelude::*; ...@@ -4,6 +4,7 @@ use gtk::prelude::*;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
#[derive(Debug)]
pub struct PaginatorWidget { pub struct PaginatorWidget {
pub widget: gtk::Box, pub widget: gtk::Box,
carousel: adw::Carousel, carousel: adw::Carousel,
......
...@@ -6,14 +6,16 @@ use std::rc::Rc; ...@@ -6,14 +6,16 @@ use std::rc::Rc;
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::config::{APP_ID, PROFILE};
use crate::Application;
#[derive(Debug)]
pub struct Window { pub struct Window {
pub widget: adw::ApplicationWindow, pub widget: adw::ApplicationWindow,
pub paginator: RefCell<Rc<PaginatorWidget>>, pub paginator: RefCell<Rc<PaginatorWidget>>,
} }
impl Window { impl Window {
pub fn new(app: &adw::Application) -> Self { pub fn new(app: &Application) -> Self {
let widget = adw::ApplicationWindow::new(app); let widget = adw::ApplicationWindow::new(app);
let paginator = RefCell::new(PaginatorWidget::new()); let paginator = RefCell::new(PaginatorWidget::new());
......
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