use super::*;
impl Kuizer {
pub(super) fn render_titlebar(
&self,
window: &Window,
cx: &mut Context<Self>,
) -> gpui::Stateful<Div> {
let start = matches!(self.phase, Phase::Start);
let roots = if start {
"Choose what to scan".to_owned()
} else {
self.roots
.iter()
.map(|root| display_path(root))
.collect::<Vec<_>>()
.join(", ")
};
let busy = self.busy();
let refreshing = self.refreshing();
let scanning = matches!(self.phase, Phase::Loading(_)) || refreshing;
let finished = matches!(self.phase, Phase::Ready(_) | Phase::Failed(_)) && !refreshing;
let partial = (finished
&& self
.report()
.is_some_and(|report| report.summary.cancelled))
.then(|| {
ui::tag("partial", "Partial", ui::Tone::Review).tip(tooltip::plain(
"The scan was stopped early, so some folders and sections are missing. Rescan to get everything",
))
});
let stale = self.stale.map(|saved| {
div()
.flex()
.flex_none()
.items_center()
.gap(unit(space::S))
.debug_selector(|| "stale".to_owned())
.child(
ui::tag(
"stale-age",
snapshot::age_label(saved, SystemTime::now()),
ui::Tone::Review,
)
.tip(tooltip::plain(
"These results come from the last scan of these folders. A fresh scan is running and replaces them when it finishes",
)),
)
.child(
ui::tag("refreshing", "Refreshing", ui::Tone::Neutral)
.tip(Tooltip::new("Stop the fresh scan").keys(&["Esc"]).build()),
)
});
let controls = frame::controls(window);
let custom = controls.is_some();
let aligned = !matches!(self.phase, Phase::Start)
&& !self.collapsed
&& frame::inset(window).is_none();
let actions = div()
.debug_selector(|| "titlebar-actions".to_owned())
.flex()
.flex_none()
.items_center()
.gap(unit(space::L))
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
.when(scanning, |this| {
this.child(
self.button(
"stop-scan",
"Stop here",
ButtonKind::Ghost,
true,
cx,
|app, _, cx| {
app.stop_scan(cx);
},
)
.tip(
Tooltip::new("Stop the scan and keep what it found so far")
.keys(&["S"])
.build(),
),
)
.child(
self.button(
"cancel-scan",
"Cancel scan",
ButtonKind::Secondary,
true,
cx,
|app, _, cx| app.cancel_scan(cx),
)
.tip(
Tooltip::new("Stop the scan and go back to the start screen")
.keys(&["Esc"])
.build(),
),
)
})
.when(finished, |this| {
this.child(
self.button(
"new-scan",
"New scan",
ButtonKind::Ghost,
!busy,
cx,
|app, _, cx| {
app.new_scan(cx);
},
)
.tip(tooltip::plain(
"Choose another folder or change the scan options",
)),
)
.child(
self.button(
"rescan",
"Rescan",
ButtonKind::Secondary,
!busy,
cx,
|app, _, cx| {
if !app.busy() {
app.rescan(cx);
}
},
)
.tip(tooltip::plain(
"Scan the same folders again with the same options",
)),
)
});
div()
.id("titlebar")
.debug_selector(|| "titlebar".to_owned())
.flex()
.flex_none()
.items_center()
.gap(unit(space::L))
.h(unit(size::BAR))
.min_h(frame::TITLEBAR)
.px(unit(space::GUTTER))
.when_some(frame::inset(window), |this, inset| this.pl(inset))
.when(custom, |this| this.pr(unit(space::S)))
.bg(theme::panel())
.border_b_1()
.border_color(theme::border())
.on_mouse_down(MouseButton::Left, cx.listener(Self::titlebar_down))
.on_mouse_up(MouseButton::Left, cx.listener(Self::titlebar_up))
.on_mouse_move(cx.listener(Self::titlebar_move))
.when(custom, |this| {
this.on_mouse_down(MouseButton::Right, |event, window, _| {
window.show_window_menu(event.position);
})
})
.child(
div()
.flex()
.flex_1()
.gap(unit(space::L))
.child(
div()
.debug_selector(|| "brand".to_owned())
.font_weight(FontWeight::SEMIBOLD)
.when(aligned, |this| {
this.flex_basis(unit(size::SIDEBAR - space::L))
})
.child("kuizer"),
)
.child(
div().relative().flex_1().child(
div()
.id("roots")
.absolute()
.inset_0()
.flex()
.items_center()
.gap(unit(space::S))
.text_color(theme::muted())
.when(!start, |this| {
this.tip(tooltip::plain(roots.clone())).child(icon::view(
Icon::Folder,
theme::faint(),
None,
self.zoom.rem_size(),
))
})
.child(div().flex_1().min_w_0().truncate().child(roots)),
),
),
)
.child(
div()
.flex()
.flex_1()
.items_center()
.justify_end()
.gap(unit(space::L))
.child(self.render_search(cx))
.children(partial)
.children(stale)
.child(actions)
.children(controls),
)
}
pub(super) fn titlebar_down(
&mut self,
event: &MouseDownEvent,
window: &mut Window,
_: &mut Context<Self>,
) {
self.moving = frame::titlebar_down(event.click_count, window);
}
pub(super) fn titlebar_up(&mut self, _: &MouseUpEvent, _: &mut Window, _: &mut Context<Self>) {
self.moving = false;
}
pub(super) fn titlebar_move(
&mut self,
_: &MouseMoveEvent,
window: &mut Window,
_: &mut Context<Self>,
) {
if self.moving {
self.moving = false;
window.start_window_move();
}
}
pub(super) fn tab_count(&self, tab: Tab) -> Option<usize> {
let report = self.report()?;
match tab {
Tab::Overview => None,
Tab::Artifacts => Some(report.candidates.len()),
Tab::Caches => Some(report.caches.len()),
Tab::Toolchains => Some(report.toolchains.len()),
Tab::Docker => Some(report.docker.len()),
Tab::Repos => Some(report.repos.len()),
Tab::Duplicates => report.duplicates.as_ref().map(|found| found.groups.len()),
Tab::History => report
.history
.as_ref()
.ok()
.map(|view| view.delete_runs().count()),
Tab::Issues => Some(report.issues.len()),
}
}
pub(super) fn tab_bytes(&self, tab: Tab) -> Option<u64> {
let report = self.report()?;
let bytes = match tab {
Tab::Overview | Tab::History | Tab::Issues => return None,
Tab::Artifacts => self.report_stats.artifacts.all.bytes,
Tab::Caches => self.report_stats.caches.all.bytes,
Tab::Toolchains => self.report_stats.toolchains.all.bytes,
Tab::Docker => self.report_stats.docker.all.bytes,
Tab::Repos => self.reclaimable(report).repos,
Tab::Duplicates => self.reclaimable(report).duplicates,
};
Some(bytes)
}
pub(super) fn reclaimable(&self, report: &Report) -> Reclaimable {
if let Some((generation, figures)) = self.reclaimable.get()
&& generation == self.report_generation
{
return figures;
}
let figures = Reclaimable {
repos: report
.repos
.iter()
.filter(|repository| repository.collectable(self.config.git.min_gain.0))
.map(|repository| repository.objects.gain())
.sum(),
duplicates: report
.duplicates
.as_ref()
.map_or(0, kuizer::dupes::Duplicates::reclaimable),
};
self.reclaimable
.set(Some((self.report_generation, figures)));
figures
}
pub(super) fn tab_figure(&self, tab: Tab) -> Option<String> {
match self.tab_bytes(tab) {
Some(bytes) if bytes > 0 => Some(theme::bytes(bytes)),
_ => self
.tab_count(tab)
.filter(|&count| count > 0)
.map(|count| count.to_string()),
}
}
pub(super) fn sidebar_toggle(&self, cx: &mut Context<Self>) -> Stateful<Div> {
let collapsed = self.collapsed;
let toggle = div()
.id("sidebar-toggle")
.debug_selector(|| "sidebar-toggle".to_owned())
.flex()
.flex_none()
.items_center()
.justify_center()
.size(unit(size::CONTROL))
.rounded(theme::radius_control())
.cursor_pointer()
.text_color(theme::faint())
.hover(|style| style.bg(theme::hover()).text_color(theme::muted()))
.tip_hover(
Tooltip::new(if collapsed {
"Show section names"
} else {
"Show only icons"
})
.keys(&[hint(&Command::Sidebar)])
.build(),
cx.listener(|app, hovered: &bool, _, cx| {
app.hover_icon(Icon::Sidebar, *hovered, cx);
}),
)
.child(icon::view(
Icon::Sidebar,
theme::faint(),
self.icon_epoch(Icon::Sidebar),
self.zoom.rem_size(),
));
self.control(toggle, cx, |app, _, cx| {
app.collapsed = !app.collapsed;
cx.notify();
})
}
pub(super) fn render_sidebar(&self, cx: &mut Context<Self>) -> Stateful<Div> {
let collapsed = self.collapsed;
self.stops
.borrow_mut()
.within(Some(self.sidebar_scroll.clone()));
let mut items: Vec<AnyElement> = Vec::new();
for tab in Tab::ALL {
let heading = tab.heading().map(|heading| {
if collapsed {
ui::divider().mx(unit(space::M)).into_any_element()
} else {
div()
.flex()
.items_end()
.h(unit(size::ROW))
.px(unit(space::M))
.pb(unit(space::S))
.text_size(unit(font::CAPTION))
.font_weight(FontWeight::MEDIUM)
.text_color(theme::faint())
.child(heading)
.into_any_element()
}
});
items.extend(heading);
items.push(self.render_tab(tab, cx).into_any_element());
}
self.stops.borrow_mut().within(None);
let settings = div()
.id("sidebar-settings")
.debug_selector(|| "sidebar-settings".to_owned())
.flex()
.flex_none()
.items_center()
.gap(unit(space::M))
.h(unit(size::ROW))
.px(unit(space::M))
.rounded(theme::radius_control())
.cursor_pointer()
.text_color(theme::muted())
.hover(|style| style.bg(theme::hover()).text_color(theme::text()))
.tip_hover(
Tooltip::new("Settings")
.keys(&[hint(&Command::Settings)])
.build(),
cx.listener(|app, hovered: &bool, _, cx| {
app.hover_icon(Icon::Settings, *hovered, cx);
}),
)
.child(icon::view(
Icon::Settings,
theme::muted(),
self.icon_epoch(Icon::Settings),
self.zoom.rem_size(),
))
.when(!collapsed, |this| {
this.child(div().flex_1().min_w_0().truncate().child("Settings"))
.child(ui::kbd(commands::label("secondary-,")))
});
let settings = self.control(settings, cx, |app, _, cx| app.open_settings(cx));
div()
.id("sidebar")
.debug_selector(|| "sidebar".to_owned())
.flex()
.flex_col()
.flex_none()
.w(unit(if collapsed {
size::SIDEBAR_COLLAPSED
} else {
size::SIDEBAR
}))
.py(unit(space::M))
.px(unit(space::INSET))
.bg(theme::panel())
.border_r_1()
.border_color(theme::border())
.child(
div()
.id("sidebar-items")
.track_scroll(&self.sidebar_scroll)
.flex()
.flex_col()
.flex_1()
.min_h_0()
.overflow_y_scroll()
.children(items),
)
.child(settings)
}
pub(super) fn hover_icon(&mut self, icon: Icon, hovered: bool, cx: &mut Context<Self>) {
if hovered && !motion::reduced() {
*self.icon_hovers.entry(icon.name()).or_default() += 1;
self.redraw_chrome(cx);
}
}
pub(super) fn icon_epoch(&self, icon: Icon) -> Option<u64> {
self.icon_hovers.get(icon.name()).copied()
}
pub(super) fn render_tab(&self, tab: Tab, cx: &mut Context<Self>) -> Stateful<Div> {
let active = self.tab == tab;
let collapsed = self.collapsed;
let figure = self.tab_figure(tab);
let shortcuts = &self.config.route_shortcuts;
let hints = shortcuts.enabled && shortcuts.hints;
let tip = Tooltip::new(tab.term().text())
.title(match (collapsed, &figure) {
(true, Some(figure)) => format!("{} {figure}", tab.label()),
_ => tab.label().to_owned(),
})
.keys(if hints { tab.keys() } else { &[] })
.build();
let item = div()
.id(tab.label())
.debug_selector(|| tab.label().to_owned())
.relative()
.flex()
.flex_none()
.items_center()
.gap(unit(space::M))
.h(unit(size::ROW))
.px(unit(space::M))
.rounded(theme::radius_control())
.cursor_pointer()
.text_color(if active {
theme::text()
} else {
theme::muted()
})
.when(active, |this| {
this.bg(theme::selected()).font_weight(FontWeight::MEDIUM)
})
.when(!active, |this| {
this.hover(|style| style.bg(theme::hover()).text_color(theme::text()))
})
.tip_hover(
tip,
cx.listener(move |app, hovered: &bool, _, cx| {
app.hover_icon(tab.icon(), *hovered, cx);
}),
)
.child(icon::view(
tab.icon(),
if active {
theme::text()
} else {
theme::muted()
},
self.icon_epoch(tab.icon()),
self.zoom.rem_size(),
))
.when(!collapsed, |this| {
this.child(div().flex_1().min_w_0().truncate().child(tab.label()))
.children(figure.map(|figure| {
ui::figure(figure)
.text_size(unit(font::SMALL))
.font_weight(FontWeight::NORMAL)
.text_color(if active {
theme::muted()
} else {
theme::faint()
})
}))
.children(
tab.keys()
.first()
.filter(|_| hints)
.map(|key| ui::kbd(commands::label(key))),
)
});
self.control(item, cx, move |app, _, cx| app.go_to(tab, cx))
}
pub(super) fn render_statusbar(&self, cx: &mut Context<Self>) -> AnyElement {
if let Some(confirm) = self.render_confirm(cx) {
return confirm;
}
self.render_selection_bar(cx).into_any_element()
}
fn render_selection_bar(&self, cx: &mut Context<Self>) -> Div {
let stats = self.selection.stats();
let total = stats.total();
let empty = total.all.count == 0;
let review = total.review.count;
let summary = if empty {
"Nothing selected".to_owned()
} else {
format!(
"{} selected, {}",
total.all.count,
theme::bytes(total.all.bytes)
)
};
let ready = self.report().is_some() && !self.busy();
let refreshing = self.refreshing();
let can_select = ready && self.tab.selects();
div()
.flex()
.flex_none()
.items_center()
.gap(unit(space::M))
.h(unit(size::BAR))
.px(unit(space::GUTTER))
.bg(theme::panel())
.border_t_1()
.border_color(theme::border())
.child(
div()
.id("selection")
.when(!empty, |this| this.tip(selection_details(stats)))
.child(summary),
)
.when(review > 0, |this| {
this.child(
div()
.id("marked-review")
.text_color(theme::review())
.tip(tooltip::term(Term::Review))
.child(format!("{review} marked review")),
)
})
.child(div().flex_1())
.child(
self.button(
"select-safe",
"Select safe",
ButtonKind::Ghost,
can_select,
cx,
|app, _, cx| {
if app.tab.selects() && !app.busy() {
app.select_safe(cx);
}
},
)
.tip(tooltip::term(Term::SelectSafe)),
)
.child(
self.button("clear", "Clear", ButtonKind::Ghost, ready && !empty, cx, |app, _, cx| {
if !app.busy() {
app.selection.clear();
cx.notify();
}
})
.tip(tooltip::plain("Clear the selection in every tab")),
)
.child(
self.button(
"trash",
"Delete",
ButtonKind::Primary,
ready && !refreshing && !empty,
cx,
|app, window, cx| app.open_confirm(window, cx),
)
.when(refreshing, |this| {
this.tip(tooltip::plain(
"Available once the fresh scan finishes. These results come from an earlier scan, so every item is checked again first",
))
})
.when(!refreshing, |this| this.tip(tooltip::term(Term::Trash))),
)
}
}