#![allow(unknown_lints)]
#![allow(clippy::new_without_default, clippy::uninlined_format_args)]
#![warn(
missing_docs,
trivial_casts,
trivial_numeric_casts,
unused_extern_crates,
unused_import_braces,
unused_qualifications,
)]
mod component;
mod container;
mod core;
mod drawing;
mod macros;
mod state;
mod widget;
#[doc(hidden)]
pub use fragile::Fragile;
#[doc(hidden)]
pub use glib::{
Cast,
IsA,
Object,
StaticType,
ToValue,
Value,
};
#[doc(hidden)]
pub use glib::translate::{FromGlibPtrNone, IntoGlib, ToGlibPtr};
#[doc(hidden)]
pub use gobject_sys::{GParameter, g_object_newv};
use glib::Continue;
pub use crate::core::{Channel, EventStream, Sender, StreamHandle};
pub use crate::state::{
DisplayVariant,
IntoOption,
IntoPair,
Relm,
Update,
UpdateNew,
execute,
};
use state::init_component;
pub use component::Component;
pub use container::{Container, ContainerComponent, ContainerWidget};
pub use drawing::DrawHandler;
pub use widget::{Widget, WidgetTest};
#[macro_export]
macro_rules! impl_widget {
($($tt:tt)*) => {
()
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! use_impl_self_type {
(impl $(::relm::)*Widget for $self_type:ident { $($tts:tt)* }) => {
pub use self::__relm_gen_private::$self_type;
};
}
fn create_widget_test<WIDGET>(model_param: WIDGET::ModelParam) -> (Component<WIDGET>, WIDGET::Streams, WIDGET::Widgets)
where WIDGET: Widget + WidgetTest + 'static,
WIDGET::Msg: DisplayVariant + 'static,
{
let (component, widget, relm) = create_widget::<WIDGET>(model_param);
let widgets = widget.get_widgets();
let streams = widget.get_streams();
init_component::<WIDGET>(component.owned_stream(), widget, &relm);
(component, streams, widgets)
}
pub fn create_component<CHILDWIDGET>(model_param: CHILDWIDGET::ModelParam)
-> Component<CHILDWIDGET>
where CHILDWIDGET: Widget + 'static,
CHILDWIDGET::Msg: DisplayVariant + 'static,
{
let (component, widget, child_relm) = create_widget::<CHILDWIDGET>(model_param);
init_component::<CHILDWIDGET>(component.owned_stream(), widget, &child_relm);
component
}
pub fn create_container<CHILDWIDGET>(model_param: CHILDWIDGET::ModelParam)
-> ContainerComponent<CHILDWIDGET>
where CHILDWIDGET: Container + Widget + 'static,
CHILDWIDGET::Msg: DisplayVariant + 'static,
{
let (component, widget, child_relm) = create_widget::<CHILDWIDGET>(model_param);
let container = widget.container().clone();
let containers = widget.other_containers();
init_component::<CHILDWIDGET>(component.owned_stream(), widget, &child_relm);
ContainerComponent::new(component, container, containers)
}
fn create_widget<WIDGET>(model_param: WIDGET::ModelParam)
-> (Component<WIDGET>, WIDGET, Relm<WIDGET>)
where WIDGET: Widget + 'static,
WIDGET::Msg: DisplayVariant + 'static,
{
let stream = EventStream::new();
let relm = Relm::new(&stream);
let model = WIDGET::model(&relm, model_param);
let mut widget = WIDGET::view(&relm, model);
widget.init_view();
let root = widget.root();
(Component::new(stream, root), widget, relm)
}
type InitTestComponents<WIDGET> = (Component<WIDGET>, <WIDGET as WidgetTest>::Streams, <WIDGET as WidgetTest>::Widgets);
pub fn init_test<WIDGET>(model_param: WIDGET::ModelParam) ->
Result<InitTestComponents<WIDGET>, glib::BoolError>
where WIDGET: Widget + WidgetTest + 'static,
WIDGET::Msg: DisplayVariant + 'static,
{
gtk::init()?;
let main_context = glib::MainContext::default();
let _context = main_context.acquire()?;
let component = create_widget_test::<WIDGET>(model_param);
Ok(component)
}
pub fn init<WIDGET>(model_param: WIDGET::ModelParam) -> Result<Component<WIDGET>, glib::BoolError>
where WIDGET: Widget + 'static,
WIDGET::Msg: DisplayVariant + 'static
{
let (component, widget, relm) = create_widget::<WIDGET>(model_param);
init_component::<WIDGET>(component.owned_stream(), widget, &relm);
Ok(component)
}
pub fn run<WIDGET>(model_param: WIDGET::ModelParam) -> Result<(), glib::BoolError>
where WIDGET: Widget + 'static,
{
let main_context = glib::MainContext::default();
let _context = main_context.acquire()?;
gtk::init()?;
let _component = init::<WIDGET>(model_param)?;
gtk::main();
Ok(())
}
pub fn interval<F: Fn() -> MSG + 'static, MSG: 'static>(stream: &StreamHandle<MSG>, duration: u32, constructor: F) {
let stream = stream.clone();
glib::timeout_add_local(std::time::Duration::from_millis(duration as u64), move || {
let msg = constructor();
stream.emit(msg);
Continue(true)
});
}
pub fn timeout<F: Fn() -> MSG + 'static, MSG: 'static>(stream: &StreamHandle<MSG>, duration: u32, constructor: F) {
let stream = stream.clone();
glib::timeout_add_local(std::time::Duration::from_millis(duration as u64), move || {
let msg = constructor();
stream.emit(msg);
Continue(false)
});
}