use std::rc::Rc;
mod density_prefs;
pub use density_prefs::save_density;
mod body;
mod toolbar;
pub use body::PanelBody;
use gpui_kit::component::v_flex;
use gpui_kit::prelude::FluentBuilder as _;
use gpui_kit::{
AnyElement, App, AppContext, BorrowAppContext as _, Div, ElementId, EmptyView, FocusHandle,
InteractiveElement, IntoElement, KeyBinding, ParentElement, SharedString, Stateful,
StatefulInteractiveElement, Styled, Window, actions, div, px, relative,
};
pub use subspace_lucide::IconName;
use crate::AddToChat;
use crate::action::{self, Intent};
use crate::anchored::AnchoredContextMenuExt as _;
use crate::density::Density;
use crate::links;
use crate::list::{Glyph, ServiceLink};
use crate::row_menu::{self, RowMenu};
use crate::tokens::Tokens;
actions!(
subspace_panel,
[PanelWider, PanelNarrower, PanelMuchWider, PanelMuchNarrower]
);
pub const RESIZE_CONTEXT: &str = "SubspacePanelResize";
const WIDTH_TOKEN: &str = "--panel-w";
pub const MIN_WIDTH: f32 = 320.0;
const KEY_STEP: f32 = 16.0;
const KEY_STEP_LARGE: f32 = 64.0;
const MAX_WINDOW_SHARE: f32 = 0.75;
const FULL_PARENT_WIDTH: f32 = 1.;
const BAR_HEADING_SIZE_PX: f32 = 13.;
const BAR_PADDING_X_PX: f32 = 8.;
const HANDLE_OFFSET_PX: f32 = -3.;
const HANDLE_WIDTH_PX: f32 = 6.;
const HANDLE_HOVER_OPACITY: f32 = 0.4;
const HANDLE_FOCUS_OPACITY: f32 = 0.6;
const RESET_CLICK_COUNT: usize = 2;
pub(crate) fn bind_keys(cx: &mut App) {
let context = Some(RESIZE_CONTEXT);
cx.bind_keys([
KeyBinding::new("left", PanelWider, context),
KeyBinding::new("right", PanelNarrower, context),
KeyBinding::new("shift-left", PanelMuchWider, context),
KeyBinding::new("shift-right", PanelMuchNarrower, context),
]);
}
pub fn clamp_width(width: f32, window_width: f32) -> f32 {
width
.min(window_width * MAX_WINDOW_SHARE)
.max(MIN_WIDTH)
.round()
}
fn set_width(width: f32, window: &mut Window, cx: &mut App) {
let width = clamp_width(width, f32::from(window.viewport_size().width));
cx.update_default_global::<Density, _>(|density, _| density.set(WIDTH_TOKEN, width));
save_density(cx);
window.refresh();
}
fn step_width(step: f32, window: &mut Window, cx: &mut App) {
let current = rendered_width(window, cx);
set_width(current + step, window, cx);
}
pub fn rendered_width(window: &Window, cx: &App) -> f32 {
let tokens = Tokens::current(cx);
clamp_width(
f32::from(tokens.panel_width),
f32::from(window.viewport_size().width),
)
}
pub struct LeftResize;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct PanelHeader {
pub icon: Option<Glyph>,
pub kind: SharedString,
pub id: SharedString,
pub label: Option<SharedString>,
pub link: Option<ServiceLink>,
}
pub type StepCallback = Rc<dyn Fn(isize, &mut Window, &mut App)>;
pub struct PanelActions {
close: Rc<dyn Fn(&mut Window, &mut App)>,
step: Option<StepCallback>,
}
impl PanelActions {
pub fn new(
on_close: impl Fn(&mut Window, &mut App) + 'static,
on_step: Option<StepCallback>,
) -> Self {
Self {
close: Rc::new(on_close),
step: on_step,
}
}
}
pub fn frame(
header: PanelHeader,
body: impl Into<PanelBody>,
actions: PanelActions,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
let tokens = Tokens::current(cx);
let width = rendered_width(window, cx);
let handle_focus = window
.use_keyed_state("panel-resize-focus", cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
let facts = panel_facts(&header);
let bar = toolbar::render(&tokens, header, actions, window, cx);
let handle = resize_handle(&tokens, handle_focus, window);
panel_box(&tokens, width)
.child(bar)
.child(body::render(body.into()))
.child(handle)
.anchored_context_menu(move |popup, window, cx| {
let selected = row_menu::hold_selection(window, cx);
row_menu::popup(popup, row_menu::panel_menu_items(&facts, &selected), None)
})
.into_any_element()
}
fn panel_box(tokens: &Tokens, width: f32) -> Stateful<Div> {
v_flex()
.id("detail-panel")
.relative()
.flex_shrink_0()
.w(px(width))
.max_w(relative(FULL_PARENT_WIDTH))
.h_full()
.bg(tokens.surface)
.border_l_1()
.border_color(tokens.line)
.on_drag_move::<LeftResize>(|event, window, cx| {
let width = event.bounds.right() - event.event.position.x;
set_width(f32::from(width), window, cx);
})
}
fn icon_button(
id: &'static str,
icon: subspace_lucide::SvgIcon,
label: &'static str,
) -> crate::icon_button::IconButton {
action::icon_button(id, Intent::Quiet, icon, label)
}
pub fn service_link(id: impl Into<ElementId>, link: ServiceLink) -> impl IntoElement {
let url = link.url;
action::action_button(
id,
Intent::Neutral,
Some(IconName::ExternalLink),
link.label,
)
.on_click(move |_, _, cx| {
links::open(&url, cx);
})
}
fn add_to_chat_button() -> impl IntoElement {
action::icon_button(
"panel-add-to-chat",
Intent::Quiet,
IconName::MessageSquarePlus,
"Add to chat",
)
.on_click(|_, window, cx| window.dispatch_action(Box::new(AddToChat), cx))
}
fn resize_handle(tokens: &Tokens, handle_focus: FocusHandle, window: &Window) -> Stateful<Div> {
left_resize_handle("panel-resize", tokens)
.key_context(RESIZE_CONTEXT)
.track_focus(&handle_focus)
.when(handle_focus.is_focused(window), |handle| {
handle.bg(tokens.accent.opacity(HANDLE_FOCUS_OPACITY))
})
.on_action(|_: &PanelWider, window, cx| step_width(KEY_STEP, window, cx))
.on_action(|_: &PanelNarrower, window, cx| step_width(-KEY_STEP, window, cx))
.on_action(|_: &PanelMuchWider, window, cx| step_width(KEY_STEP_LARGE, window, cx))
.on_action(|_: &PanelMuchNarrower, window, cx| step_width(-KEY_STEP_LARGE, window, cx))
.on_click(move |event, window, cx| {
if event.click_count() >= RESET_CLICK_COUNT {
reset_width(window, cx);
} else {
handle_focus.focus(window, cx);
}
})
}
pub fn left_resize_handle(id: impl Into<gpui_kit::ElementId>, tokens: &Tokens) -> Stateful<Div> {
div()
.id(id)
.absolute()
.left(px(HANDLE_OFFSET_PX))
.top_0()
.bottom_0()
.w(px(HANDLE_WIDTH_PX))
.cursor_col_resize()
.hover(|style| style.bg(tokens.accent.opacity(HANDLE_HOVER_OPACITY)))
.on_drag(LeftResize, |_, _, _, cx| cx.new(|_| EmptyView))
}
fn reset_width(window: &mut Window, cx: &mut App) {
if cx.has_global::<Density>() {
cx.global_mut::<Density>().remove(WIDTH_TOKEN);
}
save_density(cx);
window.refresh();
}
fn panel_facts(header: &PanelHeader) -> RowMenu {
let facts = RowMenu::new(header.label.clone().unwrap_or_default())
.id(header.id.clone())
.opens_details(false);
match &header.link {
Some(link) => facts.link(link.label.trim_end_matches('↗').trim(), &link.url),
None => facts,
}
}
#[cfg(test)]
mod tests {
use super::{PanelHeader, clamp_width, panel_facts};
#[test]
fn the_width_stays_between_320_px_and_three_quarters_of_the_window() {
assert_eq!(clamp_width(600.0, 1280.0), 600.0);
assert_eq!(clamp_width(1200.0, 1280.0), 960.0);
assert_eq!(clamp_width(100.0, 1280.0), 320.0);
assert_eq!(clamp_width(600.0, 300.0), 320.0);
assert_eq!(clamp_width(600.4, 1280.0), 600.0);
}
#[test]
fn a_friendly_header_keeps_the_real_identifier_for_copy() {
let header = PanelHeader {
id: "19:opaque@thread.v2".into(),
label: Some("Delivery team".into()),
..Default::default()
};
let facts = panel_facts(&header);
assert_eq!(facts.title, "Delivery team");
assert_eq!(facts.id.as_deref(), Some("19:opaque@thread.v2"));
}
}