Titles are typed right in their boxes on the canvas: the box grows with the text, the terminal's cursor sits in it (a bar while typing, a block on the letter with vim keys), and the border turns the insert color. `i`, `A` and Enter edit at the end, `I` at the start, `cc` and `S` type the title anew. The document follows the text as it's typed, so the layout stays current, and the whole edit is one undo step. Esc gives vim's normal mode inside the text (TextEditor, in ui/): h l w b e 0 ^ $, f t F T with ; and ,, the d c y operators with any of those motions or the iw aw i" a" i( a( text objects, x X s S D C Y, r ~, p P, counts, u and Ctrl-r for the edit's own changes, and `.` for the last one, typing included. j k gg G and J work on lines, for bodies. It follows vim where it's quirky too: cw acts like ce, dw stops at the end of the line, Esc steps back onto the last letter. Keys have two new tables, `text` and `insert`, and in text an operator takes motions as targets (`dw`, `c$`, `2d3fx`), which which-key lists. Rapid entry: after `o`, `O` or `a`, the new idea's title is typed at once; Enter starts the next idea, Tab puts the one being typed under the idea above, Shift-Tab takes it out a level, and Enter on an empty idea stops. An idea left empty goes away without a trace in the undo history (a group whose edits cancel out leaves no step); a title left empty comes back as it was. Inside a title, `o` and `O` start a new sibling. Registers are shared by the whole app, so text yanked in one title pastes into another; `"a`-`"z` and `"_` work, and `"+` stays inside cumulus until the app core connects the clipboard. The mouse while editing: a click in the text moves the text cursor, a click anywhere else keeps the text and finishes the edit, and a double-click on a title starts editing at that letter. `>>` and `<<` now move nodes: under the node above, as its last child, or out a level, right after the old parent, with counts taking in the next siblings. The command line's input became TextInput, line-aware so the editor can use it for bodies too; bodies themselves get edited in the Note view. The example's tests type every key above into the app and check the text and the cursor after each one against what vim does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
97 lines
3.7 KiB
Rust
97 lines
3.7 KiB
Rust
//! Where new nodes go on the canvas: next to their parent or sibling, in a
|
|
//! free spot. Nothing else moves to make room.
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use crate::model::{CanvasPos, CanvasRect, CanvasSize, Document, NodeId};
|
|
|
|
/// Columns between a parent and its children: room for the branch lines.
|
|
const BRANCH_GAP: i32 = 6;
|
|
/// Rows kept free between boxes stacked one above another.
|
|
const ROW_GAP: i32 = 1;
|
|
|
|
/// Where a new last child of `parent` goes: to the right of the parent, below
|
|
/// its last child (level with the parent for a first child), then further
|
|
/// down until it's clear of every other box.
|
|
pub fn child_spot(
|
|
doc: &Document,
|
|
parent: NodeId,
|
|
size: CanvasSize,
|
|
rects: &BTreeMap<NodeId, CanvasRect>,
|
|
) -> CanvasPos {
|
|
let Some(&parent_rect) = rects.get(&parent) else { return CanvasPos::default() };
|
|
match doc.children(parent).last().and_then(|child| rects.get(child)) {
|
|
Some(last) => free_spot(CanvasPos::new(last.left(), last.bottom() + ROW_GAP), size, rects),
|
|
None => first_child_spot(parent_rect, size, rects),
|
|
}
|
|
}
|
|
|
|
/// Where the first child of a box goes: to its right, level with it, then
|
|
/// further down until it's clear. The box doesn't need to be in `rects` yet,
|
|
/// so this works for a parent that's being added in the same edit.
|
|
pub fn first_child_spot(
|
|
parent: CanvasRect,
|
|
size: CanvasSize,
|
|
rects: &BTreeMap<NodeId, CanvasRect>,
|
|
) -> CanvasPos {
|
|
free_spot(CanvasPos::new(parent.right() + BRANCH_GAP, parent.top()), size, rects)
|
|
}
|
|
|
|
/// Where a new node just after `sibling` goes: right below it, then further
|
|
/// down until it's clear.
|
|
pub fn below_spot(sibling: NodeId, size: CanvasSize, rects: &BTreeMap<NodeId, CanvasRect>) -> CanvasPos {
|
|
let Some(&rect) = rects.get(&sibling) else { return CanvasPos::default() };
|
|
spot_below(rect, size, rects)
|
|
}
|
|
|
|
/// Where a box goes below another box: right below it, then further down
|
|
/// until it's clear. The box above doesn't need to be in `rects`.
|
|
pub fn spot_below(above: CanvasRect, size: CanvasSize, rects: &BTreeMap<NodeId, CanvasRect>) -> CanvasPos {
|
|
free_spot(CanvasPos::new(above.left(), above.bottom() + ROW_GAP), size, rects)
|
|
}
|
|
|
|
/// The first spot at `start` or below it where a box of `size` keeps clear of
|
|
/// every box in `rects` (by a row above and below).
|
|
pub fn free_spot(start: CanvasPos, size: CanvasSize, rects: &BTreeMap<NodeId, CanvasRect>) -> CanvasPos {
|
|
let mut spot = start;
|
|
// Each step jumps below the box in the way, and no box is in the way
|
|
// twice, so there are at most as many steps as boxes.
|
|
for _ in 0..=rects.len() {
|
|
let wanted = CanvasRect::new(
|
|
CanvasPos::new(spot.x, spot.y - ROW_GAP),
|
|
CanvasSize::new(size.width, size.height + 2 * ROW_GAP as u16),
|
|
);
|
|
match rects.values().filter(|rect| rect.overlaps(wanted)).map(|rect| rect.bottom()).max() {
|
|
Some(bottom) => spot.y = bottom + ROW_GAP,
|
|
None => break,
|
|
}
|
|
}
|
|
spot
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::model::test_support::garden;
|
|
use crate::ui::glyphs::Glyphs;
|
|
use crate::view::canvas::boxes::box_rects;
|
|
|
|
#[test]
|
|
fn new_nodes_find_a_free_spot_nearby() {
|
|
let (doc, g) = garden();
|
|
let rects = box_rects(&doc, 30, Glyphs::Unicode);
|
|
let size = CanvasSize::new(10, 3);
|
|
assert_eq!(child_spot(&doc, g.root, size, &rects), CanvasPos::new(22, 12), "below the last child");
|
|
assert_eq!(
|
|
child_spot(&doc, g.cedar, size, &rects),
|
|
CanvasPos::new(55, 4),
|
|
"first child: level with it"
|
|
);
|
|
assert_eq!(
|
|
below_spot(g.soil, size, &rects),
|
|
CanvasPos::new(22, 12),
|
|
"Raised beds and Watering are in the way"
|
|
);
|
|
}
|
|
}
|