diff --git a/examples/shots/xloadbadge.png b/examples/shots/xloadbadge.png new file mode 100644 index 0000000..2271694 Binary files /dev/null and b/examples/shots/xloadbadge.png differ diff --git a/examples/shots/xmonitorbutton.png b/examples/shots/xmonitorbutton.png new file mode 100644 index 0000000..55a596e Binary files /dev/null and b/examples/shots/xmonitorbutton.png differ diff --git a/examples/shots/xmonitorgrid.png b/examples/shots/xmonitorgrid.png new file mode 100644 index 0000000..e344809 Binary files /dev/null and b/examples/shots/xmonitorgrid.png differ diff --git a/examples/shots/xprocesslist.png b/examples/shots/xprocesslist.png new file mode 100644 index 0000000..950c7d9 Binary files /dev/null and b/examples/shots/xprocesslist.png differ diff --git a/examples/shots/xsysbox.png b/examples/shots/xsysbox.png new file mode 100644 index 0000000..d3ecd16 Binary files /dev/null and b/examples/shots/xsysbox.png differ diff --git a/examples/xloadbadge.py b/examples/xloadbadge.py new file mode 100644 index 0000000..9d36c78 --- /dev/null +++ b/examples/xloadbadge.py @@ -0,0 +1,190 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 xloadbadge.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'XLoadBadge example' (252x92) with 1 widget(s). + +import math +import sys + +import gi + +gi.require_version("Gtk", "3.0") +gi.require_version("Gdk", "3.0") + +from gi.repository import Gdk, GLib, Gtk # noqa: E402 +from lightgui.xframe.core import create, parse_color # noqa: E402 + + +def on_XLoadBadge_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('XLoadBadge_001', 'OnClick'): on_XLoadBadge_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'XLoadBadge_001': (True, True, False, False, 16, 16, 220, 60), +} + + +def _apply_anchors(widgets, width, height): + """Reposition anchored widgets for a drawing area of + *width* x *height* (Flag 1: minimal anchor layout). + Computed from the design geometry every time, so a + resize is idempotent — no drift across resizes.""" + design_w, design_h = _DESIGN_SIZE + dx = width - design_w + dy = height - design_h + for widget in widgets: + top, left, right, bottom, ox, oy, ow, oh = \ + _ANCHORS[widget.name] + x, y, w, h = ox, oy, ow, oh + if right and not left: + x = ox + dx + elif right and left: + w = ow + dx + if bottom and not top: + y = oy + dy + elif bottom and top: + h = oh + dy + widget.move_to(x, y) + widget.resize_to(w, h) + +def _build_widgets(): + """Create every placed widget with its props.""" + widgets = [] + widget = create('XLoadBadge', 'XLoadBadge_001', 16, 16, 220, 60) + # Live data: load averages + uptime from the OS. + widget.refresh_load() + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (252, 92) +_BACKGROUND = '' +_WIDGETS = [] +_LAST_SIZE = [None] +_PRESSED = [None] # Task 4: widget that got the ButtonPress + + +def _hit_widget(widgets, x, y): + """Topmost visible widget containing (*x*, *y*).""" + for widget in reversed(widgets): + if widget.get_prop("visible", "true") in ("false", "0"): + continue + if widget.contains(x, y): + return widget + return None + + +def _dispatch_to(widget, event): + """Call the *event* handler of *widget* (already chosen).""" + if widget is None: + return + if widget.get_prop("enabled", "true") in ("false", "0"): + return + handler = _HANDLERS.get((widget.name, event)) + if handler is not None: + handler(widget) + + +def _dispatch(widgets, x, y, event): + """Call the *event* handler of the widget at (*x*, *y*), + if it has one wired.""" + _dispatch_to(_hit_widget(widgets, x, y), event) + + +def _on_draw(area, cr): + bg = parse_color(_BACKGROUND, None) + if bg is None: + bg = (1.0, 1.0, 1.0) + cr.set_source_rgb(*bg) + cr.paint() + for widget in _WIDGETS: + widget.draw(cr) + return False + + +def _on_button_press(area, event): + if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnDblClick") + elif event.type == Gdk.EventType.BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnMouseDown") + _dispatch(_WIDGETS, event.x, event.y, "OnClick") + # Task 4: remember the pressed widget; release goes there. + _PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y) + return False + + +def _on_button_release(area, event): + # Task 4: release goes to the widget that got the press, + # even if the pointer moved off it. + widget = _PRESSED[0] + _PRESSED[0] = None + _dispatch_to(widget, "OnMouseUp") + return False + + +def _on_area_resize(area, allocation): + size = (allocation.width, allocation.height) + if size == _LAST_SIZE[0]: + return False + _LAST_SIZE[0] = size + _apply_anchors(_WIDGETS, size[0], size[1]) + area.queue_draw() + return False + + +# --- live demo (hand-added, not designer codegen) ---------------------------- +# The widget shows live data: _tick() feeds a new sample +# every 1000 ms and redraws the drawing area. _TICK starts +# at 60 so the timer continues the seeded sine waves +# from _build_widgets() without a phase jump. +_TICK = [60] +_AREA = [] + + +def _tick(): + _TICK[0] += 1 + t = _TICK[0] + widget = _WIDGETS[0] + widget.refresh_load() + if _AREA: + _AREA[0].queue_draw() + return True + +def main(): + window = Gtk.Window(title='XLoadBadge example') + window.set_default_size(252, 92) + window.connect("destroy", Gtk.main_quit) + vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, + spacing=0) + window.add(vbox) + area = Gtk.DrawingArea() + area.set_hexpand(True) + area.set_vexpand(True) + area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | + Gdk.EventMask.BUTTON_RELEASE_MASK) + area.connect("draw", _on_draw) + area.connect("button-press-event", _on_button_press) + area.connect("button-release-event", _on_button_release) + area.connect("size-allocate", _on_area_resize) + vbox.pack_start(area, True, True, 0) + _WIDGETS.extend(_build_widgets()) + _AREA.append(area) + GLib.timeout_add(1000, _tick) + window.show_all() + Gtk.main() + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/examples/xmonitorbutton.py b/examples/xmonitorbutton.py new file mode 100644 index 0000000..936be40 --- /dev/null +++ b/examples/xmonitorbutton.py @@ -0,0 +1,193 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 xmonitorbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'XMonitorButton example' (232x80) with 1 widget(s). + +import math +import sys + +import gi + +gi.require_version("Gtk", "3.0") +gi.require_version("Gdk", "3.0") + +from gi.repository import Gdk, GLib, Gtk # noqa: E402 +from lightgui.xframe.core import create, parse_color # noqa: E402 + + +def on_XMonitorButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('XMonitorButton_001', 'OnClick'): on_XMonitorButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'XMonitorButton_001': (True, True, False, False, 16, 16, 200, 48), +} + + +def _apply_anchors(widgets, width, height): + """Reposition anchored widgets for a drawing area of + *width* x *height* (Flag 1: minimal anchor layout). + Computed from the design geometry every time, so a + resize is idempotent — no drift across resizes.""" + design_w, design_h = _DESIGN_SIZE + dx = width - design_w + dy = height - design_h + for widget in widgets: + top, left, right, bottom, ox, oy, ow, oh = \ + _ANCHORS[widget.name] + x, y, w, h = ox, oy, ow, oh + if right and not left: + x = ox + dx + elif right and left: + w = ow + dx + if bottom and not top: + y = oy + dy + elif bottom and top: + h = oh + dy + widget.move_to(x, y) + widget.resize_to(w, h) + +def _build_widgets(): + """Create every placed widget with its props.""" + widgets = [] + widget = create('XMonitorButton', 'XMonitorButton_001', 16, 16, 200, 48) + widget.set_prop("label", "SSH :22") + widget.set_prop("check-type", "Port") + widget.set_prop("target", "22") + # Live data: poll the check now; the timer re-polls. + widget.poll() + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (232, 80) +_BACKGROUND = '' +_WIDGETS = [] +_LAST_SIZE = [None] +_PRESSED = [None] # Task 4: widget that got the ButtonPress + + +def _hit_widget(widgets, x, y): + """Topmost visible widget containing (*x*, *y*).""" + for widget in reversed(widgets): + if widget.get_prop("visible", "true") in ("false", "0"): + continue + if widget.contains(x, y): + return widget + return None + + +def _dispatch_to(widget, event): + """Call the *event* handler of *widget* (already chosen).""" + if widget is None: + return + if widget.get_prop("enabled", "true") in ("false", "0"): + return + handler = _HANDLERS.get((widget.name, event)) + if handler is not None: + handler(widget) + + +def _dispatch(widgets, x, y, event): + """Call the *event* handler of the widget at (*x*, *y*), + if it has one wired.""" + _dispatch_to(_hit_widget(widgets, x, y), event) + + +def _on_draw(area, cr): + bg = parse_color(_BACKGROUND, None) + if bg is None: + bg = (1.0, 1.0, 1.0) + cr.set_source_rgb(*bg) + cr.paint() + for widget in _WIDGETS: + widget.draw(cr) + return False + + +def _on_button_press(area, event): + if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnDblClick") + elif event.type == Gdk.EventType.BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnMouseDown") + _dispatch(_WIDGETS, event.x, event.y, "OnClick") + # Task 4: remember the pressed widget; release goes there. + _PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y) + return False + + +def _on_button_release(area, event): + # Task 4: release goes to the widget that got the press, + # even if the pointer moved off it. + widget = _PRESSED[0] + _PRESSED[0] = None + _dispatch_to(widget, "OnMouseUp") + return False + + +def _on_area_resize(area, allocation): + size = (allocation.width, allocation.height) + if size == _LAST_SIZE[0]: + return False + _LAST_SIZE[0] = size + _apply_anchors(_WIDGETS, size[0], size[1]) + area.queue_draw() + return False + + +# --- live demo (hand-added, not designer codegen) ---------------------------- +# The widget shows live data: _tick() feeds a new sample +# every 2000 ms and redraws the drawing area. _TICK starts +# at 60 so the timer continues the seeded sine waves +# from _build_widgets() without a phase jump. +_TICK = [60] +_AREA = [] + + +def _tick(): + _TICK[0] += 1 + t = _TICK[0] + widget = _WIDGETS[0] + widget.poll() + if _AREA: + _AREA[0].queue_draw() + return True + +def main(): + window = Gtk.Window(title='XMonitorButton example') + window.set_default_size(232, 80) + window.connect("destroy", Gtk.main_quit) + vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, + spacing=0) + window.add(vbox) + area = Gtk.DrawingArea() + area.set_hexpand(True) + area.set_vexpand(True) + area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | + Gdk.EventMask.BUTTON_RELEASE_MASK) + area.connect("draw", _on_draw) + area.connect("button-press-event", _on_button_press) + area.connect("button-release-event", _on_button_release) + area.connect("size-allocate", _on_area_resize) + vbox.pack_start(area, True, True, 0) + _WIDGETS.extend(_build_widgets()) + _AREA.append(area) + GLib.timeout_add(2000, _tick) + window.show_all() + Gtk.main() + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/examples/xmonitorgrid.py b/examples/xmonitorgrid.py new file mode 100644 index 0000000..fb332f6 --- /dev/null +++ b/examples/xmonitorgrid.py @@ -0,0 +1,196 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 xmonitorgrid.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'XMonitorGrid example' (352x232) with 1 widget(s). + +import math +import sys + +import gi + +gi.require_version("Gtk", "3.0") +gi.require_version("Gdk", "3.0") + +from gi.repository import Gdk, GLib, Gtk # noqa: E402 +from lightgui.xframe.core import create, parse_color # noqa: E402 + + +def on_XMonitorGrid_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('XMonitorGrid_001', 'OnClick'): on_XMonitorGrid_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'XMonitorGrid_001': (True, True, False, False, 16, 16, 320, 200), +} + + +def _apply_anchors(widgets, width, height): + """Reposition anchored widgets for a drawing area of + *width* x *height* (Flag 1: minimal anchor layout). + Computed from the design geometry every time, so a + resize is idempotent — no drift across resizes.""" + design_w, design_h = _DESIGN_SIZE + dx = width - design_w + dy = height - design_h + for widget in widgets: + top, left, right, bottom, ox, oy, ow, oh = \ + _ANCHORS[widget.name] + x, y, w, h = ox, oy, ow, oh + if right and not left: + x = ox + dx + elif right and left: + w = ow + dx + if bottom and not top: + y = oy + dy + elif bottom and top: + h = oh + dy + widget.move_to(x, y) + widget.resize_to(w, h) + +def _build_widgets(): + """Create every placed widget with its props.""" + widgets = [] + widget = create('XMonitorGrid', 'XMonitorGrid_001', 16, 16, 320, 200) + widget.set_prop("label", "Services") + # Four live monitors: process, systemd unit, open port, + # closed port (shows the red/down state). + widget.add_monitor("process", "python3", "Python") + widget.add_monitor("unit", "sshd", "sshd unit") + widget.add_monitor("port", "22", "SSH :22") + widget.add_monitor("port", "9999", "Port 9999") + widget.poll_all() + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (352, 232) +_BACKGROUND = '' +_WIDGETS = [] +_LAST_SIZE = [None] +_PRESSED = [None] # Task 4: widget that got the ButtonPress + + +def _hit_widget(widgets, x, y): + """Topmost visible widget containing (*x*, *y*).""" + for widget in reversed(widgets): + if widget.get_prop("visible", "true") in ("false", "0"): + continue + if widget.contains(x, y): + return widget + return None + + +def _dispatch_to(widget, event): + """Call the *event* handler of *widget* (already chosen).""" + if widget is None: + return + if widget.get_prop("enabled", "true") in ("false", "0"): + return + handler = _HANDLERS.get((widget.name, event)) + if handler is not None: + handler(widget) + + +def _dispatch(widgets, x, y, event): + """Call the *event* handler of the widget at (*x*, *y*), + if it has one wired.""" + _dispatch_to(_hit_widget(widgets, x, y), event) + + +def _on_draw(area, cr): + bg = parse_color(_BACKGROUND, None) + if bg is None: + bg = (1.0, 1.0, 1.0) + cr.set_source_rgb(*bg) + cr.paint() + for widget in _WIDGETS: + widget.draw(cr) + return False + + +def _on_button_press(area, event): + if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnDblClick") + elif event.type == Gdk.EventType.BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnMouseDown") + _dispatch(_WIDGETS, event.x, event.y, "OnClick") + # Task 4: remember the pressed widget; release goes there. + _PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y) + return False + + +def _on_button_release(area, event): + # Task 4: release goes to the widget that got the press, + # even if the pointer moved off it. + widget = _PRESSED[0] + _PRESSED[0] = None + _dispatch_to(widget, "OnMouseUp") + return False + + +def _on_area_resize(area, allocation): + size = (allocation.width, allocation.height) + if size == _LAST_SIZE[0]: + return False + _LAST_SIZE[0] = size + _apply_anchors(_WIDGETS, size[0], size[1]) + area.queue_draw() + return False + + +# --- live demo (hand-added, not designer codegen) ---------------------------- +# The widget shows live data: _tick() feeds a new sample +# every 2000 ms and redraws the drawing area. _TICK starts +# at 60 so the timer continues the seeded sine waves +# from _build_widgets() without a phase jump. +_TICK = [60] +_AREA = [] + + +def _tick(): + _TICK[0] += 1 + t = _TICK[0] + widget = _WIDGETS[0] + widget.poll_all() + if _AREA: + _AREA[0].queue_draw() + return True + +def main(): + window = Gtk.Window(title='XMonitorGrid example') + window.set_default_size(352, 232) + window.connect("destroy", Gtk.main_quit) + vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, + spacing=0) + window.add(vbox) + area = Gtk.DrawingArea() + area.set_hexpand(True) + area.set_vexpand(True) + area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | + Gdk.EventMask.BUTTON_RELEASE_MASK) + area.connect("draw", _on_draw) + area.connect("button-press-event", _on_button_press) + area.connect("button-release-event", _on_button_release) + area.connect("size-allocate", _on_area_resize) + vbox.pack_start(area, True, True, 0) + _WIDGETS.extend(_build_widgets()) + _AREA.append(area) + GLib.timeout_add(2000, _tick) + window.show_all() + Gtk.main() + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/examples/xprocesslist.py b/examples/xprocesslist.py new file mode 100644 index 0000000..ab55edc --- /dev/null +++ b/examples/xprocesslist.py @@ -0,0 +1,191 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 xprocesslist.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'XProcessList example' (392x252) with 1 widget(s). + +import math +import sys + +import gi + +gi.require_version("Gtk", "3.0") +gi.require_version("Gdk", "3.0") + +from gi.repository import Gdk, GLib, Gtk # noqa: E402 +from lightgui.xframe.core import create, parse_color # noqa: E402 + + +def on_XProcessList_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('XProcessList_001', 'OnClick'): on_XProcessList_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'XProcessList_001': (True, True, False, False, 16, 16, 360, 220), +} + + +def _apply_anchors(widgets, width, height): + """Reposition anchored widgets for a drawing area of + *width* x *height* (Flag 1: minimal anchor layout). + Computed from the design geometry every time, so a + resize is idempotent — no drift across resizes.""" + design_w, design_h = _DESIGN_SIZE + dx = width - design_w + dy = height - design_h + for widget in widgets: + top, left, right, bottom, ox, oy, ow, oh = \ + _ANCHORS[widget.name] + x, y, w, h = ox, oy, ow, oh + if right and not left: + x = ox + dx + elif right and left: + w = ow + dx + if bottom and not top: + y = oy + dy + elif bottom and top: + h = oh + dy + widget.move_to(x, y) + widget.resize_to(w, h) + +def _build_widgets(): + """Create every placed widget with its props.""" + widgets = [] + widget = create('XProcessList', 'XProcessList_001', 16, 16, 360, 220) + widget.set_prop("label", "Processes") + # Live data: read /proc now; the timer refreshes it. + widget.refresh_processes() + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (392, 252) +_BACKGROUND = '' +_WIDGETS = [] +_LAST_SIZE = [None] +_PRESSED = [None] # Task 4: widget that got the ButtonPress + + +def _hit_widget(widgets, x, y): + """Topmost visible widget containing (*x*, *y*).""" + for widget in reversed(widgets): + if widget.get_prop("visible", "true") in ("false", "0"): + continue + if widget.contains(x, y): + return widget + return None + + +def _dispatch_to(widget, event): + """Call the *event* handler of *widget* (already chosen).""" + if widget is None: + return + if widget.get_prop("enabled", "true") in ("false", "0"): + return + handler = _HANDLERS.get((widget.name, event)) + if handler is not None: + handler(widget) + + +def _dispatch(widgets, x, y, event): + """Call the *event* handler of the widget at (*x*, *y*), + if it has one wired.""" + _dispatch_to(_hit_widget(widgets, x, y), event) + + +def _on_draw(area, cr): + bg = parse_color(_BACKGROUND, None) + if bg is None: + bg = (1.0, 1.0, 1.0) + cr.set_source_rgb(*bg) + cr.paint() + for widget in _WIDGETS: + widget.draw(cr) + return False + + +def _on_button_press(area, event): + if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnDblClick") + elif event.type == Gdk.EventType.BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnMouseDown") + _dispatch(_WIDGETS, event.x, event.y, "OnClick") + # Task 4: remember the pressed widget; release goes there. + _PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y) + return False + + +def _on_button_release(area, event): + # Task 4: release goes to the widget that got the press, + # even if the pointer moved off it. + widget = _PRESSED[0] + _PRESSED[0] = None + _dispatch_to(widget, "OnMouseUp") + return False + + +def _on_area_resize(area, allocation): + size = (allocation.width, allocation.height) + if size == _LAST_SIZE[0]: + return False + _LAST_SIZE[0] = size + _apply_anchors(_WIDGETS, size[0], size[1]) + area.queue_draw() + return False + + +# --- live demo (hand-added, not designer codegen) ---------------------------- +# The widget shows live data: _tick() feeds a new sample +# every 1000 ms and redraws the drawing area. _TICK starts +# at 60 so the timer continues the seeded sine waves +# from _build_widgets() without a phase jump. +_TICK = [60] +_AREA = [] + + +def _tick(): + _TICK[0] += 1 + t = _TICK[0] + widget = _WIDGETS[0] + widget.refresh_processes() + if _AREA: + _AREA[0].queue_draw() + return True + +def main(): + window = Gtk.Window(title='XProcessList example') + window.set_default_size(392, 252) + window.connect("destroy", Gtk.main_quit) + vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, + spacing=0) + window.add(vbox) + area = Gtk.DrawingArea() + area.set_hexpand(True) + area.set_vexpand(True) + area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | + Gdk.EventMask.BUTTON_RELEASE_MASK) + area.connect("draw", _on_draw) + area.connect("button-press-event", _on_button_press) + area.connect("button-release-event", _on_button_release) + area.connect("size-allocate", _on_area_resize) + vbox.pack_start(area, True, True, 0) + _WIDGETS.extend(_build_widgets()) + _AREA.append(area) + GLib.timeout_add(1000, _tick) + window.show_all() + Gtk.main() + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/examples/xsysbox.py b/examples/xsysbox.py new file mode 100644 index 0000000..08cbfc8 --- /dev/null +++ b/examples/xsysbox.py @@ -0,0 +1,196 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 xsysbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'XSysBox example' (352x232) with 1 widget(s). + +import math +import sys + +import gi + +gi.require_version("Gtk", "3.0") +gi.require_version("Gdk", "3.0") + +from gi.repository import Gdk, GLib, Gtk # noqa: E402 +from lightgui.xframe.core import create, parse_color # noqa: E402 + + +def on_XSysBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('XSysBox_001', 'OnClick'): on_XSysBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'XSysBox_001': (True, True, False, False, 16, 16, 320, 200), +} + + +def _apply_anchors(widgets, width, height): + """Reposition anchored widgets for a drawing area of + *width* x *height* (Flag 1: minimal anchor layout). + Computed from the design geometry every time, so a + resize is idempotent — no drift across resizes.""" + design_w, design_h = _DESIGN_SIZE + dx = width - design_w + dy = height - design_h + for widget in widgets: + top, left, right, bottom, ox, oy, ow, oh = \ + _ANCHORS[widget.name] + x, y, w, h = ox, oy, ow, oh + if right and not left: + x = ox + dx + elif right and left: + w = ow + dx + if bottom and not top: + y = oy + dy + elif bottom and top: + h = oh + dy + widget.move_to(x, y) + widget.resize_to(w, h) + +def _build_widgets(): + """Create every placed widget with its props.""" + widgets = [] + widget = create('XSysBox', 'XSysBox_001', 16, 16, 320, 200) + widget.set_prop("label", "CPU") + widget.set_prop("value", "43") + widget.set_prop("unit", "%") + # Seeded demo data: 60 samples so the graph is full. + for i in range(60): + widget.push(55 + 25 * math.sin(i / 6.0)) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (352, 232) +_BACKGROUND = '' +_WIDGETS = [] +_LAST_SIZE = [None] +_PRESSED = [None] # Task 4: widget that got the ButtonPress + + +def _hit_widget(widgets, x, y): + """Topmost visible widget containing (*x*, *y*).""" + for widget in reversed(widgets): + if widget.get_prop("visible", "true") in ("false", "0"): + continue + if widget.contains(x, y): + return widget + return None + + +def _dispatch_to(widget, event): + """Call the *event* handler of *widget* (already chosen).""" + if widget is None: + return + if widget.get_prop("enabled", "true") in ("false", "0"): + return + handler = _HANDLERS.get((widget.name, event)) + if handler is not None: + handler(widget) + + +def _dispatch(widgets, x, y, event): + """Call the *event* handler of the widget at (*x*, *y*), + if it has one wired.""" + _dispatch_to(_hit_widget(widgets, x, y), event) + + +def _on_draw(area, cr): + bg = parse_color(_BACKGROUND, None) + if bg is None: + bg = (1.0, 1.0, 1.0) + cr.set_source_rgb(*bg) + cr.paint() + for widget in _WIDGETS: + widget.draw(cr) + return False + + +def _on_button_press(area, event): + if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnDblClick") + elif event.type == Gdk.EventType.BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnMouseDown") + _dispatch(_WIDGETS, event.x, event.y, "OnClick") + # Task 4: remember the pressed widget; release goes there. + _PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y) + return False + + +def _on_button_release(area, event): + # Task 4: release goes to the widget that got the press, + # even if the pointer moved off it. + widget = _PRESSED[0] + _PRESSED[0] = None + _dispatch_to(widget, "OnMouseUp") + return False + + +def _on_area_resize(area, allocation): + size = (allocation.width, allocation.height) + if size == _LAST_SIZE[0]: + return False + _LAST_SIZE[0] = size + _apply_anchors(_WIDGETS, size[0], size[1]) + area.queue_draw() + return False + + +# --- live demo (hand-added, not designer codegen) ---------------------------- +# The widget shows live data: _tick() feeds a new sample +# every 1000 ms and redraws the drawing area. _TICK starts +# at 60 so the timer continues the seeded sine waves +# from _build_widgets() without a phase jump. +_TICK = [60] +_AREA = [] + + +def _tick(): + _TICK[0] += 1 + t = _TICK[0] + widget = _WIDGETS[0] + v = 55 + 25 * math.sin(t / 6.0) + widget.push(v) + widget.set_value(v) + if _AREA: + _AREA[0].queue_draw() + return True + +def main(): + window = Gtk.Window(title='XSysBox example') + window.set_default_size(352, 232) + window.connect("destroy", Gtk.main_quit) + vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, + spacing=0) + window.add(vbox) + area = Gtk.DrawingArea() + area.set_hexpand(True) + area.set_vexpand(True) + area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | + Gdk.EventMask.BUTTON_RELEASE_MASK) + area.connect("draw", _on_draw) + area.connect("button-press-event", _on_button_press) + area.connect("button-release-event", _on_button_release) + area.connect("size-allocate", _on_area_resize) + vbox.pack_start(area, True, True, 0) + _WIDGETS.extend(_build_widgets()) + _AREA.append(area) + GLib.timeout_add(1000, _tick) + window.show_all() + Gtk.main() + return 0 + + +if __name__ == "__main__": + sys.exit(main())