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.
417 lines
15 KiB
Python
417 lines
15 KiB
Python
"""Canvas: a Gtk.DrawingArea that draws all placed widgets with Cairo.
|
|
|
|
Interaction ports the C core (src/xframe_canvas_core.cpp) and the Rust
|
|
canvas (crates/lightgui-designer/src/canvas.rs):
|
|
|
|
- placement: pick a palette kind, click empty canvas to place it
|
|
(centred on the click, registry default size); the pending kind
|
|
persists across placements, so repeated clicks place many widgets
|
|
- selection: click selects; a single click selects and arms a drag,
|
|
handles off
|
|
- double-click: two presses within 450ms on the same widget show the
|
|
handles, regardless of a small jiggle between the presses
|
|
- drag: move the widget once the pointer moves more than the 8px
|
|
threshold from the press point; dragging a handle resizes it
|
|
- Delete/BackSpace deletes the selected widget; Escape clears the
|
|
selection (returns it to the window root) and hides the handles
|
|
- the canvas tracks its viewport size on resize; there is no fixed
|
|
800x600 canvas — 800x600 is only the project's default window size
|
|
|
|
The event state machine lives in :class:`CanvasState`, which is pure
|
|
Python over a :class:`lightgui.model.Project` so it can be unit-tested
|
|
without a display. :class:`Canvas` is the thin GTK shell around it.
|
|
"""
|
|
|
|
import time
|
|
|
|
import gi
|
|
|
|
gi.require_version("Gtk", "3.0")
|
|
gi.require_version("Gdk", "3.0")
|
|
|
|
import cairo # noqa: F401 (registers the cairo.Context converter with gi)
|
|
|
|
from gi.repository import Gdk, Gtk # noqa: E402
|
|
|
|
from lightgui.model import Project # noqa: E402
|
|
from lightgui.xframe import core as _core # noqa: E402
|
|
|
|
BACKGROUND_RGB = (1.0, 1.0, 1.0)
|
|
GRID_RGB = (0.88, 0.88, 0.90)
|
|
|
|
|
|
def _now_ms():
|
|
return time.monotonic() * 1000.0
|
|
|
|
|
|
class CanvasState:
|
|
"""Canvas interaction state, display-free.
|
|
|
|
Operates on a :class:`lightgui.model.Project`; selection lives on
|
|
the project (``selected_id``), handles state lives here (it is UI
|
|
state, not saved in .fdj).
|
|
"""
|
|
|
|
def __init__(self, project=None):
|
|
self.project = project if project is not None else Project()
|
|
self.pending_kind = None # registry name to place on next click
|
|
self.handles_on = False
|
|
self.viewport_width = 0
|
|
self.viewport_height = 0
|
|
# Drag bookkeeping.
|
|
self.dragging = False # a move drag is armed
|
|
self.drag_active = False # past the 8px threshold
|
|
self.handle_drag = False
|
|
self.drag_handle = -1
|
|
self.press_x = 0
|
|
self.press_y = 0
|
|
self.grab_dx = 0
|
|
self.grab_dy = 0
|
|
self.drag_start_x = 0
|
|
self.drag_start_y = 0
|
|
# Double-click bookkeeping.
|
|
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).
|
|
self.on_selection_change = None
|
|
# Phase 7: called when the SELECTED widget's bounds change
|
|
# without a selection change (programmatic resize via
|
|
# set_prop / resize_to / set_bounds, or a handle drag). The
|
|
# Inspector refreshes only its geometry rows on it — a full
|
|
# rebuild would steal focus while typing in a row.
|
|
self.on_bounds_change = None
|
|
self._last_selected_id = None
|
|
self._last_bounds = None
|
|
|
|
# --- selection --------------------------------------------------------
|
|
@property
|
|
def selected(self):
|
|
return self.project.selected
|
|
|
|
def select(self, widget_id):
|
|
self.project.select(widget_id)
|
|
if widget_id is None:
|
|
self.handles_on = False
|
|
self._changed()
|
|
|
|
def clear_selection(self):
|
|
self.project.clear_selection()
|
|
self.handles_on = False
|
|
self._changed()
|
|
|
|
def show_handles(self, widget_id):
|
|
if self.project.select(widget_id):
|
|
self.handles_on = True
|
|
self._changed()
|
|
return True
|
|
return False
|
|
|
|
def set_pending_kind(self, kind):
|
|
self.pending_kind = kind if _core.is_known_kind(kind) else None
|
|
self._changed()
|
|
|
|
# --- placement / mutation ----------------------------------------------
|
|
def _wire(self, widget):
|
|
# Live-apply: a widget prop change notifies the canvas, which
|
|
# redraws (Widget.set_prop is state-only; the host redraws).
|
|
if widget is not None:
|
|
widget.on_change = self._changed
|
|
|
|
def place(self, kind, x, y, width=None, height=None):
|
|
widget = self.project.add_widget(kind, x, y, width, height)
|
|
if widget is not None:
|
|
self._wire(widget)
|
|
self.handles_on = False
|
|
self._changed()
|
|
return widget
|
|
|
|
def delete_selected(self):
|
|
selected = self.project.selected
|
|
if selected is None:
|
|
return False
|
|
self.project.remove_widget(selected.id)
|
|
self.handles_on = False
|
|
self._changed()
|
|
return True
|
|
|
|
def resize_viewport(self, width, height):
|
|
self.viewport_width = max(int(width), 0)
|
|
self.viewport_height = max(int(height), 0)
|
|
|
|
# --- input: direct ports of the Rust canvas_* functions ------------------
|
|
def button_press(self, x, y, button=1, now_ms=None):
|
|
"""Returns True when the press was consumed."""
|
|
if button != 1:
|
|
return False
|
|
if now_ms is None:
|
|
now_ms = _now_ms()
|
|
x, y = int(x), int(y)
|
|
selected = self.project.selected
|
|
# Handle grab first.
|
|
if self.handles_on and selected is not None:
|
|
handle = selected.handle_at(x, y)
|
|
if handle >= 0:
|
|
self.handle_drag = True
|
|
self.drag_handle = handle
|
|
self.drag_start_x = x
|
|
self.drag_start_y = y
|
|
return True
|
|
hit = self.project.hit_test(x, y)
|
|
if hit is not None:
|
|
double_click = (hit.id == self.last_press_id
|
|
and now_ms - self.last_press_ms
|
|
< _core.DBLCLICK_MS)
|
|
self.last_press_ms = now_ms
|
|
self.last_press_id = hit.id
|
|
if double_click:
|
|
self.project.select(hit.id)
|
|
self.handles_on = True
|
|
else:
|
|
self.project.select(hit.id)
|
|
self.handles_on = False
|
|
self.dragging = True
|
|
self.drag_active = False
|
|
self.press_x = x
|
|
self.press_y = y
|
|
self.grab_dx = x - hit.x
|
|
self.grab_dy = y - hit.y
|
|
self._changed()
|
|
return True
|
|
# Empty click: clear selection/handles; place the pending
|
|
# palette widget centred on the click, if one is pending.
|
|
self.last_press_ms = now_ms
|
|
self.last_press_id = -1
|
|
self.clear_selection()
|
|
if self.pending_kind is not None:
|
|
placed = self.project.add_widget(
|
|
self.pending_kind,
|
|
max(x - self._pending_default_w() // 2, 0),
|
|
max(y - self._pending_default_h() // 2, 0))
|
|
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):
|
|
from lightgui.xframe import palette
|
|
entry = palette.find(self.pending_kind)
|
|
return entry.default_w if entry else 100
|
|
|
|
def _pending_default_h(self):
|
|
from lightgui.xframe import palette
|
|
entry = palette.find(self.pending_kind)
|
|
return entry.default_h if entry else 30
|
|
|
|
def button_release(self, x, y, button=1):
|
|
was_drag = self.handle_drag or self.dragging
|
|
self.dragging = False
|
|
self.handle_drag = False
|
|
self.drag_handle = -1
|
|
if was_drag:
|
|
self._changed()
|
|
return was_drag
|
|
|
|
def motion(self, x, y):
|
|
"""Pointer motion. Returns True when the canvas must redraw."""
|
|
x, y = int(x), int(y)
|
|
if self.handle_drag or self.dragging:
|
|
selected = self.project.selected
|
|
if selected is None:
|
|
return False
|
|
if self.handle_drag:
|
|
dx = x - self.drag_start_x
|
|
dy = y - self.drag_start_y
|
|
self.drag_start_x = x
|
|
self.drag_start_y = y
|
|
selected.apply_handle_drag(self.drag_handle, dx, dy)
|
|
self._changed()
|
|
return True
|
|
if not self.drag_active:
|
|
if (abs(x - self.press_x) <= _core.DRAG_THRESHOLD
|
|
and abs(y - self.press_y) <= _core.DRAG_THRESHOLD):
|
|
return False
|
|
self.drag_active = True
|
|
selected.move_to(x - self.grab_dx, y - self.grab_dy)
|
|
self._changed()
|
|
return True
|
|
return False
|
|
|
|
def key(self, key_name):
|
|
"""Delete/BackSpace/Escape only. Returns True when handled."""
|
|
if key_name in ("Delete", "BackSpace"):
|
|
if self.project.selected is None:
|
|
return False
|
|
return self.delete_selected()
|
|
if key_name == "Escape":
|
|
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
|
|
|
|
def _changed(self):
|
|
# Keep the per-widget handles flag in sync: Widget.handle_at()
|
|
# only answers when its own handles_on is set.
|
|
selected = self.project.selected
|
|
for widget in self.project.widgets:
|
|
widget.handles_on = (widget is selected and self.handles_on)
|
|
if self.on_change is not None:
|
|
self.on_change()
|
|
selected_id = self.project.selected_id
|
|
selection_changed = selected_id != self._last_selected_id
|
|
if selection_changed:
|
|
self._last_selected_id = selected_id
|
|
if self.on_selection_change is not None:
|
|
self.on_selection_change()
|
|
# Phase 7: bounds moved under the same selection — tell the
|
|
# Inspector to re-sync just its geometry rows. A selection
|
|
# change already rebuilt the rows, so it needs no hook.
|
|
bounds = None
|
|
if selected is not None:
|
|
bounds = (selected.x, selected.y,
|
|
selected.width, selected.height)
|
|
if bounds != self._last_bounds:
|
|
self._last_bounds = bounds
|
|
if (not selection_changed and bounds is not None
|
|
and self.on_bounds_change is not None):
|
|
self.on_bounds_change()
|
|
|
|
|
|
class Canvas(Gtk.DrawingArea):
|
|
"""Gtk.DrawingArea shell around :class:`CanvasState`."""
|
|
|
|
def __init__(self, project=None, state=None):
|
|
super().__init__()
|
|
self.state = state if state is not None else CanvasState(project)
|
|
self.state.on_change = self._on_state_change
|
|
for widget in self.state.project.widgets:
|
|
self.state._wire(widget)
|
|
self.set_hexpand(True)
|
|
self.set_vexpand(True)
|
|
self.set_can_focus(True)
|
|
self.add_events(
|
|
Gdk.EventMask.BUTTON_PRESS_MASK
|
|
| Gdk.EventMask.BUTTON_RELEASE_MASK
|
|
| Gdk.EventMask.POINTER_MOTION_MASK
|
|
| Gdk.EventMask.KEY_PRESS_MASK
|
|
| Gdk.EventMask.STRUCTURE_MASK)
|
|
self.connect("draw", self._on_draw)
|
|
self.connect("button-press-event", self._on_button_press)
|
|
self.connect("button-release-event", self._on_button_release)
|
|
self.connect("motion-notify-event", self._on_motion)
|
|
self.connect("key-press-event", self._on_key_press)
|
|
self.connect("size-allocate", self._on_size_allocate)
|
|
|
|
# --- public API ---------------------------------------------------------
|
|
@property
|
|
def project(self):
|
|
return self.state.project
|
|
|
|
@property
|
|
def pending_kind(self):
|
|
return self.state.pending_kind
|
|
|
|
def set_pending_kind(self, kind):
|
|
self.state.set_pending_kind(kind)
|
|
|
|
def place(self, kind, x, y, width=None, height=None):
|
|
return self.state.place(kind, x, y, width, height)
|
|
|
|
@property
|
|
def selected(self):
|
|
return self.state.selected
|
|
|
|
@property
|
|
def viewport_size(self):
|
|
return (self.state.viewport_width, self.state.viewport_height)
|
|
|
|
# --- GTK event plumbing ---------------------------------------------------
|
|
def _on_state_change(self):
|
|
self.queue_draw()
|
|
|
|
def _on_size_allocate(self, _widget, allocation):
|
|
self.state.resize_viewport(allocation.width, allocation.height)
|
|
|
|
def _on_button_press(self, _widget, event):
|
|
if self.state.button_press(event.x, event.y, event.button):
|
|
self.grab_focus()
|
|
return True
|
|
return False
|
|
|
|
def _on_button_release(self, _widget, event):
|
|
self.state.button_release(event.x, event.y, event.button)
|
|
return True
|
|
|
|
def _on_motion(self, _widget, event):
|
|
self.state.motion(event.x, event.y)
|
|
return True
|
|
|
|
def _on_key_press(self, _widget, event):
|
|
key_name = Gdk.keyval_name(event.keyval)
|
|
return self.state.key(key_name)
|
|
|
|
# --- drawing ---------------------------------------------------------------
|
|
def _on_draw(self, _widget, cr):
|
|
width = self.state.viewport_width
|
|
height = self.state.viewport_height
|
|
if width <= 0 or height <= 0:
|
|
alloc = self.get_allocation()
|
|
width, height = alloc.width, alloc.height
|
|
cr.save()
|
|
cr.set_source_rgb(
|
|
*_core.parse_color(self.state.project.background,
|
|
BACKGROUND_RGB))
|
|
cr.paint()
|
|
# Dotted grid, one dot every 24px (Rust redraw_canvas).
|
|
cr.set_source_rgb(*GRID_RGB)
|
|
gx = _core.GRID_STEP
|
|
while gx < width:
|
|
gy = _core.GRID_STEP
|
|
while gy < height:
|
|
cr.rectangle(gx, gy, 1, 1)
|
|
gy += _core.GRID_STEP
|
|
gx += _core.GRID_STEP
|
|
cr.fill()
|
|
cr.restore()
|
|
for widget in self.state.project.widgets:
|
|
widget.draw(cr)
|
|
widget.dirty = False
|
|
selected = self.state.selected
|
|
if selected is not None:
|
|
cr.save()
|
|
cr.set_source_rgb(*_core.SELECT_RGB)
|
|
cr.set_line_width(1.5)
|
|
cr.set_dash([4.0, 3.0])
|
|
cr.rectangle(selected.x - 2.5, selected.y - 2.5,
|
|
selected.width + 5, selected.height + 5)
|
|
cr.stroke()
|
|
cr.restore()
|
|
if self.state.handles_on:
|
|
for i in range(_core.HANDLE_COUNT):
|
|
hx, hy, hw, hh = selected.handle_rect(i)
|
|
cr.save()
|
|
cr.set_source_rgb(*_core.HANDLE_RGB)
|
|
cr.rectangle(hx, hy, hw, hh)
|
|
cr.fill_preserve()
|
|
cr.set_source_rgb(0, 0, 0)
|
|
cr.set_line_width(1)
|
|
cr.stroke()
|
|
cr.restore()
|
|
return False
|