//! Editable tracks and drag destinations for column layouts. mod resize; use gpdoc::{BlockDropTarget, ViewPlugin}; use gpdoc_plugin::core::{ReplaceAroundStep, ReplaceStep, ResolvedPos}; use gpdoc_plugin::prelude::*; use gpdoc_plugin_drag_handle::{BlockDragLayout, BlockPlacement, DragHandle}; use gpui::{App, Bounds, Div, Pixels, Window, div, point, prelude::*, px, size}; #[cfg(target_family = "wasm")] use gpui_browser as gpui; use std::ops::Range; pub(super) const DRAG_LAYOUT: BlockDragLayout = BlockDragLayout { container_node: "columns", gutter_node: "column", placement, remove_empty, }; /// Presentation of one measured column in a side-by-side layout. pub struct ColumnTrack { content: Div, divider: Div, } impl ColumnTrack { /// Wrap the editable blocks in this container. pub fn content(mut self, container: Div) -> Self { self.content = container; self } /// Style the resize handle on the right edge, when another column follows. pub fn divider(mut self, element: Div) -> Self { self.divider = element; self } } /// Compose each column while the plugin owns track sizing, hit testing, and resizing. pub fn column_layout( render: impl Fn(ColumnTrack, &mut Window, &mut App) -> ColumnTrack + 'static, ) -> ViewPlugin { ViewPlugin::new("columns", resize::ColumnResize::default) .for_plugin("columns") .child_layout("columns", move |group, children, window, cx| { let gutter = DragHandle::installed_gutter_width(group.view.read(cx)); // Padding belongs to the measured tracks, so the gap and the space // below a short column remain unambiguous pointer targets. let total: f64 = children.iter().map(|col| resize::weight(col.node())).sum(); let count = children.len(); div() .w_full() .flex() .py(px(6.)) .children(children.into_iter().enumerate().map(|(index, col)| { let fraction = (resize::weight(col.node()) / total) as f32; let track = render( ColumnTrack { content: div(), divider: div() .hover(|el| el.border_r_2().border_color(gpui::rgb(0xaaaaaa))), }, window, cx, ); col.render_with(window, cx, |body| { div() .relative() .w(gpui::relative(fraction)) .min_w_0() .pl(gutter) .pr(px(6.)) .child(track.content.child(body)) .when(index + 1 < count, |el| { el.child(resize::divider(track.divider)) }) }) })) }) } fn side(target: &BlockDropTarget, left: Pixels) -> Option { let rect = target.bounds; let p = target.pointer; let inset = px(5.).min(rect.size.height / 4.); if p.y < rect.top() + inset || p.y > rect.bottom() - inset { return None; } let zone = px(10.).min(rect.size.width / 5.); if p.x <= left + zone { Some(false) } else if p.x >= rect.right() - zone { Some(true) } else { None } } fn vertical(mut target: BlockDropTarget, at: usize, x: Pixels) -> BlockDropTarget { target.at = DocPos(at); target.indicator = Bounds::new( point(x, target.bounds.top()), size(px(2.), target.bounds.size.height), ); target } fn placement( editor: &Editor, selection: Selection, targets: &[BlockDropTarget], gutter: Pixels, ) -> Option { let doc = editor.document(); let hit = *targets.first()?; let before = hit.block_before?; let range = selection.bounds(doc); let kind = |target: &BlockDropTarget| Some(doc.node_at(target.block_before?)?.kind.as_str()); if let Some(index) = targets.iter().position(|t| kind(t) == Some("columns")) { let group = targets[index]; let rect = group.bounds; // The top and bottom padding is an exit from the whole group. if hit.pointer.y <= rect.top() + px(5.) || hit.pointer.y >= rect.bottom() - px(5.) { return Some(BlockPlacement::Boundary(group)); } if let Some(track) = targets[..index].iter().find(|t| kind(t) == Some("column")) { let start = track.block_before?; let end = start.0 + doc.node_at(start)?.size(); if let Some(right) = side(track, track.bounds.left() + gutter) { let (at, x) = if right { (end, track.bounds.right()) } else { (start.0, track.bounds.left()) }; return insert_column(editor, range, vertical(*track, at, x)); } if before == start { let mut target = *track; target.at = DocPos(end - 1); target.indicator.origin.y = track.bounds.bottom(); return Some(BlockPlacement::Boundary(target)); } } else if before == group.block_before? { // The outer gutter can be wider than the track padding. let right = side(&group, rect.left() + gutter)?; let (at, x) = if right { (before.0 + doc.node_at(before)?.size() - 1, rect.right()) } else { (before.0 + 1, rect.left() + gutter) }; return insert_column(editor, range, vertical(group, at, x)); } return None; } // Resolve a nested hit to the same block its drag handle would move. let owner = editor .config() .drag_handle_owner(&doc.resolve(before).ok()?)?; let target = *targets.iter().find(|t| t.block_before == Some(owner))?; let root = doc.resolve(owner).ok()?.depth() == 0; let left = target.bounds.left() + if root { gutter } else { px(0.) }; let right = side(&target, left)?; let node = doc.node_at(owner)?; let end = owner.0 + node.size(); if range.start.0 < end && owner.0 < range.end.0 { return Some(BlockPlacement::Reject); } let moved = dragged_column(editor, &range)?; let width = moved.content.size(); let empty = moved.copy(Fragment::default()); let cols = if right { vec![empty, moved] } else { vec![moved, empty] }; let group = editor .schema() .node("columns", Default::default(), cols.into(), vec![]) .ok()?; // Each opening or closing node boundary occupies one document position. // Keep the stationary block in the gap to preserve its position mappings. let step = Step::ReplaceAround(ReplaceAroundStep { from: owner.0, to: end, gap_from: owner.0, gap_to: end, slice: Slice::new(group.into(), 0, 0), insert: if right { 2 } else { width + 4 }, structure: true, }); let target = vertical( target, if right { end } else { owner.0 }, if right { target.bounds.right() } else { left }, ); Some(BlockPlacement::Insert { target, step, moved_at: owner.0 + if right { node.size() + 4 } else { 2 }, }) } fn dragged_column(editor: &Editor, range: &Range) -> Option { let blocks = editor .document() .root() .slice(range.start.0, range.end.0, false) .ok()? .content; editor .schema() .node("column", Default::default(), blocks, vec![]) .ok() } fn insert_column( editor: &Editor, range: Range, target: BlockDropTarget, ) -> Option { let at = target.at.0; let src = editor.document().resolve(range.start).ok()?; let depth = src.depth(); // Dropping every block back on either edge of its own column is a no-op. if range.contains(&target.at) || (src.parent().kind == "column" && range.start.0 == src.start(depth) && range.end.0 == src.end(depth) && (at == src.before(depth).ok()? || at == src.after(depth).ok()?)) { return Some(BlockPlacement::Reject); } let column = dragged_column(editor, &range)?; let step = Step::Replace(ReplaceStep { from: at, to: at, slice: Slice::new(column.into(), 0, 0), structure: false, }); Some(BlockPlacement::Insert { target, step, moved_at: at + 1, }) } /// In a two-column group, lift the surviving blocks through the replacement gap /// so their positions and bookmarks continue to map through the move. fn remove_empty(source: &ResolvedPos, depth: usize) -> Option { if depth < 2 || source.node(depth).kind != "column" { return None; } let group = source.node(depth - 1); if group.kind != "columns" { return None; } if group.child_count() > 2 { return Some(Step::Replace(ReplaceStep { from: source.before(depth).ok()?, to: source.after(depth).ok()?, slice: Slice::empty(), structure: false, })); } let other = 1 - source.index(depth - 1); let start = source.pos_at_index(other, depth - 1) + 1; Some(Step::ReplaceAround(ReplaceAroundStep { from: source.before(depth - 1).ok()?, to: source.after(depth - 1).ok()?, gap_from: start, gap_to: start + group.child(other).content.size(), slice: Slice::empty(), insert: 0, structure: false, })) }