diff --git a/examples/pile.rs b/examples/pile.rs index 983c9d34b..0297c62a3 100644 --- a/examples/pile.rs +++ b/examples/pile.rs @@ -1,7 +1,7 @@ use cushy::value::Dynamic; use cushy::widget::{MakeWidget, WidgetList}; use cushy::widgets::input::InputValue; -use cushy::widgets::pile::{Focus, Pile}; +use cushy::widgets::pile::Pile; use cushy::Run; fn main() -> cushy::Result { @@ -20,7 +20,7 @@ fn main() -> cushy::Result { .into_button() .on_click({ let section = handle.clone(); - move |_| section.show(Focus::Unchanged) + move |_| section.show_and_focus() }) .make_widget(); let button_id = button.id(); diff --git a/src/widgets/pile.rs b/src/widgets/pile.rs index e6ad5fee7..8c7a97f93 100644 --- a/src/widgets/pile.rs +++ b/src/widgets/pile.rs @@ -25,18 +25,11 @@ pub struct Pile { data: Dynamic, } -#[derive(Default, Debug)] -pub enum Focus { - Focused, - #[default] - Unchanged -} - #[derive(Default, Debug)] struct PileData { widgets: Lots>, visible: VecDeque, - focus_visible: Focus, + focus_on_show: bool, } impl PileData { @@ -129,7 +122,7 @@ impl Widget for WidgetPile { .expect("visible widget") .mounted(context); let mut child_context = context.for_other(&visible); - if matches!(pile.focus_visible, Focus::Focused) && self.last_visible != Some(id) { + if pile.focus_on_show && self.last_visible != Some(id) { child_context.focus(); } let size = child_context.layout(available_space); @@ -197,20 +190,27 @@ pub struct PiledWidget(Arc); impl PiledWidget { /// Shows this widget in its pile. - /// - /// If `focus` is true, the widget will be focused when shown. - pub fn show(&self, focus: Focus) { + pub fn show(&self) { + self.show_inner(false); + } + + /// Shows this widget in its pile and directs keyboard focus to it. + pub fn show_and_focus(&self) { + self.show_inner(true); + } + + fn show_inner(&self, focus: bool) { let mut pile = self.0.pile.data.lock(); pile.hide_id(self.0.id); pile.visible.push_front(self.0.id); - pile.focus_visible = focus; + pile.focus_on_show = focus; } /// Removes this widget from the pile. pub fn remove(&self) { let mut pile = self.0.pile.data.lock(); if pile.visible.front() == Some(&self.0.id) { - pile.focus_visible = Focus::Unchanged; + pile.focus_on_show = false; } pile.hide_id(self.0.id); pile.widgets.remove(self.0.id);