Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make AdaptEvents a thin wrapper over an inner widget #430

Merged
merged 10 commits into from
Dec 20, 2023
3 changes: 2 additions & 1 deletion crates/kas-core/src/event/cx/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use crate::{Id, Node};
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};

#[allow(unused)] use crate::{event::Event, Events, Layout};
#[allow(unused)] use crate::event::{Event, EventCx};
#[allow(unused)] use crate::{Events, Layout};

/// Widget configuration and update context
///
Expand Down
8 changes: 8 additions & 0 deletions crates/kas-core/src/event/cx/cx_pub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,14 @@ impl<'a> EventCx<'a> {
self.messages.try_observe()
}

/// Try getting a debug representation of the last message on the stack
///
/// Note: this method will always return `None` in release builds.
/// This may or may not change in future versions.
pub fn try_debug(&self) -> Option<&dyn Debug> {
self.messages.try_debug()
}

/// Set a scroll action
///
/// When setting [`Scroll::Rect`], use the widget's own coordinate space.
Expand Down
20 changes: 20 additions & 0 deletions crates/kas-core/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,26 @@ impl MessageStack {
None
}
}

/// Try getting a debug representation of the last message on the stack
///
/// Note: this method will always return `None` in release builds.
/// This may or may not change in future versions.
pub fn try_debug(&self) -> Option<&dyn Debug> {
cfg_if::cfg_if! {
if #[cfg(debug_assertions)] {
if let Some(m) = self.stack.last(){
println!("message: {:?}", &m.fmt);
} else {
println!("empty stack");
}
self.stack.last().map(|m| &m.fmt as &dyn Debug)
} else {
println!("release");
None
}
}
}
}

impl Drop for MessageStack {
Expand Down
38 changes: 24 additions & 14 deletions crates/kas-macros/src/make_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,25 @@ impl Tree {
}

// Excludes: fn nav_next
pub fn layout_methods(&self, core_path: &Toks) -> Result<Toks> {
pub fn layout_methods(&self, core_path: &Toks, debug_assertions: bool) -> Result<Toks> {
let (dbg_size, dbg_set, dbg_require) = if debug_assertions {
(
quote! {
#[cfg(debug_assertions)]
#core_path.status.size_rules(&#core_path.id, axis);
},
quote! {
#[cfg(debug_assertions)]
#core_path.status.set_rect(&#core_path.id);
},
quote! {
#[cfg(debug_assertions)]
#core_path.status.require_rect(&#core_path.id);
},
)
} else {
(quote! {}, quote! {}, quote! {})
};
let layout = self.0.generate(core_path)?;
Ok(quote! {
fn size_rules(
Expand All @@ -77,9 +95,7 @@ impl Tree {
axis: ::kas::layout::AxisInfo,
) -> ::kas::layout::SizeRules {
use ::kas::{Layout, layout};

#[cfg(debug_assertions)]
#core_path.status.size_rules(&#core_path.id, axis);
#dbg_size

(#layout).size_rules(sizer, axis)
}
Expand All @@ -90,19 +106,15 @@ impl Tree {
rect: ::kas::geom::Rect,
) {
use ::kas::{Layout, layout};

#[cfg(debug_assertions)]
#core_path.status.set_rect(&#core_path.id);
#dbg_set

#core_path.rect = rect;
(#layout).set_rect(cx, rect);
}

fn find_id(&mut self, coord: ::kas::geom::Coord) -> Option<::kas::Id> {
use ::kas::{layout, Layout, LayoutExt};

#[cfg(debug_assertions)]
#core_path.status.require_rect(&#core_path.id);
#dbg_require

if !self.rect().contains(coord) {
return None;
Expand All @@ -113,9 +125,7 @@ impl Tree {

fn draw(&mut self, draw: ::kas::theme::DrawCx) {
use ::kas::{Layout, layout};

#[cfg(debug_assertions)]
#core_path.status.require_rect(&#core_path.id);
#dbg_require

(#layout).draw(draw);
}
Expand Down Expand Up @@ -205,7 +215,7 @@ impl Tree {
true,
);

let layout_methods = self.layout_methods(&core_path)?;
let layout_methods = self.layout_methods(&core_path, true)?;
let nav_next = match self.nav_next(std::iter::empty()) {
Ok(result) => Some(result),
Err((span, msg)) => {
Expand Down
3 changes: 2 additions & 1 deletion crates/kas-macros/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,8 @@ pub fn widget(attr_span: Span, mut args: WidgetArgs, scope: &mut Scope) -> Resul
}
};

let layout_methods = layout.layout_methods(&quote! { self.#core })?;
// Generated layout methods are wrapped, so we don't require debug assertions here.
let layout_methods = layout.layout_methods(&quote! { self.#core }, false)?;
scope.generated.push(quote! {
impl #impl_generics ::kas::layout::AutoLayout for #impl_target {
#layout_methods
Expand Down
2 changes: 1 addition & 1 deletion crates/kas-view/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ pub trait Driver<Item, Data: SharedData<Item = Item>> {
fn on_messages(
&mut self,
cx: &mut EventCx,
widget: &mut Self::Widget,
data: &Data,
key: &Data::Key,
widget: &mut Self::Widget,
) {
let _ = (cx, data, key, widget);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/kas-view/src/list_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ impl_scope! {
None => return,
};

self.driver.on_messages(cx, data, &key, &mut w.widget);
self.driver.on_messages(cx, &mut w.widget, data, &key);
} else {
// Message is from self
key = match self.press_target.as_ref() {
Expand Down
2 changes: 1 addition & 1 deletion crates/kas-view/src/matrix_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ impl_scope! {
None => return,
};

self.driver.on_messages(cx, data, &key, &mut w.widget);
self.driver.on_messages(cx, &mut w.widget, data, &key);
} else {
// Message is from self
key = match self.press_target.clone() {
Expand Down
Loading