fix(segmented_button): crash when enumerating context menu buttons

Cherry-pick of upstream fix (pop-os/libcosmic#1354).

The .enumerate().find_map() pattern counted ALL items (buttons +
dividers) but the resulting index was used to access a children
array containing only button entries. When a divider preceded the
target button (e.g. trash in nav bar), the index was off-by-N,
causing an out-of-bounds panic and crashing cosmic-files.

Fix: filter_map first to keep only buttons, then enumerate.
This commit is contained in:
Lionel DARNIS 2026-07-25 22:20:01 +02:00
parent 3e4ab4576d
commit 28f7ab4651

View file

@ -981,11 +981,12 @@ where
let Some((mut bounds, i)) = self let Some((mut bounds, i)) = self
.variant_bounds(state, layout.bounds()) .variant_bounds(state, layout.bounds())
.enumerate() .filter_map(|item| match item {
.find_map(|(i, item)| match item { ItemBounds::Button(entity, bounds) => Some((bounds, entity)),
ItemBounds::Button(e, bounds) if e == entity => Some((bounds, i)),
_ => None, _ => None,
}) })
.enumerate()
.find_map(|(i, (bounds, e))| if e == entity { Some((bounds, i)) } else { None })
else { else {
return; return;
}; };
@ -2581,11 +2582,12 @@ where
let (mut bounds, i) = self let (mut bounds, i) = self
.variant_bounds(state, layout.bounds()) .variant_bounds(state, layout.bounds())
.enumerate() .filter_map(|item| match item {
.find_map(|(i, item)| match item { ItemBounds::Button(entity, bounds) => Some((bounds, entity)),
ItemBounds::Button(e, bounds) if e == entity => Some((bounds, i)),
_ => None, _ => None,
})?; })
.enumerate()
.find_map(|(i, (bounds, e))| if e == entity { Some((bounds, i)) } else { None })?;
assert!( assert!(
self.context_menu self.context_menu