Fix A: the Breakpoints label sat below the breakpoint list, directly above the Output label, and the two read as one cramped/overlapping block at the bottom-left of the debugger window. It now sits above its list (like Locals in the right pane) and the Output label has 6px top/start margins so it cannot crowd the middle pane. Fix B: the Inspector only rebuilt on selection change, so a programmatic resize (set_prop/resize_to/set_bounds) left the geometry spins stale until the next selection, while canvas and status bar already showed the new size. CanvasState now fires on_bounds_change when the selected widget bounds move under the same selection; the Inspector answers with refresh_geometry(), which syncs just the four spins in place (no rebuild, no focus loss). Handle drags fire the same hook, so drags keep working and now track live.
662 lines
26 KiB
Python
662 lines
26 KiB
Python
"""Application bootstrap and main window assembly (IDE chrome).
|
|
|
|
Phase 4: menus, project I/O, code view, codegen, build/run. The menu
|
|
bar lives in the Code window, not the main designer window — the C
|
|
designer's placement (src/main.cpp: the menu bar is taken out of the
|
|
main window and packed into the Code window, so the layout window is
|
|
just the layout manager). Ctrl+E toggles the Code window from the
|
|
main window, which is the way back in when it is hidden; the palette
|
|
pane also carries a "Code (Ctrl+E)" button, next to the Object
|
|
Inspector reopen button, for the same reason.
|
|
|
|
Dirty tracking is snapshot-based: the project is dirty when its
|
|
current .fdj text differs from the last saved/loaded text. That
|
|
covers every mutation path (canvas drags, Inspector edits, deletes)
|
|
without false positives from selection or palette-picking changes,
|
|
which do not change the .fdj text.
|
|
"""
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
import gi
|
|
|
|
gi.require_version("Gtk", "3.0")
|
|
gi.require_version("Gdk", "3.0")
|
|
|
|
from gi.repository import Gdk, Gtk # noqa: E402
|
|
|
|
from lightgui import __version__ # noqa: E402
|
|
from lightgui import build as build_mod # noqa: E402
|
|
from lightgui import generator as generator_mod # noqa: E402
|
|
from lightgui.codeview import CodeWindow # noqa: E402
|
|
from lightgui.inspector import Inspector # noqa: E402
|
|
from lightgui.model import Project # noqa: E402
|
|
from lightgui.project_io import ProjectFile # noqa: E402
|
|
from lightgui.xframe import palette as palette_data # noqa: E402
|
|
from lightgui.xframe.canvas import Canvas # noqa: E402
|
|
|
|
|
|
def _git_hash():
|
|
"""Short git hash of this checkout, or 'unknown'."""
|
|
repo = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
try:
|
|
proc = subprocess.run(
|
|
["git", "-C", repo, "rev-parse", "--short", "HEAD"],
|
|
capture_output=True, text=True, timeout=5)
|
|
except (OSError, subprocess.SubprocessError):
|
|
return "unknown"
|
|
text = proc.stdout.strip()
|
|
return text or "unknown"
|
|
|
|
|
|
class Application:
|
|
def __init__(self):
|
|
self.project = Project()
|
|
self.project_file = ProjectFile(self.project)
|
|
self._saved_snapshot = self.project.save_text()
|
|
self.generated_path = None
|
|
self._run_console = None
|
|
self.debugger_window = None # Phase 5: lazy DebuggerWindow
|
|
|
|
self.window = Gtk.Window(title="LightGUI — Python/GTK")
|
|
self.window.set_default_size(1280, 800)
|
|
self.window.connect("destroy", Gtk.main_quit)
|
|
self.window.connect("delete-event", self._on_window_delete)
|
|
self.window.connect("key-press-event", self._on_window_key_press)
|
|
|
|
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
|
|
self.window.add(vbox)
|
|
|
|
paned = Gtk.Paned(orientation=Gtk.Orientation.HORIZONTAL)
|
|
paned.set_position(280)
|
|
vbox.pack_start(paned, True, True, 0)
|
|
|
|
self.canvas = Canvas(self.project)
|
|
self.canvas.state.on_change = self._on_canvas_change
|
|
# Phase 3: the Object Inspector opens as its own window
|
|
# alongside the designer (C behaviour) and follows canvas
|
|
# selection.
|
|
self.inspector = Inspector(
|
|
self.project, canvas_state=self.canvas.state,
|
|
on_status=self.set_status,
|
|
on_change=self._on_content_change)
|
|
self.canvas.state.on_selection_change = self.inspector.refresh
|
|
# Phase 7: a programmatic resize of the selected widget
|
|
# refreshes just the Inspector geometry rows (no rebuild).
|
|
self.canvas.state.on_bounds_change = \
|
|
self.inspector.refresh_geometry
|
|
paned.pack1(self._build_palette_pane(), False, False)
|
|
paned.pack2(self.canvas, True, False)
|
|
|
|
self.statusbar = Gtk.Label(label="Ready", xalign=0)
|
|
self.statusbar.set_margin_start(6)
|
|
vbox.pack_start(self.statusbar, False, False, 0)
|
|
|
|
# The Code window holds the menu bar (C placement) and the
|
|
# tabbed code editor; it starts hidden, Ctrl+E shows it.
|
|
self.code_window = CodeWindow(on_status=self.set_status)
|
|
self.menubar = self._build_menubar()
|
|
self.code_window.set_menubar(self.menubar)
|
|
|
|
self._update_status()
|
|
|
|
# --- menus ---------------------------------------------------------------
|
|
# File (New/Open/Save/Save As/Generate/Build/Run/Quit), View
|
|
# (Code window Ctrl+E, Object Inspector), Help (About). The bar is
|
|
# packed into the Code window by __init__ (C placement).
|
|
def _build_menubar(self):
|
|
menubar = Gtk.MenuBar()
|
|
|
|
file_menu = Gtk.Menu()
|
|
file_item = Gtk.MenuItem(label="File")
|
|
file_item.set_submenu(file_menu)
|
|
menubar.append(file_item)
|
|
for label, callback in (
|
|
("New", self.do_new),
|
|
("Open…", self.do_open),
|
|
("Save", self.do_save),
|
|
("Save As…", self.do_save_as),
|
|
(None, None),
|
|
("Generate…", self.do_generate),
|
|
("Build", self.do_build),
|
|
("Run", self.do_run),
|
|
(None, None),
|
|
("Quit", self.do_quit)):
|
|
if label is None:
|
|
file_menu.append(Gtk.SeparatorMenuItem())
|
|
continue
|
|
item = Gtk.MenuItem(label=label)
|
|
item.connect("activate", lambda _i, cb=callback: cb())
|
|
file_menu.append(item)
|
|
|
|
view_menu = Gtk.Menu()
|
|
view_item = Gtk.MenuItem(label="View")
|
|
view_item.set_submenu(view_menu)
|
|
menubar.append(view_item)
|
|
# Guarded: _sync_code_menu_item() sets this item's state
|
|
# programmatically, which must not re-fire the toggle.
|
|
self._syncing_menu = False
|
|
self._code_menu_item = Gtk.CheckMenuItem(label="Code View")
|
|
self._code_menu_item.connect(
|
|
"toggled",
|
|
lambda _i: None if self._syncing_menu
|
|
else self.toggle_code_window())
|
|
view_menu.append(self._code_menu_item)
|
|
self._inspector_menu_item = Gtk.CheckMenuItem(
|
|
label="Object Inspector")
|
|
self._inspector_menu_item.set_active(True)
|
|
self._inspector_menu_item.connect(
|
|
"toggled", lambda _i: self.inspector.toggle())
|
|
view_menu.append(self._inspector_menu_item)
|
|
|
|
debug_menu = Gtk.Menu()
|
|
debug_item = Gtk.MenuItem(label="Debug")
|
|
debug_item.set_submenu(debug_menu)
|
|
menubar.append(debug_item)
|
|
for label, callback in (
|
|
("Start Debugging", self.do_debug_start),
|
|
("Toggle Breakpoint", self.do_debug_toggle_breakpoint),
|
|
(None, None),
|
|
("Step Into", self.do_debug_step_into),
|
|
("Step Over", self.do_debug_step_over),
|
|
("Step Out", self.do_debug_step_out),
|
|
("Continue", self.do_debug_continue),
|
|
("Stop", self.do_debug_stop)):
|
|
if label is None:
|
|
debug_menu.append(Gtk.SeparatorMenuItem())
|
|
continue
|
|
item = Gtk.MenuItem(label=label)
|
|
item.connect("activate", lambda _i, cb=callback: cb())
|
|
debug_menu.append(item)
|
|
|
|
tools_menu = Gtk.Menu()
|
|
tools_item = Gtk.MenuItem(label="Tools")
|
|
tools_item.set_submenu(tools_menu)
|
|
menubar.append(tools_item)
|
|
install_item = Gtk.MenuItem(label="Install Libraries\u2026")
|
|
install_item.connect("activate",
|
|
lambda _i: self.do_install_libraries())
|
|
tools_menu.append(install_item)
|
|
|
|
help_menu = Gtk.Menu()
|
|
help_item = Gtk.MenuItem(label="Help")
|
|
help_item.set_submenu(help_menu)
|
|
menubar.append(help_item)
|
|
about = Gtk.MenuItem(label="About")
|
|
about.connect("activate", lambda _i: self.show_about())
|
|
help_menu.append(about)
|
|
|
|
menubar.show_all()
|
|
return menubar
|
|
|
|
def show_about(self):
|
|
dialog = Gtk.AboutDialog()
|
|
dialog.set_program_name("LightGUI")
|
|
dialog.set_version(__version__)
|
|
dialog.set_comments(
|
|
"A visual GUI designer (Python/GTK port).\n"
|
|
"Git commit: %s" % _git_hash())
|
|
dialog.set_website("https://www.lightgui.com")
|
|
dialog.set_website_label("www.lightgui.com")
|
|
dialog.set_copyright("Copyright (c) 2026 XFrame")
|
|
dialog.set_transient_for(self.window)
|
|
dialog.run()
|
|
dialog.destroy()
|
|
|
|
# --- palette pane -------------------------------------------------------
|
|
def _build_palette_pane(self):
|
|
pane = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
|
|
search = Gtk.SearchEntry()
|
|
search.set_placeholder_text("Filter palette…")
|
|
search.connect("search-changed", self._on_palette_filter)
|
|
pane.pack_start(search, False, False, 0)
|
|
self._palette_filter_text = ""
|
|
|
|
# TreeStore: section rows (bold) -> widget rows (kind, name).
|
|
self.palette_store = Gtk.TreeStore(str, str)
|
|
for section in palette_data.SECTIONS:
|
|
parent = self.palette_store.append(None, [section, ""])
|
|
for entry in palette_data.by_section(section):
|
|
self.palette_store.append(parent, [entry.name, entry.name])
|
|
self.palette_filter = self.palette_store.filter_new()
|
|
self.palette_filter.set_visible_func(self._palette_visible)
|
|
|
|
tree = Gtk.TreeView(model=self.palette_filter)
|
|
tree.set_headers_visible(False)
|
|
renderer = Gtk.CellRendererText()
|
|
column = Gtk.TreeViewColumn("Palette", renderer, text=0)
|
|
tree.append_column(column)
|
|
tree.connect("cursor-changed", self._on_palette_cursor_changed)
|
|
tree.connect("row-activated", self._on_palette_row_activated)
|
|
self.palette_tree = tree
|
|
|
|
scrolled = Gtk.ScrolledWindow()
|
|
scrolled.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
|
|
scrolled.add(tree)
|
|
pane.pack_start(scrolled, True, True, 0)
|
|
|
|
hint = Gtk.Label(
|
|
label="Pick a widget, then click the canvas to place it.",
|
|
xalign=0)
|
|
hint.set_line_wrap(True)
|
|
hint.set_max_width_chars(32)
|
|
hint.set_margin_start(6)
|
|
hint.set_margin_top(4)
|
|
pane.pack_start(hint, False, False, 0)
|
|
# Reopen the Inspector after closing it (it only hides; the
|
|
# C designer uses View -> Object Inspector — the menu bar
|
|
# lives in the Code window, so the palette pane carries the
|
|
# toggles as well).
|
|
insp_button = Gtk.Button(label="Object Inspector")
|
|
insp_button.connect("clicked",
|
|
lambda _b: self.inspector.toggle())
|
|
insp_button.set_margin_start(6)
|
|
insp_button.set_margin_end(6)
|
|
insp_button.set_margin_top(6)
|
|
pane.pack_start(insp_button, False, False, 0)
|
|
code_button = Gtk.Button(label="Code (Ctrl+E)")
|
|
code_button.connect("clicked",
|
|
lambda _b: self.toggle_code_window())
|
|
code_button.set_margin_start(6)
|
|
code_button.set_margin_end(6)
|
|
code_button.set_margin_bottom(6)
|
|
pane.pack_start(code_button, False, False, 0)
|
|
return pane
|
|
|
|
def _palette_visible(self, model, tree_iter, _data):
|
|
if not self._palette_filter_text:
|
|
return True
|
|
section = model.get_value(tree_iter, 0)
|
|
kind = model.get_value(tree_iter, 1)
|
|
if kind:
|
|
return self._palette_filter_text in kind.lower()
|
|
# Section row: visible when any child matches.
|
|
child = model.iter_children(tree_iter)
|
|
while child is not None:
|
|
if self._palette_filter_text in \
|
|
model.get_value(child, 0).lower():
|
|
return True
|
|
child = model.iter_next(child)
|
|
return False
|
|
|
|
def _on_palette_filter(self, entry):
|
|
self._palette_filter_text = entry.get_text().strip().lower()
|
|
self.palette_filter.refilter()
|
|
self.palette_tree.expand_all()
|
|
|
|
def _selected_palette_kind(self):
|
|
selection = self.palette_tree.get_selection()
|
|
model, tree_iter = selection.get_selected()
|
|
if tree_iter is None:
|
|
return None
|
|
kind = model.get_value(tree_iter, 1)
|
|
return kind or None
|
|
|
|
def _on_palette_cursor_changed(self, _tree):
|
|
kind = self._selected_palette_kind()
|
|
if kind is not None:
|
|
self.canvas.set_pending_kind(kind)
|
|
self._update_status()
|
|
|
|
def _on_palette_row_activated(self, _tree, _path, _column):
|
|
# Double-click a palette row: arm placement and focus the canvas.
|
|
kind = self._selected_palette_kind()
|
|
if kind is not None:
|
|
self.canvas.set_pending_kind(kind)
|
|
self.canvas.grab_focus()
|
|
|
|
# --- canvas / status ------------------------------------------------------
|
|
def set_status(self, text):
|
|
self.statusbar.set_text(text)
|
|
|
|
def _sync_dirty(self):
|
|
"""Recompute the dirty flag from the .fdj snapshot."""
|
|
self.project_file.dirty = \
|
|
self.project.save_text() != self._saved_snapshot
|
|
|
|
def _on_content_change(self):
|
|
# Inspector edit (prop / event / bulk / colour / root).
|
|
self._sync_dirty()
|
|
self._update_status()
|
|
|
|
def _on_canvas_change(self):
|
|
self._sync_dirty()
|
|
self._update_status()
|
|
self.canvas.queue_draw()
|
|
|
|
def _update_status(self):
|
|
parts = ["%s%s" % (self.project_file.title,
|
|
"*" if self.project_file.dirty else ""),
|
|
"%d widget(s)" % len(self.project.widgets)]
|
|
pending = self.canvas.pending_kind
|
|
if pending:
|
|
parts.append("placing: %s" % pending)
|
|
selected = self.project.selected
|
|
if selected is not None:
|
|
parts.append("selected: %s (%s) at %d,%d %dx%d"
|
|
% (selected.name, selected.kind, selected.x,
|
|
selected.y, selected.width, selected.height))
|
|
if self.canvas.state.handles_on:
|
|
parts.append("handles on")
|
|
self.statusbar.set_text(" | ".join(parts))
|
|
|
|
# --- File actions -----------------------------------------------------------
|
|
def _after_project_reload(self, message):
|
|
# Widgets loaded from a file were never wired to the canvas
|
|
# state; wire them so Inspector edits redraw, like placed ones.
|
|
for widget in self.project.widgets:
|
|
self.canvas.state._wire(widget)
|
|
self._saved_snapshot = self.project.save_text()
|
|
self.project_file.dirty = False
|
|
self.canvas.queue_draw()
|
|
self.inspector.refresh()
|
|
self.refresh_code_view()
|
|
self._update_status()
|
|
self.set_status(message)
|
|
|
|
def do_new(self):
|
|
if not self.project_file.confirm_discard(self.window):
|
|
return
|
|
self.project_file.new()
|
|
self.generated_path = None
|
|
self._after_project_reload("New project")
|
|
|
|
def do_open(self):
|
|
if not self.project_file.confirm_discard(self.window):
|
|
return
|
|
if self.project_file.open_dialog(self.window):
|
|
self.generated_path = None
|
|
self._after_project_reload("Opened %s"
|
|
% self.project_file.path)
|
|
|
|
def do_save(self):
|
|
try:
|
|
self.project_file.save()
|
|
except ValueError:
|
|
if not self.project_file.save_dialog(self.window):
|
|
return
|
|
self._saved_snapshot = self.project.save_text()
|
|
self.project_file.dirty = False
|
|
self._update_status()
|
|
self.set_status("Saved %s" % self.project_file.path)
|
|
|
|
def do_save_as(self):
|
|
if self.project_file.save_dialog(self.window):
|
|
self._saved_snapshot = self.project.save_text()
|
|
self.project_file.dirty = False
|
|
self._update_status()
|
|
self.set_status("Saved %s" % self.project_file.path)
|
|
|
|
# --- Generate / Build / Run ---------------------------------------------------
|
|
def _generate_text(self):
|
|
return generator_mod.generate(self.project)
|
|
|
|
def do_generate(self):
|
|
dialog = Gtk.FileChooserDialog(
|
|
title="Generate Python/XFrame — choose a folder",
|
|
parent=self.window,
|
|
action=Gtk.FileChooserAction.CREATE_FOLDER,
|
|
buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
|
|
"_Generate", Gtk.ResponseType.ACCEPT))
|
|
response = dialog.run()
|
|
folder = dialog.get_filename() \
|
|
if response == Gtk.ResponseType.ACCEPT else None
|
|
dialog.destroy()
|
|
if not folder:
|
|
return
|
|
filename = generator_mod.default_filename(self.project)
|
|
path = os.path.join(folder, filename)
|
|
generator_mod.generate_to_file(self.project, path)
|
|
self.generated_path = path
|
|
self.code_window.show_generated(self._generate_text(), path)
|
|
self.code_window.show()
|
|
self._sync_code_menu_item()
|
|
self.set_status("Generated %s — Build with File -> Build, "
|
|
"Run with File -> Run" % path)
|
|
|
|
def _ensure_generated(self):
|
|
"""Generated script path, generating into a temp dir when the
|
|
user has not run Generate yet."""
|
|
if self.generated_path and os.path.isfile(self.generated_path):
|
|
return self.generated_path
|
|
folder = tempfile.mkdtemp(prefix="lightgui_gen_")
|
|
path = os.path.join(folder,
|
|
generator_mod.default_filename(self.project))
|
|
generator_mod.generate_to_file(self.project, path)
|
|
self.generated_path = path
|
|
self.code_window.show_generated(self._generate_text(), path)
|
|
return path
|
|
|
|
def do_build(self):
|
|
path = self._ensure_generated()
|
|
result = build_mod.syntax_check(path)
|
|
build_mod.show_result_dialog(self.window, result, "Build")
|
|
self.set_status("Build OK — %s" % path if result.ok
|
|
else "Build FAILED — see log")
|
|
|
|
def do_run(self):
|
|
path = self._ensure_generated()
|
|
result = build_mod.syntax_check(path)
|
|
if not result.ok:
|
|
build_mod.show_result_dialog(self.window, result, "Build")
|
|
self.set_status("Build FAILED — see log")
|
|
return
|
|
if self._run_console is not None:
|
|
self._run_console.destroy()
|
|
console = build_mod.BuildConsole(
|
|
title="Run — %s" % os.path.basename(path),
|
|
parent=self.window)
|
|
self._run_console = console
|
|
console.show()
|
|
console.run_command([sys.executable, path])
|
|
self.set_status("Running %s" % path)
|
|
|
|
# --- Debugger (Phase 5) -----------------------------------------------------------
|
|
def open_debugger(self):
|
|
"""Create (once) and show the debugger window, targeting the
|
|
generated script. The window owns the pdb session."""
|
|
if self.debugger_window is None:
|
|
from lightgui.debugger_ui import DebuggerWindow
|
|
self.debugger_window = DebuggerWindow(
|
|
parent=self.window,
|
|
target=self._ensure_generated(),
|
|
on_status=self.set_status,
|
|
on_stop_location=self._on_debug_stop_location)
|
|
else:
|
|
self.debugger_window.set_target(self._ensure_generated())
|
|
self.debugger_window.show()
|
|
return self.debugger_window
|
|
|
|
def _debugger(self):
|
|
"""Debugger window, opened if needed (target = generated
|
|
script)."""
|
|
return self.open_debugger()
|
|
|
|
def do_debug_start(self):
|
|
window = self.open_debugger()
|
|
window._on_run()
|
|
|
|
def _current_code_location(self):
|
|
"""(file_path, 1-based line) at the cursor in the current
|
|
Code tab, or None when there is no usable tab."""
|
|
view = self.code_window.current_view()
|
|
if view is None or not view.file_path:
|
|
return None
|
|
mark = view.buffer.get_insert()
|
|
line = view.buffer.get_iter_at_mark(mark).get_line() + 1
|
|
return view.file_path, line
|
|
|
|
def do_debug_toggle_breakpoint(self):
|
|
location = self._current_code_location()
|
|
if location is None:
|
|
self.set_status("Toggle Breakpoint needs the cursor in "
|
|
"a saved Code tab - Generate first")
|
|
return
|
|
path, line = location
|
|
window = self._debugger()
|
|
now_set = window.toggle_breakpoint(path, line)
|
|
self.set_status("Breakpoint %s at %s:%d"
|
|
% ("set" if now_set else "cleared", path, line))
|
|
|
|
def do_debug_step_into(self):
|
|
self._debugger()._on_step_into()
|
|
|
|
def do_debug_step_over(self):
|
|
self._debugger()._on_step_over()
|
|
|
|
def do_debug_step_out(self):
|
|
self._debugger()._on_step_out()
|
|
|
|
def do_debug_continue(self):
|
|
self._debugger()._on_continue()
|
|
|
|
def do_debug_stop(self):
|
|
if self.debugger_window is not None:
|
|
self.debugger_window._on_stop()
|
|
|
|
def _on_debug_stop_location(self, file, line):
|
|
"""Scroll the Code window to the stopped line (debugger poll
|
|
callback). Only for files the Code window knows: the
|
|
generated script or an open tab."""
|
|
view = None
|
|
for candidate in self.code_window.views():
|
|
if candidate.file_path == file:
|
|
view = candidate
|
|
break
|
|
if view is None:
|
|
if self.generated_path and file == self.generated_path:
|
|
self.refresh_code_view(force=True)
|
|
view = self.code_window.generated_view
|
|
else:
|
|
return
|
|
if view is None:
|
|
return
|
|
self.code_window.show()
|
|
self._sync_code_menu_item()
|
|
page = self.code_window.notebook.page_num(view)
|
|
if page >= 0:
|
|
self.code_window.notebook.set_current_page(page)
|
|
line0 = max(0, min(line - 1, view.buffer.get_line_count() - 1))
|
|
cursor = view.buffer.get_iter_at_line(line0)
|
|
view.buffer.place_cursor(cursor)
|
|
view.view.scroll_to_iter(cursor, 0.0, True, 0.0, 0.5)
|
|
self.set_status("Stopped at %s:%d" % (file, line))
|
|
|
|
def _close_debugger(self):
|
|
if self.debugger_window is not None:
|
|
self.debugger_window.destroy()
|
|
self.debugger_window = None
|
|
|
|
# --- Install Libraries (Phase 5) --------------------------------------------------
|
|
def do_install_libraries(self):
|
|
from lightgui import install_libs
|
|
install_libs.show_install_libraries_dialog(self.window)
|
|
|
|
# --- Code window ---------------------------------------------------------------
|
|
def _sync_code_menu_item(self):
|
|
if getattr(self, "_code_menu_item", None) is not None:
|
|
self._syncing_menu = True
|
|
try:
|
|
self._code_menu_item.set_active(
|
|
self.code_window.is_visible())
|
|
finally:
|
|
self._syncing_menu = False
|
|
|
|
def toggle_code_window(self):
|
|
if self.code_window.is_visible():
|
|
self.code_window.hide()
|
|
else:
|
|
self.refresh_code_view(force=True)
|
|
self.code_window.show()
|
|
self._sync_code_menu_item()
|
|
if not self.code_window.is_visible():
|
|
self.set_status("Code window hidden — Ctrl+E to show it again")
|
|
|
|
def refresh_code_view(self, force=False):
|
|
"""Regenerate into the Code window's generated tab — unless
|
|
that tab holds unsaved hand edits, which are kept (C).
|
|
Skipped when there is no generated tab and the window is
|
|
hidden, unless *force* (opening the window creates the tab).
|
|
"""
|
|
if self.code_window.generated_view is None \
|
|
and not self.code_window.is_visible() and not force:
|
|
return
|
|
self.code_window.show_generated(
|
|
self._generate_text(), self.generated_path)
|
|
|
|
# --- quit / keys ------------------------------------------------------------------
|
|
def do_quit(self):
|
|
if not self.project_file.confirm_discard(self.window):
|
|
return
|
|
self._close_debugger()
|
|
Gtk.main_quit()
|
|
|
|
def _on_window_delete(self, _window, _event):
|
|
# Confirm before closing with unsaved changes; returning True
|
|
# keeps the window open.
|
|
return not self.project_file.confirm_discard(self.window)
|
|
|
|
def _on_window_key_press(self, _window, event):
|
|
# The canvas handles Delete/Escape when focused; forward them
|
|
# here too so they work when the palette has focus.
|
|
key_name = Gdk.keyval_name(event.keyval)
|
|
if key_name in ("Delete", "BackSpace", "Escape"):
|
|
if self.canvas.state.key(key_name):
|
|
return True
|
|
return False
|
|
if not event.state & Gdk.ModifierType.CONTROL_MASK:
|
|
return False
|
|
# Do not hijack typing in an entry (e.g. the palette filter);
|
|
# Ctrl+E still works there, as the way back to the Code window.
|
|
focus = self.window.get_focus()
|
|
if isinstance(focus, Gtk.Entry) and key_name not in ("e", "E"):
|
|
return False
|
|
actions = {
|
|
"e": self.toggle_code_window,
|
|
"E": self.toggle_code_window,
|
|
"s": self.do_save,
|
|
"S": self.do_save,
|
|
"o": self.do_open,
|
|
"O": self.do_open,
|
|
"n": self.do_new,
|
|
"N": self.do_new,
|
|
"g": self.do_generate,
|
|
"G": self.do_generate,
|
|
"r": self.do_run,
|
|
"R": self.do_run,
|
|
}
|
|
action = actions.get(key_name)
|
|
if action is not None:
|
|
action()
|
|
return True
|
|
return False
|
|
|
|
def run(self):
|
|
self.window.show_all()
|
|
# Park the Inspector to the right of the designer (C parks
|
|
# it beside the main window; left if there is no room).
|
|
self.inspector.show()
|
|
while Gtk.events_pending():
|
|
Gtk.main_iteration()
|
|
try:
|
|
x, y = self.window.get_position()
|
|
width, _h = self.window.get_size()
|
|
iw, _ih = self.inspector.window.get_size()
|
|
screen_w = self.window.get_screen().get_width()
|
|
ix = x + width + 12
|
|
if ix + iw > screen_w:
|
|
ix = max(x - iw - 12, 0)
|
|
self.inspector.window.move(ix, y)
|
|
except Exception:
|
|
pass
|
|
Gtk.main()
|
|
return 0
|
|
|
|
|
|
def main(argv):
|
|
app = Application()
|
|
return app.run()
|