Skip to content

Commit

Permalink
Remove trait kas::layout::AutoLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Jan 11, 2024
1 parent d2c9ac7 commit 16bd205
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 73 deletions.
29 changes: 0 additions & 29 deletions crates/kas-core/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ mod storage;
mod visitor;

use crate::dir::{Direction, Directional, Directions};
use crate::event::ConfigCx;
use crate::geom::{Coord, Rect};
use crate::theme::{DrawCx, SizeCx};
use crate::Id;

#[allow(unused)] use crate::Layout;

Expand Down Expand Up @@ -259,31 +255,6 @@ pub trait LayoutVisitor {
fn layout_visitor(&mut self) -> Visitor<'_>;
}

/// Implementation generated by use of `layout = ..` property of `#[widget]`
///
/// It is not recommended to import this trait since method names conflict with [`Layout`].
pub trait AutoLayout {
/// Get size rules for the given axis
///
/// This functions identically to [`Layout::size_rules`].
fn size_rules(&mut self, sizer: SizeCx, axis: AxisInfo) -> SizeRules;

/// Set size and position
///
/// This functions identically to [`Layout::set_rect`].
fn set_rect(&mut self, cx: &mut ConfigCx, rect: Rect);

/// Translate a coordinate to an [`Id`]
///
/// This functions identically to [`Layout::find_id`].
fn find_id(&mut self, coord: Coord) -> Option<Id>;

/// Draw a widget and its children
///
/// This functions identically to [`Layout::draw`].
fn draw(&mut self, draw: DrawCx);
}

#[cfg(test)]
#[test]
fn size() {
Expand Down
54 changes: 15 additions & 39 deletions crates/kas-macros/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,41 +707,6 @@ pub fn widget(attr_span: Span, mut args: WidgetArgs, scope: &mut Scope) -> Resul
#layout_visitor
}
}

impl #impl_generics ::kas::layout::AutoLayout for #impl_target {
fn size_rules(
&mut self,
sizer: ::kas::theme::SizeCx,
axis: ::kas::layout::AxisInfo,
) -> ::kas::layout::SizeRules {
::kas::layout::LayoutVisitor::layout_visitor(self).size_rules(sizer, axis)
}

fn set_rect(
&mut self,
cx: &mut ::kas::event::ConfigCx,
rect: ::kas::geom::Rect,
) {
#core_path.rect = rect;
::kas::layout::LayoutVisitor::layout_visitor(self).set_rect(cx, rect);
}

fn find_id(&mut self, coord: ::kas::geom::Coord) -> Option<::kas::Id> {
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()))
}

fn draw(&mut self, draw: ::kas::theme::DrawCx) {
::kas::layout::LayoutVisitor::layout_visitor(self).draw(draw);
}
}
});

fn_size_rules = Some(quote! {
Expand All @@ -752,19 +717,30 @@ pub fn widget(attr_span: Span, mut args: WidgetArgs, scope: &mut Scope) -> Resul
) -> ::kas::layout::SizeRules {
#[cfg(debug_assertions)]
#core_path.status.size_rules(&#core_path.id, axis);
<Self as ::kas::layout::AutoLayout>::size_rules(self, sizer, axis)
::kas::layout::LayoutVisitor::layout_visitor(self).size_rules(sizer, axis)
}
});
set_rect = quote! {
<Self as ::kas::layout::AutoLayout>::set_rect(self, cx, rect);
#core_path.rect = rect;
::kas::layout::LayoutVisitor::layout_visitor(self).set_rect(cx, rect);
};
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()))
};
find_id = quote! { <Self as ::kas::layout::AutoLayout>::find_id(self, coord) };
fn_draw = Some(quote! {
fn draw(&mut self, draw: ::kas::theme::DrawCx) {
#[cfg(debug_assertions)]
#core_path.status.require_rect(&#core_path.id);

<Self as ::kas::layout::AutoLayout>::draw(self, draw);
::kas::layout::LayoutVisitor::layout_visitor(self).draw(draw);
}
});
} else {
Expand Down
3 changes: 2 additions & 1 deletion crates/kas-widgets/src/check_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ impl_scope! {

impl Layout for Self {
fn set_rect(&mut self, cx: &mut ConfigCx, rect: Rect) {
<Self as kas::layout::AutoLayout>::set_rect(self, cx, rect);
self.core.rect = rect;
self.layout_visitor().set_rect(cx, rect);
let dir = self.direction();
shrink_to_text(&mut self.core.rect, dir, &self.label);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/kas-widgets/src/menu/menu_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use super::{Menu, SubItems};
use crate::{AccessLabel, CheckBox};
use kas::prelude::*;
use kas::theme::{FrameStyle, TextClass};
use kas::{layout, prelude::*};
use std::fmt::Debug;

impl_scope! {
Expand Down Expand Up @@ -127,7 +127,7 @@ impl_scope! {
fn draw(&mut self, mut draw: DrawCx) {
let mut draw = draw.re_id(self.checkbox.id());
draw.frame(self.rect(), FrameStyle::MenuEntry, Default::default());
<Self as layout::AutoLayout>::draw(self, draw);
self.layout_visitor().draw(draw);
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/kas-widgets/src/radio_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ impl_scope! {

impl Layout for Self {
fn set_rect(&mut self, cx: &mut ConfigCx, rect: Rect) {
<Self as kas::layout::AutoLayout>::set_rect(self, cx, rect);
self.core.rect = rect;
self.layout_visitor().set_rect(cx, rect);
let dir = self.direction();
crate::check_box::shrink_to_text(&mut self.core.rect, dir, &self.label);
}
Expand Down
3 changes: 2 additions & 1 deletion crates/kas-widgets/src/spinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ impl_scope! {

impl Layout for Self {
fn set_rect(&mut self, cx: &mut ConfigCx, rect: Rect) {
<Self as kas::layout::AutoLayout>::set_rect(self, cx, rect);
self.core.rect = rect;
self.layout_visitor().set_rect(cx, rect);
self.edit.set_outer_rect(rect, FrameStyle::EditBox);
}

Expand Down

0 comments on commit 16bd205

Please sign in to comment.