use gpui::{
AnyElement, App, ElementId, Entity, FontWeight, InteractiveElement as _, IntoElement,
ParentElement as _, RenderOnce, SharedString, StyleRefinement, Styled, Window, div,
prelude::FluentBuilder as _, relative,
};
use gpui_deck::{
Collapsible, CollapsibleContent, CollapsibleState, CollapsibleTrigger, TestSupportExt as _,
TestTextExt as _,
};
use super::{super::Palette, Paragraph};
#[derive(IntoElement)]
pub(crate) struct Trace {
base: Collapsible,
id: ElementId,
state: Entity<CollapsibleState>,
summary: SharedString,
details_label: SharedString,
detail_text: SharedString,
action: Option<AnyElement>,
palette: Palette,
}
impl Trace {
pub(crate) fn new(
id: impl Into<ElementId>,
state: &Entity<CollapsibleState>,
summary: impl Into<SharedString>,
details_label: impl Into<SharedString>,
detail_text: impl Into<SharedString>,
palette: Palette,
) -> Self {
let id = id.into();
Self {
base: Collapsible::new(id.clone(), state)
.w_full()
.min_w_0()
.flex_shrink_0()
.flex()
.flex_col()
.border_1()
.border_color(palette.border)
.rounded(palette.radius)
.bg(palette.field)
.text_size(palette.font_size)
.text_color(palette.foreground),
id,
state: state.clone(),
summary: summary.into(),
details_label: details_label.into(),
detail_text: detail_text.into(),
action: None,
palette,
}
}
pub(crate) fn action(mut self, action: impl IntoElement) -> Self {
self.action = Some(action.into_any_element());
self
}
}
impl Styled for Trace {
fn style(&mut self) -> &mut StyleRefinement {
self.base.style()
}
}
impl RenderOnce for Trace {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
let state = self.state.read(cx);
let indicator: SharedString = if state.is_open() { "−" } else { "+" }.into();
let trigger = CollapsibleTrigger::new(&self.state)
.accessibility_label(self.summary.clone())
.w_full()
.min_w_0()
.flex_shrink_0()
.items_start()
.justify_start()
.gap_2()
.p_3()
.border_1()
.border_color(self.palette.field)
.rounded(self.palette.radius)
.font_weight(FontWeight::SEMIBOLD)
.line_height(relative(1.5))
.hover(|style| style.bg(self.palette.highlight))
.focus_visible(|style| style.border_color(self.palette.accent))
.when(state.is_disabled(), |trigger| trigger.opacity(0.5))
.child(
div()
.id((self.id.clone(), "summary"))
.test_support()
.flex_1()
.min_w_0()
.whitespace_normal()
.text_left()
.child(self.summary.clone().test_text(self.summary)),
)
.child(
div()
.id((self.id.clone(), "indicator"))
.test_support()
.flex_shrink_0()
.child(indicator.clone().test_text(indicator)),
);
let content = CollapsibleContent::new(&self.state)
.accessibility_label(self.details_label)
.w_full()
.min_w_0()
.flex_shrink_0()
.flex()
.flex_col()
.gap_2()
.px_3()
.pb_3()
.child(Paragraph::new((self.id.clone(), "body"), self.palette).text(self.detail_text))
.when_some(self.action, |content, action| {
content.child(
div()
.id((self.id, "action"))
.test_support()
.w_full()
.min_w_0()
.flex()
.flex_wrap()
.gap_2()
.child(action),
)
});
self.base.child(trigger).child(content)
}
}
#[cfg(all(test, feature = "test-support"))]
mod tests {
use gpui::{
AppContext as _, Context, Pixels, Render, Role, Subscription, TestAppContext,
VisualTestContext, px, size,
};
use gpui_deck::{Root, test_support};
use super::super::super::Appearance;
use super::*;
struct Sample {
appearance: Appearance,
width: Pixels,
state: Entity<CollapsibleState>,
summary: SharedString,
details: SharedString,
_subscription: Subscription,
}
impl Render for Sample {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().w(self.width).flex().flex_col().child(Trace::new(
"sample-trace",
&self.state,
self.summary.clone(),
"Configuration details",
self.details.clone(),
self.appearance.palette(),
))
}
}
fn harness(
cx: &mut TestAppContext,
appearance: Appearance,
) -> (&mut VisualTestContext, Entity<Sample>) {
let (root, cx) = cx.add_window_view(|window, cx| {
let sample = cx.new(|cx| {
let state = cx.new(CollapsibleState::new);
let subscription = cx.observe(&state, |_, _, cx| cx.notify());
Sample {
appearance,
width: px(360.),
state,
summary: "Inspect configuration".into(),
details: "The current session has a retained note.".into(),
_subscription: subscription,
}
});
Root::new(sample, window, cx)
});
let sample = root
.read_with(cx, |root, _| root.view().clone())
.downcast::<Sample>()
.unwrap();
draw(cx);
(cx, sample)
}
fn draw(cx: &mut VisualTestContext) {
cx.update(|window, cx| window.draw(cx).clear(cx));
}
fn part_ids(sample: &Entity<Sample>, cx: &VisualTestContext) -> (ElementId, ElementId) {
sample.read_with(cx, |sample, _| {
(
("collapsible-trigger", sample.state.entity_id()).into(),
("collapsible-content", sample.state.entity_id()).into(),
)
})
}
#[gpui::test]
fn trace_summary_and_details_wrap_with_disclosure_semantics(cx: &mut TestAppContext) {
cx.update(gpui_deck::init);
let summary = format!(
"설정 상세 보기 {} {}",
"공백없는긴한글요약".repeat(4),
"unbroken_english_summary_0123456789".repeat(3),
);
let details = format!(
"한국어 상세 내용입니다.\nEnglish details preserve newlines.\n\n{} {}\n",
"공백없는한글상세내용".repeat(8),
"unbroken_english_detail_0123456789".repeat(5),
);
let id = ElementId::from("sample-trace");
for appearance in [Appearance::Compact, Appearance::Settings] {
let (cx, sample) = harness(cx, appearance);
cx.simulate_resize(size(px(500.), px(2500.)));
sample.update(cx, |sample, cx| {
sample.summary = summary.clone().into();
sample.details = details.clone().into();
cx.notify();
});
cx.update(|window, cx| {
let state = sample.read(cx).state.clone();
state.update(cx, |state, cx| state.set_open(true, window, cx));
});
let (trigger_id, content_id) = part_ids(&sample, cx);
let mut wide_height = None;
for width in [360., 160., 360.] {
sample.update(cx, |sample, cx| {
sample.width = px(width);
cx.notify();
});
draw(cx);
cx.run_until_parked();
draw(cx);
cx.update(|window, _| {
let trace = test_support::find(window, &[], &id).unwrap();
assert_eq!(trace.role(), Some(Role::Group));
assert_eq!(trace.label(), None);
assert!(trace.bounds().size.width <= px(width) + px(1.));
let trigger = test_support::find(window, &[], &trigger_id).unwrap();
assert_eq!(trigger.role(), Some(Role::Button));
assert_eq!(trigger.label(), Some(summary.as_str()));
assert_eq!(trigger.expanded(), Some(true));
let content = test_support::find(window, &[], &content_id).unwrap();
assert_eq!(content.role(), Some(Role::Region));
assert_eq!(content.label(), Some("Configuration details"));
assert!(content.bounds().top() >= trigger.bounds().bottom());
for node in [&trigger, &content] {
assert!(node.bounds().left() >= trace.bounds().left());
assert!(node.bounds().right() <= trace.bounds().right() + px(1.));
}
for (part, expected, role) in [
("summary", summary.as_str(), None),
("indicator", "−", None),
("body", details.as_str(), Some(Role::Paragraph)),
] {
let part_id = (id.clone(), part).into();
let node = test_support::find(window, &[], &part_id).unwrap();
assert_eq!(node.role(), role);
assert_eq!(node.label(), role.map(|_| expected));
assert_eq!(node.focused(), None);
assert!(node.bounds().left() >= trace.bounds().left());
assert!(node.bounds().right() <= trace.bounds().right() + px(1.));
assert!(node.bounds().bottom() <= trace.bounds().bottom() + px(1.));
let texts = test_support::texts(window);
let text = texts
.iter()
.find(|text| text.path().contains(&part_id))
.unwrap();
assert_eq!(text.text(), expected);
assert!(text.visible());
assert!(text.bounds().size.width <= node.bounds().size.width + px(1.));
}
let named: Vec<_> = test_support::snapshots(window)
.into_iter()
.filter(|node| node.path().contains(&id) && node.label().is_some())
.collect();
assert_eq!(named.len(), 3);
assert!(
test_support::find(window, &[], &(id.clone(), "action").into()).is_none()
);
let height = trace.bounds().size.height;
match wide_height {
None => wide_height = Some(height),
Some(wide) if width == 360. => assert_eq!(height, wide),
Some(wide) => assert!(height > wide),
}
});
}
cx.update(|window, cx| {
let state = sample.read(cx).state.clone();
state.update(cx, |state, cx| state.set_open(false, window, cx));
});
draw(cx);
cx.update(|window, _| {
let trigger = test_support::find(window, &[], &trigger_id).unwrap();
assert_eq!(trigger.expanded(), Some(false));
assert!(test_support::find(window, &[], &content_id).is_none());
assert!(test_support::find(window, &[], &(id.clone(), "body").into()).is_none());
});
}
}
}