mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
feat(editor): copy Edgeless selections as images
Render the selected scene to PNG for the native clipboard. Split external multi-select text on commas and keep structured table labels intact.
This commit is contained in:
1 parent
3fe764490f
commit
169df763f2
16 files changed
+1148
-284
No files matched your search
@@ -301,8 +301,7 @@ fn write_cells<'a>(
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
_ => option_labels_from_text(&value, options)
|
||||
.into_iter()
|
||||
_ => option_labels_from_text(&value)
|
||||
.map(|label| (label, None))
|
||||
.collect::<Vec<_>>(),
|
||||
};
|
||||
@@ -385,25 +384,10 @@ fn write_cells<'a>(
|
||||
.map(|commit| commit.is_some())
|
||||
}
|
||||
|
||||
fn option_labels_from_text<'a>(value: &'a str, options: &[DatabaseSelectOption]) -> Vec<&'a str> {
|
||||
if options.iter().any(|option| option.value == value) {
|
||||
return vec![value];
|
||||
}
|
||||
|
||||
let mut remaining = value.trim();
|
||||
let mut labels = Vec::new();
|
||||
while !remaining.is_empty() {
|
||||
let (label, rest) = remaining
|
||||
.match_indices(',')
|
||||
.map(|(end, _)| (remaining[..end].trim_end(), &remaining[end + 1..]))
|
||||
.chain(std::iter::once((remaining, "")))
|
||||
.rfind(|(label, _)| options.iter().any(|opt| opt.value == *label))
|
||||
.unwrap_or_else(|| remaining.split_once(',').unwrap_or((remaining, "")));
|
||||
let label = label.trim_end();
|
||||
if !label.is_empty() {
|
||||
labels.push(label);
|
||||
}
|
||||
remaining = rest.trim_start();
|
||||
}
|
||||
labels
|
||||
/// BlockSuite `fromString` for select columns: split at every comma, trim, drop empties.
|
||||
fn option_labels_from_text(value: &str) -> impl Iterator<Item = &str> {
|
||||
value
|
||||
.split(',')
|
||||
.map(str::trim)
|
||||
.filter(|label| !label.is_empty())
|
||||
}
|
||||
@@ -110,7 +110,7 @@ pub struct EdgelessCamera {
|
||||
pub pan_x: f32,
|
||||
/// World Y coordinate at the top-left of the viewport.
|
||||
pub pan_y: f32,
|
||||
/// Uniform zoom factor in `[MIN_ZOOM, MAX_ZOOM]`.
|
||||
/// Positive zoom factor. Interactive zoom stays in `[MIN_ZOOM, MAX_ZOOM]`.
|
||||
pub zoom: f32,
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ impl EdgelessCamera {
|
||||
screen: Point<Pixels>,
|
||||
viewport_origin: Point<Pixels>,
|
||||
) -> Point<Pixels> {
|
||||
let z = self.zoom.max(MIN_ZOOM);
|
||||
let z = self.zoom.max(f32::MIN_POSITIVE);
|
||||
point(
|
||||
px((f32::from(screen.x) - f32::from(viewport_origin.x)) / z + self.pan_x),
|
||||
px((f32::from(screen.y) - f32::from(viewport_origin.y)) / z + self.pan_y),
|
||||
@@ -171,7 +171,7 @@ impl EdgelessCamera {
|
||||
|
||||
/// Visible world-space rectangle covered by a viewport of `viewport_size`.
|
||||
pub fn visible_world_bounds(&self, viewport_size: Size<Pixels>) -> WorldRect {
|
||||
let z = self.zoom.max(MIN_ZOOM);
|
||||
let z = self.zoom.max(f32::MIN_POSITIVE);
|
||||
WorldRect::new(
|
||||
self.pan_x,
|
||||
self.pan_y,
|
||||
@@ -288,7 +288,7 @@ impl EdgelessCamera {
|
||||
|
||||
/// World coordinate at the center of `viewport_size`.
|
||||
pub fn viewport_center_world(&self, viewport_size: Size<Pixels>) -> (f32, f32) {
|
||||
let z = self.zoom.max(MIN_ZOOM);
|
||||
let z = self.zoom.max(f32::MIN_POSITIVE);
|
||||
(
|
||||
self.pan_x + f32::from(viewport_size.width) / (2.0 * z),
|
||||
self.pan_y + f32::from(viewport_size.height) / (2.0 * z),
|
||||
|
||||
@@ -123,6 +123,8 @@ pub struct EdgelessCanvasState {
|
||||
pub last_frame_relayouts: u64,
|
||||
/// Notes and synced-doc embeds that can carry a header, indexed per page snapshot.
|
||||
pub note_headers: NoteHeaderIndexCache,
|
||||
/// Temporary whitelist of element ids kept visible during offscreen image capture.
|
||||
pub capture_keep_ids: Option<BTreeSet<String>>,
|
||||
}
|
||||
|
||||
/// A root Note that can carry a header: its position in `root.children` and its id hash.
|
||||
|
||||
@@ -99,84 +99,91 @@ pub fn build_edgeless_layer_prepared(
|
||||
let camera = state.camera;
|
||||
let palette = *input.palette;
|
||||
let theme = input.theme;
|
||||
let capture_keep_ids = state.capture_keep_ids.clone();
|
||||
let capturing = capture_keep_ids.is_some();
|
||||
let visible_world =
|
||||
camera.visible_world_bounds(size(px(input.viewport_width), px(input.viewport_height)));
|
||||
|
||||
let snapshot_markers = ui::harness::snapshot_children(|markers| {
|
||||
let vp_origin = point(px(0.0), px(0.0));
|
||||
let push_marker = |markers: &mut Vec<AnyElement>,
|
||||
id: std::fmt::Arguments<'_>,
|
||||
rect: WorldRect,
|
||||
text: Option<&str>| {
|
||||
let tl = camera.world_to_screen(point(px(rect.x), px(rect.y)), vp_origin);
|
||||
let br = camera.world_to_screen(point(px(rect.right()), px(rect.bottom())), vp_origin);
|
||||
let x = f32::from(tl.x);
|
||||
let y = f32::from(tl.y);
|
||||
let w = f32::from(br.x) - x;
|
||||
let h = f32::from(br.y) - y;
|
||||
if x >= 0.0
|
||||
&& y >= 0.0
|
||||
&& x + w <= input.viewport_width
|
||||
&& y + h <= input.viewport_height
|
||||
&& w >= 1.0
|
||||
&& h >= 1.0
|
||||
{
|
||||
let mut el = div()
|
||||
.absolute()
|
||||
.left(px(x))
|
||||
.top(px(y))
|
||||
.w(px(w))
|
||||
.h(px(h))
|
||||
.snapshot(SharedString::from(id), Some(EDGELESS_VIEWPORT_ID));
|
||||
if let Some(t) = text {
|
||||
el = el.snapshot_text(t);
|
||||
let snapshot_markers = if capturing {
|
||||
Vec::new()
|
||||
} else {
|
||||
ui::harness::snapshot_children(|markers| {
|
||||
let vp_origin = point(px(0.0), px(0.0));
|
||||
let push_marker = |markers: &mut Vec<AnyElement>,
|
||||
id: std::fmt::Arguments<'_>,
|
||||
rect: WorldRect,
|
||||
text: Option<&str>| {
|
||||
let tl = camera.world_to_screen(point(px(rect.x), px(rect.y)), vp_origin);
|
||||
let br =
|
||||
camera.world_to_screen(point(px(rect.right()), px(rect.bottom())), vp_origin);
|
||||
let x = f32::from(tl.x);
|
||||
let y = f32::from(tl.y);
|
||||
let w = f32::from(br.x) - x;
|
||||
let h = f32::from(br.y) - y;
|
||||
if x >= 0.0
|
||||
&& y >= 0.0
|
||||
&& x + w <= input.viewport_width
|
||||
&& y + h <= input.viewport_height
|
||||
&& w >= 1.0
|
||||
&& h >= 1.0
|
||||
{
|
||||
let mut el = div()
|
||||
.absolute()
|
||||
.left(px(x))
|
||||
.top(px(y))
|
||||
.w(px(w))
|
||||
.h(px(h))
|
||||
.snapshot(SharedString::from(id), Some(EDGELESS_VIEWPORT_ID));
|
||||
if let Some(t) = text {
|
||||
el = el.snapshot_text(t);
|
||||
}
|
||||
markers.push(el.into_any_element());
|
||||
}
|
||||
markers.push(el.into_any_element());
|
||||
}
|
||||
};
|
||||
for frame in &frames {
|
||||
push_marker(
|
||||
markers,
|
||||
format_args!("edgeless.frame.{}", frame.id),
|
||||
frame.rect,
|
||||
Some(frame.title.as_str()),
|
||||
);
|
||||
}
|
||||
for layout in surfaces.iter() {
|
||||
for (id, bounds) in layout.bounds() {
|
||||
};
|
||||
for frame in &frames {
|
||||
push_marker(
|
||||
markers,
|
||||
format_args!("edgeless.surface.{id}"),
|
||||
world_rect(bounds),
|
||||
format_args!("edgeless.frame.{}", frame.id),
|
||||
frame.rect,
|
||||
Some(frame.title.as_str()),
|
||||
);
|
||||
}
|
||||
for layout in surfaces.iter() {
|
||||
for (id, bounds) in layout.bounds() {
|
||||
push_marker(
|
||||
markers,
|
||||
format_args!("edgeless.surface.{id}"),
|
||||
world_rect(bounds),
|
||||
None,
|
||||
);
|
||||
}
|
||||
}
|
||||
for note in ¬es {
|
||||
push_marker(
|
||||
markers,
|
||||
format_args!("edgeless.note.{}", note.id),
|
||||
note.rect,
|
||||
None,
|
||||
);
|
||||
}
|
||||
}
|
||||
for note in ¬es {
|
||||
push_marker(
|
||||
markers,
|
||||
format_args!("edgeless.note.{}", note.id),
|
||||
note.rect,
|
||||
None,
|
||||
);
|
||||
}
|
||||
for et in &texts {
|
||||
push_marker(
|
||||
markers,
|
||||
format_args!("edgeless.text.{}", et.id),
|
||||
et.world_aabb,
|
||||
None,
|
||||
);
|
||||
}
|
||||
for sb in &surface_blocks {
|
||||
push_marker(
|
||||
markers,
|
||||
format_args!("edgeless.block.{}", sb.id),
|
||||
sb.world_aabb,
|
||||
None,
|
||||
);
|
||||
}
|
||||
});
|
||||
for et in &texts {
|
||||
push_marker(
|
||||
markers,
|
||||
format_args!("edgeless.text.{}", et.id),
|
||||
et.world_aabb,
|
||||
None,
|
||||
);
|
||||
}
|
||||
for sb in &surface_blocks {
|
||||
push_marker(
|
||||
markers,
|
||||
format_args!("edgeless.block.{}", sb.id),
|
||||
sb.world_aabb,
|
||||
None,
|
||||
);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let frame_border = theme
|
||||
.css_var("--affine-v2-edgeless-frame-border-default")
|
||||
@@ -184,73 +191,97 @@ pub fn build_edgeless_layer_prepared(
|
||||
let frame_border_active = theme
|
||||
.css_var("--affine-v2-edgeless-frame-border-active")
|
||||
.unwrap_or(palette.brand);
|
||||
let hovered_frame_id = super::hovered_frame_for_drag(
|
||||
&state.authoring.painted_items,
|
||||
state.authoring.gesture.as_ref(),
|
||||
);
|
||||
let remote_presence_overlays = super::build_remote_presence_overlay(
|
||||
&state.remote_peers,
|
||||
&state.authoring.painted_items,
|
||||
camera,
|
||||
input.viewport_width,
|
||||
input.viewport_height,
|
||||
);
|
||||
let authoring_overlays = build_edgeless_authoring_overlay(
|
||||
&state.authoring,
|
||||
camera,
|
||||
input.viewport_width,
|
||||
input.viewport_height,
|
||||
&palette,
|
||||
theme,
|
||||
window,
|
||||
);
|
||||
let editing_overlay: Option<(String, bool, Vec<FlowRect>, Option<FlowRect>)> = input
|
||||
.editing_selection
|
||||
.as_ref()
|
||||
.and_then(|(editing_id, is_note, sel, focus)| {
|
||||
let layout = state.cached_block_layout(editing_id, *is_note)?;
|
||||
let sel_rects = crate::overlays::text_selection_rects(
|
||||
sel,
|
||||
&layout,
|
||||
crate::overlays::TextSelectionStyle::EDGELESS,
|
||||
);
|
||||
let caret = sel
|
||||
.empty()
|
||||
.then(|| {
|
||||
layout
|
||||
.block(&focus.block_id)
|
||||
.and_then(|b| b.caret_rect(focus.caret))
|
||||
})
|
||||
.flatten();
|
||||
Some((editing_id.clone(), *is_note, sel_rects, caret))
|
||||
});
|
||||
let editing_label_id: Option<String> = match &state.authoring.inline_edit {
|
||||
Some(EdgelessInlineEdit::Label(ed)) => Some(ed.target.item_id().to_string()),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let erase_targets: std::collections::HashSet<String> = match &state.authoring.gesture {
|
||||
Some(super::EdgelessGesture::Erase { targets, .. }) => targets.iter().cloned().collect(),
|
||||
_ => std::collections::HashSet::new(),
|
||||
let (
|
||||
hovered_frame_id,
|
||||
remote_presence_overlays,
|
||||
authoring_overlays,
|
||||
editing_overlay,
|
||||
editing_label_id,
|
||||
erase_targets,
|
||||
) = if capturing {
|
||||
(
|
||||
None,
|
||||
Vec::new(),
|
||||
Vec::new(),
|
||||
None,
|
||||
None,
|
||||
std::collections::HashSet::new(),
|
||||
)
|
||||
} else {
|
||||
let hovered = super::hovered_frame_for_drag(
|
||||
&state.authoring.painted_items,
|
||||
state.authoring.gesture.as_ref(),
|
||||
);
|
||||
let remote = super::build_remote_presence_overlay(
|
||||
&state.remote_peers,
|
||||
&state.authoring.painted_items,
|
||||
camera,
|
||||
input.viewport_width,
|
||||
input.viewport_height,
|
||||
);
|
||||
let authoring = build_edgeless_authoring_overlay(
|
||||
&state.authoring,
|
||||
camera,
|
||||
input.viewport_width,
|
||||
input.viewport_height,
|
||||
&palette,
|
||||
theme,
|
||||
window,
|
||||
);
|
||||
let editing: Option<(String, bool, Vec<FlowRect>, Option<FlowRect>)> = input
|
||||
.editing_selection
|
||||
.as_ref()
|
||||
.and_then(|(editing_id, is_note, sel, focus)| {
|
||||
let layout = state.cached_block_layout(editing_id, *is_note)?;
|
||||
let sel_rects = crate::overlays::text_selection_rects(
|
||||
sel,
|
||||
&layout,
|
||||
crate::overlays::TextSelectionStyle::EDGELESS,
|
||||
);
|
||||
let caret = sel
|
||||
.empty()
|
||||
.then(|| {
|
||||
layout
|
||||
.block(&focus.block_id)
|
||||
.and_then(|b| b.caret_rect(focus.caret))
|
||||
})
|
||||
.flatten();
|
||||
Some((editing_id.clone(), *is_note, sel_rects, caret))
|
||||
});
|
||||
let label_id = match &state.authoring.inline_edit {
|
||||
Some(EdgelessInlineEdit::Label(ed)) => Some(ed.target.item_id().to_string()),
|
||||
_ => None,
|
||||
};
|
||||
let erase = match &state.authoring.gesture {
|
||||
Some(super::EdgelessGesture::Erase { targets, .. }) => {
|
||||
targets.iter().cloned().collect()
|
||||
}
|
||||
_ => std::collections::HashSet::new(),
|
||||
};
|
||||
(hovered, remote, authoring, editing, label_id, erase)
|
||||
};
|
||||
|
||||
let paint_canvas = canvas(
|
||||
|_, _, _| (),
|
||||
move |bounds, _, window, cx| {
|
||||
// 1. Single-draw-call procedural dot-grid background.
|
||||
let gap = bg_grid_gap(camera.zoom);
|
||||
let (phase_x, phase_y) = camera.dot_grid_phase(gap);
|
||||
window.paint_quad(ui::fill(
|
||||
bounds,
|
||||
Background::dot_grid(
|
||||
palette.background,
|
||||
palette.edgeless_grid,
|
||||
gap,
|
||||
1.0,
|
||||
phase_x,
|
||||
phase_y,
|
||||
),
|
||||
));
|
||||
// 1. Solid canvas fill during offscreen image capture; procedural dot-grid otherwise.
|
||||
if capturing {
|
||||
window.paint_quad(ui::fill(bounds, palette.background));
|
||||
} else {
|
||||
let gap = bg_grid_gap(camera.zoom);
|
||||
let (phase_x, phase_y) = camera.dot_grid_phase(gap);
|
||||
window.paint_quad(ui::fill(
|
||||
bounds,
|
||||
Background::dot_grid(
|
||||
palette.background,
|
||||
palette.edgeless_grid,
|
||||
gap,
|
||||
1.0,
|
||||
phase_x,
|
||||
phase_y,
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
let cam_matrix = camera.to_transformation_matrix(bounds.origin);
|
||||
let cull_world = visible_world.dilate(120.0);
|
||||
@@ -262,6 +293,19 @@ pub fn build_edgeless_layer_prepared(
|
||||
size(px(visible_world.w), px(visible_world.h)),
|
||||
);
|
||||
for item in order.iter() {
|
||||
let id = match *item {
|
||||
PaintItem::Frame(i) => frames[i].id.as_str(),
|
||||
PaintItem::Surface(l, i) => surfaces[l].item_id(i),
|
||||
PaintItem::Note(i) => notes[i].id.as_str(),
|
||||
PaintItem::Text(i) => texts[i].id.as_str(),
|
||||
PaintItem::SurfaceBlock(i) => surface_blocks[i].id.as_str(),
|
||||
};
|
||||
if capture_keep_ids
|
||||
.as_ref()
|
||||
.is_some_and(|keep| !keep.contains(id))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let paint = |window: &mut Window| match *item {
|
||||
PaintItem::Frame(i) => {
|
||||
let frame = &frames[i];
|
||||
@@ -364,7 +408,8 @@ pub fn build_edgeless_layer_prepared(
|
||||
),
|
||||
};
|
||||
// Compact Page reading-order badge, rendered ONLY when this Page Note is selected.
|
||||
if let Some(ref badge_inline) = note.page_order_badge {
|
||||
if !capturing && let Some(ref badge_inline) = note.page_order_badge
|
||||
{
|
||||
let inv_zoom = 1.0 / camera.zoom.max(0.1);
|
||||
let badge_box = 24.0_f32;
|
||||
let header_y = note.rect.y - (badge_box + 6.0) * inv_zoom;
|
||||
@@ -565,20 +610,9 @@ pub fn build_edgeless_layer_prepared(
|
||||
}
|
||||
}
|
||||
};
|
||||
let erase_opacity = if erase_targets.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let id = match *item {
|
||||
PaintItem::Frame(i) => frames[i].id.as_str(),
|
||||
PaintItem::Surface(l, i) => surfaces[l].item_id(i),
|
||||
PaintItem::Note(i) => notes[i].id.as_str(),
|
||||
PaintItem::Text(i) => texts[i].id.as_str(),
|
||||
PaintItem::SurfaceBlock(i) => surface_blocks[i].id.as_str(),
|
||||
};
|
||||
erase_targets
|
||||
.contains(id)
|
||||
.then_some(super::eraser::ERASE_TARGET_OPACITY)
|
||||
};
|
||||
let erase_opacity = (!erase_targets.is_empty()
|
||||
&& erase_targets.contains(id))
|
||||
.then_some(super::eraser::ERASE_TARGET_OPACITY);
|
||||
window.with_element_opacity(erase_opacity, paint);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -204,6 +204,7 @@ pub enum EdgelessElementAction {
|
||||
Copy,
|
||||
CopyElementLink,
|
||||
CopyAsMarkdown,
|
||||
CopyAsImage,
|
||||
OpenLinkedDoc(EdgelessDocOpenMode),
|
||||
TurnIntoLinkedDoc,
|
||||
CreateLinkedDoc,
|
||||
|
||||
@@ -280,6 +280,14 @@ pub(super) fn build_more_menu<V: EdgelessElementToolbarHost>(
|
||||
destructive: false,
|
||||
});
|
||||
}
|
||||
clip_section.push(MoreMenuItem {
|
||||
id: "copy_as_image",
|
||||
label: "Copy as image",
|
||||
icon: "ImageIcon",
|
||||
shortcut: None,
|
||||
action: EdgelessElementAction::CopyAsImage,
|
||||
destructive: false,
|
||||
});
|
||||
if let Some(sb) = &summary.surface_block {
|
||||
if matches!(
|
||||
sb.kind,
|
||||
|
||||
@@ -471,7 +471,7 @@ impl PlaygroundView {
|
||||
// Prepare the spatial scene once before building floating selection chrome so
|
||||
// `painted_items` reflects same-frame font-size, stroke-width, and lock updates.
|
||||
let prepared = {
|
||||
let doc_dir: &dyn crate::directory::WorkspaceDocDirectory = self
|
||||
let doc_dir = self
|
||||
.custom_doc_directory
|
||||
.as_deref()
|
||||
.unwrap_or(&self.default_doc_directory);
|
||||
@@ -496,7 +496,17 @@ impl PlaygroundView {
|
||||
editing_selection: None,
|
||||
local_collapsed: Some(&self.flow.local_collapsed),
|
||||
};
|
||||
edgeless::prepare_edgeless_scene(&mut self.edgeless, &prep_input, window)
|
||||
let prepared =
|
||||
edgeless::prepare_edgeless_scene(&mut self.edgeless, &prep_input, window);
|
||||
if self.edgeless.capture_keep_ids.is_some() {
|
||||
return edgeless::render_edgeless_capture(
|
||||
&mut self.edgeless,
|
||||
prepared,
|
||||
&prep_input,
|
||||
window,
|
||||
);
|
||||
}
|
||||
prepared
|
||||
};
|
||||
let (canvas_controls, mut toolbar) = if presenting {
|
||||
self.presentation_controls(vp_size, palette, cx)
|
||||
@@ -517,7 +527,7 @@ impl PlaygroundView {
|
||||
self.render_edgeless_note_headers(palette, viewport_width, viewport_height, cx);
|
||||
let caption_elements =
|
||||
self.edgeless_caption_elements(window, palette, self.caret_blink_alpha(now_ms));
|
||||
let doc_dir: &dyn crate::directory::WorkspaceDocDirectory = self
|
||||
let doc_dir = self
|
||||
.custom_doc_directory
|
||||
.as_deref()
|
||||
.unwrap_or(&self.default_doc_directory);
|
||||
@@ -676,22 +686,8 @@ impl ui::Render for PlaygroundView {
|
||||
}
|
||||
|
||||
let presences = self.remote_presences(&palette);
|
||||
let switcher_left = ((viewport_width - PILL_WIDTH) / 2.0).floor().max(0.0);
|
||||
let mode_switcher = self.render_mode_switcher(&palette, switcher_left, window, cx);
|
||||
let doc_panel_toggle = self.render_doc_panel_toggle(&palette, viewport_width < 440.0, cx);
|
||||
let theme_toggle = self.render_theme_toggle(&palette, cx);
|
||||
let presence_room = viewport_width
|
||||
- TOPBAR_EDGE
|
||||
- THEME_TOGGLE_SIZE
|
||||
- 8.0
|
||||
- (switcher_left + PILL_WIDTH + 8.0);
|
||||
let badges = Self::render_presence(
|
||||
&presences,
|
||||
&palette,
|
||||
presence_room,
|
||||
viewport_width < 720.0,
|
||||
window,
|
||||
);
|
||||
let topbar = (!self.hide_chrome)
|
||||
.then(|| self.render_topbar(&palette, viewport_width, &presences, window, cx));
|
||||
|
||||
self.sync_embeds_shielded();
|
||||
|
||||
@@ -711,24 +707,26 @@ impl ui::Render for PlaygroundView {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
self.reconcile_wasm_html_embeds(viewport_width, viewport_height);
|
||||
let mut popovers = Vec::new();
|
||||
if let Some((_, layout, _)) = self.active_overlay_target() {
|
||||
popovers.extend(self.database_popover_elements(&layout, window, cx));
|
||||
popovers.extend(self.database_dnd_overlay_elements(window, cx));
|
||||
if self.edgeless.capture_keep_ids.is_none() {
|
||||
if let Some((_, layout, _)) = self.active_overlay_target() {
|
||||
popovers.extend(self.database_popover_elements(&layout, window, cx));
|
||||
popovers.extend(self.database_dnd_overlay_elements(window, cx));
|
||||
}
|
||||
if self.mode == PlaygroundMode::Edgeless {
|
||||
self.sync_hover_link_popup(self.now_ms(cx));
|
||||
}
|
||||
popovers.extend(self.overlay_popovers(&palette, window, cx));
|
||||
popovers.extend(self.render_touch_selection_menu(&palette, window, cx));
|
||||
popovers.extend(self.render_database_record_sheet(&palette, window, cx));
|
||||
popovers.extend(self.render_doc_panel(
|
||||
&palette,
|
||||
viewport_width,
|
||||
viewport_height,
|
||||
window.layout_direction(),
|
||||
cx,
|
||||
));
|
||||
popovers.extend(self.render_pdf_viewer(&palette, window, cx));
|
||||
}
|
||||
if self.mode == PlaygroundMode::Edgeless {
|
||||
self.sync_hover_link_popup(self.now_ms(cx));
|
||||
}
|
||||
popovers.extend(self.overlay_popovers(&palette, window, cx));
|
||||
popovers.extend(self.render_touch_selection_menu(&palette, window, cx));
|
||||
popovers.extend(self.render_database_record_sheet(&palette, window, cx));
|
||||
popovers.extend(self.render_doc_panel(
|
||||
&palette,
|
||||
viewport_width,
|
||||
viewport_height,
|
||||
window.layout_direction(),
|
||||
cx,
|
||||
));
|
||||
popovers.extend(self.render_pdf_viewer(&palette, window, cx));
|
||||
if self.images.has_droppable_pending() {
|
||||
window.request_animation_frame();
|
||||
}
|
||||
@@ -751,44 +749,8 @@ impl ui::Render for PlaygroundView {
|
||||
}
|
||||
}),
|
||||
);
|
||||
if !self.hide_chrome {
|
||||
let topbar_h = self.topbar_height();
|
||||
root = root.snapshot("root.canvas", None).child(
|
||||
div()
|
||||
.relative()
|
||||
.flex_none()
|
||||
.w_full()
|
||||
.h(px(topbar_h))
|
||||
.bg(palette.panel)
|
||||
.border_b_1()
|
||||
.border_color(palette.border)
|
||||
.snapshot("chrome.topbar", Some("root.canvas"))
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.left(px(TOPBAR_EDGE))
|
||||
.top(px(0.0))
|
||||
.h(px(topbar_h - 1.0))
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap(px(6.0))
|
||||
.child(doc_panel_toggle)
|
||||
.children(self.render_nav_back(&palette, cx)),
|
||||
)
|
||||
.child(mode_switcher)
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.right(px(TOPBAR_EDGE))
|
||||
.top(px(0.0))
|
||||
.h(px(topbar_h - 1.0))
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap(px(8.0))
|
||||
.children(badges)
|
||||
.child(theme_toggle),
|
||||
),
|
||||
);
|
||||
if let Some(topbar) = topbar {
|
||||
root = root.snapshot("root.canvas", None).child(topbar);
|
||||
} else {
|
||||
root = root.child(self.embedded_origin_probe());
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use ui::{
|
||||
|
||||
use crate::{
|
||||
PILL_EDGELESS_X, PILL_PAGE_X, PILL_WIDTH, PlaygroundMode, PlaygroundTheme, PlaygroundView,
|
||||
THEME_TOGGLE_SIZE,
|
||||
THEME_TOGGLE_SIZE, TOPBAR_EDGE,
|
||||
block_flow::RemotePresence,
|
||||
theme::{FONT_SANS, Palette},
|
||||
};
|
||||
@@ -302,4 +302,66 @@ impl PlaygroundView {
|
||||
}
|
||||
badges
|
||||
}
|
||||
|
||||
pub(super) fn render_topbar(
|
||||
&mut self,
|
||||
palette: &Palette,
|
||||
viewport_width: f32,
|
||||
presences: &[RemotePresence],
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> AnyElement {
|
||||
let switcher_left = ((viewport_width - PILL_WIDTH) / 2.0).floor().max(0.0);
|
||||
let mode_switcher = self.render_mode_switcher(palette, switcher_left, window, cx);
|
||||
let doc_panel_toggle = self.render_doc_panel_toggle(palette, viewport_width < 440.0, cx);
|
||||
let theme_toggle = self.render_theme_toggle(palette, cx);
|
||||
let presence_room = viewport_width
|
||||
- TOPBAR_EDGE
|
||||
- THEME_TOGGLE_SIZE
|
||||
- 8.0
|
||||
- (switcher_left + PILL_WIDTH + 8.0);
|
||||
let badges = Self::render_presence(
|
||||
presences,
|
||||
palette,
|
||||
presence_room,
|
||||
viewport_width < 720.0,
|
||||
window,
|
||||
);
|
||||
let topbar_h = self.topbar_height();
|
||||
div()
|
||||
.relative()
|
||||
.flex_none()
|
||||
.w_full()
|
||||
.h(px(topbar_h))
|
||||
.bg(palette.panel)
|
||||
.border_b_1()
|
||||
.border_color(palette.border)
|
||||
.snapshot("chrome.topbar", Some("root.canvas"))
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.left(px(TOPBAR_EDGE))
|
||||
.top(px(0.0))
|
||||
.h(px(topbar_h - 1.0))
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap(px(6.0))
|
||||
.child(doc_panel_toggle)
|
||||
.children(self.render_nav_back(palette, cx)),
|
||||
)
|
||||
.child(mode_switcher)
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.right(px(TOPBAR_EDGE))
|
||||
.top(px(0.0))
|
||||
.h(px(topbar_h - 1.0))
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap(px(8.0))
|
||||
.children(badges)
|
||||
.child(theme_toggle),
|
||||
)
|
||||
.into_any_element()
|
||||
}
|
||||
}
|
||||
@@ -163,20 +163,27 @@ fn select_and_multi_select_clipboard_preserve_comma_labels_and_option_ids() {
|
||||
table.undo();
|
||||
assert_eq!(table.read().cells, before);
|
||||
}
|
||||
for (src, text, ids) in [
|
||||
("r1", "Sales, APAC", vec!["target-combined".to_string()]),
|
||||
let multi = column_type == "multi-select";
|
||||
let cell = |ids: &[&str]| {
|
||||
if multi {
|
||||
DatabaseCellValue::MultiSelect(ids.iter().map(|id| id.to_string()).collect())
|
||||
} else {
|
||||
DatabaseCellValue::Text(ids[0].to_string())
|
||||
}
|
||||
};
|
||||
// (source row, copied text, cell after a table paste, cell after a plain-text paste)
|
||||
for (src, text, copied, typed) in [
|
||||
(
|
||||
"r1",
|
||||
"Sales, APAC",
|
||||
cell(&["target-combined"]),
|
||||
cell(&["target-sales", "target-apac"]),
|
||||
),
|
||||
(
|
||||
"r2",
|
||||
if column_type == "multi-select" {
|
||||
"Sales,APAC"
|
||||
} else {
|
||||
"APAC"
|
||||
},
|
||||
if column_type == "multi-select" {
|
||||
vec!["target-sales".to_string(), "target-apac".to_string()]
|
||||
} else {
|
||||
vec!["target-apac".to_string()]
|
||||
},
|
||||
if multi { "Sales,APAC" } else { "APAC" },
|
||||
cell(&["target-sales", "target-apac"][usize::from(!multi)..]),
|
||||
cell(&["target-sales", "target-apac"][usize::from(!multi)..]),
|
||||
),
|
||||
] {
|
||||
table.select_cell(src, "sales");
|
||||
@@ -188,14 +195,9 @@ fn select_and_multi_select_clipboard_preserve_comma_labels_and_option_ids() {
|
||||
});
|
||||
table.select_cell("r2", "destination");
|
||||
table.key("v", undo_modifiers());
|
||||
let value = if column_type == "multi-select" {
|
||||
DatabaseCellValue::MultiSelect(ids)
|
||||
} else {
|
||||
DatabaseCellValue::Text(ids[0].clone())
|
||||
};
|
||||
assert_eq!(
|
||||
table.read().cells[&("r2".into(), "destination".into())],
|
||||
value
|
||||
copied
|
||||
);
|
||||
table.undo();
|
||||
assert_eq!(table.read().cells, before);
|
||||
@@ -203,17 +205,44 @@ fn select_and_multi_select_clipboard_preserve_comma_labels_and_option_ids() {
|
||||
table.paste_text(text);
|
||||
assert_eq!(
|
||||
table.read().cells[&("r2".into(), "destination".into())],
|
||||
value
|
||||
typed
|
||||
);
|
||||
table.undo();
|
||||
assert_eq!(table.read().cells, before);
|
||||
}
|
||||
table.select_cell("r2", "sales");
|
||||
table.paste_text("Sales, APAC");
|
||||
assert_eq!(
|
||||
table.read().cells[&("r2".into(), "sales".into())],
|
||||
before[&("r1".into(), "sales".into())]
|
||||
);
|
||||
// Foreign text splits at every comma, even where an option holds the
|
||||
// whole text or a prefix of it.
|
||||
let columns = table.read().columns;
|
||||
table.select_cell("r2", "destination");
|
||||
table.paste_text("Sales, APAC, EMEA");
|
||||
let after = table.read();
|
||||
let options = after
|
||||
.columns
|
||||
.iter()
|
||||
.find(|column| column.id == "destination")
|
||||
.unwrap()
|
||||
.select_options();
|
||||
let labels: Vec<_> = after.cells[&("r2".into(), "destination".into())]
|
||||
.as_string_list()
|
||||
.iter()
|
||||
.map(|id| {
|
||||
&options
|
||||
.iter()
|
||||
.find(|option| option.id == *id)
|
||||
.unwrap()
|
||||
.value
|
||||
})
|
||||
.cloned()
|
||||
.collect();
|
||||
let expected: &[&str] = if multi {
|
||||
&["Sales", "APAC", "EMEA"]
|
||||
} else {
|
||||
&["Sales"]
|
||||
};
|
||||
assert_eq!(labels, expected);
|
||||
table.undo();
|
||||
assert_eq!(table.read().cells, before);
|
||||
assert_eq!(table.read().columns, columns);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,783 @@
|
||||
//! Edgeless `More -> Copy as image` GPU scene readback export matching `0.27.5` `copy-as-image.ts`.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use blocksuite_playground_doc::read::{BlockContent, PageSnapshot, SurfaceElementKind};
|
||||
use ui::{AnyElement, Context, Window, div, prelude::*};
|
||||
|
||||
use crate::PlaygroundView;
|
||||
|
||||
use super::{
|
||||
EdgelessCanvasState, EdgelessRenderInput, PreparedEdgelessScene, WorldRect,
|
||||
build_edgeless_layer_prepared, collect_frame_children, find_surface_element, rotated_bounds,
|
||||
};
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
use {
|
||||
std::io::Cursor,
|
||||
ui::{
|
||||
App, ClipboardEntry, ClipboardItem, Entity, Image, ImageFormat, Pixels, Point, Size, point,
|
||||
px,
|
||||
},
|
||||
};
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
use super::EdgelessCamera;
|
||||
|
||||
#[cfg_attr(not(target_family = "wasm"), allow(dead_code))]
|
||||
const WEB_UNAVAILABLE_TOAST: &str =
|
||||
"The 'Copy as image' feature is only available on our desktop app";
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
const COPY_AS_IMAGE_MARGIN_WORLD: f32 = 20.0;
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
const FRAME_CAPTURE_INSET_PX: f32 = -2.0;
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
const COPY_SUCCESS_TOAST: &str = "Image copied";
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
const COPY_FAILED_TOAST: &str = "Image copy failed";
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
struct CopyAsImagePlan {
|
||||
keep_ids: BTreeSet<String>,
|
||||
crop_bound: WorldRect,
|
||||
capture_camera: EdgelessCamera,
|
||||
is_frame_selected: bool,
|
||||
vp_origin: Point<Pixels>,
|
||||
vp_size: Size<Pixels>,
|
||||
}
|
||||
|
||||
/// Resolve Mindmap single-node selection promotion and recursively collect all container
|
||||
/// descendants (`affine:frame`, `Group`, `Mindmap`) matching `0.27.5` `withDescendantElements`.
|
||||
fn resolve_target_and_descendants(
|
||||
snapshot: &PageSnapshot,
|
||||
selection: &BTreeSet<String>,
|
||||
) -> (Vec<String>, BTreeSet<String>) {
|
||||
let mut target_roots: Vec<String> = selection.iter().cloned().collect();
|
||||
if target_roots.len() == 1
|
||||
&& let Some(single_id) = target_roots.first()
|
||||
&& let Some(mindmap_id) = find_enclosing_mindmap_id(snapshot, single_id)
|
||||
{
|
||||
target_roots = vec![mindmap_id];
|
||||
}
|
||||
|
||||
let mut keep_ids = BTreeSet::new();
|
||||
let mut stack = target_roots.clone();
|
||||
while let Some(id) = stack.pop() {
|
||||
if !keep_ids.insert(id.clone()) {
|
||||
continue;
|
||||
}
|
||||
if let Some(node) = snapshot.block(&id)
|
||||
&& matches!(node.content, BlockContent::Frame { .. })
|
||||
{
|
||||
stack.extend(collect_frame_children(node));
|
||||
continue;
|
||||
}
|
||||
if let Some((_, el)) = find_surface_element(snapshot, &id) {
|
||||
match &el.kind {
|
||||
SurfaceElementKind::Group { children, .. } => {
|
||||
stack.extend(children.iter().cloned());
|
||||
}
|
||||
SurfaceElementKind::Mindmap(mm) => {
|
||||
stack.extend(mm.nodes.keys().cloned());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
(target_roots, keep_ids)
|
||||
}
|
||||
|
||||
fn find_enclosing_mindmap_id(snapshot: &PageSnapshot, node_id: &str) -> Option<String> {
|
||||
let root = snapshot.root.as_ref()?;
|
||||
for block in root.walk() {
|
||||
if let BlockContent::Surface { elements, .. } = &block.content {
|
||||
for el in elements {
|
||||
if let SurfaceElementKind::Mindmap(mm) = &el.kind
|
||||
&& mm.nodes.contains_key(node_id)
|
||||
{
|
||||
return Some(el.id.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
fn capture_margin_px(zoom: f32, is_frame_selected: bool) -> f32 {
|
||||
if is_frame_selected {
|
||||
FRAME_CAPTURE_INSET_PX
|
||||
} else {
|
||||
COPY_AS_IMAGE_MARGIN_WORLD * zoom
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
fn screen_capture_rect(
|
||||
bound: WorldRect,
|
||||
camera: EdgelessCamera,
|
||||
vp_origin: Point<Pixels>,
|
||||
is_frame_selected: bool,
|
||||
) -> (f32, f32, f32, f32) {
|
||||
let tl = camera.world_to_screen(point(px(bound.x), px(bound.y)), vp_origin);
|
||||
let br = camera.world_to_screen(point(px(bound.right()), px(bound.bottom())), vp_origin);
|
||||
let margin = capture_margin_px(camera.zoom, is_frame_selected);
|
||||
(
|
||||
f32::from(tl.x) - margin,
|
||||
f32::from(tl.y) - margin,
|
||||
f32::from(br.x) + margin,
|
||||
f32::from(br.y) + margin,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
fn resolve_capture_camera(
|
||||
current: EdgelessCamera,
|
||||
fit_bound: WorldRect,
|
||||
crop_bound: WorldRect,
|
||||
is_frame_selected: bool,
|
||||
vp_origin: Point<Pixels>,
|
||||
vp_size: Size<Pixels>,
|
||||
) -> EdgelessCamera {
|
||||
let vp_l = f32::from(vp_origin.x);
|
||||
let vp_t = f32::from(vp_origin.y);
|
||||
let vp_w = f32::from(vp_size.width).max(1.0);
|
||||
let vp_h = f32::from(vp_size.height).max(1.0);
|
||||
let (l, t, r, b) = screen_capture_rect(crop_bound, current, vp_origin, is_frame_selected);
|
||||
let fits_viewport = l >= vp_l && t >= vp_t && r <= vp_l + vp_w && b <= vp_t + vp_h;
|
||||
if current.zoom >= 1.0 && fits_viewport {
|
||||
return current;
|
||||
}
|
||||
let padding = if is_frame_selected {
|
||||
0.0
|
||||
} else {
|
||||
2.0 * COPY_AS_IMAGE_MARGIN_WORLD
|
||||
};
|
||||
let fit_zoom = (vp_w / (fit_bound.w + padding).max(1.0))
|
||||
.min(vp_h / (fit_bound.h + padding).max(1.0))
|
||||
.min(1.0);
|
||||
let (cx_w, cy_w) = fit_bound.center();
|
||||
EdgelessCamera {
|
||||
pan_x: cx_w - (vp_w * 0.5) / fit_zoom,
|
||||
pan_y: cy_w - (vp_h * 0.5) / fit_zoom,
|
||||
zoom: fit_zoom,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
fn crop_device_rect(
|
||||
plan: &CopyAsImagePlan,
|
||||
scale_factor: f32,
|
||||
img_width: u32,
|
||||
img_height: u32,
|
||||
) -> Option<(u32, u32, u32, u32)> {
|
||||
let (l, t, r, b) = screen_capture_rect(
|
||||
plan.crop_bound,
|
||||
plan.capture_camera,
|
||||
plan.vp_origin,
|
||||
plan.is_frame_selected,
|
||||
);
|
||||
let vp_l = f32::from(plan.vp_origin.x);
|
||||
let vp_t = f32::from(plan.vp_origin.y);
|
||||
let vp_r = vp_l + f32::from(plan.vp_size.width).max(0.0);
|
||||
let vp_b = vp_t + f32::from(plan.vp_size.height).max(0.0);
|
||||
let cl = l.max(vp_l);
|
||||
let ct = t.max(vp_t);
|
||||
let cr = r.min(vp_r);
|
||||
let cb = b.min(vp_b);
|
||||
if cr <= cl || cb <= ct {
|
||||
return None;
|
||||
}
|
||||
let scale = scale_factor.max(0.5);
|
||||
let x0 = (cl * scale).floor().clamp(0.0, img_width as f32) as u32;
|
||||
let y0 = (ct * scale).floor().clamp(0.0, img_height as f32) as u32;
|
||||
let x1 = (cr * scale).ceil().clamp(0.0, img_width as f32) as u32;
|
||||
let y1 = (cb * scale).ceil().clamp(0.0, img_height as f32) as u32;
|
||||
let w = x1.saturating_sub(x0);
|
||||
let h = y1.saturating_sub(y0);
|
||||
(w > 0 && h > 0).then_some((x0, y0, w, h))
|
||||
}
|
||||
|
||||
/// Clean Edgeless-only viewport tree rendered during the single offscreen GPU readback frame.
|
||||
pub(crate) fn render_edgeless_capture(
|
||||
state: &mut EdgelessCanvasState,
|
||||
prepared: PreparedEdgelessScene,
|
||||
input: &EdgelessRenderInput<'_>,
|
||||
window: &mut Window,
|
||||
) -> AnyElement {
|
||||
let layer = build_edgeless_layer_prepared(
|
||||
state,
|
||||
prepared,
|
||||
input,
|
||||
window,
|
||||
Vec::new(),
|
||||
true,
|
||||
std::iter::empty(),
|
||||
);
|
||||
div()
|
||||
.relative()
|
||||
.flex_1()
|
||||
.w_full()
|
||||
.min_h_0()
|
||||
.overflow_hidden()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.child(layer)
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
impl PlaygroundView {
|
||||
/// Execute `More -> Copy as image` for the current Edgeless selection.
|
||||
pub(crate) fn edgeless_copy_as_image(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.dismiss_edgeless_element_popovers();
|
||||
if self.edgeless.authoring.selection.is_empty() {
|
||||
cx.notify();
|
||||
return;
|
||||
}
|
||||
#[cfg(target_family = "wasm")]
|
||||
{
|
||||
let _ = window;
|
||||
let _ = resolve_target_and_descendants;
|
||||
self.show_timed_context_toast(WEB_UNAVAILABLE_TOAST, cx);
|
||||
cx.notify();
|
||||
}
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
{
|
||||
let view = cx.entity();
|
||||
window.defer(cx, move |window, cx| {
|
||||
Self::finish_copy_as_image_readback(view, window, cx);
|
||||
});
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
fn build_copy_as_image_plan(&self, window: &Window) -> Option<CopyAsImagePlan> {
|
||||
let snapshot = self.snapshot.as_ref()?;
|
||||
if self.edgeless.authoring.selection.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let (target_roots, keep_ids) =
|
||||
resolve_target_and_descendants(snapshot, &self.edgeless.authoring.selection);
|
||||
let is_frame_selected = target_roots.len() == 1
|
||||
&& snapshot
|
||||
.block(&target_roots[0])
|
||||
.is_some_and(|node| matches!(node.content, BlockContent::Frame { .. }));
|
||||
|
||||
let mut fit_bound = WorldRect::new(0.0, 0.0, 0.0, 0.0);
|
||||
let mut frame_rect = None;
|
||||
for item in self.edgeless.authoring.painted_items.iter() {
|
||||
if keep_ids.contains(&item.id) {
|
||||
let aabb = rotated_bounds(item.rect, item.rotate_deg);
|
||||
fit_bound = fit_bound.union(aabb);
|
||||
if is_frame_selected && item.id == target_roots[0] {
|
||||
frame_rect = Some(item.rect);
|
||||
}
|
||||
}
|
||||
}
|
||||
let crop_bound = if is_frame_selected {
|
||||
frame_rect.unwrap_or(fit_bound)
|
||||
} else {
|
||||
fit_bound
|
||||
};
|
||||
if crop_bound.is_empty() || fit_bound.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let (vp_origin, vp_size) = self.canvas_rect(window);
|
||||
let capture_camera = resolve_capture_camera(
|
||||
self.edgeless.camera,
|
||||
fit_bound,
|
||||
crop_bound,
|
||||
is_frame_selected,
|
||||
vp_origin,
|
||||
vp_size,
|
||||
);
|
||||
Some(CopyAsImagePlan {
|
||||
keep_ids,
|
||||
crop_bound,
|
||||
capture_camera,
|
||||
is_frame_selected,
|
||||
vp_origin,
|
||||
vp_size,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
fn finish_copy_as_image_readback(view: Entity<Self>, window: &mut Window, cx: &mut App) {
|
||||
let setup = view.update(cx, |this, _cx| {
|
||||
let plan = this.build_copy_as_image_plan(window)?;
|
||||
let saved_camera = this.edgeless.camera;
|
||||
let saved_camera_initialized = this.edgeless.camera_initialized;
|
||||
let saved_transition = this.edgeless_camera_transition.take();
|
||||
let saved_pan_fling = this.edgeless_pan_fling.take();
|
||||
this.edgeless.camera = plan.capture_camera;
|
||||
this.edgeless.camera_initialized = true;
|
||||
this.edgeless.capture_keep_ids = Some(plan.keep_ids.clone());
|
||||
Some((
|
||||
plan,
|
||||
saved_camera,
|
||||
saved_camera_initialized,
|
||||
saved_transition,
|
||||
saved_pan_fling,
|
||||
))
|
||||
});
|
||||
let Some((plan, saved_camera, saved_camera_initialized, saved_transition, saved_pan_fling)) =
|
||||
setup
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
window.refresh();
|
||||
let capture_clear = window.draw(cx);
|
||||
let rendered = window.render_to_image();
|
||||
capture_clear.clear(cx);
|
||||
|
||||
let scale_factor = window.scale_factor();
|
||||
let encoded = rendered.and_then(|img| {
|
||||
let (x, y, w, h) = crop_device_rect(&plan, scale_factor, img.width(), img.height())
|
||||
.ok_or_else(|| anyhow::anyhow!("capture bounds outside viewport"))?;
|
||||
let cropped = image::imageops::crop_imm(&img, x, y, w, h).to_image();
|
||||
let mut png_bytes = Vec::new();
|
||||
cropped.write_to(&mut Cursor::new(&mut png_bytes), image::ImageFormat::Png)?;
|
||||
Ok(png_bytes)
|
||||
});
|
||||
|
||||
view.update(cx, |this, cx| {
|
||||
this.edgeless.camera = saved_camera;
|
||||
this.edgeless.camera_initialized = saved_camera_initialized;
|
||||
this.edgeless_camera_transition = saved_transition;
|
||||
this.edgeless_pan_fling = saved_pan_fling;
|
||||
this.edgeless.capture_keep_ids = None;
|
||||
match encoded {
|
||||
Ok(png_bytes) => {
|
||||
cx.write_to_clipboard(ClipboardItem {
|
||||
entries: vec![ClipboardEntry::Image(Image::from_bytes(
|
||||
ImageFormat::Png,
|
||||
png_bytes,
|
||||
))],
|
||||
});
|
||||
this.show_timed_context_toast(COPY_SUCCESS_TOAST, cx);
|
||||
}
|
||||
Err(_) => {
|
||||
this.show_timed_context_toast(COPY_FAILED_TOAST, cx);
|
||||
}
|
||||
}
|
||||
cx.notify();
|
||||
});
|
||||
|
||||
window.refresh();
|
||||
let restore_clear = window.draw(cx);
|
||||
restore_clear.clear(cx);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
use blocksuite_playground_doc::schema::SurfaceElementSpec;
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
use ui::{ClipboardEntry, ImageFormat};
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
use y_octo::{Any, SessionValue};
|
||||
|
||||
use super::*;
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
use crate::edgeless::{
|
||||
EdgelessElementAction, ElementSubMenu,
|
||||
edgeless_canvas_media_tests::{click, frame},
|
||||
edgeless_toolbar_tests::{start_edgeless_toolbar_view, start_edgeless_toolbar_view_sized},
|
||||
};
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
fn read_clipboard_png(app: &ui::ApplicationHandle) -> (Vec<u8>, image::RgbaImage) {
|
||||
let item = app
|
||||
.update(|cx| cx.read_from_clipboard())
|
||||
.expect("clipboard should hold an image");
|
||||
assert_eq!(item.entries.len(), 1);
|
||||
let ClipboardEntry::Image(img) = &item.entries[0] else {
|
||||
panic!("expected image clipboard entry, got {:?}", item.entries);
|
||||
};
|
||||
assert_eq!(img.format, ImageFormat::Png);
|
||||
let decoded = image::load_from_memory_with_format(&img.bytes, image::ImageFormat::Png)
|
||||
.expect("clipboard bytes must decode as valid PNG")
|
||||
.to_rgba8();
|
||||
(img.bytes.clone(), decoded)
|
||||
}
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
fn count_red_pixels(img: &image::RgbaImage) -> usize {
|
||||
img.pixels()
|
||||
.filter(|p| p[0] > 200 && p[1] < 70 && p[2] < 70)
|
||||
.count()
|
||||
}
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
fn count_dark_ink_pixels(img: &image::RgbaImage) -> usize {
|
||||
img.pixels()
|
||||
.filter(|p| p[0] < 90 && p[1] < 90 && p[2] < 90)
|
||||
.count()
|
||||
}
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
fn add_colored_shape(
|
||||
app: &ui::ApplicationHandle,
|
||||
win: ui::WindowHandle<PlaygroundView>,
|
||||
id: &str,
|
||||
xywh: &str,
|
||||
color: &str,
|
||||
ts: u64,
|
||||
) {
|
||||
app.update(|cx| {
|
||||
win.update(cx, |view, _, cx| {
|
||||
let commit = view
|
||||
.page_mut()
|
||||
.unwrap()
|
||||
.structural_edit(ts, |page| {
|
||||
page.add_surface_element(
|
||||
"surf",
|
||||
id,
|
||||
"z9",
|
||||
SurfaceElementSpec::Shape {
|
||||
shape_type: "rect".into(),
|
||||
xywh: xywh.into(),
|
||||
seed: 99,
|
||||
filled: true,
|
||||
text: Some("UNSELECTED".into()),
|
||||
},
|
||||
ts,
|
||||
)?;
|
||||
page.set_surface_element_field(
|
||||
"surf",
|
||||
id,
|
||||
"fillColor",
|
||||
SessionValue::Any(Any::String(color.into())),
|
||||
ts,
|
||||
)?;
|
||||
page.set_surface_element_field(
|
||||
"surf",
|
||||
id,
|
||||
"strokeColor",
|
||||
SessionValue::Any(Any::String(color.into())),
|
||||
ts,
|
||||
)?;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
view.did_edgeless_doc_commit(commit, cx);
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
#[test]
|
||||
fn copy_as_image_exports_gpu_rendered_text_and_excludes_grid_and_unselected_overlap() {
|
||||
let (platform, app, win, hw) = start_edgeless_toolbar_view();
|
||||
// Put a bright-red unselected shape directly overlapping `shape-a` (`[420,160,150,84]`).
|
||||
add_colored_shape(
|
||||
&app,
|
||||
win,
|
||||
"overlap-red",
|
||||
"[450,150,140,110]",
|
||||
"#ff0000",
|
||||
300,
|
||||
);
|
||||
|
||||
// Select `shape-a` and open the `More` menu.
|
||||
app.update(|cx| {
|
||||
win.update(cx, |view, _, cx| {
|
||||
view.set_edgeless_camera(0.0, 0.0, 1.0, cx);
|
||||
view.edgeless_set_selection(["shape-a".into()], cx);
|
||||
view.apply_edgeless_element_action(
|
||||
EdgelessElementAction::ToggleSubMenu(ElementSubMenu::More),
|
||||
cx,
|
||||
);
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
let menu_snap = frame(&app, win, &hw);
|
||||
let row = menu_snap
|
||||
.elements
|
||||
.get("chrome.edgeless_element_toolbar.more.copy_as_image")
|
||||
.expect("More menu must include Copy as image");
|
||||
assert_eq!(row.text.as_deref(), Some("Copy as image"));
|
||||
|
||||
// Capture full canvas before clicking (contains dot-grid, red overlap, toolbar & More menu).
|
||||
let before_canvas = app
|
||||
.update(|cx| {
|
||||
win.update(cx, |_, window, _| window.render_to_image())
|
||||
.unwrap()
|
||||
})
|
||||
.unwrap();
|
||||
assert!(
|
||||
count_red_pixels(&before_canvas) > 2000,
|
||||
"red unselected overlap must be visible on canvas before copy"
|
||||
);
|
||||
|
||||
let camera_before = app
|
||||
.update(|cx| win.update(cx, |v, _, _| v.edgeless_camera()))
|
||||
.unwrap();
|
||||
|
||||
// Click `Copy as image` in the More menu.
|
||||
click(
|
||||
&app,
|
||||
win,
|
||||
&hw,
|
||||
&platform,
|
||||
"chrome.edgeless_element_toolbar.more.copy_as_image",
|
||||
);
|
||||
|
||||
let (_png_bytes, exported) = read_clipboard_png(&app);
|
||||
// `shape-a` is 150x84 with 20px margin on each side -> 190x124 at 1.0x zoom.
|
||||
assert!(
|
||||
(exported.width() as i32 - 190).abs() <= 2
|
||||
&& (exported.height() as i32 - 124).abs() <= 2,
|
||||
"expected ~190x124 exported PNG, got {}x{}",
|
||||
exported.width(),
|
||||
exported.height()
|
||||
);
|
||||
// Overlapping unselected red shape is completely hidden.
|
||||
assert_eq!(
|
||||
count_red_pixels(&exported),
|
||||
0,
|
||||
"overlapping unselected element must not appear in exported PNG"
|
||||
);
|
||||
// Shape text ("Collect") is rendered by the real GPU pipeline.
|
||||
assert!(
|
||||
count_dark_ink_pixels(&exported) > 40,
|
||||
"exported shape PNG must contain real rendered text ink"
|
||||
);
|
||||
// Background dot-grid is hidden: outer 4px margin frame must be uniform background.
|
||||
let bg = *exported.get_pixel(0, 0);
|
||||
for x in 0..exported.width() {
|
||||
assert_eq!(
|
||||
*exported.get_pixel(x, 0),
|
||||
bg,
|
||||
"top edge must have no grid dots"
|
||||
);
|
||||
assert_eq!(
|
||||
*exported.get_pixel(x, exported.height() - 1),
|
||||
bg,
|
||||
"bottom edge must have no grid dots"
|
||||
);
|
||||
}
|
||||
|
||||
// Camera, selection, and UI chrome are restored cleanly in the same frame.
|
||||
let (camera_after, selection_after, toast, more_open) = app
|
||||
.update(|cx| {
|
||||
win.update(cx, |v, _, _| {
|
||||
(
|
||||
v.edgeless_camera(),
|
||||
v.edgeless_selection().clone(),
|
||||
v.overlays().context_toast_message().map(str::to_string),
|
||||
v.edgeless.authoring.open_sub_menu,
|
||||
)
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(camera_after, camera_before);
|
||||
assert!(selection_after.contains("shape-a"));
|
||||
assert_eq!(toast.as_deref(), Some(COPY_SUCCESS_TOAST));
|
||||
assert_eq!(more_open, None);
|
||||
let restored_snap = frame(&app, win, &hw);
|
||||
assert!(
|
||||
restored_snap
|
||||
.elements
|
||||
.contains_key("chrome.edgeless_toolbar")
|
||||
);
|
||||
assert!(
|
||||
restored_snap
|
||||
.elements
|
||||
.contains_key("edgeless.selection_box")
|
||||
);
|
||||
|
||||
if let Ok(dir) = std::env::var("COPY_AS_IMAGE_EVIDENCE_DIR") {
|
||||
let dir = std::path::PathBuf::from(dir);
|
||||
let _ = std::fs::create_dir_all(&dir);
|
||||
let _ = before_canvas.save(dir.join("01_before_more_menu_and_overlap.png"));
|
||||
let _ = exported.save(dir.join("02_after_shape_exported.png"));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
#[test]
|
||||
fn copy_as_image_handles_frame_inset_note_connector_and_zoom_fit_restore() {
|
||||
let (_platform, app, win, hw) = start_edgeless_toolbar_view();
|
||||
// Add an overlapping unselected red shape over `frame-outer` (`[390,90,700,260]`).
|
||||
add_colored_shape(
|
||||
&app,
|
||||
win,
|
||||
"frame-overlap-red",
|
||||
"[560,120,120,120]",
|
||||
"#ff0000",
|
||||
310,
|
||||
);
|
||||
|
||||
// 1. Single Frame export: margin = -2.0 trims frame border and includes child shapes.
|
||||
app.update(|cx| {
|
||||
win.update(cx, |view, _, cx| {
|
||||
view.set_edgeless_camera(0.0, 0.0, 1.0, cx);
|
||||
view.edgeless_set_selection(["frame-outer".into()], cx);
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
frame(&app, win, &hw);
|
||||
app.update(|cx| {
|
||||
win.update(cx, |view, window, cx| {
|
||||
view.edgeless_copy_as_image(window, cx);
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
let (_, frame_img) = read_clipboard_png(&app);
|
||||
// `frame-outer` is 700x260, inset by -2px per side -> 696x256.
|
||||
assert!(
|
||||
(frame_img.width() as i32 - 696).abs() <= 2
|
||||
&& (frame_img.height() as i32 - 256).abs() <= 2,
|
||||
"single frame export must apply -2px inset (~696x256), got {}x{}",
|
||||
frame_img.width(),
|
||||
frame_img.height()
|
||||
);
|
||||
assert_eq!(
|
||||
count_red_pixels(&frame_img),
|
||||
0,
|
||||
"unselected shape overlapping frame must be hidden"
|
||||
);
|
||||
assert!(
|
||||
count_dark_ink_pixels(&frame_img) > 120,
|
||||
"frame export must render child shapes and their labels"
|
||||
);
|
||||
|
||||
// 2. Multi-select Note + Shape + Connector while zoomed out and panned off-center:
|
||||
// camera temporarily fits to 1.0x, exports all text/strokes, and restores original camera.
|
||||
app.update(|cx| {
|
||||
win.update(cx, |view, _, cx| {
|
||||
view.set_edgeless_camera(-300.0, -250.0, 0.5, cx);
|
||||
view.edgeless_set_selection(
|
||||
[
|
||||
"note-main".into(),
|
||||
"shape-a".into(),
|
||||
"shape-b".into(),
|
||||
"conn-1".into(),
|
||||
],
|
||||
cx,
|
||||
);
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
frame(&app, win, &hw);
|
||||
let zoomed_out_cam = app
|
||||
.update(|cx| win.update(cx, |v, _, _| v.edgeless_camera()))
|
||||
.unwrap();
|
||||
app.update(|cx| {
|
||||
win.update(cx, |view, window, cx| {
|
||||
view.edgeless_copy_as_image(window, cx);
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
let (_, combo_img) = read_clipboard_png(&app);
|
||||
// Union of note-main `[60,120,280,190]` + shape-a/b + conn-1 is ~750x190 + 40px margins.
|
||||
assert!(
|
||||
combo_img.width() >= 760 && combo_img.height() >= 210,
|
||||
"zoomed-out multi-select must fit to 1.0x before readback, got {}x{}",
|
||||
combo_img.width(),
|
||||
combo_img.height()
|
||||
);
|
||||
assert_eq!(count_red_pixels(&combo_img), 0);
|
||||
assert!(
|
||||
count_dark_ink_pixels(&combo_img) > 250,
|
||||
"exported Note + Shape + Connector PNG must contain real rendered text ink"
|
||||
);
|
||||
let restored_cam = app
|
||||
.update(|cx| win.update(cx, |v, _, _| v.edgeless_camera()))
|
||||
.unwrap();
|
||||
assert_eq!(restored_cam, zoomed_out_cam);
|
||||
|
||||
// 3. Mindmap single-node promotion collects the mindmap and all its node shapes.
|
||||
let keep = app
|
||||
.update(|cx| {
|
||||
win.update(cx, |v, _, _| {
|
||||
let snap = v.page_snapshot().unwrap();
|
||||
let sel = BTreeSet::from(["mm-c1".to_string()]);
|
||||
resolve_target_and_descendants(snap, &sel)
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(keep.0, vec!["mm-1".to_string()]);
|
||||
for id in ["mm-1", "mm-root", "mm-c1", "mm-c2"] {
|
||||
assert!(keep.1.contains(id), "mindmap export must include {id}");
|
||||
}
|
||||
assert_eq!(
|
||||
WEB_UNAVAILABLE_TOAST,
|
||||
"The 'Copy as image' feature is only available on our desktop app"
|
||||
);
|
||||
|
||||
if let Ok(dir) = std::env::var("COPY_AS_IMAGE_EVIDENCE_DIR") {
|
||||
let dir = std::path::PathBuf::from(dir);
|
||||
let _ = std::fs::create_dir_all(&dir);
|
||||
let _ = frame_img.save(dir.join("03_after_frame_exported.png"));
|
||||
let _ = combo_img.save(dir.join("04_after_note_shape_connector_exported.png"));
|
||||
}
|
||||
}
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
#[test]
|
||||
fn copy_as_image_keeps_both_ends_of_wide_selections_and_restores_camera() {
|
||||
for (width, height, right_x, right_color, right_channel) in [
|
||||
(400, 480, 4800, "#ff0000", 0),
|
||||
(800, 600, 12000, "#0000ff", 2),
|
||||
] {
|
||||
let (_, app, win, hw) = start_edgeless_toolbar_view_sized(width, height);
|
||||
add_colored_shape(&app, win, "left", "[0,120,180,100]", "#ff0000", 320);
|
||||
add_colored_shape(
|
||||
&app,
|
||||
win,
|
||||
"right",
|
||||
&format!("[{right_x},120,180,100]"),
|
||||
right_color,
|
||||
321,
|
||||
);
|
||||
let before = app
|
||||
.update(|cx| {
|
||||
win.update(cx, |view, _, cx| {
|
||||
view.set_edgeless_camera(-20.0, 50.0, 0.5, cx);
|
||||
view.edgeless_set_selection(["left".into(), "right".into()], cx);
|
||||
view.edgeless_camera()
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
frame(&app, win, &hw);
|
||||
app.update(|cx| {
|
||||
win.update(cx, |view, window, cx| {
|
||||
view.edgeless_copy_as_image(window, cx);
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
let (_, png) = read_clipboard_png(&app);
|
||||
if let Ok(dir) = std::env::var("COPY_AS_IMAGE_EVIDENCE_DIR") {
|
||||
let dir = std::path::PathBuf::from(dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
png.save(dir.join(format!("wide_selection_{width}.png")))
|
||||
.unwrap();
|
||||
}
|
||||
for (left, channel) in [(true, 0), (false, right_channel)] {
|
||||
assert!(
|
||||
png.enumerate_pixels().any(|(x, _, p)| {
|
||||
(x < png.width() / 2) == left
|
||||
&& p[channel] > 200
|
||||
&& (0..3).all(|other| other == channel || p[other] < 70)
|
||||
}),
|
||||
"viewport {width}x{height}, right x={right_x}, PNG {}x{} lacks color in the {} half",
|
||||
png.width(),
|
||||
png.height(),
|
||||
if left { "left" } else { "right" },
|
||||
);
|
||||
}
|
||||
let after = app
|
||||
.update(|cx| win.update(cx, |view, _, _| view.edgeless_camera()))
|
||||
.unwrap();
|
||||
assert_eq!(after, before);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -120,6 +120,10 @@ impl EdgelessElementToolbarHost for PlaygroundView {
|
||||
self.open_edgeless_linked_doc(mode, window, cx);
|
||||
return;
|
||||
}
|
||||
if matches!(action, EdgelessElementAction::CopyAsImage) {
|
||||
self.edgeless_copy_as_image(window, cx);
|
||||
return;
|
||||
}
|
||||
if matches!(
|
||||
action,
|
||||
EdgelessElementAction::MediaCaption | EdgelessElementAction::MediaOpenLink
|
||||
@@ -357,6 +361,10 @@ impl PlaygroundView {
|
||||
self.edgeless_copy_selection(false, cx);
|
||||
return;
|
||||
}
|
||||
EdgelessElementAction::CopyAsImage => {
|
||||
self.dismiss_edgeless_element_popovers();
|
||||
return;
|
||||
}
|
||||
EdgelessElementAction::Group => {
|
||||
self.dismiss_edgeless_element_popovers();
|
||||
self.edgeless_group_selection(cx);
|
||||
|
||||
@@ -13,6 +13,8 @@ mod canvas_interaction_tests;
|
||||
mod commands;
|
||||
mod commit;
|
||||
mod connector_edit;
|
||||
mod copy_as_image;
|
||||
pub(crate) use copy_as_image::render_edgeless_capture;
|
||||
mod copy_cut;
|
||||
#[cfg(test)]
|
||||
mod edgeless_canvas_media_tests;
|
||||
|
||||
@@ -51,7 +51,6 @@ use anyhow::bail;
|
||||
use anyhow::{Context as _, Result};
|
||||
use async_task::Runnable;
|
||||
use futures::channel::oneshot;
|
||||
#[cfg(any(test, feature = "harness"))]
|
||||
use image::RgbaImage;
|
||||
use image::codecs::{gif::GifDecoder, webp::WebPDecoder};
|
||||
use image::{DynamicImage, Frame, Rgba};
|
||||
@@ -1323,7 +1322,6 @@ pub trait PlatformWindow: HasWindowHandle + HasDisplayHandle {
|
||||
/// Renders the given scene to a texture and returns the pixel data as an RGBA image.
|
||||
/// This does not present the frame to screen - useful for visual testing where we want
|
||||
/// to capture what would be rendered without displaying it or requiring the window to be visible.
|
||||
#[cfg(any(test, feature = "harness"))]
|
||||
fn render_to_image(&self, _scene: &Scene) -> Result<RgbaImage> {
|
||||
anyhow::bail!("render_to_image not implemented for this platform")
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ use crate::{
|
||||
};
|
||||
use anyhow::{Context as _, Result};
|
||||
use block2::RcBlock;
|
||||
#[cfg(any(test, feature = "harness"))]
|
||||
use image::RgbaImage;
|
||||
use objc2::{Message, rc::Retained, runtime::ProtocolObject};
|
||||
use objc2_core_foundation::{CFRetained, CGSize};
|
||||
@@ -26,7 +25,6 @@ use objc2_metal::{
|
||||
MTLRenderPipelineState, MTLResourceOptions, MTLStorageMode, MTLStoreAction, MTLTexture,
|
||||
MTLTextureDescriptor, MTLTextureType, MTLTextureUsage, MTLViewport,
|
||||
};
|
||||
#[cfg(any(test, feature = "harness"))]
|
||||
use objc2_metal::{MTLBlitCommandEncoder, MTLOrigin, MTLRegion, MTLSize};
|
||||
use objc2_quartz_core::{CAMetalDrawable, CAMetalLayer};
|
||||
use parking_lot::Mutex;
|
||||
@@ -707,7 +705,6 @@ impl MetalRenderer {
|
||||
///
|
||||
/// Note: This requires a layer-backed renderer. For headless rendering,
|
||||
/// use `render_scene_to_image()` instead.
|
||||
#[cfg(any(test, feature = "harness"))]
|
||||
pub fn render_to_image(&mut self, scene: &Scene) -> Result<RgbaImage> {
|
||||
let layer = self
|
||||
.layer
|
||||
@@ -746,7 +743,6 @@ impl MetalRenderer {
|
||||
|
||||
/// Encodes a scene into a new offscreen texture and returns `(target_texture, command_buffer, uses_shared_storage)`
|
||||
/// without committing the command buffer.
|
||||
#[cfg(any(test, feature = "harness"))]
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub fn encode_offscreen_frame(
|
||||
&mut self,
|
||||
@@ -788,7 +784,6 @@ impl MetalRenderer {
|
||||
|
||||
/// Renders a scene into a new offscreen texture of `size` and reads it
|
||||
/// back, without presenting anything.
|
||||
#[cfg(any(test, feature = "harness"))]
|
||||
fn render_offscreen(&mut self, scene: &Scene, size: Size<DevicePixels>) -> Result<RgbaImage> {
|
||||
let (target_texture, command_buffer, uses_shared_storage) =
|
||||
self.encode_offscreen_frame(scene, size)?;
|
||||
@@ -1370,7 +1365,6 @@ pub(crate) fn new_command_encoder_for_texture(
|
||||
Ok(command_encoder)
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "harness"))]
|
||||
fn read_texture_to_image(texture: &ProtocolObject<dyn MTLTexture>) -> Result<RgbaImage> {
|
||||
let width = texture.width() as u32;
|
||||
let height = texture.height() as u32;
|
||||
|
||||
@@ -16,7 +16,6 @@ use crate::{
|
||||
use anyhow::Result;
|
||||
use block2::RcBlock;
|
||||
use dispatch2::DispatchQueue;
|
||||
#[cfg(any(test, feature = "harness"))]
|
||||
use image::RgbaImage;
|
||||
|
||||
use crate::gpui_util::ResultExt;
|
||||
@@ -2487,7 +2486,6 @@ impl PlatformWindow for MacWindow {
|
||||
NSBeep()
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "harness"))]
|
||||
fn render_to_image(&self, scene: &crate::Scene) -> Result<RgbaImage> {
|
||||
let mut this = self.0.lock();
|
||||
this.renderer.render_to_image(scene)
|
||||
|
||||
@@ -2863,7 +2863,6 @@ impl Window {
|
||||
/// Renders the current frame's scene to a texture and returns the pixel data as an RGBA image.
|
||||
/// This does not present the frame to screen - useful for visual testing where we want
|
||||
/// to capture what would be rendered without displaying it or requiring the window to be visible.
|
||||
#[cfg(any(test, feature = "harness"))]
|
||||
pub fn render_to_image(&self) -> anyhow::Result<image::RgbaImage> {
|
||||
self.platform_window
|
||||
.render_to_image(&self.rendered_frame.scene)
|
||||
|
||||
Reference in new issue
Block a user