Panics · P05 Buttons: a segmented choice missing from its segments marks none; slide logs and shows no marker
This commit is contained in:
1 parent
9bb342cb16
commit
73a8bf2e34
4 files changed
+40
-13
No files matched your search
@@ -69,10 +69,7 @@ impl RenderOnce for SegmentedControl {
|
||||
.iter()
|
||||
.map(|(value, ..)| value.clone())
|
||||
.collect();
|
||||
let chosen = values
|
||||
.iter()
|
||||
.position(|value| *value == self.selected)
|
||||
.unwrap_or_else(|| panic!("segmented control has no segment {}", self.selected));
|
||||
let chosen = values.iter().position(|value| *value == self.selected);
|
||||
let (state, marker) = slide(self.id.clone(), &values, &self.selected, window, cx);
|
||||
let scroll = window
|
||||
.use_keyed_state((self.id.clone(), "scroll"), cx, |_, _| ScrollHandle::new())
|
||||
@@ -133,7 +130,7 @@ impl RenderOnce for SegmentedControl {
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(ix, (value, label, icon))| {
|
||||
let on = ix == chosen;
|
||||
let on = Some(ix) == chosen;
|
||||
let fg = if on { colors.fg } else { colors.fg_muted };
|
||||
let (focus, reveal) = stops.remove(0);
|
||||
let (named, labeled) = (value.clone(), value.clone());
|
||||
|
||||
+29
-1
@@ -3,7 +3,7 @@ use gpui::{
|
||||
Render, Styled, TestAppContext, VisualTestContext, Window, div, point, px,
|
||||
};
|
||||
|
||||
use super::{Button, ToggleButton, ToggleItem};
|
||||
use super::{Button, SegmentedControl, ToggleButton, ToggleItem};
|
||||
use crate::{
|
||||
primitives::{FocusNext, FocusScope},
|
||||
theme::Theme,
|
||||
@@ -98,3 +98,31 @@ fn a_disabled_toggle_ignores_clicks(cx: &mut TestAppContext) {
|
||||
cx.simulate_click(point(px(4.0), px(4.0)), Modifiers::none());
|
||||
assert_eq!(view.read_with(cx, |toggles, _| toggles.presses), 1);
|
||||
}
|
||||
|
||||
/// Segments whose chosen value left with a change of list.
|
||||
struct Stale;
|
||||
|
||||
impl Render for Stale {
|
||||
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
|
||||
div().w(px(300.0)).child(
|
||||
SegmentedControl::new("views", "gone")
|
||||
.segment("list", "List", None)
|
||||
.segment("grid", "Grid", None),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_choice_missing_from_its_segments_marks_none(cx: &mut TestAppContext) {
|
||||
cx.update(Theme::init);
|
||||
let (_, cx) = cx.add_window_view(|_, _| Stale);
|
||||
for _ in 0..3 {
|
||||
cx.run_until_parked();
|
||||
cx.update(|window, _| window.refresh());
|
||||
}
|
||||
assert!(cx.debug_bounds("segment list").is_some());
|
||||
assert!(
|
||||
cx.debug_bounds("segment-thumb").is_none(),
|
||||
"no thumb marks a choice"
|
||||
);
|
||||
}
|
||||
+7
-6
@@ -46,7 +46,7 @@ pub(crate) struct Marker {
|
||||
pub generation: u64,
|
||||
}
|
||||
|
||||
/// The slide over `values` with `selected` chosen; no marker until that item is measured.
|
||||
/// The slide over `values` with `selected` chosen; no marker until it is measured, or if it is not there.
|
||||
pub(crate) fn slide(
|
||||
id: impl Into<ElementId>,
|
||||
values: &[SharedString],
|
||||
@@ -54,14 +54,15 @@ pub(crate) fn slide(
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> (Entity<Slide>, Option<Marker>) {
|
||||
let chosen = values
|
||||
.iter()
|
||||
.position(|value| value == selected)
|
||||
.unwrap_or_else(|| panic!("no item {selected} to slide to"));
|
||||
let state = window.use_keyed_state(id, cx, |_, _| Slide::default());
|
||||
let id = id.into();
|
||||
let state = window.use_keyed_state(id.clone(), cx, |_, _| Slide::default());
|
||||
if state.read(cx).spans.len() != values.len() {
|
||||
state.update(cx, |slide, _| slide.spans = vec![None; values.len()]);
|
||||
}
|
||||
let Some(chosen) = values.iter().position(|value| value == selected) else {
|
||||
log::error!("slide {id:?}: no item {selected} among {values:?}; nothing marked");
|
||||
return (state, None);
|
||||
};
|
||||
if state.read(cx).last.as_ref() != Some(selected) {
|
||||
state.update(cx, |slide, _| {
|
||||
let previous = slide
|
||||
|
||||
Reference in new issue
Block a user