//! Helpers for the number, blend and kerning fields the panels build from framework components. use craft::behavior::NumberCodec; use craft::ui::contract::{Bind, Entry}; use craft::ui::patterns::blend_keys::{BlendKeyEntry, BlendShortcut, blend_entries as framework_blend_entries}; use craft::ui::runtime::env::{Env, Label, LengthUnits, UnitKind, Units}; use craft::ui::text::NumberField; use egui::Ui; use vectorcraft_color::BlendMode; /// Every blend mode with its Shift+Alt letter, in the order of the menu. fn blend_table() -> Vec<(BlendMode, BlendShortcut)> { use BlendShortcut as S; BlendMode::ALL .into_iter() .map(|mode| { let shortcut = match mode { BlendMode::Normal => S::Normal, BlendMode::Darken => S::Darken, BlendMode::Multiply => S::Multiply, BlendMode::ColorBurn => S::ColorBurn, BlendMode::Lighten => S::Lighten, BlendMode::Screen => S::Screen, BlendMode::ColorDodge => S::ColorDodge, BlendMode::Overlay => S::Overlay, BlendMode::SoftLight => S::SoftLight, BlendMode::HardLight => S::HardLight, BlendMode::Difference => S::Difference, BlendMode::Exclusion => S::Exclusion, BlendMode::Hue => S::Hue, BlendMode::Saturation => S::Saturation, BlendMode::Color => S::Color, BlendMode::Luminosity => S::Luminosity, }; (mode, shortcut) }) .collect() } /// The blend-mode list: Normal | darken | lighten | contrast | inversion | component modes, the /// groups separated where the framework's blend list separates them. pub fn blend_entries() -> Vec> { let entries: Vec<_> = blend_table().into_iter().map(|(mode, shortcut)| BlendKeyEntry::new(mode, shortcut)).collect(); framework_blend_entries(&entries, |entry| mode_label(entry.key)) } /// The modes of an effect's blend-mode row. The entries use the stored IDs and translated names. /// The framework uses each mode's key metadata to separate the existing groups. pub fn effect_blend_modes() -> Vec<(BlendKeyEntry, Label)> { blend_table() .into_iter() .map(|(mode, shortcut)| (BlendKeyEntry::new(vectorcraft_effects::blend_mode_id(mode), shortcut), mode_label(mode))) .collect() } /// `mode`'s name in the interface language. fn mode_label(mode: BlendMode) -> Label { Label::from(crate::i18n::t(mode.label())) } /// `lengths` with `bare_points` (Numbers Without Units Are Points) applied: a bare number in a picas /// field counts as points. pub fn bare_points(lengths: LengthUnits, on: bool) -> LengthUnits { if on && lengths.shown == UnitKind::Picas { lengths.bare_in(UnitKind::Points) } else { lengths } } /// The codec of a size field: a length field's reader at the shown unit's places, and a lone `50%` read as that share /// of the size the edit began on. pub fn size_codec(units: &Units, lengths: LengthUnits) -> NumberCodec { NumberCodec::new(units.length_reader(lengths), *lengths.shown.decimals().end()).share_of_start() } /// A length field over a size (a width, height, radius or handle length) of `value`, in `lengths.stored`, read with /// [`size_codec`]. pub fn size_field(ui: &Ui, label: impl Into