subspace-dashboard 0.2.0

Subspace Framework board kit: the Dashboard trait, shared widgets and design tokens. Internal crate; apps depend on `subspace-framework`.
Documentation
//! Drag and keyboard resizing of the shared message input.

use super::{ChatInput, sizing};
use crate::Tokens;
use gpui_kit::{
    AnyElement, App, AppContext as _, Context, DragMoveEvent, EmptyView, EntityId,
    InteractiveElement as _, IntoElement as _, KeyBinding, ParentElement as _,
    StatefulInteractiveElement as _, Styled as _, Window, actions, div, px,
};

const CONTEXT: &str = "SubspaceMessageResize";
const HANDLE_HEIGHT_PX: f32 = 6.0;
const STEP_PX: f32 = 16.0;
const GRIP_WIDTH_PX: f32 = 24.0;
const GRIP_HEIGHT_PX: f32 = 2.0;
const HANDLE_OFFSET_PX: f32 = -HANDLE_HEIGHT_PX * 0.5;
const HOVER_OPACITY: f32 = 0.25;
const RESET_CLICK_COUNT: usize = 2;

actions!(
    subspace_message_resize,
    [InputTaller, InputShorter, InputReset]
);

pub(super) fn bind_keys(cx: &mut App) {
    cx.bind_keys([
        KeyBinding::new("up", InputTaller, Some(CONTEXT)),
        KeyBinding::new("down", InputShorter, Some(CONTEXT)),
        KeyBinding::new("home", InputReset, Some(CONTEXT)),
    ]);
}

#[derive(Clone)]
pub(super) struct InputResize(EntityId);

pub(super) fn height(input: &ChatInput, window: &Window) -> Option<f32> {
    let message = input.message.as_ref()?;
    let available = if message.available > 0.0 {
        message.available
    } else {
        f32::from(window.viewport_size().height)
    };
    Some(sizing::clamp_height(
        message.height,
        sizing::minimum_height(f32::from(window.rem_size())),
        available,
    ))
}

fn set_height(input: &mut ChatInput, requested: Option<f32>, cx: &mut Context<ChatInput>) {
    if let Some(message) = &mut input.message {
        message.height = requested;
        cx.notify();
    }
}

fn step(input: &mut ChatInput, delta: f32, window: &Window, cx: &mut Context<ChatInput>) {
    if let Some(height) = height(input, window) {
        set_height(input, Some(height + delta), cx);
    }
}

pub(super) fn drag(
    input: &mut ChatInput,
    event: &DragMoveEvent<InputResize>,
    _: &mut Window,
    cx: &mut Context<ChatInput>,
) {
    if event.drag(cx).0 == cx.entity_id() {
        set_height(
            input,
            Some(f32::from(event.bounds.bottom() - event.event.position.y)),
            cx,
        );
    }
}

pub(super) fn handle(input: &ChatInput, cx: &Context<ChatInput>) -> Option<AnyElement> {
    let focus = input.message.as_ref()?.resize_focus.clone();
    let tokens = Tokens::current(cx);
    Some(
        div()
            .id("message-input-resize")
            .absolute()
            .left_0()
            .right_0()
            .top(px(HANDLE_OFFSET_PX))
            .h(px(HANDLE_HEIGHT_PX))
            .key_context(CONTEXT)
            .track_focus(&focus)
            .tab_stop(true)
            .role(gpui_kit::Role::Button)
            .aria_label("Resize message input. Up or Down adjusts height; Home resets.")
            .cursor_row_resize()
            .flex()
            .items_center()
            .justify_center()
            .hover(|style| style.bg(tokens.accent.opacity(HOVER_OPACITY)))
            .child(
                div()
                    .w(px(GRIP_WIDTH_PX))
                    .h(px(GRIP_HEIGHT_PX))
                    .bg(tokens.muted),
            )
            .on_drag(InputResize(cx.entity_id()), |_, _, _, cx| {
                cx.new(|_| EmptyView)
            })
            .on_click(
                cx.listener(move |this, event: &gpui_kit::ClickEvent, window, cx| {
                    focus.focus(window, cx);
                    if event.click_count() >= RESET_CLICK_COUNT {
                        set_height(this, None, cx);
                    }
                }),
            )
            .on_action(
                cx.listener(|this, _: &InputTaller, window, cx| step(this, STEP_PX, window, cx)),
            )
            .on_action(
                cx.listener(|this, _: &InputShorter, window, cx| step(this, -STEP_PX, window, cx)),
            )
            .on_action(cx.listener(|this, _: &InputReset, _, cx| set_height(this, None, cx)))
            .into_any_element(),
    )
}