Skip to content

Commit

Permalink
Merge pull request #434 from kas-gui/work3
Browse files Browse the repository at this point in the history
AutoLayout → LayoutVisitor
  • Loading branch information
dhardy committed Jan 16, 2024
2 parents bdec1a6 + 53ad645 commit 2f6b417
Show file tree
Hide file tree
Showing 11 changed files with 630 additions and 332 deletions.
44 changes: 14 additions & 30 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 All @@ -61,7 +57,7 @@ pub use size_rules::SizeRules;
pub use size_types::*;
pub use sizer::{solve_size_rules, RulesSetter, RulesSolver, SolveCache};
pub use storage::*;
pub use visitor::{FrameStorage, PackStorage, Visitor};
pub use visitor::{FrameStorage, PackStorage, Visitable, VisitableList, Visitor};

/// Information on which axis is being resized
///
Expand Down Expand Up @@ -222,10 +218,14 @@ impl From<AxisInfo> for Directions {
}
}

/// Implementation generated by use of `layout = ..` property of `#[widget]`
/// Macro-generated implementation of layout over a [`Visitor`]
///
/// This trait need not be implemented by the user, however it may be useful to
/// adjust the result of an automatic implementation, for example:
/// This method is implemented by the [`#widget`] macro when a [`layout`]
/// specification is provided.
/// Direct implementations of this trait are not supported.
///
/// This trait may be used in user-code where a `layout` specification is used
/// *and* custom behaviour is provided for one or more layout methods, for example:
/// ```
/// # extern crate kas_core as kas;
/// use kas::prelude::*;
Expand All @@ -240,35 +240,19 @@ impl From<AxisInfo> for Directions {
/// }
/// impl Layout for Self {
/// fn size_rules(&mut self, sizer: SizeCx, axis: AxisInfo) -> SizeRules {
/// let mut rules = kas::layout::AutoLayout::size_rules(self, sizer, axis);
/// let mut rules = self.layout_visitor().size_rules(sizer, axis);
/// rules.set_stretch(Stretch::High);
/// rules
/// }
/// }
/// }
/// ```
///
/// 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);
/// [`#widget`]: crate::widget
/// [`layout`]: crate::widget#layout-1
pub trait LayoutVisitor {
/// Layout defined by a [`Visitor`]
fn layout_visitor(&mut self) -> Visitor<impl Visitable>;
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 2f6b417

Please sign in to comment.