From da84fba814bfab25c67201736824063087feb5bf Mon Sep 17 00:00:00 2001 From: cnmcdee Date: Wed, 2 Dec 2026 03:08:39 +0000 Subject: [PATCH] Single-placement mode + code-view sync on placement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Task 1: placing a widget now disarms placement mode — the next canvas click places nothing until another palette widget is selected. Escape (canvas or palette window) and clicking the selected palette entry again also cancel an armed placement. Previously sel_type persisted across placements, so every click placed another fresh widget. Task 2: placing a widget refreshes the Code window at once (Lazarus-style) via refresh_code_view() after place_widget(), so the generated code shows the new widget without pressing Generate. Verified live under Xvfb (9/9 in r-place-verify.sh): place-once, Escape-cancel, toggle-cancel, re-arm, code sync, clean app log. --- crates/lightgui-designer/src/canvas.rs | 27 ++++++++++++++++-- crates/lightgui-designer/src/main.rs | 23 +++++++++++++++ crates/lightgui-designer/src/palette_ui.rs | 33 ++++++++++++++++------ 3 files changed, 71 insertions(+), 12 deletions(-) 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);