Skip to content

Commit

Permalink
Fix: the gtk expander widget bugfix (#1132)
Browse files Browse the repository at this point in the history
* Fixed the gtk `expander` widget

* Add changelog entry for the `expander` widget bugfix
  • Loading branch information
ovalkonia authored Aug 24, 2024
1 parent 3673639 commit f01396f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ All notable changes to eww will be listed here, starting at changes since versio
- Fix nix flake
- Fix `jq` (By: w-lfchen)
- Labels now use gtk's truncation system (By: Rayzeq).
- Fix the gtk `expander` widget (By: ovalkonia)

### Features
- Add `systray` widget (By: ralismark)
Expand Down
36 changes: 35 additions & 1 deletion crates/eww/src/widgets/widget_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,46 @@ const WIDGET_NAME_EXPANDER: &str = "expander";
/// @desc A widget that can expand and collapse, showing/hiding it's children.
fn build_gtk_expander(bargs: &mut BuilderArgs) -> Result<gtk::Expander> {
let gtk_widget = gtk::Expander::new(None);

match bargs.widget_use.children.len().cmp(&1) {
Ordering::Less => {
return Err(DiagError(gen_diagnostic!("expander must contain exactly one element", bargs.widget_use.span)).into());
}
Ordering::Greater => {
let (_, additional_children) = bargs.widget_use.children.split_at(1);
// we know that there is more than one child, so unwrapping on first and last here is fine.
let first_span = additional_children.first().unwrap().span();
let last_span = additional_children.last().unwrap().span();
return Err(DiagError(gen_diagnostic!(
"expander must contain exactly one element, but got more",
first_span.to(last_span)
))
.into());
}
Ordering::Equal => {
let mut 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(),
)
});
// we have exactly one child, we can unwrap
let child = children.next().unwrap()?;
gtk_widget.add(&child);
child.show();
}
}

def_widget!(bargs, _g, gtk_widget, {
// @prop name - name of the expander
prop(name: as_string) {gtk_widget.set_label(Some(&name));},
prop(name: as_string) { gtk_widget.set_label(Some(&name)); },
// @prop expanded - sets if the tree is expanded
prop(expanded: as_bool) { gtk_widget.set_expanded(expanded); }
});

Ok(gtk_widget)
}

Expand Down

0 comments on commit f01396f

Please sign in to comment.