mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 05:14:24 +00:00
merge-fwa: FW5: New Document Bitmap (1 bit, no Transparent), Pixel Aspect Ratio row, Web/Mobile cards turn Artboards on, Save Document Preset icon and pane
This commit is contained in:
@@ -54,7 +54,8 @@ mod new_document;
|
||||
|
||||
pub use new_document::{
|
||||
BACKGROUND_COLOR_KEY, BackgroundContents, BitDepth, ColorMode, ColorProfiles, DocumentOptions, DocumentPreset, HostedNewDocument,
|
||||
NewDocumentDialog, NewDocumentOutput, NewDocumentPresetWords, NewDocumentResult, NewDocumentWords, Orientation, PresetCategory,
|
||||
NewDocumentDialog, NewDocumentOutput, NewDocumentPresetWords, NewDocumentResult, NewDocumentWords, Orientation, PIXEL_ASPECTS, PresetCategory,
|
||||
SQUARE_PIXELS,
|
||||
};
|
||||
|
||||
mod about;
|
||||
|
||||
@@ -32,7 +32,8 @@ use details::Right;
|
||||
|
||||
pub(crate) use presets::push_recent;
|
||||
pub use presets::{
|
||||
BackgroundContents, BitDepth, ColorMode, ColorProfiles, DocumentOptions, DocumentPreset, NewDocumentPresetWords, Orientation, PresetCategory,
|
||||
BackgroundContents, BitDepth, ColorMode, ColorProfiles, DocumentOptions, DocumentPreset, NewDocumentPresetWords, Orientation, PIXEL_ASPECTS,
|
||||
PresetCategory, SQUARE_PIXELS,
|
||||
};
|
||||
|
||||
/// The key of the colour request Custom makes.
|
||||
@@ -84,19 +85,34 @@ craft_ui::words! {
|
||||
transparent: "newDocument.transparent" = "Transparent";
|
||||
custom_color: "newDocument.customColor" = "Custom…";
|
||||
custom_swatch: "newDocument.customSwatch" = "Custom background color";
|
||||
save_preset: "newDocument.savePreset" = "Save Preset…";
|
||||
/// The icon right of Name that opens the naming pane, and the pane's header.
|
||||
save_preset_title: "newDocument.savePresetTitle" = "Save Document Preset";
|
||||
/// The naming pane's button that saves the preset.
|
||||
save_preset: "newDocument.savePreset" = "Save Preset";
|
||||
preset_name: "newDocument.presetName" = "Preset Name";
|
||||
save: "newDocument.save" = "Save";
|
||||
/// The name the naming pane proposes: `{n}` is the first number no saved preset has.
|
||||
saved_name: template "newDocument.savedName" = "Saved {n}";
|
||||
no_saved: "newDocument.noSaved" = "No saved presets yet.";
|
||||
too_many: "newDocument.tooMany" = "At most 256 document presets can be saved";
|
||||
bitmap: "newDocument.bitmap" = "Bitmap";
|
||||
grayscale: "newDocument.grayscale" = "Grayscale";
|
||||
rgb_color: "newDocument.rgbColor" = "RGB Color";
|
||||
cmyk_color: "newDocument.cmykColor" = "CMYK Color";
|
||||
lab_color: "newDocument.labColor" = "Lab Color";
|
||||
depth_1: "newDocument.depth1" = "1 bit";
|
||||
depth_8: "newDocument.depth8" = "8 bit";
|
||||
depth_16: "newDocument.depth16" = "16 bit";
|
||||
depth_32: "newDocument.depth32" = "32 bit (float)";
|
||||
orientation: "newDocument.orientation" = "Orientation";
|
||||
pixel_aspect: "newDocument.pixelAspect" = "Pixel Aspect Ratio";
|
||||
square_pixels: "newDocument.pixelAspectSquare" = "Square Pixels";
|
||||
d1_dv_ntsc: "newDocument.pixelAspectD1DvNtsc" = "D1/DV NTSC (0.91)";
|
||||
d1_dv_pal: "newDocument.pixelAspectD1DvPal" = "D1/DV PAL (1.09)";
|
||||
d1_dv_ntsc_widescreen: "newDocument.pixelAspectD1DvNtscWidescreen" = "D1/DV NTSC Widescreen (1.21)";
|
||||
hdv_1080: "newDocument.pixelAspectHdv1080" = "HDV 1080/DVCPRO HD 720 (1.33)";
|
||||
d1_dv_pal_widescreen: "newDocument.pixelAspectD1DvPalWidescreen" = "D1/DV PAL Widescreen (1.46)";
|
||||
anamorphic_2_to_1: "newDocument.pixelAspectAnamorphic2To1" = "Anamorphic 2:1 (2)";
|
||||
dvcpro_hd_1080: "newDocument.pixelAspectDvcproHd1080" = "DVCPRO HD 1080 (1.5)";
|
||||
/// `{n}` is how many cards the open tab lists.
|
||||
presets_heading: template "newDocument.presetsHeading" = "Blank Document Presets ({n})";
|
||||
preset_details: "newDocument.presetDetails" = "Preset Details";
|
||||
@@ -123,6 +139,7 @@ struct Setup {
|
||||
screen_ppi: f64,
|
||||
profiles: Vec<ColorProfiles>,
|
||||
app_name: Option<Label>,
|
||||
custom_pixel_aspects: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
/// What the columns read besides the session: the setup, the remembered lists, the words and the units.
|
||||
@@ -164,7 +181,7 @@ struct Shared<'a> {
|
||||
}
|
||||
|
||||
/// What the dialog keeps while it is open besides the settings: the fields that show the size, the open tab and card,
|
||||
/// and the state of the Save Preset row. It lives in the dialog's body slot.
|
||||
/// the Save Document Preset pane and whether a card turned Artboards on. It lives in the dialog's body slot.
|
||||
#[derive(Clone)]
|
||||
struct Session {
|
||||
/// The width, height and resolution fields of the settings.
|
||||
@@ -176,8 +193,11 @@ struct Session {
|
||||
card: Option<usize>,
|
||||
/// The orientation picked with the control; built-in cards turn with it.
|
||||
orientation: Option<Orientation>,
|
||||
/// The Save Preset name row, when open.
|
||||
/// The Save Document Preset pane's name, while the pane stands in for the details.
|
||||
naming: Option<String>,
|
||||
/// A Web or Mobile card turned Artboards on, so a card of another tab turns it off again; the user's own tick ends
|
||||
/// it.
|
||||
preset_artboard: bool,
|
||||
/// A save was refused and its reason shows under the name field.
|
||||
name_tried: bool,
|
||||
focus_name: bool,
|
||||
@@ -193,6 +213,7 @@ impl Default for Session {
|
||||
card: None,
|
||||
orientation: None,
|
||||
naming: None,
|
||||
preset_artboard: false,
|
||||
name_tried: false,
|
||||
focus_name: true,
|
||||
focus_preset_name: false,
|
||||
@@ -201,7 +222,7 @@ impl Default for Session {
|
||||
}
|
||||
|
||||
impl Session {
|
||||
/// Drops what the user was doing with the settings: the picked card, the orientation, the Save Preset row and a
|
||||
/// Drops what the user was doing with the settings: the picked card, the orientation, the Save Document Preset pane and a
|
||||
/// running size edit. The fields that show the size follow the settings by themselves.
|
||||
fn reset(&mut self) {
|
||||
self.card = None;
|
||||
@@ -216,10 +237,22 @@ impl Session {
|
||||
fn apply_card(&mut self, values: &mut DocumentOptions, card: &cards::Card, points_per_inch: f64) {
|
||||
if card.restores_all {
|
||||
restore_all(values, &card.settings);
|
||||
self.preset_artboard = false;
|
||||
} else {
|
||||
values.size = card.settings.size;
|
||||
values.pixels_per_inch = card.settings.pixels_per_inch;
|
||||
values.unit = card.settings.unit;
|
||||
match card.artboard {
|
||||
Some(true) if !values.artboard => {
|
||||
values.artboard = true;
|
||||
self.preset_artboard = true;
|
||||
}
|
||||
Some(false) if self.preset_artboard => {
|
||||
values.artboard = false;
|
||||
self.preset_artboard = false;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
self.size = values.linked(points_per_inch);
|
||||
self.gesture = None;
|
||||
@@ -257,6 +290,7 @@ fn open_options(values: &mut DocumentOptions, clipboard: Option<PixelSize>, scre
|
||||
|
||||
fn mode_label(words: &NewDocumentWords, mode: ColorMode) -> &Label {
|
||||
match mode {
|
||||
ColorMode::Bitmap => &words.bitmap,
|
||||
ColorMode::Grayscale => &words.grayscale,
|
||||
ColorMode::Rgb => &words.rgb_color,
|
||||
ColorMode::Cmyk => &words.cmyk_color,
|
||||
@@ -266,6 +300,7 @@ fn mode_label(words: &NewDocumentWords, mode: ColorMode) -> &Label {
|
||||
|
||||
fn depth_label(words: &NewDocumentWords, depth: BitDepth) -> &Label {
|
||||
match depth {
|
||||
BitDepth::One => &words.depth_1,
|
||||
BitDepth::Eight => &words.depth_8,
|
||||
BitDepth::Sixteen => &words.depth_16,
|
||||
BitDepth::ThirtyTwo => &words.depth_32,
|
||||
@@ -459,11 +494,15 @@ fn body(ui: &mut Ui, form: &mut FormCx<'_, '_>, values: &mut DocumentOptions, sh
|
||||
/// then the recent documents kept under [`remember_recent`](NewDocumentDialog::remember_recent) (the last 20), then the built-in defaults not yet among them. Saved lists the user's presets in a list
|
||||
/// whose Delete key removes the picked ones, with "No saved presets yet." when there are none.
|
||||
/// - Cards: a click or an arrow fills the details; Enter on a card or a double click creates. A built-in card turns with
|
||||
/// the orientation control. Saved and recent cards restore every setting but the name.
|
||||
/// - Details: Name, Width and Height with their units, Orientation, Artboards, Resolution, Color Mode, Depth, Color
|
||||
/// Profile and Background Contents. Save Preset opens a name row under the column; Enter there saves the preset and
|
||||
/// does not create the document. The name is checked by the preset library (not blank, different from the saved
|
||||
/// names ignoring case), and at most 256 presets are kept.
|
||||
/// the orientation control. Saved and recent cards restore every setting but the name. A Web or Mobile card turns
|
||||
/// Artboards on; a card of another category turns it off again only when such a card turned it on.
|
||||
/// - Details: Name with the Save Document Preset icon, Width and Height with their units, Orientation, Artboards,
|
||||
/// Resolution, Color Mode, Depth, Color Profile, Pixel Aspect Ratio (the built-in ratios, then the app's
|
||||
/// [`custom_pixel_aspects`](NewDocumentDialog::custom_pixel_aspects)) and Background Contents. Bitmap takes only 1
|
||||
/// bit and offers no Transparent background ([`DocumentOptions::fitted`]).
|
||||
/// - Save Document Preset: the icon opens a pane in place of the details with the first free `Saved N` selected;
|
||||
/// Save Preset or Enter saves the preset and does not create the document, Cancel goes back. The name is checked by
|
||||
/// the preset library (not blank, different from the saved names ignoring case), and at most 256 presets are kept.
|
||||
/// - Keys: Tab walks the fields, Enter is Create unless a field owns it, Escape closes. Holding Alt turns Close into
|
||||
/// Reset, which restores the settings from when the dialog opened and keeps it open. Ctrl+Z inside the dialog steps
|
||||
/// the dialog's own settings.
|
||||
@@ -505,6 +544,10 @@ pub struct NewDocumentDialog<'a> {
|
||||
/// The colour profiles each mode offers.
|
||||
#[prop]
|
||||
profiles: Vec<ColorProfiles>,
|
||||
/// The app's own pixel aspect ratios as `(id, name)`, listed after the built-in ones ([`PIXEL_ASPECTS`]); the
|
||||
/// names are the user's and are not translated.
|
||||
#[prop]
|
||||
custom_pixel_aspects: Vec<(String, String)>,
|
||||
/// Binds the saved presets of the Saved tab to `saved`, in place of the list the dialog keeps across runs. Each
|
||||
/// save and delete is also reported in [`NewDocumentOutput::presets`] for the app to store.
|
||||
#[prop]
|
||||
@@ -547,6 +590,7 @@ impl<'a> NewDocumentDialog<'a> {
|
||||
print_resolution: 300.0,
|
||||
screen_resolution: 72.0,
|
||||
profiles: Vec::new(),
|
||||
custom_pixel_aspects: Vec::new(),
|
||||
saved: None,
|
||||
recent: None,
|
||||
tab: None,
|
||||
@@ -570,6 +614,7 @@ impl<'a> NewDocumentDialog<'a> {
|
||||
print_resolution,
|
||||
screen_resolution,
|
||||
profiles,
|
||||
custom_pixel_aspects,
|
||||
mut saved,
|
||||
recent: mut recent_bound,
|
||||
mut tab,
|
||||
@@ -601,6 +646,7 @@ impl<'a> NewDocumentDialog<'a> {
|
||||
screen_ppi: screen_resolution,
|
||||
profiles,
|
||||
app_name,
|
||||
custom_pixel_aspects,
|
||||
};
|
||||
let untitled = words.untitled.as_str().to_owned();
|
||||
let shared = Shared {
|
||||
@@ -814,7 +860,11 @@ impl StackDialog for HostedNewDocument {
|
||||
restore_all(&mut self.options, &item.value);
|
||||
Ok(())
|
||||
}
|
||||
_ => set_serde_field(&mut self.options, field, value),
|
||||
_ => {
|
||||
set_serde_field(&mut self.options, field, value)?;
|
||||
self.options = self.options.clone().fitted();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,9 @@ pub(super) struct Card {
|
||||
/// What the card sets: the size and unit, or (a recent document, a saved preset) everything but the name.
|
||||
pub(super) settings: DocumentOptions,
|
||||
pub(super) restores_all: bool,
|
||||
/// What a category card does to Artboards: `Some(true)` turns it on (Web, Mobile), `Some(false)` takes back what
|
||||
/// such a card turned on; `None` (Recent, Clipboard, Saved) leaves it.
|
||||
pub(super) artboard: Option<bool>,
|
||||
/// The hover text of a recent document, which names everything it restores.
|
||||
pub(super) detail: Option<String>,
|
||||
/// The width and height the page outline has.
|
||||
@@ -259,6 +262,7 @@ fn cards_of(tab: TabId, session: &Session, values: &DocumentOptions, read: &Read
|
||||
aspect: (settings.size.width, settings.size.height),
|
||||
settings,
|
||||
restores_all: false,
|
||||
artboard: Some(preset.artboard),
|
||||
detail: None,
|
||||
saved: None,
|
||||
}
|
||||
@@ -282,6 +286,7 @@ fn cards_of(tab: TabId, session: &Session, values: &DocumentOptions, read: &Read
|
||||
aspect: (settings.size.width, settings.size.height),
|
||||
settings: settings.clone(),
|
||||
restores_all: true,
|
||||
artboard: None,
|
||||
detail: None,
|
||||
saved: Some(preset.id),
|
||||
}
|
||||
@@ -297,6 +302,7 @@ fn cards_of(tab: TabId, session: &Session, values: &DocumentOptions, read: &Read
|
||||
aspect: (size.width, size.height),
|
||||
settings,
|
||||
restores_all: false,
|
||||
artboard: None,
|
||||
detail: None,
|
||||
saved: None,
|
||||
});
|
||||
@@ -316,12 +322,13 @@ fn cards_of(tab: TabId, session: &Session, values: &DocumentOptions, read: &Read
|
||||
aspect: (recent.size.width, recent.size.height),
|
||||
settings: recent.clone(),
|
||||
restores_all: true,
|
||||
artboard: None,
|
||||
detail: Some(detail),
|
||||
saved: None,
|
||||
});
|
||||
}
|
||||
for preset in &setup.recent_defaults {
|
||||
let card = preset_card(preset);
|
||||
let card = Card { artboard: None, ..preset_card(preset) };
|
||||
let listed =
|
||||
recents.iter().any(|recent| recent.size == card.settings.size && recent.pixels_per_inch == card.settings.pixels_per_inch);
|
||||
if !listed {
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
//! The details column of New Document: name, size, orientation, resolution, colour settings and the Save Preset row.
|
||||
//! The details column of New Document: name, size, orientation, resolution, colour settings, and the Save Document Preset
|
||||
//! pane that stands in for them while a preset is named.
|
||||
|
||||
use super::presets::{BackgroundContents, BitDepth, ColorMode, DocumentOptions, MAX_NAME, MAX_SAVED, Orientation, RESOLUTION_UNITS, SIZE_UNITS};
|
||||
use super::presets::{
|
||||
BackgroundContents, ColorMode, DocumentOptions, MAX_NAME, MAX_SAVED, Orientation, PIXEL_ASPECTS, RESOLUTION_UNITS, SIZE_UNITS, SQUARE_PIXELS,
|
||||
};
|
||||
use super::{NewDocumentWords, Read, Session, background_rgb, depth_label, mode_label};
|
||||
use crate::buttons::Button;
|
||||
use crate::buttons::{Button, IconButton};
|
||||
use crate::choice::{Checkbox, Segmented, Swatch};
|
||||
use crate::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _, Validity, clicked};
|
||||
use crate::display::{Shade, Text};
|
||||
@@ -16,7 +19,7 @@ use crate::runtime::focus::{self, FocusTarget};
|
||||
use crate::runtime::words::CommonWords;
|
||||
use crate::style::TypeRole;
|
||||
use crate::text::TextField;
|
||||
use craft_behavior::{EditEvent, EditEvents, PixelSize};
|
||||
use craft_behavior::{EditEvent, EditEvents, IconId, PixelSize};
|
||||
use craft_theme::Srgba;
|
||||
use egui::Ui;
|
||||
|
||||
@@ -84,6 +87,10 @@ fn unit_entries(read: &Read<'_>, kinds: &[UnitKind]) -> Vec<Entry<UnitKind>> {
|
||||
|
||||
/// Shows the column.
|
||||
pub(super) fn show(ui: &mut Ui, session: &mut Session, values: &mut DocumentOptions, read: &Read<'_>, right: &mut Right) {
|
||||
if session.naming.is_some() {
|
||||
save_pane(ui, session, read, right);
|
||||
return;
|
||||
}
|
||||
let (words, setup) = (read.words, read.setup);
|
||||
let before = values.clone();
|
||||
let mut keep_selection = false;
|
||||
@@ -91,32 +98,50 @@ pub(super) fn show(ui: &mut Ui, session: &mut Session, values: &mut DocumentOpti
|
||||
let size_units = unit_entries(read, &SIZE_UNITS);
|
||||
let resolution_units = unit_entries(read, &RESOLUTION_UNITS);
|
||||
let orientations = [Entry::new(Orientation::Portrait, words.portrait.clone()), Entry::new(Orientation::Landscape, words.landscape.clone())];
|
||||
let modes = [ColorMode::Grayscale, ColorMode::Rgb, ColorMode::Cmyk, ColorMode::Lab].map(|mode| Entry::new(mode, mode_label(words, mode).clone()));
|
||||
let depths = [BitDepth::Eight, BitDepth::Sixteen, BitDepth::ThirtyTwo].map(|depth| Entry::new(depth, depth_label(words, depth).clone()));
|
||||
let modes = ColorMode::ALL.map(|mode| Entry::new(mode, mode_label(words, mode).clone()));
|
||||
let depths: Vec<_> = values.color_mode.depths().iter().map(|&depth| Entry::new(depth, depth_label(words, depth).clone())).collect();
|
||||
let working = setup.profiles.iter().find(|profiles| profiles.mode == values.color_mode);
|
||||
let working_label =
|
||||
working.map_or_else(|| words.working.clone(), |profiles| Label::from(words.working_profile.fill(&[("name", profiles.working.as_str())])));
|
||||
let others: Vec<&String> = working.map_or_else(Vec::new, |profiles| profiles.others.iter().collect());
|
||||
let mut profiles = vec![Entry::new(None, working_label)];
|
||||
profiles.extend(others.iter().map(|name| Entry::new(Some((*name).clone()), (*name).clone())));
|
||||
let backgrounds = [
|
||||
let mut backgrounds = vec![
|
||||
Entry::new(BackgroundKind::White, words.white.clone()),
|
||||
Entry::new(BackgroundKind::Black, words.black.clone()),
|
||||
Entry::new(BackgroundKind::BackgroundColor, words.background_color.clone()),
|
||||
Entry::new(BackgroundKind::Transparent, words.transparent.clone()),
|
||||
Entry::new(BackgroundKind::Custom, words.custom_color.clone()),
|
||||
];
|
||||
if !values.color_mode.allows_transparent() {
|
||||
backgrounds.retain(|entry| entry.value() != Some(&BackgroundKind::Transparent));
|
||||
}
|
||||
let mut pixel_aspects: Vec<Entry<String>> =
|
||||
PIXEL_ASPECTS.iter().map(|(id, _)| Entry::new((*id).to_owned(), pixel_aspect_label(words, id).clone())).collect();
|
||||
pixel_aspects.extend(setup.custom_pixel_aspects.iter().map(|(id, name)| Entry::new(id.clone(), name.clone())));
|
||||
|
||||
Text::new(words.preset_details.clone()).role(TypeRole::Label).shade(Shade::Muted).show(ui);
|
||||
FieldGroup::new(words.preset_details.clone()).show(ui, |ui, _| {
|
||||
Field::new(words.name.clone()).id_salt("name").show(ui, |ui, _| {
|
||||
let shown = TextField::new(words.name.clone(), Bind::given(values.name.clone())).limit(MAX_NAME).select_on_focus().show(ui);
|
||||
if let Some(name) = shown.value().cloned() {
|
||||
values.name = name;
|
||||
}
|
||||
if std::mem::take(&mut session.focus_name) {
|
||||
focus::request(ui.ctx(), FocusTarget::Id(shown.response.id));
|
||||
}
|
||||
Row::new().show(ui, |ui, _| {
|
||||
let shown = TextField::new(words.name.clone(), Bind::given(values.name.clone())).limit(MAX_NAME).select_on_focus().show(ui);
|
||||
if let Some(name) = shown.value().cloned() {
|
||||
values.name = name;
|
||||
}
|
||||
if std::mem::take(&mut session.focus_name) {
|
||||
focus::request(ui.ctx(), FocusTarget::Id(shown.response.id));
|
||||
}
|
||||
let save = IconButton::new(IconId::new("save"))
|
||||
.label(words.save_preset_title.clone())
|
||||
.tooltip(words.save_preset_title.clone())
|
||||
.id_salt("save-preset")
|
||||
.show(ui);
|
||||
if clicked(&save) {
|
||||
session.naming = Some(proposed_name(read));
|
||||
session.name_tried = false;
|
||||
session.focus_preset_name = true;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
for side in [Side::Width, Side::Height] {
|
||||
@@ -157,6 +182,7 @@ pub(super) fn show(ui: &mut Ui, session: &mut Session, values: &mut DocumentOpti
|
||||
let shown = Checkbox::new(Bind::given(values.artboard)).label(words.artboards.clone()).show(ui);
|
||||
if let Some(on) = shown.value().cloned() {
|
||||
values.artboard = on;
|
||||
session.preset_artboard = false;
|
||||
}
|
||||
|
||||
Field::new(words.resolution.clone()).id_salt("resolution").show(ui, |ui, _| {
|
||||
@@ -197,6 +223,14 @@ pub(super) fn show(ui: &mut Ui, session: &mut Session, values: &mut DocumentOpti
|
||||
values.profile = profile;
|
||||
}
|
||||
});
|
||||
Field::new(words.pixel_aspect.clone()).id_salt("pixel-aspect").show(ui, |ui, _| {
|
||||
let listed = pixel_aspects.iter().any(|entry| entry.value() == Some(&values.pixel_aspect));
|
||||
let current = if listed { values.pixel_aspect.clone() } else { SQUARE_PIXELS.to_owned() };
|
||||
let shown = Select::new(words.pixel_aspect.clone(), Bind::given(current), &pixel_aspects).show(ui);
|
||||
if let Some(id) = shown.value().cloned() {
|
||||
values.pixel_aspect = id;
|
||||
}
|
||||
});
|
||||
|
||||
Field::new(words.background.clone()).id_salt("background").show(ui, |ui, _| {
|
||||
Row::new().show(ui, |ui, _| {
|
||||
@@ -220,7 +254,10 @@ pub(super) fn show(ui: &mut Ui, session: &mut Session, values: &mut DocumentOpti
|
||||
});
|
||||
});
|
||||
|
||||
save_row(ui, session, read, right);
|
||||
let fitted = values.clone().fitted();
|
||||
if fitted != *values {
|
||||
*values = fitted;
|
||||
}
|
||||
|
||||
let mut same = values.clone();
|
||||
same.name.clone_from(&before.name);
|
||||
@@ -283,15 +320,34 @@ fn refusal_message(read: &Read<'_>, refusal: &Refusal) -> Label {
|
||||
}
|
||||
}
|
||||
|
||||
/// The Save Preset button, or the name row it opens.
|
||||
fn save_row(ui: &mut Ui, session: &mut Session, read: &Read<'_>, right: &mut Right) {
|
||||
/// The first `Saved N` the saved presets leave free: the name the naming pane proposes.
|
||||
fn proposed_name(read: &Read<'_>) -> String {
|
||||
(1..)
|
||||
.map(|n: usize| read.words.saved_name.fill(&[("n", n.to_string().as_str())]))
|
||||
.find(|name| read.library.name_error(name).is_none())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The name of a built-in pixel aspect ratio; an unknown id reads as square pixels.
|
||||
fn pixel_aspect_label<'w>(words: &'w NewDocumentWords, id: &str) -> &'w Label {
|
||||
match id {
|
||||
"d1DvNtsc" => &words.d1_dv_ntsc,
|
||||
"d1DvPal" => &words.d1_dv_pal,
|
||||
"d1DvNtscWidescreen" => &words.d1_dv_ntsc_widescreen,
|
||||
"hdv1080" => &words.hdv_1080,
|
||||
"d1DvPalWidescreen" => &words.d1_dv_pal_widescreen,
|
||||
"anamorphic2To1" => &words.anamorphic_2_to_1,
|
||||
"dvcproHd1080" => &words.dvcpro_hd_1080,
|
||||
_ => &words.square_pixels,
|
||||
}
|
||||
}
|
||||
|
||||
/// The Save Document Preset pane in place of the details: its header, the name (the proposed one selected, so typing
|
||||
/// replaces it) and Save Preset and Cancel. Enter in the name saves the preset and never creates the document; Cancel
|
||||
/// goes back to the details.
|
||||
fn save_pane(ui: &mut Ui, session: &mut Session, read: &Read<'_>, right: &mut Right) {
|
||||
let words = read.words;
|
||||
let cancel = Env::of(ui).words::<CommonWords>().cancel.clone();
|
||||
if session.naming.is_none() && clicked(&Button::new(words.save_preset.clone()).id_salt("save-preset").show(ui)) {
|
||||
session.naming = Some(String::new());
|
||||
session.name_tried = false;
|
||||
session.focus_preset_name = true;
|
||||
}
|
||||
let Some(name) = session.naming.clone() else { return };
|
||||
let tried = session.name_tried;
|
||||
let library = read.library;
|
||||
@@ -299,8 +355,10 @@ fn save_row(ui: &mut Ui, session: &mut Session, read: &Read<'_>, right: &mut Rig
|
||||
Some(refused) if tried => Validity::Error(Message::new(refusal_message(read, &refused))),
|
||||
_ => Validity::Ok,
|
||||
};
|
||||
Text::new(words.save_preset_title.clone()).role(TypeRole::Label).shade(Shade::Muted).show(ui);
|
||||
Field::new(words.preset_name.clone()).id_salt("preset-name").show(ui, |ui, _| {
|
||||
let shown = TextField::new(words.preset_name.clone(), Bind::given(name.clone()).check(check)).limit(MAX_NAME).submits().show(ui);
|
||||
let shown =
|
||||
TextField::new(words.preset_name.clone(), Bind::given(name.clone()).check(check)).limit(MAX_NAME).select_on_focus().submits().show(ui);
|
||||
if let Some(typed) = shown.value().cloned() {
|
||||
session.naming = Some(typed);
|
||||
session.name_tried = false;
|
||||
@@ -314,7 +372,7 @@ fn save_row(ui: &mut Ui, session: &mut Session, read: &Read<'_>, right: &mut Rig
|
||||
});
|
||||
session.focus_preset_name = false;
|
||||
Row::new().show(ui, |ui, _| {
|
||||
if clicked(&Button::new(words.save.clone()).id_salt("save").show(ui)) {
|
||||
if clicked(&Button::new(words.save_preset.clone()).id_salt("save").show(ui)) {
|
||||
right.save = true;
|
||||
}
|
||||
if clicked(&Button::new(cancel.clone()).id_salt("cancel-save").show(ui)) {
|
||||
|
||||
@@ -33,6 +33,8 @@ pub(super) const RESOLUTION_UNITS: [UnitKind; 2] = [UnitKind::PixelsPerInch, Uni
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[non_exhaustive]
|
||||
pub enum ColorMode {
|
||||
/// One bit per pixel, black or white.
|
||||
Bitmap,
|
||||
/// One grey channel.
|
||||
Grayscale,
|
||||
/// Red, green and blue.
|
||||
@@ -48,6 +50,8 @@ pub enum ColorMode {
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[non_exhaustive]
|
||||
pub enum BitDepth {
|
||||
/// 1 bit: a Bitmap document's only depth.
|
||||
One,
|
||||
/// 8 bits per channel.
|
||||
#[default]
|
||||
Eight,
|
||||
@@ -57,6 +61,45 @@ pub enum BitDepth {
|
||||
ThirtyTwo,
|
||||
}
|
||||
|
||||
impl ColorMode {
|
||||
/// Every mode in the Color Mode list's order (Photoshop's).
|
||||
pub const ALL: [ColorMode; 5] = [ColorMode::Bitmap, ColorMode::Grayscale, ColorMode::Rgb, ColorMode::Cmyk, ColorMode::Lab];
|
||||
|
||||
/// The depths the mode takes, the one a mode switch lands on first: Bitmap is 1 bit only, every other mode 8,
|
||||
/// 16 or 32.
|
||||
pub fn depths(self) -> &'static [BitDepth] {
|
||||
match self {
|
||||
ColorMode::Bitmap => &[BitDepth::One],
|
||||
_ => &[BitDepth::Eight, BitDepth::Sixteen, BitDepth::ThirtyTwo],
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the background may be transparent: a Bitmap pixel is black or white.
|
||||
pub fn allows_transparent(self) -> bool {
|
||||
self != ColorMode::Bitmap
|
||||
}
|
||||
}
|
||||
|
||||
/// The pixel aspect ratios New Document lists before the app's own, as `(id, ratio)` in Photoshop's order. The ids
|
||||
/// are the ones PhotoCraft's View › Pixel Aspect Ratio uses; `square` is the default.
|
||||
pub const PIXEL_ASPECTS: [(&str, f64); 8] = [
|
||||
(SQUARE_PIXELS, 1.0),
|
||||
("d1DvNtsc", 0.91),
|
||||
("d1DvPal", 1.09),
|
||||
("d1DvNtscWidescreen", 1.21),
|
||||
("hdv1080", 1.33),
|
||||
("d1DvPalWidescreen", 1.46),
|
||||
("anamorphic2To1", 2.0),
|
||||
("dvcproHd1080", 1.5),
|
||||
];
|
||||
|
||||
/// The id of square pixels, the default pixel aspect ratio.
|
||||
pub const SQUARE_PIXELS: &str = "square";
|
||||
|
||||
fn square_pixels() -> String {
|
||||
SQUARE_PIXELS.to_owned()
|
||||
}
|
||||
|
||||
/// What fills the new document's background layer.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
#[non_exhaustive]
|
||||
@@ -118,6 +161,10 @@ pub struct DocumentOptions {
|
||||
pub background: BackgroundContents,
|
||||
/// Artboards: the document starts with one artboard of this size.
|
||||
pub artboard: bool,
|
||||
/// The pixel aspect ratio's id: one of [`PIXEL_ASPECTS`] or an id the app listed with
|
||||
/// `NewDocumentDialog::custom_pixel_aspects`.
|
||||
#[serde(default = "square_pixels")]
|
||||
pub pixel_aspect: String,
|
||||
}
|
||||
|
||||
impl Default for DocumentOptions {
|
||||
@@ -134,6 +181,7 @@ impl Default for DocumentOptions {
|
||||
profile: None,
|
||||
background: BackgroundContents::White,
|
||||
artboard: false,
|
||||
pixel_aspect: square_pixels(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -149,6 +197,21 @@ impl DocumentOptions {
|
||||
&& self.profile == other.profile
|
||||
}
|
||||
|
||||
/// The settings with the depth and background the colour mode takes: a depth the mode lacks becomes the mode's
|
||||
/// first (switching to Bitmap gives 1 bit, leaving it 8 bit), and a transparent background under Bitmap becomes
|
||||
/// white.
|
||||
#[must_use]
|
||||
pub fn fitted(mut self) -> Self {
|
||||
let depths = self.color_mode.depths();
|
||||
if !depths.contains(&self.depth) {
|
||||
self.depth = depths[0];
|
||||
}
|
||||
if !self.color_mode.allows_transparent() && self.background == BackgroundContents::Transparent {
|
||||
self.background = BackgroundContents::White;
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Whether the settings fit what the dialog can show: a damaged remembered value does not.
|
||||
pub fn fits(&self) -> bool {
|
||||
let side = |n: u32| (1..=MAX_DOCUMENT_SIDE as u32).contains(&n);
|
||||
@@ -200,12 +263,21 @@ pub struct DocumentPreset {
|
||||
pub height: u32,
|
||||
/// Resolution in pixels per inch.
|
||||
pub pixels_per_inch: f64,
|
||||
/// Picking the card turns Artboards on (Photoshop's Web and Mobile presets).
|
||||
pub artboard: bool,
|
||||
}
|
||||
|
||||
impl DocumentPreset {
|
||||
/// The card `id`, named `name` in English, for a page of `width` x `height` pixels at `pixels_per_inch`.
|
||||
pub const fn new(id: &'static str, name: &'static str, width: u32, height: u32, pixels_per_inch: f64) -> Self {
|
||||
Self { id, name, width, height, pixels_per_inch }
|
||||
Self { id, name, width, height, pixels_per_inch, artboard: false }
|
||||
}
|
||||
|
||||
/// The card turns Artboards on when picked.
|
||||
#[must_use]
|
||||
pub const fn artboard(mut self) -> Self {
|
||||
self.artboard = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// Whether the preset is for print (300 ppi and up): its card and fields show inches at the app's print resolution;
|
||||
@@ -309,22 +381,22 @@ impl PresetCategory {
|
||||
"web",
|
||||
"Web",
|
||||
vec![
|
||||
DocumentPreset::new("web-most-common", "Web Most Common", 1366, 768, 72.0),
|
||||
DocumentPreset::new("web-minimum", "Web Minimum", 1024, 768, 72.0),
|
||||
DocumentPreset::new("web-large", "Web Large", 1920, 1080, 72.0),
|
||||
DocumentPreset::new("macbook-pro-16", "MacBook Pro 16\"", 3456, 2234, 72.0),
|
||||
DocumentPreset::new("imac-24", "iMac 24\"", 4480, 2520, 72.0),
|
||||
DocumentPreset::new("web-most-common", "Web Most Common", 1366, 768, 72.0).artboard(),
|
||||
DocumentPreset::new("web-minimum", "Web Minimum", 1024, 768, 72.0).artboard(),
|
||||
DocumentPreset::new("web-large", "Web Large", 1920, 1080, 72.0).artboard(),
|
||||
DocumentPreset::new("macbook-pro-16", "MacBook Pro 16\"", 3456, 2234, 72.0).artboard(),
|
||||
DocumentPreset::new("imac-24", "iMac 24\"", 4480, 2520, 72.0).artboard(),
|
||||
],
|
||||
),
|
||||
PresetCategory::new(
|
||||
"mobile",
|
||||
"Mobile",
|
||||
vec![
|
||||
DocumentPreset::new("iphone-16", "iPhone 16", 1179, 2556, 72.0),
|
||||
DocumentPreset::new("iphone-16-pro-max", "iPhone 16 Pro Max", 1320, 2868, 72.0),
|
||||
DocumentPreset::new("ipad-pro-13", "iPad Pro 13\"", 2064, 2752, 72.0),
|
||||
DocumentPreset::new("android-1080p", "Android 1080p", 1080, 1920, 72.0),
|
||||
DocumentPreset::new("apple-watch-45", "Apple Watch 45mm", 396, 484, 72.0),
|
||||
DocumentPreset::new("iphone-16", "iPhone 16", 1179, 2556, 72.0).artboard(),
|
||||
DocumentPreset::new("iphone-16-pro-max", "iPhone 16 Pro Max", 1320, 2868, 72.0).artboard(),
|
||||
DocumentPreset::new("ipad-pro-13", "iPad Pro 13\"", 2064, 2752, 72.0).artboard(),
|
||||
DocumentPreset::new("android-1080p", "Android 1080p", 1080, 1920, 72.0).artboard(),
|
||||
DocumentPreset::new("apple-watch-45", "Apple Watch 45mm", 396, 484, 72.0).artboard(),
|
||||
],
|
||||
),
|
||||
PresetCategory::new(
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
//! New Document's saved presets: each row's Delete button removes its own preset.
|
||||
//! New Document: saved presets, the Save Document Preset pane, Bitmap's depth, the Web and Mobile cards' Artboards and
|
||||
//! the pixel aspect ratio.
|
||||
|
||||
use craft_ui::contract::{Bind, SaveKey};
|
||||
use craft_ui::patterns::form_dialogs::{DocumentOptions, NewDocumentDialog};
|
||||
use craft_ui::patterns::presets::{NamedPresetEvent, PresetLibrary};
|
||||
use craft_ui::patterns::form_dialogs::{BackgroundContents, BitDepth, ColorMode, DocumentOptions, NewDocumentDialog};
|
||||
use craft_ui::patterns::presets::{GroupPath, NamedPresetEvent, PresetLibrary};
|
||||
use craft_ui::runtime::memory::MemoryStage;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::Vec2;
|
||||
use egui::{Key, Modifiers, Vec2};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// Opens the Save Document Preset pane with the icon beside Name, types `name` over the proposed one and saves.
|
||||
fn save_preset(fixture: &mut Fixture, name: &str) {
|
||||
let save = fixture.by_label("Save Preset…").expect("the details offer Save Preset…");
|
||||
let save = fixture.by_label("Save Document Preset").expect("the icon beside Name");
|
||||
fixture.click(&save).idle(10);
|
||||
let field = fixture.by_label("Preset Name").expect("Save Preset… asks for a name");
|
||||
fixture.click(&field).idle(4);
|
||||
assert!(fixture.by_label("Preset Name").is_some(), "the pane asks for a name");
|
||||
fixture.type_text(name).idle(4);
|
||||
let confirm = fixture.by_label("Save").expect("the name row has Save");
|
||||
let confirm = fixture.by_label("Save Preset").expect("the pane has Save Preset");
|
||||
fixture.click(&confirm).idle(10);
|
||||
}
|
||||
|
||||
@@ -74,3 +75,102 @@ fn a_bound_library_takes_the_saved_and_deleted_presets_and_reports_each_once() {
|
||||
let kept: PresetLibrary<DocumentOptions> = MemoryStage::peek_saved(fixture.ctx(), &SaveKey::new("craft_ui.newDocument.presets"));
|
||||
assert_eq!(kept.total(), 0, "a bound library leaves the dialog's own saved list alone");
|
||||
}
|
||||
|
||||
/// The icon beside Name opens the pane with the first free `Saved N` selected; typing replaces it, and Enter saves the
|
||||
/// preset without creating the document.
|
||||
#[trial]
|
||||
fn the_save_icon_proposes_a_free_name_and_enter_saves_without_creating() {
|
||||
let library = Rc::new(RefCell::new(PresetLibrary::<DocumentOptions>::new()));
|
||||
library.borrow_mut().add(&GroupPath::root(), "Saved 1", DocumentOptions::default()).unwrap();
|
||||
let created = Rc::new(RefCell::new(false));
|
||||
let mut options = DocumentOptions::default();
|
||||
let (held, made) = (library.clone(), created.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut bound = held.borrow().clone();
|
||||
let shown = NewDocumentDialog::new(Bind::bound(&mut options)).saved(Bind::bound(&mut bound)).show(ui);
|
||||
*made.borrow_mut() |= shown.result.is_some();
|
||||
*held.borrow_mut() = bound;
|
||||
});
|
||||
fixture.room(Vec2::new(1400.0, 1000.0)).idle(30);
|
||||
let save = fixture.by_label("Save Document Preset").expect("the icon beside Name");
|
||||
fixture.click(&save).idle(10);
|
||||
assert!(shows(&fixture, "Saved 2"), "the first free name is proposed");
|
||||
fixture.type_text("Poster").idle(4);
|
||||
fixture.key(Key::Enter, Modifiers::NONE).idle(10);
|
||||
let names: Vec<String> = library.borrow().items().iter().map(|item| item.name.clone()).collect();
|
||||
assert_eq!(names, ["Saved 1", "Poster"], "Enter saved the typed name");
|
||||
assert!(!*created.borrow(), "Enter in the pane does not create the document");
|
||||
}
|
||||
|
||||
/// Bitmap takes only 1 bit and no transparent background, in the dialog and through automation; leaving it gives 8 bit.
|
||||
#[trial]
|
||||
fn bitmap_is_one_bit_without_a_transparent_background() {
|
||||
let options = |mode: ColorMode, depth: BitDepth| {
|
||||
let mut options = DocumentOptions::default();
|
||||
(options.color_mode, options.depth) = (mode, depth);
|
||||
options
|
||||
};
|
||||
let mut bitmap = options(ColorMode::Bitmap, BitDepth::Sixteen);
|
||||
bitmap.background = BackgroundContents::Transparent;
|
||||
let fitted = bitmap.fitted();
|
||||
assert_eq!((fitted.depth, fitted.background), (BitDepth::One, BackgroundContents::White));
|
||||
let back = options(ColorMode::Rgb, fitted.depth).fitted();
|
||||
assert_eq!(back.depth, BitDepth::Eight, "leaving Bitmap goes back to 8 bit");
|
||||
|
||||
let shared = Rc::new(RefCell::new(options(ColorMode::Bitmap, BitDepth::Sixteen)));
|
||||
let held = shared.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut options = held.borrow().clone();
|
||||
let _ = NewDocumentDialog::new(Bind::bound(&mut options)).show(ui);
|
||||
*held.borrow_mut() = options;
|
||||
});
|
||||
fixture.room(Vec2::new(1400.0, 1000.0)).idle(30);
|
||||
assert_eq!(shared.borrow().depth, BitDepth::One, "the dialog fits the depth to Bitmap");
|
||||
assert!(shows(&fixture, "1 bit"));
|
||||
}
|
||||
|
||||
/// A Web card turns Artboards on and a Photo card turns it off again; Artboards the user ticked stays on.
|
||||
#[trial]
|
||||
fn web_cards_turn_artboards_on_and_only_take_back_their_own() {
|
||||
let shared = Rc::new(RefCell::new(DocumentOptions::default()));
|
||||
let held = shared.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut options = held.borrow().clone();
|
||||
let _ = NewDocumentDialog::new(Bind::bound(&mut options)).show(ui);
|
||||
*held.borrow_mut() = options;
|
||||
});
|
||||
fixture.room(Vec2::new(1400.0, 1000.0)).idle(30);
|
||||
let pick = |fixture: &mut Fixture, tab: &str, card: &str| {
|
||||
let target = fixture.by_label(tab).unwrap_or_else(|| panic!("the {tab} tab"));
|
||||
fixture.click(&target).idle(10);
|
||||
// A card's node is named by its title and size line together; its title is clicked where it is painted.
|
||||
let title = fixture.texts().iter().find(|painted| painted.text == card).map(|painted| painted.rect.center());
|
||||
fixture.click_at(title.unwrap_or_else(|| panic!("the {card} card in {tab}"))).idle(10);
|
||||
};
|
||||
pick(&mut fixture, "Web", "Web Most Common");
|
||||
assert!(shared.borrow().artboard, "a Web card turns Artboards on");
|
||||
pick(&mut fixture, "Photo", "Landscape, 6 x 4");
|
||||
assert!(!shared.borrow().artboard, "a Photo card takes it back");
|
||||
let artboards = fixture.by_label("Artboards").expect("the Artboards box");
|
||||
fixture.click(&artboards).idle(10);
|
||||
pick(&mut fixture, "Photo", "Landscape, 7 x 5");
|
||||
assert!(shared.borrow().artboard, "the user's tick stays");
|
||||
}
|
||||
|
||||
/// The Pixel Aspect Ratio row shows an id it no longer lists as Square Pixels, and the app's custom entries follow the
|
||||
/// built-in ones.
|
||||
#[trial]
|
||||
fn pixel_aspect_lists_the_custom_entries_and_shows_an_unknown_id_as_square() {
|
||||
let mut options = DocumentOptions::default();
|
||||
options.pixel_aspect = "custom:1.5:Gone".to_owned();
|
||||
assert_eq!(DocumentOptions::default().pixel_aspect, "square");
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let _ =
|
||||
NewDocumentDialog::new(Bind::bound(&mut options)).custom_pixel_aspects(vec![("custom:1.2:Mine".to_owned(), "Mine".to_owned())]).show(ui);
|
||||
});
|
||||
fixture.room(Vec2::new(1400.0, 1000.0)).idle(30);
|
||||
assert!(shows(&fixture, "Square Pixels"), "an unlisted id shows as Square Pixels");
|
||||
let row = fixture.by_label("Pixel Aspect Ratio").expect("the row under Color Profile");
|
||||
fixture.click(&row).idle(10);
|
||||
assert!(shows(&fixture, "DVCPRO HD 1080 (1.5)") && shows(&fixture, "Mine"), "the built-in ratios, then the app's");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user