Skip to content

Commit

Permalink
Add AdaptWidget::on_message, on_messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Nov 22, 2023
1 parent 173f046 commit db920ae
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
39 changes: 34 additions & 5 deletions crates/kas-widgets/src/adapt/adapt_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

//! Event adapters
use kas::event::ConfigCx;
use kas::event::{ConfigCx, EventCx};
use kas::{autoimpl, impl_scope, widget_index, Events, Widget};
use std::fmt::Debug;

impl_scope! {
/// Wrapper to call a closure on update
Expand All @@ -21,6 +22,7 @@ impl_scope! {
pub inner: W,
on_configure: Option<Box<dyn Fn(&mut ConfigCx, &mut W)>>,
on_update: Option<Box<dyn Fn(&mut ConfigCx, &mut W, &W::Data)>>,
message_handlers: Vec<Box<dyn Fn(&mut EventCx, &mut W, &W::Data)>>,
}

impl Self {
Expand All @@ -32,12 +34,11 @@ impl_scope! {
inner,
on_configure: None,
on_update: None,
message_handlers: vec![],
}
}

/// Call the given closure on [`Events::configure`]
///
/// Returns a wrapper around the input widget.
#[must_use]
pub fn on_configure<F>(mut self, f: F) -> Self
where
Expand All @@ -48,8 +49,6 @@ impl_scope! {
}

/// Call the given closure on [`Events::update`]
///
/// Returns a wrapper around the input widget.
#[must_use]
pub fn on_update<F>(mut self, f: F) -> Self
where
Expand All @@ -58,6 +57,30 @@ impl_scope! {
self.on_update = Some(Box::new(f));
self
}

/// Add a handler on message of type `M`
#[must_use]
pub fn on_message<M, H>(self, handler: H) -> Self
where
M: Debug + 'static,
H: Fn(&mut EventCx, &mut W, &W::Data, M) + 'static,
{
self.on_messages(move |cx, w, data| {
if let Some(m) = cx.try_pop() {
handler(cx, w, data, m);
}
})
}

/// Add a generic message handler
#[must_use]
pub fn on_messages<H>(mut self, handler: H) -> Self
where
H: Fn(&mut EventCx, &mut W, &W::Data) + 'static,
{
self.message_handlers.push(Box::new(handler));
self
}
}

impl Events for Self {
Expand All @@ -83,5 +106,11 @@ impl_scope! {
f(cx, &mut self.inner, data);
}
}

fn handle_messages(&mut self, cx: &mut EventCx, data: &W::Data) {
for handler in self.message_handlers.iter() {
handler(cx, &mut self.inner, data);
}
}
}
}
28 changes: 26 additions & 2 deletions crates/kas-widgets/src/adapt/adapt_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
use super::{Map, MapAny, OnUpdate, Reserve, WithLabel};
use kas::cast::{Cast, CastFloat};
use kas::dir::Directional;
use kas::event::ConfigCx;
use kas::event::{ConfigCx, EventCx};
use kas::geom::Vec2;
use kas::layout::{AxisInfo, SizeRules};
use kas::text::AccessString;
use kas::theme::SizeCx;
#[allow(unused)] use kas::Events;
use kas::Widget;
#[allow(unused)] use kas::{Events, Layout};
use std::fmt::Debug;

/// Provides `.map_any()`
///
Expand Down Expand Up @@ -66,6 +67,29 @@ pub trait AdaptWidget: Widget + Sized {
OnUpdate::new(self).on_update(f)
}

/// Add a handler on message of type `M`
///
/// Returns a wrapper around the input widget.
#[must_use]
fn on_message<M, H>(self, handler: H) -> OnUpdate<Self>
where
M: Debug + 'static,
H: Fn(&mut EventCx, &mut Self, &Self::Data, M) + 'static,
{
OnUpdate::new(self).on_message(handler)
}

/// Add a generic message handler
///
/// Returns a wrapper around the input widget.
#[must_use]
fn on_messages<H>(self, handler: H) -> OnUpdate<Self>
where
H: Fn(&mut EventCx, &mut Self, &Self::Data) + 'static,
{
OnUpdate::new(self).on_messages(handler)
}

/// Construct a wrapper, setting minimum size in pixels
///
/// The input size is scaled by the scale factor.
Expand Down

0 comments on commit db920ae

Please sign in to comment.