Skip to content

Commit

Permalink
Split PiledWidget::show into two functions
Browse files Browse the repository at this point in the history
I wasn't too keen on such a purpose-built enum, and it seems like all
the use cases I can think of either the widget is going to want to allow
focus to change or not at compile time, not at runtime.
  • Loading branch information
ecton committed Jan 5, 2025
1 parent 5356014 commit 1dd83c2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions examples/pile.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();
Expand Down
28 changes: 14 additions & 14 deletions src/widgets/pile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,11 @@ pub struct Pile {
data: Dynamic<PileData>,
}

#[derive(Default, Debug)]
pub enum Focus {
Focused,
#[default]
Unchanged
}

#[derive(Default, Debug)]
struct PileData {
widgets: Lots<Option<WidgetInstance>>,
visible: VecDeque<LotId>,
focus_visible: Focus,
focus_on_show: bool,
}

impl PileData {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -197,20 +190,27 @@ pub struct PiledWidget(Arc<PiledWidgetData>);

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);
Expand Down

0 comments on commit 1dd83c2

Please sign in to comment.