GitCorner

Code

ossartcraftcadcraft🌐
a770fa5a5e5f
//! The OS cursor over the drawing area (#39): hidden under the drawn crosshair by default, kept
//! visible with `systemCursor` so screen magnifiers and other assistive tech can follow it.

use cadcraft_engine::Session;
use cadcraft_ui_egui::{CadApp, Services};

/// The cursor icon the app asks for with the pointer in the middle of the drawing area.
fn canvas_cursor(app: &mut CadApp) -> egui::CursorIcon {
    let ctx = egui::Context::default();
    let mut icon = egui::CursorIcon::Default;
    // A few frames: layout settles and the pointer hover is known from the second frame on.
    for _ in 0..3 {
        let raw = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1280.0, 800.0))),
            events: vec![egui::Event::PointerMoved(egui::pos2(640.0, 400.0))],
            ..Default::default()
        };
        let mut out = ctx.run_ui(raw, |ui| {
            app.logic(ui.ctx());
            app.ui(ui);
        });
        icon = out.platform_output.cursor_icon;
        out.textures_delta.clear(); // no renderer here
    }
    icon
}

#[test]
fn system_cursor_stays_visible_over_the_canvas_when_requested() {
    let mut app = CadApp::new(Session::new(), Services::default());
    app.ui.system_cursor = false;
    assert_eq!(canvas_cursor(&mut app), egui::CursorIcon::None, "default: the drawn crosshair replaces the OS cursor");

    app.run("ui.toggle.systemcursor", serde_json::json!({ "on": true })).ok();
    assert!(app.ui.system_cursor);
    assert_eq!(canvas_cursor(&mut app), egui::CursorIcon::Crosshair, "systemCursor keeps an OS cursor for magnifiers");
}

/// The host's default (the desktop app's `CADCRAFT_SYSTEM_CURSOR`) turns it on; a saved `false`
/// and the resets keep it.
#[test]
fn the_host_default_survives_saved_profiles_and_resets() {
    let mut app = CadApp::new(Session::new(), Services::default());
    app.run("options", serde_json::json!({ "reset": true })).unwrap();
    assert!(!app.ui.system_cursor, "off without a host default");

    app.set_system_cursor_default(true);
    assert!(app.ui.system_cursor);
    app.load_profile(r#"{"systemCursor": false}"#);
    assert!(app.ui.system_cursor);
    app.ui.system_cursor = false;
    app.run("options", serde_json::json!({ "reset": true })).unwrap();
    assert!(app.ui.system_cursor);
    app.ui.system_cursor = false;
    app.run("ui.resetpalettes", serde_json::json!({})).unwrap();
    assert!(app.ui.system_cursor);
}