mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Brush Alt+digit Smoothing (#12034): keep key order, end a two-digit Opacity, fix fmt
The plain and Alt digit presses of one frame now run in the order they were typed, so the Actions recorder sees them in that order, and an Alt digit ends a pending two-digit Opacity value (4, Alt+3, 5 sets 50%, not 45%). The two-digit test now sends its digits in one frame, so it no longer depends on the runner's timing. New tests: the key order, Alt with Shift/Ctrl/Cmd, and Smoothing turned on from off. The lib.rs mod line is in rustfmt order. Co-Authored-By: DuBearte <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
8e13a9b4e4
commit
28e04e0d94
3 files changed
+55
-16
No files matched your search
@@ -27,6 +27,14 @@ fn key(app: &mut PhotocraftApp, ctx: &egui::Context, key: Key, modifiers: Modifi
|
||||
ctx.end_pass().textures_delta.clear();
|
||||
}
|
||||
|
||||
/// Several key presses in one frame, in this order.
|
||||
fn keys(app: &mut PhotocraftApp, ctx: &egui::Context, presses: &[(Key, Modifiers)]) {
|
||||
let events = presses.iter().map(|&(key, modifiers)| egui::Event::Key { key, physical_key: Some(key), pressed: true, repeat: false, modifiers }).collect();
|
||||
ctx.begin_pass(egui::RawInput { events, ..Default::default() });
|
||||
crate::shortcuts::handle(app, ctx);
|
||||
ctx.end_pass().textures_delta.clear();
|
||||
}
|
||||
|
||||
fn brush(app: &PhotocraftApp) -> (f32, f32, f32) {
|
||||
let b = &app.session.tools.brush;
|
||||
(b.smoothing.amount, b.opacity, b.flow)
|
||||
@@ -39,6 +47,7 @@ fn alt_digits_set_the_smoothing() {
|
||||
let mut a = app(tool);
|
||||
let (_, opacity, flow) = brush(&a);
|
||||
for (k, want) in [(Key::Num3, 0.3), (Key::Num1, 0.1), (Key::Num9, 0.9), (Key::Num0, 1.0)] {
|
||||
a.session.tools.brush.smoothing.enabled = false;
|
||||
key(&mut a, &ctx, k, Modifiers::ALT);
|
||||
let (smoothing, o, f) = brush(&a);
|
||||
assert!((smoothing - want).abs() < 1e-6, "{tool:?} {k:?}: {smoothing}");
|
||||
@@ -74,9 +83,8 @@ fn plain_and_shift_digits_still_set_opacity_and_flow() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut a = app(Tool::Brush);
|
||||
let smoothing = brush(&a).0;
|
||||
key(&mut a, &ctx, Key::Num4, Modifiers::NONE);
|
||||
key(&mut a, &ctx, Key::Num5, Modifiers::NONE);
|
||||
key(&mut a, &ctx, Key::Num3, Modifiers::SHIFT);
|
||||
// One frame, so the two digits are "typed quickly" however slow the test runner is.
|
||||
keys(&mut a, &ctx, &[(Key::Num4, Modifiers::NONE), (Key::Num5, Modifiers::NONE), (Key::Num3, Modifiers::SHIFT)]);
|
||||
let (s, o, f) = brush(&a);
|
||||
assert!((o - 0.45).abs() < 1e-6 && (f - 0.3).abs() < 1e-6, "{o} {f}");
|
||||
assert_eq!(s, smoothing);
|
||||
@@ -98,3 +106,25 @@ fn tools_without_a_smoothing_field_ignore_alt_digits() {
|
||||
assert_eq!(layer(&a), opacity, "{tool:?}: the layer's Opacity stays");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_alt_digit_ends_a_two_digit_opacity() {
|
||||
// 4, ⌥3, 5 in one frame: the presses run in order and ⌥3 ends the "45", so 5 sets 50%.
|
||||
let ctx = egui::Context::default();
|
||||
let mut a = app(Tool::Brush);
|
||||
keys(&mut a, &ctx, &[(Key::Num4, Modifiers::NONE), (Key::Num3, Modifiers::ALT), (Key::Num5, Modifiers::NONE)]);
|
||||
let (s, o, _) = brush(&a);
|
||||
assert!((s - 0.3).abs() < 1e-6, "{s}");
|
||||
assert!((o - 0.5).abs() < 1e-6, "{o}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alt_with_other_modifiers_does_not_set_the_smoothing() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut a = app(Tool::Brush);
|
||||
let smoothing = brush(&a).0;
|
||||
for m in [Modifiers::ALT | Modifiers::SHIFT, Modifiers::ALT | Modifiers::CTRL, Modifiers::ALT | Modifiers::COMMAND] {
|
||||
key(&mut a, &ctx, Key::Num3, m);
|
||||
assert_eq!(brush(&a).0, smoothing, "{m:?}");
|
||||
}
|
||||
}
|
||||
@@ -68,12 +68,17 @@ fn has_smoothing(tool: Tool) -> bool {
|
||||
matches!(tool, Tool::Brush | Tool::Pencil | Tool::Eraser)
|
||||
}
|
||||
|
||||
/// One digit press of this frame: a plain or ⇧ digit, or an ⌥ digit for Smoothing.
|
||||
enum Press {
|
||||
Digit(u8, bool),
|
||||
Smoothing(u8),
|
||||
}
|
||||
|
||||
/// Consumes this frame's unmodified (or ⇧) digit presses and applies them, and ⌥+digit presses
|
||||
/// for the tools with a Smoothing field.
|
||||
/// for the tools with a Smoothing field, in the order they were typed.
|
||||
pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
let smoothing = has_smoothing(app.ui.tool);
|
||||
let mut alt_presses: Vec<u8> = Vec::new();
|
||||
let presses: Vec<(u8, bool)> = ctx.input_mut(|i| {
|
||||
let presses: Vec<Press> = ctx.input_mut(|i| {
|
||||
let mut out = Vec::new();
|
||||
i.events.retain(|e| match e {
|
||||
// ⌥+digit: one tens value each (1 = 10% … 0 = 100%), never two digits.
|
||||
@@ -83,7 +88,7 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
match physical_key.and_then(digit).or_else(|| digit(*key)) {
|
||||
Some(d) => {
|
||||
if !repeat {
|
||||
alt_presses.push(d);
|
||||
out.push(Press::Smoothing(d));
|
||||
}
|
||||
false
|
||||
}
|
||||
@@ -97,7 +102,7 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
// A held key's auto-repeat is still one digit, not a second one typed
|
||||
// quickly (holding 4 set 44%, #953); it is consumed all the same.
|
||||
if !repeat {
|
||||
out.push((d, modifiers.shift));
|
||||
out.push(Press::Digit(d, modifiers.shift));
|
||||
}
|
||||
false
|
||||
}
|
||||
@@ -108,13 +113,17 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
});
|
||||
out
|
||||
});
|
||||
for d in alt_presses {
|
||||
let amount = if d == 0 { 1.0 } else { f64::from(d) / 10.0 };
|
||||
let _ = app.run("tools.setBrush", json!({ "brush": { "smoothing": { "enabled": true, "amount": amount } } }));
|
||||
}
|
||||
let now = crate::gpu_canvas::now_ms();
|
||||
for (d, shift) in presses {
|
||||
press(app, d, shift, now);
|
||||
for p in presses {
|
||||
match p {
|
||||
Press::Digit(d, shift) => press(app, d, shift, now),
|
||||
Press::Smoothing(d) => {
|
||||
// An ⌥ digit ends a two-digit Opacity value: 4, ⌥3, 5 is 50%, not 45%.
|
||||
app.opacity_keys = None;
|
||||
let amount = if d == 0 { 1.0 } else { f64::from(d) / 10.0 };
|
||||
let _ = app.run("tools.setBrush", json!({ "brush": { "smoothing": { "enabled": true, "amount": amount } } }));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1050,13 +1050,13 @@ mod menu_item_labels_tests;
|
||||
#[cfg(test)]
|
||||
mod pen_path_ownership_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod brush_alt_digit_smoothing_tests;
|
||||
#[cfg(test)]
|
||||
mod artboard_options_bar_tests;
|
||||
#[cfg(test)]
|
||||
mod background_eraser_bar_angle_tests;
|
||||
#[cfg(test)]
|
||||
mod brush_alt_digit_smoothing_tests;
|
||||
#[cfg(test)]
|
||||
mod brush_opacity_minimum_tests;
|
||||
#[cfg(test)]
|
||||
mod burn_default_tip_tests;
|
||||
|
||||
Reference in new issue
Block a user