diff --git a/crates/lightgui-designer/src/canvas.rs b/crates/lightgui-designer/src/canvas.rs index ffb269c..69dcbd2 100644 --- a/crates/lightgui-designer/src/canvas.rs +++ b/crates/lightgui-designer/src/canvas.rs @@ -20,6 +20,7 @@ //! threshold — a double-click jiggles, so sub-8px motion must not move //! the widget out from under the second press. +use crate::codeview::refresh_code_view; use crate::inspector::refresh_props; use crate::state::{App, Placed, Shared}; use gtk::prelude::*; @@ -798,6 +799,17 @@ pub fn canvas_button_press(app: &Shared, x: i32, y: i32, button: u32) { }; if let Some((name, px, py, w, h)) = place { place_widget(app, &name, px, py, w, h); + // Single-placement: placing one widget disarms placement mode, + // so the next click does not place another fresh widget. + // Select a widget in the palette again to place another. + { + let mut a = app.borrow_mut(); + a.backend.sel_type = -1; + a.backend.sel_name.clear(); + } + // Lazarus-style sync: the code view shows the placed widget + // at once, without pressing Generate. + refresh_code_view(app); } redraw_canvas(app); refresh_props(app); @@ -908,20 +920,29 @@ pub fn canvas_key(app: &Shared, key: gdk::keys::Key) -> bool { return false; } if key == gdk::keys::constants::Escape { - let had = { + let (had, cancelled_placement) = { let mut a = app.borrow_mut(); - let had = a.backend.handles_on || a.selected.is_some(); + let cancelled_placement = a.backend.sel_type >= 0; + let had = a.backend.handles_on || a.selected.is_some() + || cancelled_placement; a.backend.handles_on = false; a.selected = None; + // Escape also cancels an armed placement (same as clicking + // the selected palette entry again). + a.backend.sel_type = -1; + a.backend.sel_name.clear(); if had { write_state(&a); } - had + (had, cancelled_placement) }; if had { redraw_canvas(app); refresh_props(app); } + if cancelled_placement { + app.borrow().status("Placement cancelled"); + } return had; } false diff --git a/crates/lightgui-designer/src/main.rs b/crates/lightgui-designer/src/main.rs index 86f7cc0..3eb284a 100644 --- a/crates/lightgui-designer/src/main.rs +++ b/crates/lightgui-designer/src/main.rs @@ -221,6 +221,29 @@ fn main() { // Clicking one arms it for placement; the canvas click places it. palette_build_xframe_sections(&app, &palette_box); + // Escape in the palette window cancels an armed placement (the + // palette is its own top-level window, so the canvas key handler + // never sees the key when the palette has focus). + { + let app2 = app.clone(); + palette_window.connect_key_press_event(move |_, ev| { + if ev.keyval() == gdk::keys::constants::Escape { + let was_armed = { + let mut a = app2.borrow_mut(); + let was = a.backend.sel_type >= 0; + a.backend.sel_type = -1; + a.backend.sel_name.clear(); + was + }; + if was_armed { + app2.borrow().status("Placement cancelled"); + return glib::Propagation::Stop; + } + } + glib::Propagation::Proceed + }); + } + // The XFrame canvas: a GtkDrawingArea (double-buffered FALSE) whose // GDK-managed X window the canvas core draws on with x11rb. GTK // signals drive the core (see canvas.rs). diff --git a/crates/lightgui-designer/src/palette_ui.rs b/crates/lightgui-designer/src/palette_ui.rs index 65e1a14..5ca638e 100644 --- a/crates/lightgui-designer/src/palette_ui.rs +++ b/crates/lightgui-designer/src/palette_ui.rs @@ -95,16 +95,31 @@ pub fn palette_build_xframe_sections(app: &Shared, grid: >k::Grid) { let index = e.registry_index; let app2 = app.clone(); b.connect_clicked(move |_| { - // In-process placement: arm the canvas to place this - // widget on the next canvas click (no selection file). - { + // Clicking the already-selected entry again cancels + // placement. + let armed = { let mut a = app2.borrow_mut(); - a.backend.sel_type = index; - a.backend.sel_name = name.clone(); - } - app2.borrow().status(&format!( - "XFrame selected: {name} — click in the canvas to place it" - )); + if a.backend.sel_type == index { + a.backend.sel_type = -1; + a.backend.sel_name.clear(); + false + } else { + // In-process placement: arm the canvas to place + // this widget on the next canvas click (no + // selection file). + a.backend.sel_type = index; + a.backend.sel_name = name.clone(); + true + } + }; + let msg = if armed { + format!( + "XFrame selected: {name} — click in the canvas to place it" + ) + } else { + "Placement cancelled".to_string() + }; + app2.borrow().status(&msg); }); let mut a = app.borrow_mut(); a.xframe_buttons.push(b);