chore: restructure ui components
Rust / build (macos-latest, stable) (push) Canceled after 0s
Rust / build (ubuntu-latest, stable) (push) Canceled after 0s
Rust / build (windows-latest, stable) (push) Canceled after 0s

This commit is contained in:
2026-11-27 21:05:37 +07:00
parent db3d53f2aa
commit 44c2dc6345
114 changed files with 9117 additions and 7675 deletions
+112
View File
@@ -0,0 +1,112 @@
//! One Escape closes a dialog even when a text field inside it has focus.
use gpui::{
Context, IntoElement, Modifiers, ParentElement as _, Render, Styled as _, TestAppContext,
VisualTestContext, Window, div, px, size,
};
use ui::common::{ResultLogErr as _, v_flex};
use ui::dialog::{Dialog, DialogHandle};
use ui::input::{Input, InputState};
use ui::theme::Theme;
const WINDOW_WIDTH: f32 = 800.0;
const WINDOW_HEIGHT: f32 = 600.0;
/// A dialog with a text field and an action, with its open state held by the view.
struct DialogHarness {
handle: DialogHandle,
input: gpui::Entity<InputState>,
closes: Vec<bool>,
}
impl Render for DialogHarness {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let input = self.input.clone();
let this = cx.weak_entity();
v_flex().size_full().p_4().child(
Dialog::new("escape-dialog")
.handle(self.handle.clone())
.title("Escape closes")
.on_change(move |open, _window, cx| {
this.update(cx, |harness, _| harness.closes.push(open))
.log_err();
})
.content(move |_, _| {
div()
.p_2()
.child(Input::new("dialog-field", input.clone()))
.into_any_element()
}),
)
}
}
#[gpui::test]
fn one_escape_closes_a_dialog_with_a_focused_field(cx: &mut TestAppContext) {
cx.update(|cx| {
Theme::init_with(cx, Theme::fallback_source());
Dialog::bind_keys(cx);
});
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, cx| {
DialogHarness {
handle: DialogHandle::new(true),
input: InputState::new(cx),
closes: Vec::new(),
}
});
let mut cx = VisualTestContext::from_window(window.into(), cx);
cx.run_until_parked();
let harness = window.root(&mut cx).expect("harness view");
cx.update(|window, cx| window.focus(&harness.read(cx).input.read(cx).focus_handle(), cx));
cx.update(|_, _| {});
cx.run_until_parked();
cx.simulate_keystrokes("escape");
cx.run_until_parked();
assert_eq!(
harness.read_with(&cx, |harness, _| harness.closes.clone()),
vec![false],
"one escape press reports a single close to the dialog's owner"
);
}
#[gpui::test]
fn tab_keeps_focus_inside_the_dialog(cx: &mut TestAppContext) {
cx.update(|cx| {
Theme::init_with(cx, Theme::fallback_source());
Dialog::bind_keys(cx);
});
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, cx| {
DialogHarness {
handle: DialogHandle::new(true),
input: InputState::new(cx),
closes: Vec::new(),
}
});
let mut cx = VisualTestContext::from_window(window.into(), cx);
cx.run_until_parked();
let harness = window.root(&mut cx).expect("harness view");
let field = cx.update(|_, cx| harness.read(cx).input.read(cx).focus_handle());
cx.update(|window, cx| window.focus(&field, cx));
cx.update(|_, _| {});
cx.run_until_parked();
cx.simulate_keystrokes("tab");
cx.run_until_parked();
let contained = cx.update(|window, cx| {
let dialog = window.focused(cx).expect("focus stays in the window");
dialog.contains_focused(window, cx) || dialog == field
});
assert!(contained, "tab does not move focus behind the dialog");
assert!(
cx.debug_bounds("escape-dialog").is_some()
|| harness.read_with(&cx, |h, _| h.closes.is_empty()),
"the dialog stays open"
);
let _ = Modifiers::default();
}
+64
View File
@@ -0,0 +1,64 @@
//! Ending a composition repaints the field, so its underline disappears.
use std::cell::Cell;
use std::rc::Rc;
use gpui::{
EntityInputHandler, Modifiers, Styled as _, TestAppContext, VisualTestContext, px, size,
};
use ui::input::InputState;
use ui::theme::Theme;
struct EmptyView;
impl gpui::Render for EmptyView {
fn render(
&mut self,
_window: &mut gpui::Window,
_cx: &mut gpui::Context<Self>,
) -> impl gpui::IntoElement {
gpui::div().size_full()
}
}
#[gpui::test]
fn unmarking_a_composition_notifies_the_field(cx: &mut TestAppContext) {
cx.update(|cx| Theme::init_with(cx, Theme::fallback_source()));
let window = cx.open_window(size(px(400.), px(100.)), |_, _| EmptyView);
let mut cx = VisualTestContext::from_window(window.into(), cx);
cx.run_until_parked();
let state = cx.update(|_, cx| InputState::new(cx));
let notifies = Rc::new(Cell::new(0));
let counted = notifies.clone();
let _subscription =
cx.update(|_, cx| cx.observe(&state, move |_, _| counted.set(counted.get() + 1)));
cx.update(|window, cx| {
state.update(cx, |state, cx| {
EntityInputHandler::replace_and_mark_text_in_range(
state,
None,
"ab",
Some(0..2),
window,
cx,
);
});
});
cx.run_until_parked();
let marked = notifies.get();
cx.update(|window, cx| {
state.update(cx, |state, cx| {
EntityInputHandler::unmark_text(state, window, cx);
});
});
cx.run_until_parked();
assert!(
notifies.get() > marked,
"ending the composition repaints the field"
);
let _ = Modifiers::default();
}
+106
View File
@@ -0,0 +1,106 @@
//! Menu panels render only their visible rows and dispatch navigation keys as actions.
use gpui::{
Context, InteractiveElement as _, IntoElement, ParentElement as _, Render, Styled as _,
TestAppContext, VisualTestContext, Window, div, px, size,
};
use ui::common::{ResultLogErr as _, v_flex};
use ui::menu::{Menu, MenuItem};
use ui::theme::Theme;
const WINDOW_WIDTH: f32 = 800.0;
const WINDOW_HEIGHT: f32 = 1000.0;
const ROWS: usize = 300;
/// A menu of `ROWS` action rows, with its open state held by the view.
struct MenuHarness {
open: bool,
activated: Vec<usize>,
changes: Vec<bool>,
}
impl Render for MenuHarness {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let weak = cx.weak_entity();
v_flex().size_full().p_4().child(
Menu::new("m")
.open(self.open)
.on_change(cx.processor(|this, open: bool, _, cx| {
this.open = open;
this.changes.push(open);
cx.notify();
}))
.trigger(|_, _| {
div()
.h(px(24.))
.w(px(120.))
.debug_selector(|| "m-trigger".to_string())
.into_any_element()
})
.items(move |_, _| {
(0..ROWS)
.map(|index| {
let activated = weak.clone();
MenuItem::new(format!("Item {index}")).handler(move |_, cx| {
activated
.update(cx, |harness, cx| {
harness.activated.push(index);
cx.notify();
})
.log_err();
})
})
.collect()
}),
)
}
}
/// Opens the harness's menu with its recordings cleared, and parks the frame.
fn open(cx: &mut VisualTestContext, harness: &gpui::Entity<MenuHarness>) {
harness.update(cx, |harness, cx| {
harness.open = true;
harness.activated.clear();
harness.changes.clear();
cx.notify();
});
// Draw once so the panel's paint-time focus grab runs before the keys arrive.
cx.update(|_, _| {});
cx.run_until_parked();
// Draw twice: the first paint defers the focus grab, the second registers the
// panel's focus path so dispatched keys reach it.
cx.update(|_, _| {});
cx.run_until_parked();
cx.update(|_, _| {});
cx.run_until_parked();
}
#[gpui::test]
fn menu_rows_are_virtualized(cx: &mut TestAppContext) {
cx.update(|cx| {
Theme::init_with(cx, Theme::fallback_source());
Menu::bind_keys(cx);
});
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, _| {
MenuHarness {
open: false,
activated: Vec::new(),
changes: Vec::new(),
}
});
let mut cx = VisualTestContext::from_window(window.into(), cx);
cx.run_until_parked();
let harness = window.root(&mut cx).expect("harness view");
open(&mut cx, &harness);
assert!(
cx.debug_bounds("menu-item-0").is_some(),
"the first row is on screen"
);
assert!(
cx.debug_bounds("menu-item-299").is_none(),
"rows beyond the panel's height are never laid out"
);
}
+76 -76
View File
@@ -94,6 +94,78 @@ impl Panel for FieldView {
}
}
struct MetaView {
loading: bool,
prefix: bool,
}
impl Render for MetaView {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div().size_full()
}
}
impl Panel for MetaView {
fn title(&self, _cx: &App) -> SharedString {
SharedString::from("Meta")
}
fn prefix(&self, _cx: &App) -> Option<AnyElement> {
self.prefix.then(|| {
div()
.debug_selector(|| "meta-prefix".to_string())
.into_any_element()
})
}
fn loading(&self, _cx: &App) -> bool {
self.loading
}
}
struct MetaStripHarness {
busy: Entity<MetaView>,
idle: Entity<MetaView>,
prefixed: Entity<MetaView>,
}
impl Render for MetaStripHarness {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
let busy = self.busy.clone();
let idle = self.idle.clone();
let prefixed = self.prefixed.clone();
v_flex()
.size_full()
.gap_1()
.child(
Tabs::new("busy-strip")
.tabs(move |_, cx| vec![busy.tab("busy".into(), cx).closeable(false)]),
)
.child(
Tabs::new("idle-strip")
.tabs(move |_, cx| vec![idle.tab("idle".into(), cx).closeable(false)]),
)
.child(
Tabs::new("prefix-strip")
.tabs(move |_, cx| vec![prefixed.tab("prefix".into(), cx).closeable(false)]),
)
}
}
/// The titlebar strip above the stack.
struct TabsHarness {
stack: Entity<PanelStack>,
}
impl Render for TabsHarness {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
v_flex()
.size_full()
.children(PanelStack::main_tabs(self.stack.clone(), cx))
.child(self.stack.clone())
}
}
/// A marker view.
fn tag(cx: &mut App, selector: &'static str) -> Entity<TagView> {
cx.new(|_| TagView::new(selector))
@@ -110,7 +182,7 @@ fn stack_window(
seed: impl FnOnce(&mut PanelStack, &mut Context<PanelStack>),
) -> (VisualTestContext, Entity<PanelStack>) {
let stack = cx.update(|cx| {
Theme::init(cx);
Theme::init_with(cx, Theme::fallback_source());
let stack = cx.new(|cx| PanelStack::new("panel-harness", cx));
stack.update(cx, seed);
stack
@@ -294,67 +366,9 @@ fn dragging_the_divider_resizes_the_panels(cx: &mut TestAppContext) {
assert_eq!(after.size.width, before.size.width + px(100.0));
}
struct MetaView {
loading: bool,
prefix: bool,
}
impl Render for MetaView {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div().size_full()
}
}
impl Panel for MetaView {
fn title(&self, _cx: &App) -> SharedString {
SharedString::from("Meta")
}
fn prefix(&self, _cx: &App) -> Option<AnyElement> {
self.prefix.then(|| {
div()
.debug_selector(|| "meta-prefix".to_string())
.into_any_element()
})
}
fn loading(&self, _cx: &App) -> bool {
self.loading
}
}
struct MetaStripHarness {
busy: Entity<MetaView>,
idle: Entity<MetaView>,
prefixed: Entity<MetaView>,
}
impl Render for MetaStripHarness {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
let busy = self.busy.clone();
let idle = self.idle.clone();
let prefixed = self.prefixed.clone();
v_flex()
.size_full()
.gap_1()
.child(
Tabs::new("busy-strip")
.tabs(move |_, cx| vec![busy.tab("busy".into(), cx).closeable(false)]),
)
.child(
Tabs::new("idle-strip")
.tabs(move |_, cx| vec![idle.tab("idle".into(), cx).closeable(false)]),
)
.child(
Tabs::new("prefix-strip")
.tabs(move |_, cx| vec![prefixed.tab("prefix".into(), cx).closeable(false)]),
)
}
}
#[gpui::test]
fn panel_tab_uses_title_prefix_and_loading(cx: &mut TestAppContext) {
cx.update(Theme::init);
cx.update(|cx| Theme::init_with(cx, Theme::fallback_source()));
let busy = cx.new(|_| MetaView {
loading: true,
prefix: false,
@@ -513,7 +527,7 @@ fn a_reopened_split_starts_at_half_again(cx: &mut TestAppContext) {
stack.update(cx, |stack, cx| {
stack.insert(
PanelSlot::Split,
split.clone(),
split,
Arc::new(tag(cx, "panel-test-split")),
cx,
)
@@ -532,25 +546,11 @@ fn a_reopened_split_starts_at_half_again(cx: &mut TestAppContext) {
);
}
/// The titlebar strip above the stack.
struct TabsHarness {
stack: Entity<PanelStack>,
}
impl Render for TabsHarness {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
v_flex()
.size_full()
.children(PanelStack::main_tabs(self.stack.clone(), cx))
.child(self.stack.clone())
}
}
/// The titlebar strip lists the main stack; its tabs switch and close panels.
#[gpui::test]
fn main_tabs_switch_and_close_the_main_stack(cx: &mut TestAppContext) {
let stack = cx.update(|cx| {
Theme::init(cx);
Theme::init_with(cx, Theme::fallback_source());
cx.new(|cx| PanelStack::new("panel-harness", cx))
});
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, _| {
+5 -1
View File
@@ -5,6 +5,7 @@ use gpui::{
Render, Styled as _, TestAppContext, VisualTestContext, Window, div, px, size,
};
use ui::common::v_flex;
use ui::dialog::Dialog;
use ui::popover::Popover;
use ui::theme::Theme;
@@ -60,7 +61,10 @@ fn focus_state(cx: &mut VisualTestContext) -> Option<(FocusHandle, bool)> {
#[gpui::test]
fn closing_a_popover_restores_the_previous_focus(cx: &mut TestAppContext) {
cx.update(Theme::init);
cx.update(|cx| {
Theme::init_with(cx, Theme::fallback_source());
Dialog::bind_keys(cx);
});
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, cx| {
PopoverHarness {
open: false,
+1 -1
View File
@@ -58,7 +58,7 @@ fn press_track(cx: &mut VisualTestContext) {
#[gpui::test]
fn a_covering_overlay_blocks_scrollbar_drags(cx: &mut TestAppContext) {
cx.update(Theme::init);
cx.update(|cx| Theme::init_with(cx, Theme::fallback_source()));
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, _| {
ScrollHarness {
handle: ScrollHandle::new(),
+81
View File
@@ -0,0 +1,81 @@
//! A hidden auto-hide scrollbar repaints its owner as soon as the pointer
//! reaches its track.
use std::cell::Cell;
use std::rc::Rc;
use gpui::{
Context, InteractiveElement as _, IntoElement, Modifiers, ParentElement as _, Render,
ScrollHandle, StatefulInteractiveElement as _, Styled as _, TestAppContext, VisualTestContext,
Window, div, point, px, size,
};
use ui::scrollbar::{Scrollbar, ScrollbarVisibility};
use ui::theme::Theme;
const WINDOW_WIDTH: f32 = 800.0;
const WINDOW_HEIGHT: f32 = 600.0;
const ROW_HEIGHT: f32 = 40.0;
const ROWS: usize = 50;
/// A scrolled surface with an auto-hide scrollbar overlaid on its right edge.
struct ScrollbarHarness {
handle: ScrollHandle,
renders: Rc<Cell<usize>>,
}
impl Render for ScrollbarHarness {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
self.renders.set(self.renders.get() + 1);
div()
.relative()
.size_full()
.child(
div()
.id("log")
.size_full()
.overflow_y_scroll()
.track_scroll(&self.handle)
.children(
(0..ROWS).map(|row| {
div().w_full().h(px(ROW_HEIGHT)).child(format!("row {row}"))
}),
),
)
.child(
Scrollbar::vertical("log-bar", self.handle.clone())
.visibility(ScrollbarVisibility::Autohide),
)
}
}
#[gpui::test]
fn hovering_a_hidden_scrollbar_repaints_its_owner(cx: &mut TestAppContext) {
cx.update(|cx| Theme::init_with(cx, Theme::fallback_source()));
let renders = Rc::new(Cell::new(0));
let harness = {
let renders = renders.clone();
move |_: &mut Window, _: &mut Context<ScrollbarHarness>| ScrollbarHarness {
handle: ScrollHandle::new(),
renders,
}
};
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), harness);
let mut cx = VisualTestContext::from_window(window.into(), cx);
cx.run_until_parked();
renders.set(0);
// A scrollbar that has never been touched is hidden; reaching its track must
// repaint the owner so the thumb can appear.
cx.simulate_mouse_move(
point(px(WINDOW_WIDTH - 2.), px(WINDOW_HEIGHT / 2.0)),
None,
Modifiers::default(),
);
cx.update(|_, _| {});
cx.run_until_parked();
assert!(
renders.get() > 0,
"reaching the track of a hidden scrollbar repaints its owner"
);
}
+91
View File
@@ -0,0 +1,91 @@
//! An outside click closes an open Select and leaves focus on the click target.
use gpui::{
Context, FocusHandle, InteractiveElement as _, IntoElement, Modifiers, ParentElement as _,
Render, Styled as _, TestAppContext, VisualTestContext, Window, div, px, size,
};
use ui::common::v_flex;
use ui::select::{Select, SelectOption};
use ui::theme::Theme;
const WINDOW_WIDTH: f32 = 800.0;
const WINDOW_HEIGHT: f32 = 1000.0;
/// A select above a focusable element, so the open panel never covers the target.
struct SelectHarness {
value: Option<String>,
outside_focus: FocusHandle,
}
impl Render for SelectHarness {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let outside_focus = self.outside_focus.clone();
v_flex()
.size_full()
.p_4()
.child(
Select::new("s")
.options(
["alpha", "beta"].map(|value| SelectOption::new(value.to_string(), value)),
)
.placeholder("Pick")
.on_change(cx.processor(|this, value: Option<String>, _, cx| {
this.value = value.clone();
cx.notify();
})),
)
.child(div().h(px(500.)))
.child(
div()
.id("outside")
.track_focus(&outside_focus)
.debug_selector(|| "outside".to_string())
.h(px(40.))
.w(px(200.)),
)
}
}
#[gpui::test]
fn outside_click_leaves_focus_on_the_target(cx: &mut TestAppContext) {
cx.update(|cx| Theme::init_with(cx, Theme::fallback_source()));
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, cx| {
SelectHarness {
value: None,
outside_focus: cx.focus_handle(),
}
});
let mut cx = VisualTestContext::from_window(window.into(), cx);
cx.run_until_parked();
let harness = window.root(&mut cx).expect("harness view");
let outside_focus = cx.update(|_, cx| harness.read(cx).outside_focus.clone());
let trigger = cx.debug_bounds("select-trigger").expect("trigger laid out");
cx.simulate_click(trigger.center(), Modifiers::default());
cx.run_until_parked();
assert!(
cx.debug_bounds("menu-panel").is_some(),
"clicking the trigger opens the options"
);
let outside = cx.debug_bounds("outside").expect("target laid out");
cx.simulate_click(outside.center(), Modifiers::default());
cx.run_until_parked();
assert!(
cx.debug_bounds("menu-panel").is_none(),
"the outside click closes the options"
);
assert_eq!(
cx.update(|window, cx| window.focused(cx)),
Some(outside_focus),
"focus stays on the clicked element, not the trigger"
);
assert_eq!(
harness.read_with(&cx, |harness, _| harness.value.clone()),
None,
"an outside click chooses nothing"
);
}
+1 -1
View File
@@ -59,7 +59,7 @@ impl Render for Harness {
#[gpui::test]
fn buttons_render_at_their_token_heights(cx: &mut TestAppContext) {
cx.update(Theme::init);
cx.update(|cx| Theme::init_with(cx, Theme::fallback_source()));
let window = cx.open_window(size(px(400.), px(400.)), |_, _| Harness);
let mut cx = VisualTestContext::from_window(window.into(), cx);
cx.run_until_parked();
+17 -18
View File
@@ -45,23 +45,6 @@ impl Render for StripHarness {
}
}
fn strip_window(
cx: &mut TestAppContext,
changed: Rc<RefCell<Vec<usize>>>,
reordered: Rc<RefCell<Vec<(usize, usize)>>>,
pinned: bool,
) -> VisualTestContext {
cx.update(Theme::init);
let window = cx.open_window(size(px(800.), px(600.)), |_, _| StripHarness {
changed,
reordered,
pinned,
});
let cx = VisualTestContext::from_window(window.into(), cx);
cx.run_until_parked();
cx
}
struct CloseHarness {
closed: Rc<RefCell<Vec<usize>>>,
}
@@ -82,6 +65,22 @@ impl Render for CloseHarness {
)
}
}
fn strip_window(
cx: &mut TestAppContext,
changed: Rc<RefCell<Vec<usize>>>,
reordered: Rc<RefCell<Vec<(usize, usize)>>>,
pinned: bool,
) -> VisualTestContext {
cx.update(|cx| Theme::init_with(cx, Theme::fallback_source()));
let window = cx.open_window(size(px(800.), px(600.)), |_, _| StripHarness {
changed,
reordered,
pinned,
});
let cx = VisualTestContext::from_window(window.into(), cx);
cx.run_until_parked();
cx
}
#[gpui::test]
fn a_pinned_tab_draws_inline_and_selects_itself(cx: &mut TestAppContext) {
@@ -103,7 +102,7 @@ fn a_pinned_tab_draws_inline_and_selects_itself(cx: &mut TestAppContext) {
#[gpui::test]
fn a_closeable_tab_reports_its_close(cx: &mut TestAppContext) {
cx.update(Theme::init);
cx.update(|cx| Theme::init_with(cx, Theme::fallback_source()));
let closed = Rc::new(RefCell::new(Vec::new()));
let window = cx.open_window(size(px(800.), px(600.)), |_, _| CloseHarness {
closed: closed.clone(),
+15
View File
@@ -0,0 +1,15 @@
use ui::theme::ThemeDefinition;
#[test]
fn parse_hex_rejects_non_ascii_digits() {
assert_eq!(ThemeDefinition::parse_hex("#éa"), None);
assert_eq!(ThemeDefinition::parse_hex("#éaabbcc"), None);
}
#[test]
fn parse_hex_resolves_three_and_six_digits() {
assert!(ThemeDefinition::parse_hex("#abc").is_some());
assert!(ThemeDefinition::parse_hex("#aabbcc").is_some());
assert_eq!(ThemeDefinition::parse_hex("abc"), None);
assert_eq!(ThemeDefinition::parse_hex("#ab"), None);
}
+69
View File
@@ -0,0 +1,69 @@
//! Flattening a path tree honors expansion and nests children in order.
use std::collections::HashSet;
use std::path::PathBuf;
use ui::tree::{TreeItem, TreeItemSeed};
fn ids(rows: &[TreeItem]) -> Vec<(String, usize)> {
rows.iter()
.map(|row| (row.key().to_string(), row.level()))
.collect()
}
#[test]
fn collapsed_folders_hide_children() {
let seeds = TreeItemSeed::from_paths(&[
PathBuf::from("src"),
PathBuf::from("src/lib.rs"),
PathBuf::from("README.md"),
]);
let rows = TreeItemSeed::flatten_all(&seeds, &HashSet::new());
assert_eq!(
ids(&rows),
vec![("src".to_owned(), 0), ("README.md".to_owned(), 0)]
);
assert!(rows[0].is_expandable());
assert!(!rows[0].is_expanded());
}
#[test]
fn expanding_a_folder_shows_children_in_order() {
let seeds = TreeItemSeed::from_paths(&[
PathBuf::from("src"),
PathBuf::from("src/lib.rs"),
PathBuf::from("src/views/mod.rs"),
PathBuf::from("README.md"),
]);
let expanded = HashSet::from(["src".to_owned(), "src/views".to_owned()]);
let rows = TreeItemSeed::flatten_all(&seeds, &expanded);
assert_eq!(
ids(&rows),
vec![
("src".to_owned(), 0),
("src/lib.rs".to_owned(), 1),
("src/views".to_owned(), 1),
("src/views/mod.rs".to_owned(), 2),
("README.md".to_owned(), 0),
]
);
}
#[test]
fn indices_map_to_row_ids() {
let seeds = TreeItemSeed::from_paths(&[
PathBuf::from("docs"),
PathBuf::from("docs/guide.md"),
PathBuf::from("README.md"),
]);
let expanded = HashSet::from(["docs".to_owned()]);
let rows = TreeItemSeed::flatten_all(&seeds, &expanded);
assert_eq!(rows[0].key().to_string(), "docs");
assert_eq!(rows[1].key().to_string(), "docs/guide.md");
assert_eq!(rows[2].key().to_string(), "README.md");
}