mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-25 10:17:02 -06:00
The General unit is the active document's units: Preferences > Units >
General sets them (one undo step) and is the units new documents start
in (and the unit shown with no document open); Document Setup changes
only the document. The Transform panel, Properties, the Control bar, the
Info panel, rulers, the status bar, Align spacing, Tabs, paragraph
indents, Pattern Options, dialog distances (Move, shape sizes, path
dialogs, Artboard Options, effect distances, Transform Each, parameter
dialogs), New Document and the canvas measurement labels (smart-guide
sizes, moves, the Measure tool, the Live Corners radius) all follow it,
live. Units > Type drives font size, leading and baseline shift; Units >
Stroke also reaches the Appearance panel and the Magic Wand weight
tolerance. Typed units ("5 mm", "12 pt") work in any field; all ten
units are choices; length preferences show in their unit and accept a
unit; offset/simplify/grid/type-size params read typed units. Typed text
starting with a multi-byte character no longer panics the parser.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
84 lines
2.7 KiB
Rust
84 lines
2.7 KiB
Rust
//! Units (Preferences ▸ Units): the unit every length the UI shows and reads is in.
|
|
//!
|
|
//! General (rulers, positions, sizes, dialog distances, canvas readouts) is the active document's
|
|
//! units, so they travel with the file: Document Setup (`document.setUnits`) changes them, and
|
|
//! Preferences ▸ Units ▸ General changes them too and is also the units new documents start in (and
|
|
//! the unit shown while no document is open). Stroke (weights, dashes) and Type (sizes, leading,
|
|
//! baseline shift) are application preferences. Typed values with a unit ("5 mm", "12 pt") work in
|
|
//! any unit.
|
|
|
|
use vectorcraft_doc::Unit;
|
|
|
|
use crate::{Result, Session};
|
|
|
|
/// Which Units preference a length follows.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum Measure {
|
|
General,
|
|
Stroke,
|
|
Type,
|
|
}
|
|
|
|
impl Measure {
|
|
/// Its name in `prefs.list` (`measure`).
|
|
pub fn name(self) -> &'static str {
|
|
match self {
|
|
Measure::General => "general",
|
|
Measure::Stroke => "stroke",
|
|
Measure::Type => "type",
|
|
}
|
|
}
|
|
|
|
/// The preference that picks its unit.
|
|
pub fn pref_key(self) -> &'static str {
|
|
match self {
|
|
Measure::General => "unitsGeneral",
|
|
Measure::Stroke => "unitsStroke",
|
|
Measure::Type => "unitsType",
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The unit a Units preference value names (points when it names none).
|
|
fn pref_unit(value: &str) -> Unit {
|
|
Unit::named(value).unwrap_or_default()
|
|
}
|
|
|
|
impl Session {
|
|
/// The unit lengths of kind `m` show in and bare numbers typed for them are read in.
|
|
pub fn unit(&self, m: Measure) -> Unit {
|
|
match m {
|
|
Measure::General => self.active().map_or_else(|| self.default_units(), |d| d.doc.units),
|
|
Measure::Stroke => pref_unit(&self.prefs.units_stroke),
|
|
Measure::Type => pref_unit(&self.prefs.units_type),
|
|
}
|
|
}
|
|
|
|
/// The General unit: the active document's units (Preferences ▸ Units ▸ General without one).
|
|
pub fn general_unit(&self) -> Unit {
|
|
self.unit(Measure::General)
|
|
}
|
|
|
|
/// Units ▸ Type: the unit type sizes, leading and baseline shift show in.
|
|
pub fn type_unit(&self) -> Unit {
|
|
self.unit(Measure::Type)
|
|
}
|
|
|
|
/// Preferences ▸ Units ▸ General: the units new documents start in.
|
|
pub fn default_units(&self) -> Unit {
|
|
pref_unit(&self.prefs.units_general)
|
|
}
|
|
|
|
/// Set the active document's units (one undo step, as Document Setup does; none when they
|
|
/// already are `u`).
|
|
pub(crate) fn set_document_units(&mut self, u: Unit) -> Result<()> {
|
|
if self.doc()?.doc.units == u {
|
|
return Ok(());
|
|
}
|
|
self.edit("Document Setup", |d, _| {
|
|
d.units = u;
|
|
Ok(())
|
|
})
|
|
}
|
|
}
|