This commit is contained in:
aiongg
2026-11-14 02:25:47 +00:00
parent 73679e06ce
commit 7c586f3752
7 changed files with 143 additions and 18 deletions
+3
View File
@@ -215,6 +215,9 @@ pub enum SwatchNote {
Truncated(usize),
/// Names were cut to what the format holds (`.ase`: 1024 UTF-16 units).
NamesCut(usize),
/// An `.act` read whose trailer names a transparent index: the 1-based colour number, so a read reports what it
/// could not keep.
TransparentKept(usize),
}
/// Why a swatch file could not be read or written.
+2 -2
View File
@@ -79,8 +79,8 @@ pub use samplers::{
pub use screen_picker::{CaptureResult, CaptureSource, LOUPE_PIXELS, LOUPE_ZOOM, ScreenColorPicker, ScreenPick, ScreenPickerWords, StillCapture};
pub use strip::{ColorStrip, ColorStripStyle};
pub use swatches::{
MAX_GROUPS, MAX_SWATCHES, SwatchAt, SwatchKey, SwatchLibrary, SwatchesCommand, SwatchesFileOutput, SwatchesMenu, SwatchesMenuOutput,
SwatchesPanel, SwatchesPanelOutput, SwatchesState, SwatchesWords,
MAX_GROUPS, MAX_SWATCHES, SwatchAt, SwatchEdit, SwatchKey, SwatchLibrary, SwatchesCommand, SwatchesFileOutput, SwatchesMenu, SwatchesMenuOutput,
SwatchesPanel, SwatchesPanelOutput, SwatchesState, SwatchesWords, swatch_rows,
};
pub use wheel::{
ColorWheel, ColorWheelStyle, ColorWheelWords, WheelGeometry, WheelPart, hue_at, hue_direction, triangle_corners, triangle_point, triangle_sv,
+1 -1
View File
@@ -7,7 +7,7 @@ mod panel;
#[cfg(feature = "screen-testing")]
mod stories;
pub use library::{SwatchAt, SwatchKey, SwatchLibrary, SwatchesState};
pub use library::{SwatchAt, SwatchEdit, SwatchKey, SwatchLibrary, SwatchesState, swatch_rows};
pub use menu::{SwatchesCommand, SwatchesFileOutput, SwatchesMenu, SwatchesMenuOutput};
pub use panel::{SwatchesPanel, SwatchesPanelOutput};
+86 -1
View File
@@ -3,7 +3,7 @@
use std::collections::HashSet;
use craft_ui::contract::ReorderMove;
use craft_ui::patterns::presets::PresetState;
use craft_ui::patterns::presets::{PresetRow, PresetState};
use super::{MAX_GROUPS, MAX_SWATCHES, SwatchesWords};
use crate::palette::{SwatchColor, SwatchEntry, SwatchGroup, SwatchReport};
@@ -340,3 +340,88 @@ pub(super) fn value_text(color: &SwatchColor, book: &str) -> String {
SwatchColor::Book { .. } => book.to_owned(),
}
}
/// One resolved edit of the swatch library for the host to run.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum SwatchEdit {
/// Adds `color` named `name` to `group`, or the first group.
Add {
/// The group's name, `None` for the first group.
group: Option<String>,
/// The new swatch's name.
name: String,
/// The new swatch's colour.
color: SwatchColor,
},
/// Adds an empty group.
NewGroup {
/// The new group's name.
name: String,
},
/// Renames a swatch.
Rename {
/// The swatch's current name.
swatch: String,
/// Its new name.
name: String,
},
/// Renames a group.
RenameGroup {
/// The group's current name.
group: String,
/// Its new name.
name: String,
},
/// Deletes a swatch.
Delete {
/// The swatch's name.
swatch: String,
},
/// Deletes a group with its swatches.
DeleteGroup {
/// The group's name.
group: String,
},
/// Moves a swatch.
MoveTo {
/// The swatch's name.
swatch: String,
/// The group it goes to.
group: String,
/// Its index in `group` once it is taken out: the group's length for a drop on its header.
at: usize,
},
/// Moves a group.
MoveGroup {
/// The group's name.
group: String,
/// The group's new index.
to: usize,
},
/// Puts the default swatches back.
Reset,
/// Imports `groups`, replacing the library when `replace`.
Import {
/// The groups the file holds.
groups: Vec<SwatchGroup>,
/// Whether they replace the library.
replace: bool,
},
}
impl SwatchLibrary {
/// Runs `edit` on the library: the in-memory host's path (stories, tests). False when the edit names nothing in the
/// library.
pub fn apply(&mut self, edit: &SwatchEdit) -> bool {
let _ = edit;
todo!("pn.C26")
}
}
/// The rows of the swatch list: each group at depth 0 with its count, each swatch at depth 1 with "name, value" as its
/// tooltip and accessible name. `SwatchesPanel` and `PaintChip`'s swatches list build their rows with it.
pub fn swatch_rows(groups: &[SwatchGroup], words: &SwatchesWords) -> Vec<PresetRow<SwatchKey>> {
let _ = (groups, words);
todo!("tl.T38")
}
+25 -6
View File
@@ -10,8 +10,8 @@ use craft_ui::menu::PanelRow;
use craft_ui::patterns::presets::{PresetCommands, PresetEvent, PresetExport, PresetFileRequest, PresetFiles, PresetFooterEvent};
use craft_ui::runtime::commands::{CommandRef, CommandState};
use super::{SwatchAt, SwatchKey, SwatchLibrary, SwatchesState, SwatchesWords};
use crate::palette::{SwatchColor, SwatchFormat, SwatchReport};
use super::{SwatchAt, SwatchEdit, SwatchKey, SwatchLibrary, SwatchesState, SwatchesWords};
use crate::palette::{SwatchColor, SwatchFormat, SwatchGroup, SwatchReport};
/// The purpose of the swatch file pickers: their folder is remembered under it.
const PICK_PURPOSE: &str = "swatches";
@@ -23,6 +23,8 @@ const PICK_PURPOSE: &str = "swatches";
pub struct SwatchesFileOutput {
/// The library changed: the app saves it.
pub changed: bool,
/// The edit the app runs: a read of a file imports its groups.
pub edit: Option<SwatchEdit>,
/// The notes of the swatch file that was read or written, once the panel has handled it.
pub report: Option<SwatchReport>,
/// The status message for the notes that lost something or for a failure.
@@ -77,6 +79,9 @@ impl SwatchesCommand {
pub struct SwatchesMenuOutput {
/// The library changed: the app saves it.
pub changed: bool,
/// The edit a command makes, for the host to run (New Swatch: `Add` with the foreground and the next numbered name in
/// the selected swatch's group).
pub edit: Option<SwatchEdit>,
/// The file dialog the command asks the host for; the host answers with [`SwatchesPanel::apply_file`].
pub file: Option<PresetFileRequest>,
/// The status message for a command that failed before it asked for a file.
@@ -105,9 +110,9 @@ impl SwatchesMenu {
]
}
/// The state of each command: both exports are disabled while the library has no swatch.
pub fn states(library: &SwatchLibrary) -> Vec<(SwatchesCommand, CommandState)> {
let exports = library.total() > 0;
/// The state of each command: both exports are disabled while the groups hold no swatch.
pub fn states(groups: &[SwatchGroup]) -> Vec<(SwatchesCommand, CommandState)> {
let exports = groups.iter().any(|group| !group.entries.is_empty());
SwatchesCommand::ALL
.into_iter()
.map(|command| {
@@ -121,13 +126,27 @@ impl SwatchesMenu {
/// footer's New does, and the panel selects it and opens its name for editing in its next pass; New Swatch Group
/// and Reset Swatches act on `library`. Import, Replace and the two exports give the file request for the host.
pub fn run_command(
groups: &[SwatchGroup],
state: &mut SwatchesState,
command: SwatchesCommand,
foreground: Srgba,
words: &SwatchesWords,
) -> SwatchesMenuOutput {
let _ = (groups, state, command, foreground, words);
todo!("pn.C28")
}
/// [`run_command`](Self::run_command) over a library it writes.
// P3(panels-w6): pn.C28 — the old `run_command(&mut SwatchLibrary, ..)`; phase 3 removes it with its readers.
#[allow(dead_code)]
pub(crate) fn run_command_on_library(
library: &mut SwatchLibrary,
state: &mut SwatchesState,
command: SwatchesCommand,
foreground: Srgba,
words: &SwatchesWords,
) -> SwatchesMenuOutput {
let mut output = SwatchesMenuOutput { changed: false, file: None, status: None };
let mut output = SwatchesMenuOutput { changed: false, edit: None, file: None, status: None };
let readable: Vec<&str> = SwatchFormat::ALL.iter().filter(|f| f.can_read()).map(|f| f.extension()).collect();
let import = || PresetFiles::<SwatchKey>::new(PICK_PURPOSE, words.file.as_str(), &readable).replace(true);
match command {
+24 -6
View File
@@ -17,8 +17,8 @@ use egui::{Rect, Response, Ui};
use super::library::value_text;
use super::menu::Actions;
use super::{SwatchAt, SwatchKey, SwatchLibrary, SwatchesFileOutput, SwatchesState, SwatchesWords};
use crate::palette::{SwatchColor, SwatchNote, SwatchReport};
use super::{SwatchAt, SwatchEdit, SwatchKey, SwatchLibrary, SwatchesFileOutput, SwatchesState, SwatchesWords};
use crate::palette::{SwatchColor, SwatchGroup, SwatchNote, SwatchReport};
/// What [`SwatchesPanel::show`] reports.
#[derive(Debug)]
@@ -32,6 +32,8 @@ pub struct SwatchesPanelOutput {
pub used: Option<(SwatchAt, Srgba, bool)>,
/// The library changed (new, rename, delete, move, group edits): the app saves it.
pub changed: bool,
/// The edits the user made this pass, in pass order, for the host to run; the panel never writes the library.
pub edits: Vec<SwatchEdit>,
}
impl Outcome for SwatchesPanelOutput {
@@ -74,8 +76,9 @@ struct Settled {
impl<'a> SwatchesPanel<'a> {
/// The panel over `library` and the host-owned `state`; `foreground` is what New Swatch adds.
pub fn new(library: Bind<'a, SwatchLibrary>, state: Bind<'a, SwatchesState>, foreground: Srgba) -> Self {
SwatchesPanel { library, state, cmyk: None, foreground }
pub fn new(groups: &'a [SwatchGroup], state: Bind<'a, SwatchesState>, foreground: Srgba) -> Self {
// P3(panels-w6): pn.C27 — the groups are copied into a library the body still edits.
SwatchesPanel { library: Bind::given(SwatchLibrary::new(groups.to_vec())), state, cmyk: None, foreground }
}
/// The document's CMYK converter, which draws CMYK swatches and gives their colours when used; without it they
@@ -162,13 +165,27 @@ impl<'a> SwatchesPanel<'a> {
})
.flatten()
.unwrap_or_default();
SwatchesPanelOutput { response: shown.output, used, changed }
SwatchesPanelOutput { response: shown.output, used, changed, edits: Vec::new() }
}
/// Carries out the host's answer to a file request of the panel menu: Import and Replace read the file into
/// `library`, a finished export reports the notes it kept, and a failure says why. Returns what changed and the
/// status message the host shows, if any.
pub fn apply_file(
state: &mut SwatchesState,
event: &PresetFileEvent,
words: &SwatchesWords,
presets: &PresetWords,
format: &Format,
) -> SwatchesFileOutput {
let _ = (state, event, words, presets, format);
todo!("pn.C27")
}
/// [`apply_file`](Self::apply_file) over a library it writes.
// P3(panels-w6): pn.C27 — the old `apply_file(&mut SwatchLibrary, ..)`; phase 3 removes it with its readers.
#[allow(dead_code)]
pub(crate) fn apply_file_to_library(
library: &mut SwatchLibrary,
state: &mut SwatchesState,
event: &PresetFileEvent,
@@ -176,7 +193,7 @@ impl<'a> SwatchesPanel<'a> {
presets: &PresetWords,
format: &Format,
) -> SwatchesFileOutput {
let mut output = SwatchesFileOutput { changed: false, report: None, status: None };
let mut output = SwatchesFileOutput { changed: false, edit: None, report: None, status: None };
match event {
PresetFileEvent::Loaded { file_name, bytes, replace, .. } => match crate::palette::read_swatches(bytes, file_name) {
Ok((groups, report)) => {
@@ -220,6 +237,7 @@ impl<'a> SwatchesPanel<'a> {
SwatchNote::KindsDropped(n) => count(&words.kinds_dropped, n),
SwatchNote::Truncated(n) => count(&words.truncated, n),
SwatchNote::NamesCut(n) => count(&words.names_cut, n),
SwatchNote::TransparentKept(_) => todo!("pn.C59"), // P3(panels-w31): pn.C59
})
.collect()
}
+2 -2
View File
@@ -8,11 +8,11 @@ use egui::Ui;
#[story(SwatchesPanel<'static>, covers = [], drive = Drive::Never)]
fn swatches_panel(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let (library, state) = data.value("panel", (SwatchLibrary::default(), SwatchesState::default()));
SwatchesPanel::new(Bind::bound(library), Bind::bound(state), craft_theme::Srgba::rgb(0.2, 0.4, 0.6)).show(ui).into()
SwatchesPanel::new(&library.groups, Bind::bound(state), craft_theme::Srgba::rgb(0.2, 0.4, 0.6)).show(ui).into()
}
#[story(SwatchesPanel<'static>, covers = [], drive = Drive::Never)]
fn swatches_panel_empty(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let (library, state) = data.value("empty", (SwatchLibrary { groups: Vec::new() }, SwatchesState::default()));
SwatchesPanel::new(Bind::bound(library), Bind::bound(state), craft_theme::Srgba::rgb(0.2, 0.4, 0.6)).show(ui).into()
SwatchesPanel::new(&library.groups, Bind::bound(state), craft_theme::Srgba::rgb(0.2, 0.4, 0.6)).show(ui).into()
}