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

Trait revision part 3: merge Component and Layout traits; put layout storage in widget_core!() #314

Merged
merged 16 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove kas::component::Component (replaced by Layout)
  • Loading branch information
dhardy committed May 5, 2022
commit 4ba8f7e8e53fabdfeea98683feb41461a274b428
24 changes: 3 additions & 21 deletions crates/kas-core/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,9 @@ use crate::geom::{Coord, Rect, Size};
use crate::layout::{Align, AlignHints, AxisInfo, SetRectMgr, SizeRules};
use crate::text::{format, AccelString, Text, TextApi};
use crate::theme::{DrawMgr, MarkStyle, SizeMgr, TextClass};
use crate::{TkAction, WidgetId};
use crate::{Layout, TkAction, WidgetId};
use kas_macros::{autoimpl, impl_scope};

/// Components are not true widgets, but support layout solving
///
/// TODO: since this is a sub-set of widget functionality, should [`crate::Widget`]
/// extend `Component`? (Significant trait revision would be required.)
pub trait Component {
/// Get size rules for the given axis
fn size_rules(&mut self, mgr: SizeMgr, axis: AxisInfo) -> SizeRules;

/// Apply a given `rect` to self
fn set_rect(&mut self, mgr: &mut SetRectMgr, rect: Rect, align: AlignHints);

/// Translate a coordinate to a [`WidgetId`]
fn find_id(&mut self, coord: Coord) -> Option<WidgetId>;

/// Draw the component and its children
fn draw(&mut self, draw: DrawMgr);
}

impl_scope! {
/// A label component
#[impl_default(where T: trait)]
Expand Down Expand Up @@ -110,7 +92,7 @@ impl_scope! {
}
}

impl Component for Self {
impl Layout for Self {
fn size_rules(&mut self, mgr: SizeMgr, axis: AxisInfo) -> SizeRules {
mgr.text_bound(&mut self.text, self.class, axis)
}
Expand Down Expand Up @@ -149,7 +131,7 @@ impl_scope! {
Mark { style, rect }
}
}
impl Component for Self {
impl Layout for Self {
fn size_rules(&mut self, mgr: SizeMgr, axis: AxisInfo) -> SizeRules {
mgr.mark(self.style, axis)
}
Expand Down
15 changes: 7 additions & 8 deletions crates/kas-core/src/layout/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
use super::{AlignHints, AxisInfo, RulesSetter, RulesSolver, SetRectMgr, SizeRules, Storage};
use super::{DynRowStorage, RowPositionSolver, RowSetter, RowSolver, RowStorage};
use super::{GridChildInfo, GridDimensions, GridSetter, GridSolver, GridStorage};
use crate::component::Component;
use crate::draw::color::Rgb;
use crate::geom::{Coord, Offset, Rect, Size};
use crate::theme::{Background, DrawMgr, FrameStyle, SizeMgr};
use crate::WidgetId;
use crate::{dir::Directional, Widget};
use crate::{dir::Directional, Layout, Widget};
use std::any::Any;
use std::iter::ExactSizeIterator;

Expand All @@ -36,9 +35,9 @@ enum LayoutType<'a> {
/// No layout
None,
/// A component
Component(&'a mut dyn Component),
Component(&'a mut dyn Layout),
/// A boxed component
BoxComponent(Box<dyn Component + 'a>),
BoxComponent(Box<dyn Layout + 'a>),
/// A single child widget
Single(&'a mut dyn Widget),
/// A single child widget with alignment
Expand Down Expand Up @@ -100,7 +99,7 @@ impl<'a> Visitor<'a> {
}

/// Place a component in the layout
pub fn component(component: &'a mut dyn Component) -> Self {
pub fn component(component: &'a mut dyn Layout) -> Self {
let layout = LayoutType::Component(component);
Visitor { layout }
}
Expand Down Expand Up @@ -261,7 +260,7 @@ struct List<'a, S, D, I> {
children: I,
}

impl<'a, S: RowStorage, D: Directional, I> Component for List<'a, S, D, I>
impl<'a, S: RowStorage, D: Directional, I> Layout for List<'a, S, D, I>
where
I: ExactSizeIterator<Item = Visitor<'a>>,
{
Expand Down Expand Up @@ -302,7 +301,7 @@ struct Slice<'a, W: Widget, D: Directional> {
children: &'a mut [W],
}

impl<'a, W: Widget, D: Directional> Component for Slice<'a, W, D> {
impl<'a, W: Widget, D: Directional> Layout for Slice<'a, W, D> {
fn size_rules(&mut self, mgr: SizeMgr, axis: AxisInfo) -> SizeRules {
let dim = (self.direction, self.children.len());
let mut solver = RowSolver::new(axis, dim, self.data);
Expand Down Expand Up @@ -341,7 +340,7 @@ struct Grid<'a, S, I> {
children: I,
}

impl<'a, S: GridStorage, I> Component for Grid<'a, S, I>
impl<'a, S: GridStorage, I> Layout for Grid<'a, S, I>
where
I: Iterator<Item = (GridChildInfo, Visitor<'a>)>,
{
Expand Down
12 changes: 5 additions & 7 deletions crates/kas-widgets/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
//! - [`Separator`]

use crate::Separator;
use kas::component::Component;
use kas::dir::Right;
use kas::prelude::*;
use std::fmt::Debug;
Expand All @@ -35,16 +34,15 @@ pub use submenu::SubMenu;
#[derive(Default)]
pub struct SubItems<'a> {
/// Primary label
pub label: Option<&'a mut dyn Component>,
pub label: Option<&'a mut dyn Layout>,
/// Secondary label, often used to show shortcut key
pub label2: Option<&'a mut dyn Component>,
pub label2: Option<&'a mut dyn Layout>,
/// Sub-menu indicator
pub submenu: Option<&'a mut dyn Component>,
pub submenu: Option<&'a mut dyn Layout>,
/// Icon
pub icon: Option<&'a mut dyn Component>,
pub icon: Option<&'a mut dyn Layout>,
/// Toggle mark
// TODO: should be a component?
pub toggle: Option<&'a mut dyn Widget>,
pub toggle: Option<&'a mut dyn Layout>,
}

/// Trait governing menus, sub-menus and menu-entries
Expand Down
2 changes: 1 addition & 1 deletion crates/kas-widgets/src/menu/menu_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use super::{Menu, SubItems};
use crate::CheckBoxBare;
use kas::component::{Component, Label};
use kas::component::Label;
use kas::theme::{FrameStyle, TextClass};
use kas::{layout, prelude::*};
use std::fmt::Debug;
Expand Down
2 changes: 1 addition & 1 deletion crates/kas-widgets/src/menu/submenu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use super::{BoxedMenu, Menu, SubItems};
use crate::PopupFrame;
use kas::component::{Component, Label, Mark};
use kas::component::{Label, Mark};
use kas::event::{Command, Scroll};
use kas::layout::{self, RulesSetter, RulesSolver};
use kas::prelude::*;
Expand Down