You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Navigation Menu
Copilot
Copilot navigation
Copilot Chat
GTK4 event handling code adaptation
You said: The event management code in https://github.com/HinTak/skia-python-examples/blob/main/SkiaGTK4_AdvancedGL.py on
The event management code in https://github.com/HinTak/skia-python-examples/blob/main/SkiaGTK4_AdvancedGL.py on line 52 to 61 is wrong. Can you change them into their GTK4 corresponding apis?
2/2
1 reference
Copilot said: Here’s what needs to be changed for
Here’s what needs to be changed for GTK4 event handling in the code block from line 52 to 61. In GTK4, the event mask system and event signals for widgets like Gtk.GLArea have changed:
add_events() is deprecated/removed in GTK4; event masks are set differently or are simply not needed—the widget is expected to be “event controller” driven.
Mouse and key events are handled via GtkEventController (for mouse: Gtk.EventControllerMotion and Gtk.GestureClick; for keyboard: Gtk.EventControllerKey).
Below is the correct replacement for lines 52-61, using GTK4 idioms. This will move the event handling setup to use GTK4 event controllers and gestures:
SkiaGTK4_AdvancedGL.py
v1
52| # Setup event controllers for GTK4
53| self.motion_controller = Gtk.EventControllerMotion.new(self)
54| self.motion_controller.connect("motion", self.on_motion_notify)
55|
56| self.click_gesture = Gtk.GestureClick.new(self)
57| self.click_gesture.connect("pressed", self.on_button_press)
You’ll also need to update the signatures of the event handler methods to match the new controller APIs:
on_motion_notify(self, motion_controller, x, y) (instead of (widget, event))
on_button_press(self, gesture, n_press, x, y) and on_button_release(self, gesture, n_press, x, y)
on_key_press(self, key_controller, keyval, keycode, state)
Summary of changes:
Remove add_events() and all old-style connect("event-type", ...) calls.
Use GTK4 event controllers for mouse/keyboard events.
Would you like a full code patch, including the updated event handler function signatures and logic?
You said: Yes, i want a full code path
Yes, i want a full code path
Copilot said: ```python name=SkiaGTK4_AdvancedGL.py #!/usr/bin/env python3 # Skia + Gtk4 +
SkiaGTK4_AdvancedGL.py
v2
#!/usr/bin/env python3
# Skia + Gtk4 + OpenGL (hardware accelerated, advanced GPU features) example
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk, Gdk, GLib
Summary of changes:
Old event mask and signal-based event handling (lines 52-61) are replaced with GTK4 event controllers (Gtk.EventControllerMotion, Gtk.GestureClick, Gtk.EventControllerKey).
The handler methods are updated to match the new controller signatures.
The logic inside each handler is adapted as needed to work with the new API (e.g., using gesture.get_current_button()).
This patch will make your event handling fully compatible with GTK4.
You said: Can you improve https://github.com/HinTak/skia-python-examples/blob/main/SkiaGTKGLExample.py ? It seems
Can you improve https://github.com/HinTak/skia-python-examples/blob/main/SkiaGTKGLExample.py ? It seems to be refreshing excessively.
1 reference
Copilot said: The excessive refreshing in this code is
The excessive refreshing in this code is mainly due to the line in on_realize:
Python
GLib.timeout_add(16, self.queue_draw) # ~60fps
This causes the widget to redraw at ~60fps regardless of whether something actually changed.
Improvement suggestion:
Only trigger redraws (queue_draw) when something visual actually changes (e.g., star rotation, mouse drag, or new rect).
Move the animation timer (GLib.timeout_add) to only run while the star is rotating or a drag is happening.
When idle (no animation, no drag), stop the timer to avoid unnecessary draws.
Below is an improved version of the file that only refreshes when needed.
SkiaGTKGLExample.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GObject, GLib
Key improvements:
The animation timer is not always running; it's started only when the star needs to animate and stopped when not needed.
Mouse drag events and static UI do not trigger continuous redraws—only queue a draw as needed.
The UI is now much more efficient and should not refresh excessively.
Let me know if you want further refinements (e.g., only animating star rotation on user input)!
0 commit comments