Files
cnmcdee 5b0c6f02fd Single-placement mode + code-view sync on placement
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.
2026-12-02 03:15:36 +00:00

242 lines
10 KiB
Python

"""Tests for lightgui.xframe.canvas.CanvasState (no display needed)."""
import unittest
from lightgui.model import Project
from lightgui.xframe import core
from lightgui.xframe.canvas import CanvasState
class CanvasPlacementTests(unittest.TestCase):
def setUp(self):
self.state = CanvasState(Project())
def test_empty_click_without_pending_kind_selects_nothing(self):
self.assertTrue(self.state.button_press(100, 100, now_ms=1000.0))
self.assertEqual(self.state.project.widgets, [])
self.assertIsNone(self.state.selected)
def test_pending_kind_places_centred_on_click(self):
self.state.set_pending_kind("GtkButton")
self.state.button_press(200, 150, now_ms=1000.0)
widgets = self.state.project.widgets
self.assertEqual(len(widgets), 1)
# GtkButton default 150x36, centred on (200, 150).
self.assertEqual((widgets[0].x, widgets[0].y), (125, 132))
self.assertEqual((widgets[0].width, widgets[0].height), (150, 36))
self.assertIs(self.state.selected, widgets[0])
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), 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")
self.assertIsNone(self.state.pending_kind)
def test_place_clamps_to_canvas_origin(self):
self.state.set_pending_kind("GtkButton")
self.state.button_press(10, 10, now_ms=1000.0)
widget = self.state.project.widgets[0]
self.assertEqual((widget.x, widget.y), (0, 0))
class CanvasSelectionTests(unittest.TestCase):
def setUp(self):
self.state = CanvasState(Project())
self.widget = self.state.place("GtkButton", 100, 80, 150, 36)
def test_single_click_selects_without_handles(self):
self.state.button_press(150, 100, now_ms=1000.0)
self.assertIs(self.state.selected, self.widget)
self.assertFalse(self.state.handles_on)
def test_double_click_within_450ms_shows_handles(self):
self.state.button_press(150, 100, now_ms=1000.0)
self.state.button_release(150, 100)
self.state.button_press(152, 101, now_ms=1200.0)
self.assertIs(self.state.selected, self.widget)
self.assertTrue(self.state.handles_on)
def test_double_click_jiggle_does_not_break_it(self):
# First press drags the widget a few px (under the threshold it
# does not move, but the press point jiggles); second press on
# the shifted widget still counts as a double-click.
self.state.button_press(150, 100, now_ms=1000.0)
self.state.motion(154, 103)
self.state.button_release(154, 103)
self.state.button_press(154, 103, now_ms=1300.0)
self.assertTrue(self.state.handles_on)
def test_second_press_after_450ms_is_not_a_double_click(self):
self.state.button_press(150, 100, now_ms=1000.0)
self.state.button_release(150, 100)
self.state.button_press(150, 100, now_ms=1500.0)
self.assertFalse(self.state.handles_on)
def test_second_press_on_another_widget_is_not_a_double_click(self):
other = self.state.place("GtkLabel", 300, 80, 180, 30)
self.state.button_press(150, 100, now_ms=1000.0)
self.state.button_release(150, 100)
self.state.button_press(350, 95, now_ms=1200.0)
self.assertIs(self.state.selected, other)
self.assertFalse(self.state.handles_on)
def test_empty_click_clears_selection_and_handles(self):
self.state.button_press(150, 100, now_ms=1000.0)
self.state.button_release(150, 100)
self.state.button_press(152, 101, now_ms=1200.0)
self.assertTrue(self.state.handles_on)
self.state.button_press(500, 400, now_ms=2000.0)
self.assertIsNone(self.state.selected)
self.assertFalse(self.state.handles_on)
def test_escape_clears_selection(self):
self.state.button_press(150, 100, now_ms=1000.0)
self.assertTrue(self.state.key("Escape"))
self.assertIsNone(self.state.selected)
self.assertFalse(self.state.key("Escape"))
def test_delete_removes_selected(self):
self.state.button_press(150, 100, now_ms=1000.0)
self.assertTrue(self.state.key("Delete"))
self.assertEqual(self.state.project.widgets, [])
self.assertFalse(self.state.key("Delete"))
def test_other_keys_not_handled(self):
self.assertFalse(self.state.key("a"))
self.assertFalse(self.state.key("Return"))
def test_non_button_1_ignored(self):
# place() in setUp selects the widget; clear it first.
self.state.clear_selection()
self.assertFalse(self.state.button_press(150, 100, button=3,
now_ms=1000.0))
self.assertIsNone(self.state.selected)
class CanvasDragTests(unittest.TestCase):
def setUp(self):
self.state = CanvasState(Project())
self.widget = self.state.place("GtkButton", 100, 80, 150, 36)
def test_sub_threshold_motion_does_not_move(self):
self.state.button_press(150, 100, now_ms=1000.0)
self.state.motion(155, 104)
self.assertEqual((self.widget.x, self.widget.y), (100, 80))
def test_drag_moves_widget_by_grab_offset(self):
self.state.button_press(150, 100, now_ms=1000.0)
self.state.motion(200, 150)
# Grab offset was (50, 20); widget follows the pointer.
self.assertEqual((self.widget.x, self.widget.y), (150, 130))
self.state.button_release(200, 150)
self.assertEqual((self.widget.x, self.widget.y), (150, 130))
def test_drag_clamps_at_origin(self):
self.state.button_press(110, 90, now_ms=1000.0)
self.state.motion(0, 0)
self.assertEqual((self.widget.x, self.widget.y), (0, 0))
def test_handle_drag_resizes(self):
self.state.button_press(150, 100, now_ms=1000.0)
self.state.button_release(150, 100)
self.state.button_press(152, 101, now_ms=1200.0)
self.assertTrue(self.state.handles_on)
# SE handle centre is (250, 116); grab it and drag +20/+10.
self.state.button_press(250, 116, now_ms=1300.0)
self.assertTrue(self.state.handle_drag)
self.state.motion(270, 126)
self.assertEqual((self.widget.width, self.widget.height),
(170, 46))
self.state.button_release(270, 126)
self.assertFalse(self.state.handle_drag)
def test_viewport_tracks_resize(self):
self.state.resize_viewport(1280, 720)
self.assertEqual((self.state.viewport_width,
self.state.viewport_height), (1280, 720))
class BoundsChangeTests(unittest.TestCase):
"""Phase 7 fix B: the Inspector must learn about a programmatic
resize of the selected widget without a selection change."""
def setUp(self):
self.state = CanvasState(Project())
self.bounds_calls = []
self.selection_calls = []
self.state.on_bounds_change = lambda: self.bounds_calls.append(1)
self.state.on_selection_change = (
lambda: self.selection_calls.append(1))
def test_programmatic_resize_fires_bounds_hook(self):
widget = self.state.place("GtkButton", 10, 10, 150, 36)
# place() selects: selection hook fires, bounds hook does not
# (the selection change already refreshes the Inspector).
self.assertEqual(self.selection_calls, [1])
self.assertEqual(self.bounds_calls, [])
widget.resize_to(260, 60)
self.assertEqual(len(self.bounds_calls), 1)
widget.set_prop("width", "300")
self.assertEqual(len(self.bounds_calls), 2)
self.assertEqual(widget.width, 300)
def test_selection_change_does_not_fire_bounds_hook(self):
self.state.place("GtkButton", 10, 10, 150, 36)
self.state.place("GtkLabel", 300, 10, 180, 30)
self.assertEqual(self.bounds_calls, [])
def test_unrelated_prop_change_does_not_fire_bounds_hook(self):
widget = self.state.place("GtkButton", 10, 10, 150, 36)
widget.set_prop("label", "New caption")
self.assertEqual(self.bounds_calls, [])
def test_set_bounds_and_move_fire_bounds_hook(self):
widget = self.state.place("GtkButton", 10, 10, 150, 36)
widget.set_bounds(20, 20, 200, 50)
self.assertEqual(len(self.bounds_calls), 1)
widget.move_to(30, 30)
self.assertEqual(len(self.bounds_calls), 2)
def test_handle_drag_fires_bounds_hook(self):
# The drag path (CanvasDragTests) must keep working: a handle
# drag resizes the widget and fires the hook, so the
# Inspector geometry rows track the drag live.
widget = self.state.place("GtkButton", 100, 80, 150, 36)
self.state.button_press(150, 100, now_ms=1000.0)
self.state.button_release(150, 100)
self.state.button_press(152, 101, now_ms=1200.0)
self.assertTrue(self.state.handles_on)
# SE handle centre is (250, 116); grab it and drag +20/+10.
self.state.button_press(250, 116, now_ms=1300.0)
self.assertTrue(self.state.handle_drag)
before = len(self.bounds_calls)
self.state.motion(270, 126)
self.assertGreater(len(self.bounds_calls), before)
self.assertEqual((widget.width, widget.height), (170, 46))
if __name__ == "__main__":
unittest.main()