"""Application bootstrap and main window assembly (IDE chrome). Phase 1: a minimal palette list on the side (pick a kind, click the canvas to place it — the full palette window is a later phase) and the XFrame canvas filling the centre. The canvas tracks its viewport size on resize; there is no fixed 800x600 canvas. """ 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.inspector import Inspector # noqa: E402 from lightgui.model import Project # noqa: E402 from lightgui.xframe import palette as palette_data # noqa: E402 from lightgui.xframe.canvas import Canvas # noqa: E402 class Application: def __init__(self): self.project = Project() 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("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) self.canvas.state.on_selection_change = self.inspector.refresh 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) self._update_status() # --- 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 — this app has no # menu bar yet, so the palette pane carries the toggle). 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) insp_button.set_margin_bottom(6) pane.pack_start(insp_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 _on_canvas_change(self): self._update_status() self.canvas.queue_draw() def _update_status(self): parts = ["%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)) 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 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()