From 5b0c6f02fd6cb008396f4bbf972aa0c4451ddd85 Mon Sep 17 00:00:00 2001 From: cnmcdee Date: Wed, 2 Dec 2026 03:15:36 +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 and clicking the selected palette row again also cancel an armed placement. Previously pending_kind persisted across placements, so every click placed another fresh widget. Task 2: placing a widget refreshes the Code window at once (Lazarus-style) via a new CanvasState.on_placed hook wired to Application.refresh_code_view(), so the generated code shows the new widget without pressing Generate. Tests: replaced test_pending_kind_persists_across_placements with test_pending_kind_disarms_after_one_placement, plus test_escape_cancels_pending_kind and test_on_placed_fires_after_canvas_click_placement (280 tests pass). Verified live under Xvfb (p-place-verify.sh + screenshots): place-once, Escape-cancel, toggle-cancel, re-arm, code sync, clean app log. --- lightgui/app.py | 23 +++++++++++++++++++++++ lightgui/xframe/canvas.py | 15 ++++++++++++++- tests/test_canvas.py | 23 ++++++++++++++++++++--- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/lightgui/app.py b/lightgui/app.py index 663af4c..b3dbc83 100644 --- a/lightgui/app.py +++ b/lightgui/app.py @@ -76,6 +76,9 @@ class Application: self.canvas = Canvas(self.project) self.canvas.state.on_change = self._on_canvas_change + # A widget placed by a canvas click refreshes the code view + # at once (Lazarus-style sync). + self.canvas.state.on_placed = lambda _w: self.refresh_code_view() # Phase 3: the Object Inspector opens as its own window # alongside the designer (C behaviour) and follows canvas # selection. @@ -231,6 +234,8 @@ class Application: tree.append_column(column) tree.connect("cursor-changed", self._on_palette_cursor_changed) tree.connect("row-activated", self._on_palette_row_activated) + tree.connect("button-press-event", + self._on_palette_button_press) self.palette_tree = tree scrolled = Gtk.ScrolledWindow() @@ -295,6 +300,24 @@ class Application: kind = model.get_value(tree_iter, 1) return kind or None + def _on_palette_button_press(self, tree, event): + # Clicking the already-armed row again cancels placement + # (cursor-changed does not fire for the already-selected row). + result = tree.get_path_at_pos(int(event.x), int(event.y)) + if result is None: + return False + path = result[0] + model = tree.get_model() + tree_iter = model.get_iter(path) + kind = model.get_value(tree_iter, 1) + if kind and kind == self.canvas.pending_kind: + self.canvas.set_pending_kind(None) + tree.get_selection().unselect_all() + self._update_status() + self.set_status("Placement cancelled") + return True + return False + def _on_palette_cursor_changed(self, _tree): kind = self._selected_palette_kind() if kind is not None: diff --git a/lightgui/xframe/canvas.py b/lightgui/xframe/canvas.py index 6aeaa8d..31cd78b 100644 --- a/lightgui/xframe/canvas.py +++ b/lightgui/xframe/canvas.py @@ -73,6 +73,8 @@ class CanvasState: self.last_press_id = -1 self.last_press_ms = 0.0 self.on_change = None # called after any state change + self.on_placed = None # called with the placed widget, + # after a canvas-click placement # Phase 3: called only when the selection actually changes # (the Inspector refreshes on it; prop edits and drags do # not rebuild the Inspector, or typing would lose focus). @@ -193,7 +195,14 @@ class CanvasState: if placed is not None: self._wire(placed) self.handles_on = False + # 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. + self.pending_kind = None self._changed() + if self.on_placed is not None: + self.on_placed(placed) return True def _pending_default_w(self): @@ -247,8 +256,12 @@ class CanvasState: return False return self.delete_selected() if key_name == "Escape": - had = self.handles_on or self.project.selected is not None + had = (self.handles_on or self.project.selected is not None + or self.pending_kind is not None) self.clear_selection() + # Escape also cancels an armed placement (same as clicking + # the selected palette entry again). + self.pending_kind = None return had return False diff --git a/tests/test_canvas.py b/tests/test_canvas.py index 9a8b8b3..5da961c 100644 --- a/tests/test_canvas.py +++ b/tests/test_canvas.py @@ -26,12 +26,29 @@ class CanvasPlacementTests(unittest.TestCase): self.assertEqual((widgets[0].width, widgets[0].height), (150, 36)) self.assertIs(self.state.selected, widgets[0]) - def test_pending_kind_persists_across_placements(self): + def test_pending_kind_disarms_after_one_placement(self): + # Placing one widget disarms placement mode: a second click + # on empty canvas places nothing. self.state.set_pending_kind("GtkButton") self.state.button_press(200, 150, now_ms=1000.0) + self.assertIsNone(self.state.pending_kind) self.state.button_press(400, 300, now_ms=2000.0) - self.assertEqual(len(self.state.project.widgets), 2) - self.assertEqual(self.state.pending_kind, "GtkButton") + self.assertEqual(len(self.state.project.widgets), 1) + + def test_escape_cancels_pending_kind(self): + self.state.set_pending_kind("GtkButton") + self.assertTrue(self.state.key("Escape")) + self.assertIsNone(self.state.pending_kind) + self.state.button_press(200, 150, now_ms=1000.0) + self.assertEqual(len(self.state.project.widgets), 0) + + def test_on_placed_fires_after_canvas_click_placement(self): + seen = [] + self.state.on_placed = seen.append + self.state.set_pending_kind("GtkButton") + self.state.button_press(200, 150, now_ms=1000.0) + self.assertEqual(len(seen), 1) + self.assertEqual(seen[0].kind, "GtkButton") def test_unknown_pending_kind_is_ignored(self): self.state.set_pending_kind("GtkNoSuchWidget")