Pull the view interface, actions and modes out of the canvas example
The canvas example did everything itself; now it runs the way the app will. The key engine turns keys into commands, the focused view answers with actions, and the commands every view shares (undo, marks, adding, deleting, folds, tree moves, modes) answer the rest. A view can still give a key its own meaning, since it gets the first say. `:` lines become actions the same way (app::calls). - View trait: kind, contexts (its key tables), layout, render, command and mouse. layout comes before render because a view keeps the frame it drew, to match clicks against, and follows the cursor with its camera; drawing changes nothing. CanvasView owns the camera, the last scene and the mouse drag; it reads the rest from a ViewContext. - Edits travel as data (EditOp), so views never touch the document. Buffer::apply is the one place a document changes: the edit, its undo step, where the cursor goes, marks and `''`. - Mode holds each mode's own state: move mode carries its Grab (whole branch or not), the command line lives in Mode::Command. - Undo groups: a whole move-mode session or mouse drag is one undo step, built up as it goes. After an undo the cursor lands on a node the change added or removed, before the others it touched. - Actions run in order, and a problem stops the rest. Small improvements on the way: zz centers the middle of the node, :find says which match it's on, :unlink with no links says so, folding a node without children says why nothing happened, and a captured idea is placed next to a new Inbox even on the first :in. The example has end-to-end tests now: they type key sequences and mouse gestures into it on a test terminal, and `cargo test` runs them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
57ba2c275d
commit
6eedbe0ce2
21 files changed
+2085
-763
No files matched your search
@@ -8,32 +8,18 @@ use ratatui::layout::Size;
|
||||
use ratatui::style::Color;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use super::boxes::{DEFAULT_MAX_WIDTH, NodeBox, truncate};
|
||||
use super::boxes::{NodeBox, truncate};
|
||||
use super::camera::{Camera, ScreenPos, ScreenRect, Zoom};
|
||||
use super::lines::{LineLayer, LineOwner, Paint};
|
||||
use super::route::{Bend, route};
|
||||
use crate::model::{Arrows, Border, Content, Document, Node, NodeId, Place, Visible};
|
||||
use crate::ui::glyphs::Glyphs;
|
||||
use crate::ui::theme::{LineStyle, Theme};
|
||||
use crate::view::ViewSettings;
|
||||
|
||||
/// How many columns of a title show at Far zoom.
|
||||
const FAR_TITLE_WIDTH: usize = 14;
|
||||
|
||||
/// What a scene needs besides the document, the cursor and the camera.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct SceneOptions {
|
||||
/// The width titles wrap at.
|
||||
pub max_width: u16,
|
||||
/// Focus mode: Open branches act like Auto ones.
|
||||
pub focus: bool,
|
||||
}
|
||||
|
||||
impl Default for SceneOptions {
|
||||
fn default() -> SceneOptions {
|
||||
SceneOptions { max_width: DEFAULT_MAX_WIDTH, focus: false }
|
||||
}
|
||||
}
|
||||
|
||||
/// How a node is drawn at the camera's zoom.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Shape {
|
||||
@@ -95,9 +81,9 @@ impl Scene {
|
||||
camera: Camera,
|
||||
size: Size,
|
||||
theme: &Theme,
|
||||
options: SceneOptions,
|
||||
settings: &ViewSettings,
|
||||
) -> Scene {
|
||||
let visible = Visible::new(doc, cursor, options.focus);
|
||||
let visible = Visible::new(doc, cursor, settings.focus);
|
||||
let mut scene = Scene {
|
||||
size,
|
||||
zoom: camera.zoom,
|
||||
@@ -112,7 +98,8 @@ impl Scene {
|
||||
let Some(node) = doc.node(id) else { continue };
|
||||
let style = theme.node_style(node, id == doc.root());
|
||||
let badge = visible.hidden.get(&id).map(|count| format!("+{count}")).unwrap_or_default();
|
||||
let (rect, shape) = shape(node, badge, style.border, camera, theme.glyphs, options.max_width);
|
||||
let (rect, shape) =
|
||||
shape(node, badge, style.border, camera, theme.glyphs, settings.max_node_width);
|
||||
scene.nodes.push(SceneNode { id, rect, shape, color: style.color, selected: id == cursor });
|
||||
}
|
||||
// The cursor's node goes last, on top of any it overlaps.
|
||||
|
||||
Reference in new issue
Block a user