documents and editor: typewriter room at both ends, open blocks scaled, a resize re-centers
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
508fee9d15
commit
f102ea0b6c
6 files changed
+189
-14
No files matched your search
@@ -58,22 +58,45 @@ impl MarkdownDocument {
|
||||
}
|
||||
}
|
||||
|
||||
/// Half a view of room at each end, for typewriter.
|
||||
pub(super) fn make_room(&mut self) {
|
||||
let room = match self.look.typewriter && self.mode == MarkdownMode::Visual {
|
||||
true => self.list.viewport_bounds().size.height / 2.0,
|
||||
false => gpui::Pixels::ZERO,
|
||||
};
|
||||
if room != self.room {
|
||||
self.room = room;
|
||||
let last = self.parsed.blocks.len();
|
||||
self.list.remeasure_items(0..1);
|
||||
self.list.remeasure_items(last..last + 1);
|
||||
}
|
||||
}
|
||||
|
||||
/// The current block mid-view once it or its text moves.
|
||||
pub(super) fn center_current(&mut self, cx: &gpui::App) {
|
||||
if !self.look.typewriter || self.mode != MarkdownMode::Visual {
|
||||
return;
|
||||
}
|
||||
let ix = self.current_block(cx);
|
||||
let Some(block) = self.list.bounds_for_item(ix) else {
|
||||
let Some(mut block) = self.list.bounds_for_item(ix) else {
|
||||
return;
|
||||
};
|
||||
// A new layout moves it too: its height counts.
|
||||
let height = f32::from(block.size.height).round() as u64;
|
||||
let moved = (ix, self.edits, self.typed, height);
|
||||
// The ends' room is no block.
|
||||
if ix == 0 {
|
||||
block.origin.y += self.room;
|
||||
block.size.height -= self.room;
|
||||
}
|
||||
if ix == self.parsed.blocks.len() {
|
||||
block.size.height -= self.room;
|
||||
}
|
||||
let view = self.list.viewport_bounds();
|
||||
// A new layout or a resize moves it too.
|
||||
let heights =
|
||||
[block.size.height, view.size.height].map(|height| f32::from(height).round() as u64);
|
||||
let moved = (ix, self.edits, self.typed, heights);
|
||||
if self.centered == Some(moved) {
|
||||
return;
|
||||
}
|
||||
let view = self.list.viewport_bounds();
|
||||
self.list.scroll_by(block.center().y - view.center().y);
|
||||
self.centered = Some(moved);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ use std::{ops::Range, path::Path, rc::Rc, sync::Arc, time::Duration};
|
||||
|
||||
use gpui::{
|
||||
App, AppContext as _, Context, Entity, EventEmitter, FocusHandle, Focusable, ListAlignment,
|
||||
ListState, Subscription, Task, Window,
|
||||
ListState, Pixels, Subscription, Task, Window,
|
||||
};
|
||||
|
||||
pub use attach::Attachment;
|
||||
@@ -88,8 +88,10 @@ pub struct MarkdownDocument {
|
||||
look: DocumentLook,
|
||||
/// Typing in the open block, counted.
|
||||
typed: u64,
|
||||
/// The block, text, typing and height last centered.
|
||||
centered: Option<(usize, u64, u64, u64)>,
|
||||
/// Typewriter room past each end of the list.
|
||||
room: Pixels,
|
||||
/// The block, text, typing and heights last centered.
|
||||
centered: Option<(usize, u64, u64, [u64; 2])>,
|
||||
focus: FocusHandle,
|
||||
on_link: Option<OnLink>,
|
||||
_subscriptions: Vec<Subscription>,
|
||||
@@ -146,6 +148,7 @@ impl MarkdownDocument {
|
||||
look: DocumentLook::default(),
|
||||
typed: 0,
|
||||
centered: None,
|
||||
room: Pixels::ZERO,
|
||||
focus,
|
||||
on_link: None,
|
||||
_subscriptions: subscriptions,
|
||||
|
||||
@@ -106,3 +106,113 @@ fn focus_counts_the_open_block_as_the_readers(cx: &mut TestAppContext) {
|
||||
3
|
||||
);
|
||||
}
|
||||
|
||||
fn typewriter() -> DocumentLook {
|
||||
DocumentLook {
|
||||
typewriter: true,
|
||||
..DocumentLook::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// How far block `ix` sits from mid-view.
|
||||
fn off_middle(
|
||||
document: &gpui::Entity<super::MarkdownDocument>,
|
||||
ix: usize,
|
||||
cx: &mut gpui::VisualTestContext,
|
||||
) -> gpui::Pixels {
|
||||
document.read_with(cx, |document, _| {
|
||||
let mut block = document.list.bounds_for_item(ix).expect("laid out");
|
||||
if ix == 0 {
|
||||
block.origin.y += document.room;
|
||||
block.size.height -= document.room;
|
||||
}
|
||||
if ix == document.parsed.blocks.len() {
|
||||
block.size.height -= document.room;
|
||||
}
|
||||
(block.center().y - document.list.viewport_bounds().center().y).abs()
|
||||
})
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn an_open_block_takes_the_scale(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open("A paragraph.\n", MarkdownMode::Visual, cx);
|
||||
document.update_in(cx, |document, window, cx| {
|
||||
document.open(Some(0), false, window, cx)
|
||||
});
|
||||
frames(cx);
|
||||
let plain = document
|
||||
.read_with(cx, |document, _| document.list.bounds_for_item(0))
|
||||
.expect("laid out");
|
||||
let large = DocumentLook {
|
||||
scale: 2.0,
|
||||
..DocumentLook::default()
|
||||
};
|
||||
document.update(cx, |document, cx| document.set_look(large, cx));
|
||||
frames(cx);
|
||||
let scaled = document
|
||||
.read_with(cx, |document, _| document.list.bounds_for_item(0))
|
||||
.expect("laid out");
|
||||
assert!(
|
||||
scaled.size.height > plain.size.height * 1.5,
|
||||
"{plain:?} to {scaled:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_resize_centers_the_block_again(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open(&long(), MarkdownMode::Visual, cx);
|
||||
document.update_in(cx, |document, window, cx| {
|
||||
document.set_look(typewriter(), cx);
|
||||
document.open(Some(40), false, window, cx);
|
||||
document.list.scroll_to_reveal_item(40);
|
||||
});
|
||||
frames(cx);
|
||||
cx.simulate_resize(gpui::size(px(1000.0), px(400.0)));
|
||||
frames(cx);
|
||||
assert!(off_middle(&document, 40, cx) < px(4.0));
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn the_first_and_last_blocks_center(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open(&long(), MarkdownMode::Visual, cx);
|
||||
document.update_in(cx, |document, window, cx| {
|
||||
document.set_look(typewriter(), cx);
|
||||
document.open(Some(0), false, window, cx);
|
||||
});
|
||||
frames(cx);
|
||||
let first = off_middle(&document, 0, cx);
|
||||
assert!(first < px(4.0), "the first is {first:?} off");
|
||||
document.update_in(cx, |document, window, cx| {
|
||||
document.open(Some(59), false, window, cx);
|
||||
document.list.scroll_to_reveal_item(59);
|
||||
});
|
||||
frames(cx);
|
||||
assert!(off_middle(&document, 59, cx) < px(4.0), "the last");
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn the_last_source_line_centers_and_a_resize_follows(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open(&long(), MarkdownMode::Source, cx);
|
||||
document.update(cx, |document, cx| {
|
||||
document.set_look(typewriter(), cx);
|
||||
document.set_caret(119, 0, cx);
|
||||
});
|
||||
frames(cx);
|
||||
let top = document.read_with(cx, |document, cx| document.top_line(cx));
|
||||
assert!(
|
||||
top > 80,
|
||||
"top {top}: the last line is mid-view, not at the bottom"
|
||||
);
|
||||
cx.simulate_resize(gpui::size(px(1000.0), px(400.0)));
|
||||
frames(cx);
|
||||
let resized = document.read_with(cx, |document, cx| document.top_line(cx));
|
||||
let editor = document.read_with(cx, |document, _| document.editor().clone());
|
||||
let (line, height) =
|
||||
editor.read_with(cx, |editor, _| (editor.metrics.line, editor.metrics.height));
|
||||
let half = (height / line / 2.0).floor() as usize;
|
||||
let middle = 119 - half;
|
||||
assert!(
|
||||
resized.abs_diff(middle) <= 1,
|
||||
"top {top} to {resized}; mid-view is {middle}"
|
||||
);
|
||||
}
|
||||
@@ -42,8 +42,19 @@ pub(crate) fn bind_keys(cx: &mut App) {
|
||||
}
|
||||
|
||||
impl MarkdownDocument {
|
||||
/// List item `ix`: a block, or the end.
|
||||
fn item(&mut self, ix: usize, _: &mut Window, cx: &mut Context<Self>) -> AnyElement {
|
||||
/// List item `ix`, with typewriter room at the ends.
|
||||
fn item(&mut self, ix: usize, window: &mut Window, cx: &mut Context<Self>) -> AnyElement {
|
||||
let (first, last) = (ix == 0, ix == self.parsed.blocks.len());
|
||||
let room = self.room;
|
||||
div()
|
||||
.when(first, |item| item.pt(room))
|
||||
.when(last, |item| item.pb(room))
|
||||
.child(self.block_item(ix, window, cx))
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
/// A block, or the end.
|
||||
fn block_item(&mut self, ix: usize, _: &mut Window, cx: &mut Context<Self>) -> AnyElement {
|
||||
let theme = cx.theme();
|
||||
let colors = theme.colors.clone();
|
||||
let (radius, body, mono) = (
|
||||
@@ -57,6 +68,7 @@ impl MarkdownDocument {
|
||||
let open = self.open.as_ref();
|
||||
let field = |open: &super::Open| {
|
||||
div()
|
||||
.text_size(body)
|
||||
.px_3()
|
||||
.py_2()
|
||||
.rounded(radius)
|
||||
@@ -164,6 +176,8 @@ impl Render for MarkdownDocument {
|
||||
cx.emit(super::Attachment::Files(dropped.paths().to_vec()));
|
||||
}));
|
||||
self.center_current(cx);
|
||||
// Typewriter: half a view past each end.
|
||||
self.make_room();
|
||||
let blocks = list(self.list.clone(), cx.processor(Self::item)).size_full();
|
||||
// The text column: its measure, font, centered.
|
||||
let column = |content: gpui::AnyElement, look: &super::DocumentLook| {
|
||||
|
||||
Reference in new issue
Block a user