Skip to content

Commit f6f5ad1

Browse files
authored
project panel: Make intermediate folded directories clickable (#18956)
- Closes: #18770 Release Notes: - Intermediate auto-folded project panel entries are now clickable.
1 parent db50467 commit f6f5ad1

File tree

1 file changed

+52
-61
lines changed

1 file changed

+52
-61
lines changed

crates/project_panel/src/project_panel.rs

Lines changed: 52 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,71 +2434,62 @@ impl ProjectPanel {
24342434
if let (Some(editor), true) = (Some(&self.filename_editor), show_editor) {
24352435
h_flex().h_6().w_full().child(editor.clone())
24362436
} else {
2437-
h_flex().h_6().map(|this| {
2437+
h_flex().h_6().map(|mut this| {
24382438
if let Some(folded_ancestors) =
24392439
is_active.then(|| self.ancestors.get(&entry_id)).flatten()
24402440
{
2441-
let Some(part_to_highlight) = Path::new(&file_name)
2442-
.ancestors()
2443-
.nth(folded_ancestors.current_ancestor_depth)
2444-
else {
2445-
return this;
2446-
};
2447-
2448-
let suffix = Path::new(&file_name)
2449-
.strip_prefix(part_to_highlight)
2450-
.ok()
2451-
.filter(|suffix| !suffix.as_os_str().is_empty());
2452-
let prefix = part_to_highlight
2453-
.parent()
2454-
.filter(|prefix| !prefix.as_os_str().is_empty());
2455-
let Some(part_to_highlight) = part_to_highlight
2456-
.file_name()
2457-
.and_then(|name| name.to_str().map(String::from))
2458-
else {
2459-
return this;
2460-
};
2441+
let components = Path::new(&file_name)
2442+
.components()
2443+
.map(|comp| {
2444+
let comp_str =
2445+
comp.as_os_str().to_string_lossy().into_owned();
2446+
comp_str
2447+
})
2448+
.collect::<Vec<_>>();
2449+
let components_len = components.len();
2450+
let active_index = components_len
2451+
- 1
2452+
- folded_ancestors.current_ancestor_depth;
2453+
const DELIMITER: SharedString =
2454+
SharedString::new_static(std::path::MAIN_SEPARATOR_STR);
2455+
for (index, component) in components.into_iter().enumerate() {
2456+
if index != 0 {
2457+
this = this.child(
2458+
Label::new(DELIMITER.clone())
2459+
.single_line()
2460+
.color(filename_text_color),
2461+
);
2462+
}
2463+
let id = SharedString::from(format!(
2464+
"project_panel_path_component_{}_{index}",
2465+
entry_id.to_usize()
2466+
));
2467+
let label = div()
2468+
.id(id)
2469+
.on_click(cx.listener(move |this, _, cx| {
2470+
if index != active_index {
2471+
if let Some(folds) =
2472+
this.ancestors.get_mut(&entry_id)
2473+
{
2474+
folds.current_ancestor_depth =
2475+
components_len - 1 - index;
2476+
cx.notify();
2477+
}
2478+
}
2479+
}))
2480+
.child(
2481+
Label::new(component)
2482+
.single_line()
2483+
.color(filename_text_color)
2484+
.when(index == active_index, |this| {
2485+
this.underline(true)
2486+
}),
2487+
);
2488+
2489+
this = this.child(label);
2490+
}
24612491

2462-
this.children(prefix.and_then(|prefix| {
2463-
Some(
2464-
h_flex()
2465-
.child(
2466-
Label::new(prefix.to_str().map(String::from)?)
2467-
.single_line()
2468-
.color(filename_text_color),
2469-
)
2470-
.child(
2471-
Label::new(std::path::MAIN_SEPARATOR_STR)
2472-
.single_line()
2473-
.color(filename_text_color),
2474-
),
2475-
)
2476-
}))
2477-
.child(
2478-
Label::new(part_to_highlight)
2479-
.single_line()
2480-
.color(filename_text_color)
2481-
.underline(true),
2482-
)
2483-
.children(
2484-
suffix.and_then(|suffix| {
2485-
Some(
2486-
h_flex()
2487-
.child(
2488-
Label::new(std::path::MAIN_SEPARATOR_STR)
2489-
.single_line()
2490-
.color(filename_text_color),
2491-
)
2492-
.child(
2493-
Label::new(
2494-
suffix.to_str().map(String::from)?,
2495-
)
2496-
.single_line()
2497-
.color(filename_text_color),
2498-
),
2499-
)
2500-
}),
2501-
)
2492+
this
25022493
} else {
25032494
this.child(
25042495
Label::new(file_name)

0 commit comments

Comments
 (0)