feat!(ccs): Replace run function with AppRunner trait

This commit is contained in:
Michael Aaron Murphy 2022-01-25 20:47:32 +01:00
parent 52df3570ec
commit 1ac913d5c4
No known key found for this signature in database
GPG key ID: B2732D4240C9212C
3 changed files with 30 additions and 14 deletions

View file

@ -6,10 +6,12 @@ extern crate cosmic_component_system as ccs;
mod components; mod components;
use self::components::App; use self::components::App;
use ccs::Component; use ccs::{AppRunner, Component};
fn main() { fn main() {
ccs::run(|app| { gtk4::builders::ApplicationBuilder::new()
.application_id("org.pop.CosmicComponentExample")
.cosmic_run(|app| {
App::default().register(app); App::default().register(app);
}); });
} }

View file

@ -0,0 +1,22 @@
// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use gtk4::prelude::{ApplicationExt, ApplicationExtManual};
pub trait AppRunner {
/// Convenience method to activates and run a GTK4 application.
fn cosmic_run<F: Fn(gtk4::Application) + 'static>(self, func: F);
}
impl AppRunner for gtk4::Application {
fn cosmic_run<F: Fn(gtk4::Application) + 'static>(self, func: F) {
self.connect_activate(move |app| func(app.clone()));
self.run();
}
}
impl AppRunner for gtk4::builders::ApplicationBuilder {
fn cosmic_run<F: Fn(gtk4::Application) + 'static>(self, func: F) {
self.build().cosmic_run(func);
}
}

View file

@ -1,11 +1,13 @@
// Copyright 2022 System76 <info@system76.com> // Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
mod app_runner;
mod macros; mod macros;
use gtk4::prelude::*; use gtk4::prelude::*;
use tokio::sync::mpsc; use tokio::sync::mpsc;
pub use self::app_runner::AppRunner;
pub use gtk4 as gtk; pub use gtk4 as gtk;
pub use relm4_macros::view; pub use relm4_macros::view;
@ -198,16 +200,6 @@ pub fn forward<I: 'static, O: 'static, F: (Fn(I) -> O) + 'static>(
}) })
} }
/// Convenience function for launching an application.
pub fn run<F: Fn(gtk4::Application) + 'static>(func: F) {
use gtk4::prelude::*;
let app = gtk4::Application::new(None, Default::default());
app.connect_activate(move |app| func(app.clone()));
app.run();
}
/// Convenience function for spawning tasks on the local executor /// Convenience function for spawning tasks on the local executor
pub fn spawn_local<F: std::future::Future<Output = ()> + 'static>(func: F) { pub fn spawn_local<F: std::future::Future<Output = ()> + 'static>(func: F) {
gtk4::glib::MainContext::default().spawn_local(func); gtk4::glib::MainContext::default().spawn_local(func);