From 367363975e29c8eedfd63d3fbe717d3ac768a25d Mon Sep 17 00:00:00 2001 From: ovalkonia <60359793+ovalkonia@users.noreply.github.com> Date: Sat, 24 Aug 2024 13:30:06 +0300 Subject: [PATCH] Fix: the gtk `stack` widget bugfix (#1119) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixed the gtk stack widget * Add changelog entry for the stack widget bugfix * Small code style improvement Co-authored-by: Wölfchen --------- Co-authored-by: ElKowar Co-authored-by: Wölfchen --- CHANGELOG.md | 1 + crates/eww/src/widgets/widget_definitions.rs | 44 ++++++++++---------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79f4c5f5..aa04fc99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ All notable changes to eww will be listed here, starting at changes since versio - Re-enable some scss features (By: w-lfchen) - Fix and refactor nix flake (By: w-lfchen) - Fix remove items from systray (By: vnva) +- Fix the gtk `stack` widget (By: ovalkonia) ### Features - Update rust toolchain to 1.80.1 (By: w-lfchen) diff --git a/crates/eww/src/widgets/widget_definitions.rs b/crates/eww/src/widgets/widget_definitions.rs index c4d78a69..ac451eb0 100644 --- a/crates/eww/src/widgets/widget_definitions.rs +++ b/crates/eww/src/widgets/widget_definitions.rs @@ -1089,6 +1089,27 @@ const WIDGET_NAME_STACK: &str = "stack"; /// @desc A widget that displays one of its children at a time fn build_gtk_stack(bargs: &mut BuilderArgs) -> Result { let gtk_widget = gtk::Stack::new(); + + if bargs.widget_use.children.len() < 1 { + return Err(DiagError(gen_diagnostic!("stack must contain at least one element", bargs.widget_use.span)).into()); + } + + let children = bargs.widget_use.children.iter().map(|child| { + build_gtk_widget( + bargs.scope_graph, + bargs.widget_defs.clone(), + bargs.calling_scope, + child.clone(), + bargs.custom_widget_invocation.clone(), + ) + }); + + for (i, child) in children.enumerate() { + let child = child?; + gtk_widget.add_named(&child, &i.to_string()); + child.show(); + } + def_widget!(bargs, _g, gtk_widget, { // @prop selected - index of child which should be shown prop(selected: as_i32) { gtk_widget.set_visible_child_name(&selected.to_string()); }, @@ -1098,28 +1119,7 @@ fn build_gtk_stack(bargs: &mut BuilderArgs) -> Result { prop(same_size: as_bool = false) { gtk_widget.set_homogeneous(same_size); } }); - match bargs.widget_use.children.len().cmp(&1) { - Ordering::Less => { - Err(DiagError(gen_diagnostic!("stack must contain at least one element", bargs.widget_use.span)).into()) - } - Ordering::Greater | Ordering::Equal => { - let children = bargs.widget_use.children.iter().map(|child| { - build_gtk_widget( - bargs.scope_graph, - bargs.widget_defs.clone(), - bargs.calling_scope, - child.clone(), - bargs.custom_widget_invocation.clone(), - ) - }); - for (i, child) in children.enumerate() { - let child = child?; - gtk_widget.add_named(&child, &i.to_string()); - child.show(); - } - Ok(gtk_widget) - } - } + Ok(gtk_widget) } const WIDGET_NAME_TRANSFORM: &str = "transform";