mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
fix(edgeless): show text outside its stored frame
Keep the stored width for line wrapping. Use the laid-out text bounds to decide if text is visible.
This commit is contained in:
1 parent
46c44feba2
commit
063afdfdca
2 files changed
+353
-1
No files matched your search
@@ -459,7 +459,6 @@ fn layout_items(
|
||||
slant: &s.font_style,
|
||||
align: s.text_align,
|
||||
};
|
||||
item.label_clip = Some(b);
|
||||
if let Some((layout, _)) = style.layout(&s.text, w, theme, window.text_system()) {
|
||||
item.label(Label {
|
||||
layout: Rc::new(layout),
|
||||
|
||||
@@ -0,0 +1,353 @@
|
||||
use std::{
|
||||
collections::{BTreeMap, HashMap},
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
use blocksuite_editor::{
|
||||
FormFactor, PlaygroundTheme,
|
||||
edgeless::{
|
||||
DragItemInitial, EdgelessCamera, EdgelessCanvasState, EdgelessGesture, EdgelessRenderInput,
|
||||
build_edgeless_layer_prepared, prepare_edgeless_scene,
|
||||
},
|
||||
register_bundled_fonts,
|
||||
};
|
||||
use blocksuite_playground_doc::read::{
|
||||
BlockContent, BlockNode, PageSnapshot, RichText, SurfaceElement, SurfaceElementKind,
|
||||
};
|
||||
use image::RgbaImage;
|
||||
use serde_json::{Value, json};
|
||||
use ui::{
|
||||
AppContext as _, Application, ApplicationHandle, Bounds, HeadlessPlatform, IntoElement, Render,
|
||||
Window, WindowBounds, WindowHandle, WindowOptions, point, px, size,
|
||||
};
|
||||
use y_octo::{TextDeltaOp, TextInsert};
|
||||
|
||||
const WIDTH: u32 = 400;
|
||||
const HEIGHT: u32 = 240;
|
||||
const TEXT_X: u32 = 80;
|
||||
const TEXT_Y: u32 = 80;
|
||||
const TEXT_W: u32 = 90;
|
||||
const TEXT_H: u32 = 20;
|
||||
const SHAPE_X: u32 = 288;
|
||||
const SHAPE_H: u32 = 30;
|
||||
const SHAPE_BOTTOM: u32 = TEXT_Y + SHAPE_H;
|
||||
|
||||
fn element(id: &str, properties: Value, label: &str) -> SurfaceElement {
|
||||
let text = if label.is_empty() {
|
||||
RichText::default()
|
||||
} else {
|
||||
RichText {
|
||||
plain: label.into(),
|
||||
delta: vec![TextDeltaOp::Insert {
|
||||
insert: TextInsert::Text(label.into()),
|
||||
format: None,
|
||||
}],
|
||||
}
|
||||
};
|
||||
SurfaceElement::from_properties(id.into(), properties, text)
|
||||
}
|
||||
|
||||
fn standalone_text(id: &str, x: u32, y: u32, rotate: f64, label: &str) -> SurfaceElement {
|
||||
element(
|
||||
id,
|
||||
json!({
|
||||
"type": "text", "xywh": [x, y, TEXT_W, TEXT_H],
|
||||
"rotate": rotate, "fontSize": 24, "textAlign": "left",
|
||||
}),
|
||||
label,
|
||||
)
|
||||
}
|
||||
|
||||
fn shape(label: &str) -> SurfaceElement {
|
||||
element(
|
||||
"shape",
|
||||
json!({
|
||||
"type": "shape", "index": "a1", "xywh": [SHAPE_X, TEXT_Y, 100, SHAPE_H],
|
||||
"filled": true, "fillColor": "#758daa", "strokeStyle": "none",
|
||||
"fontSize": 20, "padding": [3, 6], "textVerticalAlign": "top",
|
||||
}),
|
||||
label,
|
||||
)
|
||||
}
|
||||
|
||||
fn snapshot(elements: Vec<SurfaceElement>) -> PageSnapshot {
|
||||
PageSnapshot {
|
||||
doc_id: "board".into(),
|
||||
root: Some(BlockNode {
|
||||
id: "page".into(),
|
||||
flavour: "affine:page".into(),
|
||||
version: None,
|
||||
props: BTreeMap::new(),
|
||||
content: BlockContent::Page {
|
||||
title: RichText::default(),
|
||||
},
|
||||
children: vec![BlockNode {
|
||||
id: "surface".into(),
|
||||
flavour: "affine:surface".into(),
|
||||
version: None,
|
||||
props: BTreeMap::new(),
|
||||
content: BlockContent::Surface {
|
||||
element_count: elements.len(),
|
||||
elements,
|
||||
},
|
||||
children: Vec::new(),
|
||||
}],
|
||||
}),
|
||||
detached: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
struct Board {
|
||||
state: EdgelessCanvasState,
|
||||
snapshot: PageSnapshot,
|
||||
revision: u64,
|
||||
theme: PlaygroundTheme,
|
||||
}
|
||||
|
||||
impl Render for Board {
|
||||
fn render(&mut self, window: &mut Window, _: &mut ui::Context<Self>) -> impl IntoElement {
|
||||
let input = EdgelessRenderInput {
|
||||
snapshot: &self.snapshot,
|
||||
doc_rev: self.revision,
|
||||
env_rev: 1,
|
||||
preview_rev: 0,
|
||||
preedit_nonce: 0,
|
||||
theme: self.theme,
|
||||
palette: &self.theme.palette(),
|
||||
page_titles: &BTreeMap::new(),
|
||||
images: &HashMap::new(),
|
||||
pdf_previews: None,
|
||||
doc_directory: None,
|
||||
viewport_width: WIDTH as f32,
|
||||
viewport_height: HEIGHT as f32,
|
||||
direction: ui::Direction::Ltr,
|
||||
form_factor: FormFactor::Desktop,
|
||||
utc_offset_minutes: 0,
|
||||
utc_offset_at: None,
|
||||
editing_selection: None,
|
||||
local_collapsed: None,
|
||||
};
|
||||
let scene = prepare_edgeless_scene(&mut self.state, &input, window);
|
||||
build_edgeless_layer_prepared(&mut self.state, scene, &input, window, Vec::new(), true, [])
|
||||
}
|
||||
}
|
||||
|
||||
struct OffscreenBoard {
|
||||
app: ApplicationHandle,
|
||||
platform: Rc<HeadlessPlatform>,
|
||||
window: WindowHandle<Board>,
|
||||
}
|
||||
|
||||
impl OffscreenBoard {
|
||||
fn new(elements: Vec<SurfaceElement>, theme: PlaygroundTheme) -> Self {
|
||||
let platform = HeadlessPlatform::new(WIDTH, HEIGHT, 1.);
|
||||
let app = Application::with_platform(platform.clone())
|
||||
.run_embedded(|cx| register_bundled_fonts(cx).unwrap());
|
||||
let options = WindowOptions {
|
||||
window_bounds: Some(WindowBounds::Windowed(Bounds::new(
|
||||
point(px(0.), px(0.)),
|
||||
size(px(WIDTH as f32), px(HEIGHT as f32)),
|
||||
))),
|
||||
..Default::default()
|
||||
};
|
||||
let window = app
|
||||
.update(|cx| {
|
||||
cx.open_window(options, |_, cx| {
|
||||
cx.new(|_| Board {
|
||||
state: canvas_state(EdgelessCamera::default()),
|
||||
snapshot: snapshot(elements),
|
||||
revision: 1,
|
||||
theme,
|
||||
})
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
Self {
|
||||
app,
|
||||
platform,
|
||||
window,
|
||||
}
|
||||
}
|
||||
|
||||
fn capture(&self, update: impl FnOnce(&mut Board)) -> RgbaImage {
|
||||
self.app
|
||||
.update(|cx| {
|
||||
self.window.update(cx, |board, _, cx| {
|
||||
update(board);
|
||||
cx.notify();
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
self.platform.windows()[0].request_frame();
|
||||
self.app
|
||||
.update(|cx| {
|
||||
self.window
|
||||
.update(cx, |_, window, _| window.render_to_image().unwrap())
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
fn canvas_state(camera: EdgelessCamera) -> EdgelessCanvasState {
|
||||
let mut state = EdgelessCanvasState::new();
|
||||
state.camera = camera;
|
||||
state.camera_initialized = true;
|
||||
state
|
||||
}
|
||||
|
||||
fn changed_pixels(
|
||||
actual: &RgbaImage,
|
||||
reference: &RgbaImage,
|
||||
region: impl Fn(u32, u32) -> bool,
|
||||
) -> usize {
|
||||
assert_eq!(actual.dimensions(), (WIDTH, HEIGHT));
|
||||
assert_eq!(actual.dimensions(), reference.dimensions());
|
||||
actual
|
||||
.enumerate_pixels()
|
||||
.filter(|&(x, y, pixel)| region(x, y) && pixel != reference.get_pixel(x, y))
|
||||
.count()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrapped_surface_text_paints_below_its_stored_frame_in_both_themes() {
|
||||
for theme in [PlaygroundTheme::Light, PlaygroundTheme::Dark] {
|
||||
let board = OffscreenBoard::new(
|
||||
vec![standalone_text(
|
||||
"text",
|
||||
TEXT_X,
|
||||
TEXT_Y,
|
||||
0.,
|
||||
"Direct Drive Access",
|
||||
)],
|
||||
theme,
|
||||
);
|
||||
let text = board.capture(|_| {});
|
||||
let background = board.capture(|board| {
|
||||
assert_eq!(
|
||||
board
|
||||
.state
|
||||
.authoring
|
||||
.hit_test_world(90., 90., 1.)
|
||||
.unwrap()
|
||||
.id,
|
||||
"text"
|
||||
);
|
||||
assert!(
|
||||
board
|
||||
.state
|
||||
.authoring
|
||||
.hit_test_world(90., 110., 1.)
|
||||
.is_none()
|
||||
);
|
||||
board.snapshot = snapshot(Vec::new());
|
||||
board.revision += 1;
|
||||
});
|
||||
let wrapped_ink = changed_pixels(&text, &background, |x, y| {
|
||||
(TEXT_X..TEXT_X + TEXT_W).contains(&x) && y > TEXT_Y + TEXT_H
|
||||
});
|
||||
assert!(
|
||||
wrapped_ink > 200,
|
||||
"{theme:?}: {wrapped_ink} overflow pixels"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overflow_stays_visible_when_the_stored_frame_is_above_the_viewport() {
|
||||
let board = OffscreenBoard::new(
|
||||
vec![standalone_text(
|
||||
"text",
|
||||
TEXT_X,
|
||||
TEXT_Y,
|
||||
0.,
|
||||
"Direct Drive Access",
|
||||
)],
|
||||
PlaygroundTheme::Light,
|
||||
);
|
||||
board.capture(|_| {});
|
||||
let text = board.capture(|board| board.state.camera.pan_y = (TEXT_Y + TEXT_H + 1) as f32);
|
||||
let background = board.capture(|board| {
|
||||
board.snapshot = snapshot(Vec::new());
|
||||
board.revision += 1;
|
||||
});
|
||||
let visible_ink = changed_pixels(&text, &background, |x, y| {
|
||||
(TEXT_X..TEXT_X + TEXT_W).contains(&x) && y < 64
|
||||
});
|
||||
assert!(
|
||||
visible_ink > 200,
|
||||
"{visible_ink} pixels at the top of the viewport"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fractional_drag_matches_fresh_layout_and_keeps_the_shape_label_clipped() {
|
||||
let mut elements = vec![
|
||||
standalone_text("rotated", TEXT_X, TEXT_Y, 23., "Direct Drive Access"),
|
||||
standalone_text("upright", TEXT_X + 108, TEXT_Y, 0., "Messaging Plan"),
|
||||
shape("Attached\nlabel"),
|
||||
];
|
||||
let board = OffscreenBoard::new(elements.clone(), PlaygroundTheme::Light);
|
||||
let (dx, dy) = (16.375, 10.625);
|
||||
let before = board.capture(|_| {});
|
||||
let incremental = board.capture(|board| {
|
||||
board.state.authoring.gesture = Some(EdgelessGesture::DragSelection {
|
||||
start_world: (0., 0.),
|
||||
current_world: (dx, dy),
|
||||
initial: board
|
||||
.state
|
||||
.authoring
|
||||
.painted_items
|
||||
.iter()
|
||||
.filter(|item| item.id != "shape")
|
||||
.map(|item| DragItemInitial {
|
||||
id: item.id.clone(),
|
||||
kind: item.kind,
|
||||
surface_id: item.surface_id.clone(),
|
||||
rect: item.rect,
|
||||
rotate_deg: item.rotate_deg,
|
||||
points: None,
|
||||
connector_source: None,
|
||||
connector_target: None,
|
||||
connector_label_xywh: None,
|
||||
})
|
||||
.collect(),
|
||||
shift: false,
|
||||
alt_clone: false,
|
||||
pending_dblclick: None,
|
||||
});
|
||||
});
|
||||
for item in &mut elements {
|
||||
if let SurfaceElementKind::Text(text) = &mut item.kind {
|
||||
text.xywh[0] += f64::from(dx);
|
||||
text.xywh[1] += f64::from(dy);
|
||||
}
|
||||
}
|
||||
let fresh = board.capture(|board| {
|
||||
board.state = canvas_state(EdgelessCamera::default());
|
||||
board.snapshot = snapshot(elements.clone());
|
||||
board.revision += 1;
|
||||
});
|
||||
assert!(changed_pixels(&incremental, &before, |_, _| true) > 200);
|
||||
assert_eq!(changed_pixels(&incremental, &fresh, |_, _| true), 0);
|
||||
|
||||
let shape_only = board.capture(|board| {
|
||||
board.snapshot = snapshot(vec![elements[2].clone()]);
|
||||
board.revision += 1;
|
||||
});
|
||||
let rotated_ink = changed_pixels(&incremental, &shape_only, |x, y| x < 170 && y > 130);
|
||||
assert!(
|
||||
rotated_ink > 200,
|
||||
"{rotated_ink} pixels below the rotated frame"
|
||||
);
|
||||
|
||||
elements[2] = shape("");
|
||||
let unlabeled = board.capture(|board| {
|
||||
board.snapshot = snapshot(elements);
|
||||
board.revision += 1;
|
||||
});
|
||||
assert!(changed_pixels(&fresh, &unlabeled, |x, y| x >= SHAPE_X && y < SHAPE_BOTTOM) > 100);
|
||||
assert_eq!(
|
||||
changed_pixels(&fresh, &unlabeled, |_, y| y >= SHAPE_BOTTOM),
|
||||
0
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user