mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Magnetic Lasso: Caps Lock swaps in the Width circle cursor (#11690)
This commit is contained in:
1 parent
90cdaa615d
commit
f0e753ee8d
4 files changed
+70
-5
No files matched your search
@@ -62,8 +62,10 @@ pub mod zoom_window_fit;
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) use cursors::eyedropper_ring_colors;
|
||||
pub(crate) use cursors::{alt_picks, alt_samples, composite_color, paint_bucket_cursor, pen_cursor, pencil_cursor_rect, pencil_tip_is_round, pipette_cursor};
|
||||
use cursors::{brush_tip_centre, eyedropper_ring, precise_other_cursor, visible_crosshair};
|
||||
pub(crate) use cursors::{
|
||||
alt_picks, alt_samples, composite_color, paint_bucket_cursor, pen_cursor, pencil_cursor_rect, pencil_tip_is_round, pipette_cursor, precise_other_cursor,
|
||||
};
|
||||
use cursors::{brush_tip_centre, eyedropper_ring, visible_crosshair};
|
||||
pub(crate) use display::GPU_OUTPUT;
|
||||
use display::texture_key;
|
||||
pub(crate) use display::{cache_key, canvas_display, display_doc, display_image};
|
||||
|
||||
@@ -120,7 +120,7 @@ pub(crate) fn alt_samples(tool: Tool, mods: egui::Modifiers) -> bool {
|
||||
|
||||
/// Preferences › Cursors › Other Cursors asks for the precise crosshair, or Caps Lock swaps the
|
||||
/// standard cursor for it (and back), as in Photoshop (#4639).
|
||||
pub(super) fn precise_other_cursor(app: &PhotocraftApp) -> bool {
|
||||
pub(crate) fn precise_other_cursor(app: &PhotocraftApp) -> bool {
|
||||
(app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise) != app.caps_lock
|
||||
}
|
||||
|
||||
|
||||
@@ -451,9 +451,10 @@ pub fn draw(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, hover:
|
||||
}
|
||||
|
||||
/// The cursor over the canvas: a crosshair; Preferences › Cursors › Other Cursors › Precise also
|
||||
/// shows the detection width around it.
|
||||
/// shows the detection width around it, and Caps Lock swaps the preference either way, as in
|
||||
/// Photoshop (#11690).
|
||||
pub fn cursor(app: &PhotocraftApp, painter: &egui::Painter, p: Pos2, zoom: f32) -> egui::CursorIcon {
|
||||
if app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise {
|
||||
if crate::canvas::precise_other_cursor(app) {
|
||||
let r = (app.ui.tool_options.magnetic_width.clamp(1.0, 256.0) * zoom).max(2.0);
|
||||
painter.circle_stroke(p, r + 0.5, Stroke::new(1.0, Color32::from_black_alpha(140)));
|
||||
painter.circle_stroke(p, r, Stroke::new(1.0, Color32::from_white_alpha(220)));
|
||||
@@ -463,3 +464,6 @@ pub fn cursor(app: &PhotocraftApp, painter: &egui::Painter, p: Pos2, zoom: f32)
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod caps_lock_cursor_tests;
|
||||
@@ -0,0 +1,59 @@
|
||||
//! Caps Lock and the Magnetic Lasso's cursor (#11690): it swaps Preferences › Cursors › Other
|
||||
//! Cursors, so with Standard and Caps Lock on the pointer shows a circle as big as the detection
|
||||
//! Width (scaled with zoom), and with Precise and Caps Lock on it doesn't.
|
||||
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::state::Tool;
|
||||
|
||||
fn app(other: &str, caps_lock: bool) -> PhotocraftApp {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 64, "height": 64})).unwrap();
|
||||
app.ui.tool = Tool::MagneticLasso;
|
||||
app.ui.tool_options.magnetic_width = 10.0;
|
||||
app.run("prefs.set", json!({"path": "cursors.other", "value": other})).unwrap();
|
||||
app.caps_lock = caps_lock;
|
||||
app
|
||||
}
|
||||
|
||||
/// The radii of the circles `cursor` draws at `zoom`, and the OS cursor it asks for.
|
||||
fn drawn(app: &PhotocraftApp, zoom: f32) -> (Vec<f32>, egui::CursorIcon) {
|
||||
let ctx = egui::Context::default();
|
||||
ctx.begin_pass(egui::RawInput::default());
|
||||
let painter = ctx.layer_painter(egui::LayerId::background());
|
||||
let icon = super::cursor(app, &painter, egui::pos2(100.0, 100.0), zoom);
|
||||
let mut out = ctx.end_pass();
|
||||
out.textures_delta.clear();
|
||||
let radii = out
|
||||
.shapes
|
||||
.iter()
|
||||
.filter_map(|c| match &c.shape {
|
||||
egui::Shape::Circle(c) => Some(c.radius),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
(radii, icon)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn caps_lock_on_standard_draws_the_width_circle_scaled_with_zoom() {
|
||||
let app = app("standard", true);
|
||||
let (radii, icon) = drawn(&app, 1.0);
|
||||
assert!(radii.iter().any(|r| (r - 10.0).abs() < 1e-3), "a 10 px circle at Width 10, 100 %: {radii:?}");
|
||||
assert_eq!(icon, egui::CursorIcon::Crosshair, "with a crosshair in the middle");
|
||||
let (radii, _) = drawn(&app, 2.0);
|
||||
assert!(radii.iter().any(|r| (r - 20.0).abs() < 1e-3), "twice as big at 200 %: {radii:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn caps_lock_on_precise_swaps_back_to_no_circle() {
|
||||
let (radii, _) = drawn(&app("precise", true), 1.0);
|
||||
assert!(radii.is_empty(), "no width circle: {radii:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn without_caps_lock_the_preference_decides() {
|
||||
assert!(drawn(&app("standard", false), 1.0).0.is_empty(), "Standard: no circle");
|
||||
assert!(!drawn(&app("precise", false), 1.0).0.is_empty(), "Precise: the width circle");
|
||||
}
|
||||
Reference in new issue
Block a user