Skip to content

Commit

Permalink
Change behaviour of Layout::l_find_id
Browse files Browse the repository at this point in the history
Fix: set rect in List::l_set_rect
Tweak: Splitter: debug_assert that size_solved
  • Loading branch information
dhardy committed Dec 30, 2024
1 parent 23d1462 commit 203f262
Show file tree
Hide file tree
Showing 26 changed files with 84 additions and 135 deletions.
15 changes: 5 additions & 10 deletions crates/kas-core/src/core/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,39 +254,34 @@ pub trait Layout {
///
/// When writing a custom implementation:
///
/// - Widgets should test `self.rect().contains(coord)`, returning `None`
/// if this test is `false`; otherwise, they should always return *some*
/// [`Id`], either a childs or their own.
/// - Widgets may assume that `self.rect().contains(coord)`.
/// - If the Widget uses a translated coordinate space (i.e.
/// `self.translation() != Offset::ZERO`) then pass
/// `coord + self.translation()` to children.
///
/// The default implementation is non-trivial:
/// ```ignore
/// if !self.rect().contains(coord) {
/// return None;
/// }
/// let coord = coord + self.translation();
/// for child in ITER_OVER_CHILDREN {
/// if let Some(id) = child.find_id(coord) {
/// return Some(id);
/// }
/// }
/// Some(self.id())
/// self.id()
/// ```
fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
fn l_find_id(&mut self, coord: Coord) -> Id {
let _ = coord;
unimplemented!() // make rustdoc show that this is a provided method
}

/// Translate a coordinate to an [`Id`]
///
/// This wraps [`Layout::l_find_id`].
/// This tests whether `self.rect().contains(coord)`, then calls [`Layout::l_find_id`].
///
/// It is expected that [`Tile::set_rect`] is called before this method,
/// but failure to do so should not cause a fatal error.
fn find_id(&mut self, coord: Coord) -> Option<Id> {
self.l_find_id(coord)
self.rect().contains(coord).then(|| self.l_find_id(coord))
}

/// Draw a widget and its children
Expand Down
11 changes: 4 additions & 7 deletions crates/kas-core/src/layout/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,11 @@ pub trait Visitable {
/// In other respects, this functions identically to [`Layout::set_rect`].
fn set_rect(&mut self, cx: &mut ConfigCx, rect: Rect, hints: AlignHints);

/// Translate a coordinate to an [`Id`]
///
/// The caller is expected to
/// Look for a widget at this `coord`
///
/// 1. Return `None` if `!self.rect().contains(coord)`
/// 2. Translate `coord`: `let coord = coord + self.translation();`
/// 3. Call `find_id` (this method), returning its result if not `None`
/// 4. Otherwise return `Some(self.id())`
/// Returns the [`Id`] of a child when some child occupies `coord`. Returns
/// [`None`] when there is no (probable) child widget at `coord`, in which
/// case the caller may use its own [`Id`].
fn find_id(&mut self, coord: Coord) -> Option<Id>;

/// Draw a widget and its children
Expand Down
2 changes: 1 addition & 1 deletion crates/kas-core/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl_scope! {
self.inner.set_rect(cx, Rect::new(p_in, s_in), hints);
}

fn l_find_id(&mut self, _: Coord) -> Option<Id> {
fn l_find_id(&mut self, _: Coord) -> Id {
unimplemented!()
}

Expand Down
7 changes: 2 additions & 5 deletions crates/kas-macros/src/make_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,15 @@ impl Tree {
::kas::layout::LayoutVisitor::layout_visitor(self).set_rect(cx, rect, hints);
}

fn l_find_id(&mut self, coord: ::kas::geom::Coord) -> Option<::kas::Id> {
fn l_find_id(&mut self, coord: ::kas::geom::Coord) -> ::kas::Id {
use ::kas::{Layout, LayoutExt, layout::LayoutVisitor};
#[cfg(debug_assertions)]
#core_path.status.require_rect(&#core_path.id);

if !self.rect().contains(coord) {
return None;
}
let coord = coord + self.translation();
self.layout_visitor()
.find_id(coord)
.or_else(|| Some(self.id()))
.unwrap_or_else(|| self.id())
}

fn draw(&mut self, draw: ::kas::theme::DrawCx) {
Expand Down
9 changes: 3 additions & 6 deletions crates/kas-macros/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ pub fn widget(attr_span: Span, mut args: WidgetArgs, scope: &mut Scope) -> Resul
let mut set_rect = quote! { self.#core.rect = rect; };
let mut l_find_id = quote! {
use ::kas::{Layout, LayoutExt};
self.rect().contains(coord).then(|| self.id())
self.id()
};
let mut fn_draw = None;
if let Some(Layout { tree, .. }) = args.layout.take() {
Expand Down Expand Up @@ -425,13 +425,10 @@ pub fn widget(attr_span: Span, mut args: WidgetArgs, scope: &mut Scope) -> Resul
l_find_id = quote! {
use ::kas::{Layout, LayoutExt, layout::LayoutVisitor};

if !self.rect().contains(coord) {
return None;
}
let coord = coord + self.translation();
self.layout_visitor()
.find_id(coord)
.or_else(|| Some(self.id()))
.unwrap_or_else(|| self.id())
};
fn_draw = Some(quote! {
fn draw(&mut self, draw: ::kas::theme::DrawCx) {
Expand Down Expand Up @@ -461,7 +458,7 @@ pub fn widget(attr_span: Span, mut args: WidgetArgs, scope: &mut Scope) -> Resul
}
};
let fn_l_find_id = quote! {
fn l_find_id(&mut self, coord: ::kas::geom::Coord) -> Option<::kas::Id> {
fn l_find_id(&mut self, coord: ::kas::geom::Coord) -> ::kas::Id {
#[cfg(debug_assertions)]
#core_path.status.require_rect(&#core_path.id);

Expand Down
10 changes: 3 additions & 7 deletions crates/kas-view/src/list_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,20 +553,16 @@ impl_scope! {
self.scroll_offset()
}

fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
if !self.rect().contains(coord) {
return None;
}

fn l_find_id(&mut self, coord: Coord) -> Id {
let coord = coord + self.scroll.offset();
for child in &mut self.widgets[..self.cur_len.cast()] {
if child.key.is_some() {
if let Some(id) = child.widget.find_id(coord) {
return Some(id);
return id;
}
}
}
Some(self.id())
self.id()
}

fn draw(&mut self, mut draw: DrawCx) {
Expand Down
10 changes: 3 additions & 7 deletions crates/kas-view/src/matrix_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,21 +487,17 @@ impl_scope! {
self.scroll_offset()
}

fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
if !self.rect().contains(coord) {
return None;
}

fn l_find_id(&mut self, coord: Coord) -> Id {
let num = self.num_children();
let coord = coord + self.scroll.offset();
for child in &mut self.widgets[..num] {
if child.key.is_some() {
if let Some(id) = child.widget.find_id(coord) {
return Some(id);
return id;
}
}
}
Some(self.id())
self.id()
}

fn draw(&mut self, mut draw: DrawCx) {
Expand Down
4 changes: 2 additions & 2 deletions crates/kas-widgets/src/adapt/with_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ impl_scope! {
}

impl Layout for Self {
fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
self.rect().contains(coord).then(|| self.inner.id())
fn l_find_id(&mut self, _: Coord) -> Id {
self.inner.id()
}
}
}
4 changes: 2 additions & 2 deletions crates/kas-widgets/src/check_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ impl_scope! {
shrink_to_text(&mut self.core.rect, dir, &self.label);
}

fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
self.rect().contains(coord).then(|| self.inner.id())
fn l_find_id(&mut self, _: Coord) -> Id {
self.inner.id()
}
}

Expand Down
14 changes: 5 additions & 9 deletions crates/kas-widgets/src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,20 +396,16 @@ impl_scope! {
self.update_scroll_bar(cx);
}

fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
if !self.rect().contains(coord) {
return None;
}

fn l_find_id(&mut self, coord: Coord) -> Id {
if self.max_scroll_offset().1 > 0 {
if let Some(id) = self.bar.find_id(coord) {
return Some(id);
return id;
}
}

// If coord is over self but not over self.bar, we assign
// the event to self.inner without further question.
Some(self.inner.id())
self.inner.id()
}

fn draw(&mut self, mut draw: DrawCx) {
Expand Down Expand Up @@ -692,8 +688,8 @@ impl_scope! {
self.view_offset = self.view_offset.min(self.max_scroll_offset());
}

fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
self.outer_rect.contains(coord).then_some(self.id())
fn l_find_id(&mut self, _: Coord) -> Id {
self.id()
}

fn draw(&mut self, mut draw: DrawCx) {
Expand Down
9 changes: 3 additions & 6 deletions crates/kas-widgets/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,15 @@ impl_scope! {
}
}

fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
if !self.rect().contains(coord) {
return None;
}
fn l_find_id(&mut self, coord: Coord) -> Id {
for n in 0..self.widgets.len() {
if let Some(child) = self.widgets.get_mut_layout(n) {
if let Some(id) = child.find_id(coord) {
return Some(id);
return id;
}
}
}
Some(self.id())
self.id()
}

fn draw(&mut self, mut draw: DrawCx) {
Expand Down
3 changes: 2 additions & 1 deletion crates/kas-widgets/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ impl_scope! {
}
}

fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
fn l_find_id(&mut self, coord: Coord) -> Id {
let solver = RowPositionSolver::new(self.direction);
solver
.find_child_mut(&mut self.widgets, coord)
.and_then(|child| child.find_id(coord))
.unwrap_or_else(|| self.id())
}

fn draw(&mut self, mut draw: DrawCx) {
Expand Down
10 changes: 5 additions & 5 deletions crates/kas-widgets/src/menu/menu_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ impl_scope! {
}

impl Layout for Self {
fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
self.rect().contains(coord).then(|| self.id())
fn l_find_id(&mut self, _: Coord) -> Id {
self.id()
}

fn draw(&mut self, mut draw: DrawCx) {
draw.frame(self.rect(), FrameStyle::MenuEntry, Default::default());
self.label.draw(draw);
draw.recurse(&mut self.label);
}
}

Expand Down Expand Up @@ -120,8 +120,8 @@ impl_scope! {
}

impl Layout for Self {
fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
self.rect().contains(coord).then(|| self.checkbox.id())
fn l_find_id(&mut self, _: Coord) -> Id {
self.checkbox.id()
}

fn draw(&mut self, mut draw: DrawCx) {
Expand Down
7 changes: 2 additions & 5 deletions crates/kas-widgets/src/menu/menubar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,12 @@ impl_scope! {
}
}

fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
if !self.rect().contains(coord) {
return None;
}
fn l_find_id(&mut self, coord: Coord) -> Id {
let solver = RowPositionSolver::new(self.direction);
solver
.find_child_mut(&mut self.widgets, coord)
.and_then(|child| child.find_id(coord))
.or_else(|| Some(self.id()))
.unwrap_or_else(|| self.id())
}

fn draw(&mut self, mut draw: DrawCx) {
Expand Down
16 changes: 6 additions & 10 deletions crates/kas-widgets/src/menu/submenu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ impl_scope! {
None
}

fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
self.rect().contains(coord).then(|| self.id())
fn l_find_id(&mut self, _: Coord) -> Id {
self.id()
}

fn draw(&mut self, mut draw: DrawCx) {
draw.frame(self.rect(), FrameStyle::MenuEntry, Default::default());
self.label.draw(draw.re_id(self.id()));
draw.recurse(&mut self.label);
if self.mark.rect().size != Size::ZERO {
draw.recurse(&mut self.mark);
}
Expand Down Expand Up @@ -354,17 +354,13 @@ impl_scope! {
}
}

fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
if !self.rect().contains(coord) {
return None;
}

fn l_find_id(&mut self, coord: Coord) -> Id {
for child in self.list.iter_mut() {
if let Some(id) = child.find_id(coord) {
return Some(id);
return id;
}
}
Some(self.id())
self.id()
}

fn draw(&mut self, mut draw: DrawCx) {
Expand Down
4 changes: 2 additions & 2 deletions crates/kas-widgets/src/radio_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ impl_scope! {
crate::check_box::shrink_to_text(&mut self.core.rect, dir, &self.label);
}

fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
self.rect().contains(coord).then(|| self.inner.id())
fn l_find_id(&mut self, _: Coord) -> Id {
self.inner.id()
}
}

Expand Down
6 changes: 2 additions & 4 deletions crates/kas-widgets/src/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,9 @@ impl_scope! {
self.scroll_offset()
}

fn l_find_id(&mut self, coord: Coord) -> Option<Id> {
if !self.rect().contains(coord) {
return None;
}
fn l_find_id(&mut self, coord: Coord) -> Id {
self.inner.find_id(coord + self.translation())
.unwrap_or_else(|| self.id())
}

fn draw(&mut self, mut draw: DrawCx) {
Expand Down
Loading

0 comments on commit 203f262

Please sign in to comment.