feat!(ccs): Replace run function with AppRunner trait
This commit is contained in:
parent
52df3570ec
commit
1ac913d5c4
3 changed files with 30 additions and 14 deletions
|
|
@ -6,10 +6,12 @@ extern crate cosmic_component_system as ccs;
|
|||
mod components;
|
||||
|
||||
use self::components::App;
|
||||
use ccs::Component;
|
||||
use ccs::{AppRunner, Component};
|
||||
|
||||
fn main() {
|
||||
ccs::run(|app| {
|
||||
App::default().register(app);
|
||||
});
|
||||
gtk4::builders::ApplicationBuilder::new()
|
||||
.application_id("org.pop.CosmicComponentExample")
|
||||
.cosmic_run(|app| {
|
||||
App::default().register(app);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
22
crates/component-system/src/app_runner.rs
Normal file
22
crates/component-system/src/app_runner.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,13 @@
|
|||
// Copyright 2022 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
mod app_runner;
|
||||
mod macros;
|
||||
|
||||
use gtk4::prelude::*;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
pub use self::app_runner::AppRunner;
|
||||
pub use gtk4 as gtk;
|
||||
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
|
||||
pub fn spawn_local<F: std::future::Future<Output = ()> + 'static>(func: F) {
|
||||
gtk4::glib::MainContext::default().spawn_local(func);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue