Skip to content

Commit

Permalink
Merge pull request #234 from hydra/pile-improvements-2
Browse files Browse the repository at this point in the history
Pile - Improve API by avoiding boolean arguments on the `show` method.
  • Loading branch information
ecton authored Jan 5, 2025
2 parents 96707c1 + 1dd83c2 commit 934d94d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/pile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() -> cushy::Result {
.into_button()
.on_click({
let section = handle.clone();
move |_| section.show(true)
move |_| section.show_and_focus()
})
.make_widget();
let button_id = button.id();
Expand Down
21 changes: 14 additions & 7 deletions src/widgets/pile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Pile {
struct PileData {
widgets: Lots<Option<WidgetInstance>>,
visible: VecDeque<LotId>,
focus_visible: bool,
focus_on_show: bool,
}

impl PileData {
Expand Down Expand Up @@ -124,7 +124,7 @@ impl Widget for WidgetPile {
.expect("visible widget")
.mounted(context);
let mut child_context = context.for_other(&visible);
if pile.focus_visible && 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 @@ -205,20 +205,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: bool) {
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 = false;
pile.focus_on_show = false;
}
pile.hide_id(self.0.id);
pile.widgets.remove(self.0.id);
Expand Down

0 comments on commit 934d94d

Please sign in to comment.