//! Resize adjacent columns in a single undoable edit. use gpdoc::{AnchoredElement, EditorView, ViewBehavior, ViewFrame}; use gpdoc_plugin::prelude::*; use gpui::{ App, Bounds, Context, CursorStyle, Div, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, Point, Window, div, point, prelude::*, px, rgb, }; #[cfg(target_family = "wasm")] use gpui_browser as gpui; use std::{cell::Cell, rc::Rc}; const HIT_WIDTH: f32 = 6.; const MIN_WIDTH: f32 = 48.; pub(super) fn weight(node: &Node) -> f64 { node.attrs .get("weight") .and_then(Value::as_i64) .filter(|w| *w > 0) .unwrap_or(crate::DEFAULT_WEIGHT) as f64 } pub(super) fn divider(element: Div) -> Div { element .absolute() .right_0() .top_0() .bottom_0() .w(px(HIT_WIDTH)) .cursor(CursorStyle::ResizeColumn) } #[derive(Clone, Copy)] struct Drag { revision: u64, left: DocPos, right: DocPos, start: Pixels, left_bounds: Bounds, right_bounds: Bounds, pair_weight: f64, } impl Drag { fn at(view: &mut EditorView, pointer: Point, window: &mut Window) -> Option { let (left, left_bounds) = view.child_bounds_at(pointer, window)?; if pointer.x < left_bounds.right() - px(HIT_WIDTH) { return None; } let doc = view.editor().document(); let col = doc.node_at(left)?; if col.kind != "column" { return None; } let right = DocPos(left.0 + col.size()); let neighbor = doc.node_at(right)?; if neighbor.kind != "column" { return None; } Some(Self { revision: view.editor().revision(), pair_weight: weight(col) + weight(neighbor), left, right, start: pointer.x, left_bounds, right_bounds: view.child_bounds(right, window)?, }) } fn left_width(self, pointer: Pixels) -> Pixels { let total = self.left_bounds.size.width + self.right_bounds.size.width; let min = px(MIN_WIDTH).min(total / 2.); (self.left_bounds.size.width + (pointer - self.start)).clamp(min, total - min) } } #[derive(Default)] pub(super) struct ColumnResize(Cell>); impl ViewBehavior for ColumnResize { fn mouse_down( &self, view: &mut EditorView, event: &MouseDownEvent, window: &mut Window, _: &mut Context, ) -> bool { if view.is_composing() || event.button != gpui::MouseButton::Left { return false; } self.0.set(Drag::at(view, event.position, window)); self.0.get().is_some() } fn mouse_move( &self, _: &mut EditorView, event: &MouseMoveEvent, _: &mut Window, cx: &mut Context, ) -> bool { if self.0.get().is_some() && event.dragging() { cx.notify(); return true; } false } fn render( self: Rc, frame: &ViewFrame<'_>, window: &mut Window, cx: &mut App, ) -> Option { let drag = self.0.get()?; if frame.view.read(cx).editor().revision() != drag.revision { return None; } let bounds = frame.block_bounds(drag.left)?; let origin = point( bounds.left() + drag.left_width(window.mouse_position().x), bounds.top(), ); Some(AnchoredElement::new( div() .w(px(2.)) .h(bounds.size.height) .bg(rgb(0xaaaaaa)) .cursor(CursorStyle::ResizeColumn), move |_, _| origin, )) } fn mouse_up( &self, view: &mut EditorView, event: &MouseUpEvent, window: &mut Window, cx: &mut Context, ) -> bool { let Some(drag) = self.0.take() else { return false; }; let editor = view.editor(); if editor.revision() == drag.revision && (event.position.x - drag.start).abs() > px(1.) { let total = drag.pair_weight; let fraction = f64::from(f32::from(drag.left_width(event.position.x))) / f64::from(f32::from( drag.left_bounds.size.width + drag.right_bounds.size.width, )); let left = (total * fraction).round().clamp(1., total - 1.); let mut tx = editor.transaction(); for (pos, weight) in [(drag.left, left), (drag.right, total - left)] { tx.push(Step::set_node_attribute( pos, "weight", (weight as i64).into(), )); } if let Err(error) = view.apply_node_view_in_place(tx, window, cx) { eprintln!("Failed to resize columns: {error}"); } } cx.notify(); true } fn pointer_cancel(&self) { self.0.set(None); } }