2022-01-24 20:20:33 +01:00
|
|
|
// Copyright 2022 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2022-01-25 20:47:32 +01:00
|
|
|
mod app_runner;
|
2022-01-25 20:52:54 +01:00
|
|
|
mod component;
|
|
|
|
|
mod handle;
|
2022-01-24 20:20:33 +01:00
|
|
|
mod macros;
|
|
|
|
|
|
|
|
|
|
use gtk4::prelude::*;
|
|
|
|
|
use tokio::sync::mpsc;
|
|
|
|
|
|
2022-01-25 20:47:32 +01:00
|
|
|
pub use self::app_runner::AppRunner;
|
2022-01-25 20:52:54 +01:00
|
|
|
pub use self::component::Component;
|
|
|
|
|
pub use self::handle::{Handle, Registered};
|
2022-01-24 20:20:33 +01:00
|
|
|
pub use gtk4 as gtk;
|
|
|
|
|
pub use relm4_macros::view;
|
|
|
|
|
|
|
|
|
|
pub type Sender<T> = mpsc::UnboundedSender<T>;
|
|
|
|
|
pub type Receiver<T> = mpsc::UnboundedReceiver<T>;
|
|
|
|
|
|
|
|
|
|
/// Provides a convenience function for getting a widget out of a type.
|
|
|
|
|
pub trait Widget<W> {
|
|
|
|
|
fn widget(&self) -> &W;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convenience function for `Component::register()`.
|
|
|
|
|
pub fn register<C: Component>(
|
|
|
|
|
model: C,
|
|
|
|
|
args: C::InitialArgs,
|
|
|
|
|
) -> Registered<C::RootWidget, C::Input, C::Output> {
|
|
|
|
|
model.register(args)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convenience function for forwarding events from a receiver to different sender.
|
|
|
|
|
pub fn forward<I: 'static, O: 'static, F: (Fn(I) -> O) + 'static>(
|
|
|
|
|
mut receiver: Receiver<I>,
|
|
|
|
|
sender: Sender<O>,
|
|
|
|
|
transformer: F,
|
|
|
|
|
) {
|
|
|
|
|
spawn_local(async move {
|
|
|
|
|
while let Some(event) = receiver.recv().await {
|
|
|
|
|
if sender.send(transformer(event)).is_err() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
|
}
|