mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 19:41:36 -05:00
export-w21: net change of 1 commits
- export-w21: written from its sheet
This commit is contained in:
@@ -1,28 +1,37 @@
|
||||
//! Window → Flattener Preview: what flattening the document would touch (`flattener.preview`),
|
||||
//! highlighted in red over a grey preview of the art. Refresh takes a new snapshot of the
|
||||
//! document; Highlight picks what shows; Overprints, the preset and the options (Show Options in
|
||||
//! the panel menu) set how it flattens. Click the preview to zoom in, Alt-click to zoom out, drag
|
||||
//! to pan and double-click to fit.
|
||||
//! the panel menu) set how it flattens. The preview is a `PreviewPane`: the wheel and the pane's
|
||||
//! gestures zoom and pan, and a double click fits.
|
||||
//!
|
||||
//! The settings live in `UiState::flattener_preview` (`ui.inspect`); `ui.flattenerPreview` sets
|
||||
//! them and refreshes.
|
||||
//! them and refreshes. The snapshot, the view and the texture live in [`FlattenerPreviewModel`]
|
||||
//! on the app.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::sync::Arc;
|
||||
|
||||
use egui::{Rect, Sense, Stroke, StrokeKind, Ui, pos2, vec2};
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{Bind, Entry};
|
||||
use craft_ui::display::{Divider, Shade, Text};
|
||||
use craft_ui::layout_components::Row;
|
||||
use craft_ui::menu::PanelRow;
|
||||
use craft_ui::patterns::form_dialogs::{DialogFields, ParamRows, PreviewPane, PreviewView};
|
||||
use craft_ui::pickers::Select;
|
||||
use craft_ui::runtime::commands::{CommandRef, CommandState, CommandStates};
|
||||
use craft_ui::style::TypeRole;
|
||||
use egui::load::SizedTexture;
|
||||
use egui::{Ui, vec2};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
use serde_json::{Map, Value, json};
|
||||
use vectorcraft_doc::Document;
|
||||
use vectorcraft_engine::cmd::FlattenOptions;
|
||||
use vectorcraft_engine::cmd::flatten::{FlattenReport, Highlight, Overprints};
|
||||
use vectorcraft_geom::{Affine, Point, Rect as DRect};
|
||||
use vectorcraft_geom::{Affine, Rect as DRect};
|
||||
use vectorcraft_render::RenderOptions;
|
||||
|
||||
use super::navigator::{doc_to_thumb, thumb_to_doc};
|
||||
use crate::dialogs::flatten::{options_editor, preset_dropdown};
|
||||
use crate::dialogs::flatten;
|
||||
use crate::theme::Tokens;
|
||||
use crate::{VectorcraftApp, widgets};
|
||||
use crate::{VectorcraftApp, form};
|
||||
|
||||
/// The panel id (Window → Flattener Preview).
|
||||
pub const ID: &str = "flattenerPreview";
|
||||
@@ -69,56 +78,34 @@ struct Shot {
|
||||
generation: u64,
|
||||
}
|
||||
|
||||
/// The preview's zoom (1 = fit) and centre.
|
||||
#[derive(Clone, Copy, Default)]
|
||||
struct View {
|
||||
zoom: f64,
|
||||
center: Option<Point>,
|
||||
/// The pane's well width over its height.
|
||||
const ASPECT: f32 = 1.25;
|
||||
|
||||
/// The zoom is relative to the fitted picture: 1 is the fit, up to 8×.
|
||||
fn fitting_view() -> PreviewView {
|
||||
PreviewView::fitting().zoom_range(1.0..=8.0)
|
||||
}
|
||||
|
||||
impl View {
|
||||
/// The part of `fit` shown.
|
||||
fn visible(&self, fit: DRect) -> DRect {
|
||||
let z = self.zoom.max(1.0);
|
||||
let c = self.center.unwrap_or(fit.center());
|
||||
DRect::from_center_size(c, (fit.width() / z, fit.height() / z))
|
||||
}
|
||||
|
||||
/// Zoom by `factor` (1×–64×) keeping `at` under the pointer, panned within `fit`.
|
||||
fn zoom_at(&mut self, fit: DRect, at: Point, factor: f64) {
|
||||
let v = self.visible(fit);
|
||||
let z = (self.zoom.max(1.0) * factor).clamp(1.0, 64.0);
|
||||
let k = self.zoom.max(1.0) / z;
|
||||
let c = v.center();
|
||||
self.zoom = z;
|
||||
self.center = Some(Point::new(at.x + (c.x - at.x) * k, at.y + (c.y - at.y) * k));
|
||||
self.pan(fit, (0.0, 0.0));
|
||||
}
|
||||
|
||||
/// Move the centre by `d` (document units), keeping the view inside `fit`.
|
||||
fn pan(&mut self, fit: DRect, d: (f64, f64)) {
|
||||
let v = self.visible(fit);
|
||||
let (hw, hh) = (v.width() / 2.0, v.height() / 2.0);
|
||||
let c = v.center();
|
||||
let x = (c.x + d.0).clamp(fit.x0 + hw, (fit.x1 - hw).max(fit.x0 + hw));
|
||||
let y = (c.y + d.1).clamp(fit.y0 + hh, (fit.y1 - hh).max(fit.y0 + hh));
|
||||
self.center = Some(Point::new(x, y));
|
||||
}
|
||||
}
|
||||
|
||||
/// What the preview texture shows: (generation, highlight, simulated overprints, the visible rect's
|
||||
/// bits, its pixel size).
|
||||
type Key = (u64, Highlight, bool, [u64; 4], [u32; 2]);
|
||||
|
||||
#[derive(Default)]
|
||||
struct Cache {
|
||||
/// The panel's state outside the settings: the last snapshot, its view and its texture.
|
||||
pub(crate) struct FlattenerPreviewModel {
|
||||
shot: Option<Shot>,
|
||||
view: View,
|
||||
/// Counts snapshots, so the texture cache tells them apart.
|
||||
generation: u64,
|
||||
renderer: Option<vectorcraft_render::Renderer>,
|
||||
view: PreviewView,
|
||||
drawn: crate::graphics::TexCache<Drawn>,
|
||||
}
|
||||
|
||||
impl Default for FlattenerPreviewModel {
|
||||
fn default() -> Self {
|
||||
Self { shot: None, generation: 0, renderer: None, view: fitting_view(), drawn: Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
/// What the preview texture shows: (generation, highlight, simulated overprints, its pixel size). The
|
||||
/// whole `fit` rect is rendered, so the view is not part of the key.
|
||||
type Key = (u64, Highlight, bool, u32);
|
||||
|
||||
/// The preview as drawn: for what, and its texture.
|
||||
#[derive(Default)]
|
||||
struct Drawn {
|
||||
@@ -126,10 +113,6 @@ struct Drawn {
|
||||
tex: Option<egui::TextureHandle>,
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static CACHE: RefCell<Cache> = RefCell::new(Cache::default());
|
||||
}
|
||||
|
||||
/// Take a new snapshot of the active document and work out what flattening it would do →
|
||||
/// `flattener.preview`'s answer for the panel's highlight.
|
||||
pub fn refresh(app: &mut VectorcraftApp) -> Result<Value, String> {
|
||||
@@ -145,12 +128,10 @@ pub fn refresh(app: &mut VectorcraftApp) -> Result<Value, String> {
|
||||
}
|
||||
let fit = fit.inflate(fit.width() * 0.03 + 4.0, fit.height() * 0.03 + 4.0);
|
||||
let (doc, revision) = (st.doc.clone(), st.revision);
|
||||
CACHE.with(|c| {
|
||||
let mut c = c.borrow_mut();
|
||||
c.generation += 1;
|
||||
c.shot = Some(Shot { doc, doc_index, revision, options, overprints, report, fit, generation: c.generation });
|
||||
c.view = View::default();
|
||||
});
|
||||
let m = &mut app.flattener_preview;
|
||||
m.generation += 1;
|
||||
m.shot = Some(Shot { doc, doc_index, revision, options, overprints, report, fit, generation: m.generation });
|
||||
m.view = fitting_view();
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
@@ -189,61 +170,74 @@ fn merged(base: &FlattenOptions, over: &Value) -> Value {
|
||||
v
|
||||
}
|
||||
|
||||
/// Whether the snapshot is behind the document or the settings.
|
||||
fn stale(app: &VectorcraftApp) -> bool {
|
||||
let (Some(st), Some(s)) = (app.session.active(), app.flattener_preview.shot.as_ref()) else { return false };
|
||||
let set = &app.ui.flattener_preview;
|
||||
s.revision != st.revision || s.options != set.options || s.overprints != set.overprints
|
||||
}
|
||||
|
||||
/// Whether the options are no preset's: the preset field then reads `[Custom]`.
|
||||
fn custom(app: &VectorcraftApp) -> bool {
|
||||
let set = &app.ui.flattener_preview;
|
||||
!app.session.flattener_presets().iter().any(|p| p.name == set.preset && p.options == set.options)
|
||||
}
|
||||
|
||||
/// The panel body.
|
||||
pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let Some(st) = app.session.active() else {
|
||||
if app.session.active().is_none() {
|
||||
super::empty_state(ui, "eye", tl!("No document"), tl!("Open a document to preview its flattening."));
|
||||
return;
|
||||
};
|
||||
let (index, revision) = (app.session.active_index().unwrap_or(0), st.revision);
|
||||
}
|
||||
let index = app.session.active_index().unwrap_or(0);
|
||||
// A document the panel hasn't seen yet gets a snapshot right away.
|
||||
let seen = CACHE.with(|c| c.borrow().shot.as_ref().map(|s| s.doc_index));
|
||||
if seen != Some(index)
|
||||
if app.flattener_preview.shot.as_ref().map(|s| s.doc_index) != Some(index)
|
||||
&& let Err(e) = refresh(app)
|
||||
{
|
||||
app.status(e);
|
||||
}
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let mut set = app.ui.flattener_preview.clone();
|
||||
let mut refresh_now = false;
|
||||
ui.horizontal(|ui| {
|
||||
if widgets::flat_button(ui, tl!("Refresh"), 64.0).on_hover_text(tl!("Preview the document as it is now")).clicked() {
|
||||
refresh_now = true;
|
||||
}
|
||||
let labels = Highlight::ALL.map(Highlight::label);
|
||||
if let Some(i) = widgets::dropdown(ui, "fp-highlight", set.highlight.label(), &labels, ui.available_width().max(120.0)) {
|
||||
set.highlight = Highlight::ALL[i];
|
||||
}
|
||||
Row::new().show(ui, |ui, _| {
|
||||
refresh_now = Button::new(tl!("Refresh")).tooltip(tl!("Preview the document as it is now")).show(ui).clicked();
|
||||
let entries = Highlight::ALL.map(|h| Entry::new(h, h.label()));
|
||||
Select::new(tl!("Highlight"), Bind::bound(&mut set.highlight), &entries).show(ui);
|
||||
});
|
||||
ui.add_space(4.0);
|
||||
egui::Grid::new("fp-settings").num_columns(2).spacing([8.0, 6.0]).show(ui, |ui| {
|
||||
widgets::dim_label(ui, tl!("Overprints:"));
|
||||
let labels = Overprints::ALL.map(Overprints::label);
|
||||
if let Some(i) = widgets::dropdown(ui, "fp-overprints", set.overprints.label(), &labels, 150.0) {
|
||||
set.overprints = Overprints::ALL[i];
|
||||
Row::new().show(ui, |ui, _| {
|
||||
Text::new(tl!("Overprints:")).shade(Shade::Muted).show(ui);
|
||||
let entries = Overprints::ALL.map(|o| Entry::new(o, o.label()));
|
||||
Select::new(tl!("Overprints"), Bind::bound(&mut set.overprints), &entries).show(ui);
|
||||
});
|
||||
Row::new().show(ui, |ui, _| {
|
||||
Text::new(tl!("Preset:")).shade(Shade::Muted).show(ui);
|
||||
let (rows, ..) = flatten::preset_rows(app);
|
||||
let mut fields = DialogFields::from_pairs([("preset", set.preset.clone())]);
|
||||
flatten::preset_picker(ui, &mut fields, "preset", &rows, custom(app));
|
||||
if let Some(name) = fields.text("preset")
|
||||
&& name != set.preset
|
||||
&& let Some(p) = app.session.flattener_preset(name)
|
||||
{
|
||||
(set.preset, set.options) = (p.name, p.options);
|
||||
}
|
||||
ui.end_row();
|
||||
widgets::dim_label(ui, tl!("Preset:"));
|
||||
let presets = app.session.flattener_presets();
|
||||
if let Some(p) = preset_dropdown(ui, "fp-preset", &presets, &set.preset, Some(&set.options), 150.0) {
|
||||
set.preset = p.name.clone();
|
||||
set.options = p.options.clone();
|
||||
}
|
||||
ui.end_row();
|
||||
});
|
||||
if set.show_options {
|
||||
widgets::divider(ui);
|
||||
options_editor(ui, "flattener-preview", &mut set.options, true);
|
||||
Divider::horizontal().show(ui);
|
||||
let params = flatten::option_params(&[], "");
|
||||
let mut options = Map::new();
|
||||
flatten::put_options(&mut options, &set.options);
|
||||
// PLAN: assumed `form::fields_of(&[Param], &Map) -> DialogFields` and `form::json_of(&[Param], &DialogFields) -> Map` (wg.S12)
|
||||
let mut fields = form::fields_of(¶ms, &options);
|
||||
let out = ParamRows::new(tl!("Flattener options"), ¶ms, Bind::bound(&mut fields)).show(ui);
|
||||
if out.changed
|
||||
&& let Ok(o) = serde_json::from_value(Value::Object(form::json_of(¶ms, &fields)))
|
||||
{
|
||||
set.options = o;
|
||||
}
|
||||
}
|
||||
ui.add_space(6.0);
|
||||
preview_area(ui, &t, set.highlight, set.overprints);
|
||||
// What is highlighted, and whether the snapshot is behind the document or the settings.
|
||||
let (count, stale) = CACHE.with(|c| {
|
||||
let c = c.borrow();
|
||||
let s = c.shot.as_ref();
|
||||
let count = s.map_or(0, |s| s.report.objects(set.highlight).len() + s.report.areas(set.highlight).len());
|
||||
(count, s.is_some_and(|s| s.revision != revision || s.options != set.options || s.overprints != set.overprints))
|
||||
});
|
||||
preview_area(app, ui, &t, set.highlight, set.overprints);
|
||||
if set.highlight != Highlight::None {
|
||||
let count = app.flattener_preview.shot.as_ref().map_or(0, |s| s.report.objects(set.highlight).len() + s.report.areas(set.highlight).len());
|
||||
let areas = matches!(set.highlight, Highlight::RasterizedRegions | Highlight::AllRasterized);
|
||||
let n = count as u64;
|
||||
let msg = if areas {
|
||||
@@ -251,10 +245,10 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
} else {
|
||||
crate::i18n::tn(n, "{n} object highlighted", "{n} objects highlighted")
|
||||
};
|
||||
widgets::dim_label(ui, &msg);
|
||||
Text::new(msg).shade(Shade::Muted).show(ui);
|
||||
}
|
||||
if stale {
|
||||
ui.label(egui::RichText::new(tl!("Changed since the last Refresh.")).color(t.text_dim).size(11.5));
|
||||
if stale(app) {
|
||||
Text::new(tl!("Changed since the last Refresh.")).role(TypeRole::Caption).shade(Shade::Muted).show(ui);
|
||||
}
|
||||
if set != app.ui.flattener_preview {
|
||||
app.ui.flattener_preview = set;
|
||||
@@ -264,52 +258,34 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The preview: the snapshot rendered over the visible part, grey under a highlight with the
|
||||
/// highlighted art in `t.highlight`; click, Alt-click, drag and double-click zoom and pan.
|
||||
fn preview_area(ui: &mut Ui, t: &Tokens, h: Highlight, overprints: Overprints) {
|
||||
/// The preview: the whole snapshot rendered once per [`Key`], grey under a highlight with the
|
||||
/// highlighted art in `t.highlight`, in a pane that zooms and pans it.
|
||||
fn preview_area(app: &mut VectorcraftApp, ui: &mut Ui, t: &Tokens, h: Highlight, overprints: Overprints) {
|
||||
let FlattenerPreviewModel { shot, renderer, view, drawn, .. } = &mut app.flattener_preview;
|
||||
let Some(shot) = shot.as_ref() else { return };
|
||||
let w = ui.available_width();
|
||||
let (rect, resp) = ui.allocate_exact_size(vec2(w, (w * 0.8).clamp(140.0, 320.0)), Sense::click_and_drag());
|
||||
let resp = resp.on_hover_text(tl!("Click to zoom in, Alt-click to zoom out, drag to pan, double-click to fit"));
|
||||
ui.painter().rect_filled(rect, 0.0, t.pasteboard);
|
||||
let ppp = ui.ctx().pixels_per_point();
|
||||
CACHE.with(|c| {
|
||||
let mut c = c.borrow_mut();
|
||||
let Cache { shot, view, renderer, drawn, .. } = &mut *c;
|
||||
let Some(shot) = shot.as_ref() else { return };
|
||||
let mut drawn = drawn.borrow_mut();
|
||||
let Drawn { key, tex } = &mut *drawn;
|
||||
let visible = view.visible(shot.fit);
|
||||
if resp.double_clicked() {
|
||||
*view = View::default();
|
||||
} else if resp.clicked()
|
||||
&& let Some(p) = resp.interact_pointer_pos()
|
||||
{
|
||||
let alt = ui.input(|i| i.modifiers.alt);
|
||||
view.zoom_at(shot.fit, thumb_to_doc(visible, rect, p), if alt { 0.5 } else { 2.0 });
|
||||
} else if resp.dragged() {
|
||||
let per_px = (visible.width() / rect.width() as f64).max(visible.height() / rect.height() as f64);
|
||||
let d = resp.drag_delta();
|
||||
view.pan(shot.fit, (-d.x as f64 * per_px, -d.y as f64 * per_px));
|
||||
// A dock resize renders again only when the width crosses a power of two.
|
||||
let px = ((w * ui.ctx().pixels_per_point() * 4.0).ceil().max(1.0) as u32).next_power_of_two().min(4096);
|
||||
let mut drawn = drawn.borrow_mut();
|
||||
let Drawn { key, tex } = &mut *drawn;
|
||||
let k = (shot.generation, h, overprints == Overprints::Simulate, px);
|
||||
if *key != Some(k) || tex.is_none() {
|
||||
let r = renderer.get_or_insert_with(vectorcraft_render::Renderer::new);
|
||||
let img = render(r, shot, shot.fit, [px, px], h, overprints, t.highlight);
|
||||
match tex {
|
||||
Some(tx) => tx.set(img, egui::TextureOptions::LINEAR),
|
||||
None => *tex = Some(ui.ctx().load_texture("flattener-preview", img, egui::TextureOptions::LINEAR)),
|
||||
}
|
||||
let visible = view.visible(shot.fit);
|
||||
let px = [(rect.width() * ppp) as u32, (rect.height() * ppp) as u32];
|
||||
let k = (shot.generation, h, overprints == Overprints::Simulate, [visible.x0, visible.y0, visible.x1, visible.y1].map(f64::to_bits), px);
|
||||
if *key != Some(k) || tex.is_none() {
|
||||
let r = renderer.get_or_insert_with(vectorcraft_render::Renderer::new);
|
||||
let img = render(r, shot, visible, px, h, overprints, t.highlight);
|
||||
match tex {
|
||||
Some(tx) => tx.set(img, egui::TextureOptions::LINEAR),
|
||||
None => *tex = Some(ui.ctx().load_texture("flattener-preview", img, egui::TextureOptions::LINEAR)),
|
||||
}
|
||||
*key = Some(k);
|
||||
}
|
||||
if let Some(tx) = tex {
|
||||
let r =
|
||||
Rect::from_min_max(doc_to_thumb(visible, rect, visible.origin()), doc_to_thumb(visible, rect, Point::new(visible.x1, visible.y1)));
|
||||
ui.painter().with_clip_rect(rect).image(tx.id(), r, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), egui::Color32::WHITE);
|
||||
}
|
||||
});
|
||||
ui.painter().rect_stroke(rect, 0.0, Stroke::new(1.0, t.border), StrokeKind::Outside);
|
||||
*key = Some(k);
|
||||
}
|
||||
let Some(tx) = tex else { return };
|
||||
// At scale 1 the picture is the fit of the well (width `w`, height `w / ASPECT`), so the zoom reads relative to the fit.
|
||||
let s = (f64::from(w) / shot.fit.width()).min(f64::from(w / ASPECT) / shot.fit.height());
|
||||
let size = vec2((shot.fit.width() * s) as f32, (shot.fit.height() * s) as f32);
|
||||
let out = PreviewPane::new(tl!("Flattener Preview"), SizedTexture::new(tx.id(), size), view).aspect(ASPECT).show(ui);
|
||||
if let Some(v) = out.view {
|
||||
*view = v;
|
||||
}
|
||||
}
|
||||
|
||||
/// `visible` of the snapshot at most `px` pixels: in colour without a highlight, else in grey with
|
||||
@@ -352,36 +328,55 @@ fn render(
|
||||
crate::canvas::color_image(&img)
|
||||
}
|
||||
|
||||
pub fn menu(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
if widgets::menu_item(ui, tl!("Refresh"), app.session.active().is_some(), false)
|
||||
&& let Err(e) = refresh(app)
|
||||
{
|
||||
app.status(e);
|
||||
}
|
||||
let shown = app.ui.flattener_preview.show_options;
|
||||
if widgets::menu_item(ui, tl!("Show Options"), true, shown) {
|
||||
app.ui.flattener_preview.show_options = !shown;
|
||||
}
|
||||
if widgets::menu_item(ui, tl!("Fit in Window"), true, false) {
|
||||
CACHE.with(|c| c.borrow_mut().view = View::default());
|
||||
}
|
||||
if widgets::menu_item(ui, tl!("Save Transparency Flattener Preset…"), true, false) {
|
||||
// Saved under a new name, then shown in the presets manager to name it.
|
||||
match app.run("flattener.presets.save", json!({ "options": app.ui.flattener_preview.options })) {
|
||||
Ok(r) => {
|
||||
const REFRESH: &str = "ui.flattenerPreview.refresh";
|
||||
const SHOW_OPTIONS: &str = "ui.flattenerPreview.showOptions";
|
||||
const FIT_IN_WINDOW: &str = "ui.flattenerPreview.fitInWindow";
|
||||
const SAVE_PRESET: &str = "ui.flattenerPreview.savePreset";
|
||||
|
||||
/// The rows of the panel menu.
|
||||
pub(crate) fn menu_rows(_app: &VectorcraftApp) -> Vec<PanelRow> {
|
||||
// PLAN: owner hub-w2 registers these four commands in `menus/ui_commands.rs`.
|
||||
[REFRESH, SHOW_OPTIONS, FIT_IN_WINDOW, SAVE_PRESET].into_iter().map(|id| PanelRow::Command(CommandRef::new(id))).collect()
|
||||
}
|
||||
|
||||
/// The states of the menu's rows: Refresh needs a document, Show Options is checked while the options show.
|
||||
pub(crate) fn menu_states(app: &VectorcraftApp, states: &mut CommandStates) {
|
||||
states.set(CommandRef::new(REFRESH), CommandState::new().enabled(app.session.active().is_some()));
|
||||
states.set(CommandRef::new(SHOW_OPTIONS), CommandState::new().checked(app.ui.flattener_preview.show_options));
|
||||
}
|
||||
|
||||
/// Runs a menu row; `None` for an id that is not this panel's.
|
||||
pub(crate) fn run(app: &mut VectorcraftApp, id: &str, _params: &Value) -> Option<Result<Value, String>> {
|
||||
let r = match id {
|
||||
REFRESH => refresh(app),
|
||||
SHOW_OPTIONS => {
|
||||
let set = &mut app.ui.flattener_preview;
|
||||
set.show_options = !set.show_options;
|
||||
Ok(Value::Null)
|
||||
}
|
||||
FIT_IN_WINDOW => {
|
||||
app.flattener_preview.view = fitting_view();
|
||||
Ok(Value::Null)
|
||||
}
|
||||
SAVE_PRESET => {
|
||||
// Saved under a new name, then shown in the presets manager to name it.
|
||||
app.run("flattener.presets.save", json!({ "options": app.ui.flattener_preview.options })).map(|r| {
|
||||
let name = r["name"].as_str().unwrap_or_default().to_string();
|
||||
app.ui.flattener_preview.preset = name.clone();
|
||||
let _ = app.run("ui.flattenerPresetsDialog", json!({ "selected": name }));
|
||||
}
|
||||
Err(e) => app.status(e),
|
||||
Value::Null
|
||||
})
|
||||
}
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
Some(r)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use vectorcraft_engine::Session;
|
||||
use vectorcraft_geom::Point;
|
||||
|
||||
/// A half-transparent blue rectangle over type, and a green square apart.
|
||||
fn app() -> VectorcraftApp {
|
||||
@@ -406,60 +401,28 @@ mod tests {
|
||||
ctx
|
||||
}
|
||||
|
||||
fn pixels() -> egui::ColorImage {
|
||||
CACHE.with(|c| {
|
||||
let mut c = c.borrow_mut();
|
||||
let Cache { shot, view, renderer, .. } = &mut *c;
|
||||
let shot = shot.as_ref().unwrap();
|
||||
render(
|
||||
renderer.get_or_insert_with(vectorcraft_render::Renderer::new),
|
||||
shot,
|
||||
view.visible(shot.fit),
|
||||
[300, 200],
|
||||
Highlight::None,
|
||||
Overprints::Preserve,
|
||||
egui::Color32::RED,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_panel_takes_a_snapshot_and_highlights_in_red() {
|
||||
let mut app = app();
|
||||
frame(&mut app);
|
||||
assert!(CACHE.with(|c| c.borrow().shot.is_some()), "a first look refreshes");
|
||||
assert!(CACHE.with(|c| c.borrow().drawn.borrow().tex.is_some()), "the preview is drawn");
|
||||
assert!(app.flattener_preview.shot.is_some(), "a first look refreshes");
|
||||
assert!(app.flattener_preview.drawn.borrow().tex.is_some(), "the preview is drawn");
|
||||
let v = command(&mut app, &json!({"highlight": "allAffected"})).unwrap();
|
||||
assert_eq!(v["counts"]["allAffected"], 2);
|
||||
assert_eq!(app.ui.open_panel.as_deref(), Some(ID));
|
||||
let text = crate::tests_labels::painted_text(&mut app, show);
|
||||
assert!(text.contains("2 objects highlighted") && text.contains("All Affected Objects"), "{text}");
|
||||
let FlattenerPreviewModel { shot, renderer, .. } = &mut app.flattener_preview;
|
||||
let shot = shot.as_ref().unwrap();
|
||||
let r = renderer.get_or_insert_with(vectorcraft_render::Renderer::new);
|
||||
let fit = DRect::new(0.0, 0.0, 300.0, 200.0);
|
||||
assert!(shot.fit.contains(Point::new(300.0, 200.0)));
|
||||
// The highlight is red where the art is affected and grey elsewhere.
|
||||
let (shot_fit, img) = CACHE.with(|c| {
|
||||
let mut c = c.borrow_mut();
|
||||
let Cache { shot, renderer, .. } = &mut *c;
|
||||
let shot = shot.as_ref().unwrap();
|
||||
let fit = DRect::new(0.0, 0.0, 300.0, 200.0);
|
||||
(
|
||||
shot.fit,
|
||||
render(
|
||||
renderer.get_or_insert_with(vectorcraft_render::Renderer::new),
|
||||
shot,
|
||||
fit,
|
||||
[300, 200],
|
||||
Highlight::AllAffected,
|
||||
Overprints::Preserve,
|
||||
egui::Color32::RED,
|
||||
),
|
||||
)
|
||||
});
|
||||
assert!(shot_fit.contains(Point::new(300.0, 200.0)));
|
||||
let img = render(r, shot, fit, [300, 200], Highlight::AllAffected, Overprints::Preserve, egui::Color32::RED);
|
||||
let at = |x: usize, y: usize| img.pixels[y * img.width() + x];
|
||||
assert_eq!(at(100, 60), egui::Color32::RED, "inside the transparent rectangle");
|
||||
let apart = at(260, 160);
|
||||
assert!(apart.r() == apart.g() && apart.g() == apart.b(), "the lone square is grey: {apart:?}");
|
||||
// Without a highlight it is in colour.
|
||||
let plain = pixels();
|
||||
let plain = render(r, shot, fit, [300, 200], Highlight::None, Overprints::Preserve, egui::Color32::RED);
|
||||
assert!(plain.pixels.iter().any(|p| i32::from(p.b()) > i32::from(p.r()) + 40), "the blue shows");
|
||||
}
|
||||
|
||||
@@ -467,37 +430,21 @@ mod tests {
|
||||
fn edits_wait_for_refresh_and_settings_drive_the_report() {
|
||||
let mut app = app();
|
||||
frame(&mut app);
|
||||
let text = crate::tests_labels::painted_text(&mut app, show);
|
||||
assert!(!text.contains("Changed since"), "{text}");
|
||||
assert!(!stale(&app));
|
||||
app.run("select.all", json!({})).unwrap();
|
||||
app.run("transparency.set", json!({"opacity": 100})).unwrap();
|
||||
let text = crate::tests_labels::painted_text(&mut app, show);
|
||||
assert!(text.contains("Changed since the last Refresh."), "{text}");
|
||||
assert!(stale(&app), "an edit waits for Refresh");
|
||||
let v = command(&mut app, &json!({"highlight": "transparentObjects"})).unwrap();
|
||||
assert_eq!(v["counts"]["transparentObjects"], 0, "an opaque document");
|
||||
// A preset and options: balance 0 rasterizes; the dropdown then reads [Custom].
|
||||
assert!(!stale(&app), "command refreshes");
|
||||
// A preset and options: balance 0 rasterizes; the preset then reads [Custom].
|
||||
let v = command(&mut app, &json!({"preset": "low", "options": {"balance": 0}, "showOptions": true})).unwrap();
|
||||
assert_eq!((v["options"]["balance"].as_f64(), v["options"]["gradientPpi"].as_f64()), (Some(0.0), Some(150.0)));
|
||||
assert!(app.ui.flattener_preview.show_options);
|
||||
let text = crate::tests_labels::painted_text(&mut app, show);
|
||||
assert!(text.contains("[Custom]") && text.contains("Raster/Vector Balance:"), "{text}");
|
||||
assert!(custom(&app));
|
||||
assert!(command(&mut app, &json!({"highlight": "nope"})).is_err());
|
||||
// The settings persist with the UI state.
|
||||
let back: crate::UiState = serde_json::from_value(serde_json::to_value(&app.ui).unwrap()).unwrap();
|
||||
assert_eq!(back.flattener_preview, app.ui.flattener_preview);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zoom_and_pan_stay_inside_the_document() {
|
||||
let fit = DRect::new(0.0, 0.0, 200.0, 100.0);
|
||||
let mut v = View::default();
|
||||
assert_eq!(v.visible(fit), fit);
|
||||
v.zoom_at(fit, Point::new(50.0, 25.0), 2.0);
|
||||
let r = v.visible(fit);
|
||||
assert!((r.width() - 100.0).abs() < 1e-9 && r.x0 >= 0.0 && r.y0 >= 0.0, "{r:?}");
|
||||
v.pan(fit, (-1000.0, 0.0));
|
||||
assert_eq!(v.visible(fit).x0, 0.0);
|
||||
v.zoom_at(fit, Point::new(0.0, 0.0), 0.25);
|
||||
assert_eq!(v.visible(fit), fit, "never smaller than fit");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user