Repository navigation
Add GTK4 TextInput support - #3239
Conversation
|
It looks like the test cleanup errors are reproducible on CI. Things I have tried so far:
I also tried other combinations of this, by forcing a destroy with
I'm beginning to think that this is more deeply rooted in GTK4's internal reference management. |
That's certainly possible - at which point it's a little outside our control, so skips may be our only option. Two related things that might be worth trying: First - duplicate the If that doesn't work - use
The check is being performed against |
That didn't have an impact, a second gc.collect() returns 0 so it didn't find any more objects.
Duh, of course, thanks! I created a test of GTK4 by itself, and unfortunately I wasn't able to reproduce the reference counting issue. https://gist.github.com/danyeaw/6cd58338753fd8fbb8360dcad0bc891c |
|
Ok - I've done some poking around, and I've found the source of the problem. Since we were getting the GTK widget impl on the list of referents, I figured it might be useful to look at that object's referents. Most of the referents to the GTK impl are of the form As an experiment, I modified the cleanup test so that it would explicitly remove the focus controller and key controller from the GTK impl before performing the GC pass (i.e., This is exactly the same logic that you tried earlier in a This hasn't historically been an issue with handlers on the GTK backend - I'm guessing the extra level of indirection introduced by the controller is making the GC loop impossible to resolve. As for how to fix this - there's a slightly analogous situation on the Winforms backend. Handlers need to be passed into Python.net, at which point the references become opaque to the Python GC, so the object can't be disposed. The solution there is to use So - I'm guessing the fix here is that we need to promote WeakrefCallable from a winforms utility to a core utility ( |
|
Hi @freakboy3742, thanks so much for the help with the investigation! I updated the PR with that approach. |
freakboy3742
left a comment
There was a problem hiding this comment.
So - the code for all this looks great... but when I looked at the tests on GTK4, it looks like they're all skipped, except for the cleanup test. I can reproduce the same thing locally - AFAICT, the only test that should be skipped (based on the probe definition) is the undo-redo test.
From the look of it, the problem is caused by the SimpleProbe base class, which is skipping all tests on GTK4. Uncommenting that skip then releases a cascade of errors...
|
We now have 80% of the tests passing which good. The I also decided to skip the tests simulating a keypress by the user, this is going to require a new approach for GTK4 due to the changes in events and it seemed like a containable part for another contribution. |
🎉
This is something we've seen before; it's usually an indication that the programmatic change is triggering a signal, but but then that change is altering the value of the widget, which is also detected and reported as a "change in value". However, from a quick look at the code, I agree it's not obvious why GTK4 would be firing twice here. Does raw GTK4 usage fire 2 signals on a
Sure - that makes sense. We don't need to solve every problem at once. One issue though - CI looks like it's stalling after the |
|
I created a minimum reproducible example with a PyGObject app and it is an upstream issue with GTK4. If you have text in an Entry, and then do a I have some additional tests to skip or fix, hopefully I can get that finished up soon. |
5289ae8 to
70ed79a
Compare
|
As you mentioned, tests are hanging in our CI environment (Ubuntu 24.04) but work fine for me locally (openSUSE Tumbleweed). All the problematic tests use probe.redraw() to wait for GTK to process events. I used the tmate runner to try to debug this for almost 3 hours. 🙃 On the test suite freezing, it shows the frozen test when I hit Ctrl+C, for example: Examples of the initial tests that were hanging include: Running with In GTK4 on Ubuntu 24.04, the event processing appears to behave differently:
I have added some logic to the redraw that xfails the test after a waiting period, which was working on the runner when I tmated in, but now it looks like the runner is stuck again. I will try to also run this on Distrobox with Ubuntu 24.04 locally to see if I can reproduce the redraw errors. |
One thing to dig into here is asyncio exceptions. Consider the following:
This is, unfortunately, I suspect this may be what is happening in your test. The testbed suite uses pytest-async so that test cases and fixtures can be async; If something is crashing during setup or teardown of the test, the async task will also be stopping without displaying the error. But because the entire testbed test suite is async... the controlling task awaiting that result will also crash, and eventually, the test suite stops. But it doesn't exit, because the event loop is still running. It presents as the test suite hanging... but it's actually a complete crash of the test suite with no errors displayed. I've seen similar presentations in the past - complex dialog behaviors where you work out you're calling a method on a None object (or similar), and the test locks up as a result. I have no idea what could be causing the problem here, but it at least seems plausible as a cause. |
ebbdc4a to
5de3cee
Compare
|
I was not able to reproduce the test failures using Ubuntu 24.04 locally. However, I was able to fix the freezing on CI by reworking the |
|
I can now reproduce the GitHub Actions failures locally, so some progress. Here are the steps:
More work to do to figure out why changes to flex styling aren't propagating correctly when running the full testbed. |
|
More troubleshooting:
|
|
I worked on this over the last couple of days at the PyCon 2025 sprints, and also did some troubleshooting in person with @freakboy3742 (thanks Russ!). During the set-up of the test, a Box is created on the Window and the TextInput is added to it. Then it runs |
| """ | ||
| A wrapper for callable that holds a weak reference to it. | ||
|
|
||
| This can be useful in particular when setting winforms event handlers, to avoid |
There was a problem hiding this comment.
| This can be useful in particular when setting winforms event handlers, to avoid | |
| This can be useful in particular when setting WinForms event handlers, to avoid |
While we're tweaking WeakrefCallable, might as well fix the case for WinForms.
[Sidenote: iOS Testbed failure is unrelated.
freakboy3742
left a comment
There was a problem hiding this comment.
Nice work getting the tests passing - that's clearly a non-trivial set of changes around widget rendering.
I think the mechanics of this all make sense. I've flagged a couple of things inline; Id don't think any of them are necessarily problems - they're likely just places where some more inline documentation is needed to clarify why things are they way they are.
I've noticed two additional things in the process of reviewing. They're both pre-existing problems, so they don't need to be blockers on merging this PR; but I wanted to make sure they're on our radar.
Both of them are revealed by running tutorial 1.
If you run tutorial 1 with GTK4, all the widgets (including the text inputs) are now present and work as expected. However, the "calculate" button is the wrong size, and doesn't have any text. This looks like it's a pre-existing problem; but it's obviously a fairly big one, and one that is being ignored because almost all the button tests for GTK4 are being skipped.
The other problem is the number of DeprecationWarnings that are being raised for style handling. The APIs are obviously still working, but we'll need to make sure we're ahead of these deprecations.
The only reason I'm mentioning these at all is the potential overlap between the widget sizing changes in this PR and Toga's container management. If we're doing anything in this PR that is going to cause us headaches later, then that might be worth considering or investigating a bit. However, I could also live with landing this PR as is, given that it's clearly a significant improvement.
| frame_clock.disconnect(handler_id) | ||
|
|
||
| # Process events to ensure the UI is fully updated | ||
| for _ in range(15): |
There was a problem hiding this comment.
Is 15 a magic number here? Is there any reason this isn't "until we have a pass with no events pending?"
| hasattr(self, "native") | ||
| and self.native | ||
| and hasattr(self.native, "queue_draw") | ||
| ): |
There was a problem hiding this comment.
Is this a subtle "if GTK4" alternative? I'm trying to work out if this exists, but wasn't needed for GTK3; or it's GTK4 only, but the protection is more about the probe having a "native"?
There was a problem hiding this comment.
It's added at 641e700cc which mean that it seems like only the last "queue_draw" line should matter and the rest are added for safety in that commit. I'm not sure what other circumstances might are, so I'll leave @danyeaw to answer this one (and the rest; I'm not going to get involved over any other review conversations on this round).
There was a problem hiding this comment.
This protects against:
- Probes without self.native (e.g., AppProbe, IconProbe, ImageProbe)
- Probes with self.native that doesn't support queue_draw() (e.g., ScreenProbe with X11Monitor)
I suppose we could also check that self.native is an instance of a Gtk.Widget.
There was a problem hiding this comment.
In that case -- we can move this logic into SimpleProbe which is what is done on other backends.
Refs danyeaw#5 -- should be resolved by that. After that PR, there's still the next 3 comments that need resolving only.
There was a problem hiding this comment.
Summarizing this whole probe redraw fiesta up (mainly caused by a bunch of messy changes made by me because I didn't have a way to test CI offline, sorry):
- This thing is not a "if GTK4" alternative. This is related to the new queue_draw approach which is used by GTK4 only -- it checks that queue_draw actually exists on that probe.
- I've pointed out that this could get moved into SimpleProbe -- however for whatever miraculous reason, if I copy the exact same code from these lines without changes into SimpleProbe, the tests stop passing on CI (the sizes returned aren't 0, but are very small random numbers that doesn't change much).
- Since this is just a test, I propose keeping the code in the current position (checking for self.native and existence of queue_draw in BaseProbe root class) from the last code review. However the other simplifications for redraw logic still applies.
|
Confirming the origins of the button issues:
Thus confirming that "button support for GTK4" can be something that can be independently done in a future PR since these issues are button-specific. I am unable to reproduce any style deprecation warning off of this branch or with a merge with main. @freakboy3742 Can you post any warnings you got? I'd like to investigate them further. I am able to find a third issue: the content does not respond to window resize (really apologize for neglecting to notice that when I asked danyeaw that this is able to get an upstream review). I'll try to fix it and PR against this PR removing return None for Gtk.Orientation (since I"m fairly certain it's not needed) and also this issue. Patch for Buttondiff --git a/gtk/src/toga_gtk/widgets/button.py b/gtk/src/toga_gtk/widgets/button.py
index 63026de08..33ad76c5d 100644
--- a/gtk/src/toga_gtk/widgets/button.py
+++ b/gtk/src/toga_gtk/widgets/button.py
@@ -12,6 +12,7 @@ class Button(Widget):
self.native.connect("clicked", self.gtk_clicked)
self._icon = None
+ self._label = self.native.get_child()
def get_text(self):
text = self.native.get_label()
@@ -21,6 +22,7 @@ class Button(Widget):
def set_text(self, text):
self.native.set_label(text)
+ self._label = self.native.get_child()
def get_icon(self):
return self._icon
@@ -42,7 +44,7 @@ class Button(Widget):
text = self.native.get_label()
if text:
self.native.set_label(text)
- self.native.set_child(None)
+ self.native.set_child(self._label)
def set_enabled(self, value):
self.native.set_sensitive(value)
@@ -65,7 +67,16 @@ class Button(Widget):
self.interface.intrinsic.width = at_least(width[0])
self.interface.intrinsic.height = height[1]
else: # pragma: no-cover-if-gtk3
- pass
+ # print(
+ # "REHINT",
+ # self,
+ # self.native.get_preferred_size()[0].width,
+ # self.native.get_preferred_size()[0].height,
+ # )
+ min_size, size = self.native.get_preferred_size()
+
+ self.interface.intrinsic.width = at_least(min_size.width)
+ self.interface.intrinsic.height = size.height
def gtk_clicked(self, event):
self.interface.on_press() |
|
@danyeaw I've found the source of the test failures -- if we don't put the Gtk.events_pending call or something like that in a separate method, it'll fail to detect any pending events -- I suspect it's a timing issue. This can be fixed by using the GTK4 logic also for GTK3-- none of the APIs changed. I'll try to send a PR soon (with an additional unrelated fix for present() not working on old GTK3 + Wayland in the focus tests). EDIT -- from further investigations, looks like the behavior difference is from polymorphism -- SimpleProbe changes the behaviro for repaint_needed to detect if container finished redrawing -- however, I argue that using GTK4 solution with frameclock is cleaner than using inherited function to check the internal state of Container [edit] and keep calling GTK to pump events even where there are none pending just to wait for the redraw phase [end edit]. |
Fixup test redraw; unrelated fix for GTK3
This reverts commit 2f773e1.
Remove more of unused repaint_needed, move the code back
This is what I get when I run the testbed under Ubuntu 24.04.1 (GTK 4.14.5), Python3.12, after running Some of those are expected - StatusIcons aren't implemented, for example - but the style related ones all seem problematic. |
…, minor code simplify (#7) * try not surpress to see what the heck it's for * experiment to remove ancestor check * add comment, revert experiment * Apply suggestion from @johnzhou721
freakboy3742
left a comment
There was a problem hiding this comment.
Since this is now passing CI, and has a number of other improvements, I think it's worth landing, and treating any other issues as follow ups.
Thanks for the persistence @danyeaw, and to @johnzhou721 for the assistance on resolving the final testing issues.
|
Thanks @freakboy3742 - and thanks again @johnzhou721 for helping to get the CI failures figured out. |
|
Congrats to all the work y'all have done! In addition to all the above: Thanks to @MuhammadMuradG for getting the initial set of changes -- we're now officially hitting the ground running! I'll start opening PRs to fix the Button issues and the other things mentioned in this PR. |
Adds TextInput widget for GTK4 as part of #3069.
I'm getting a local test cleanup failure when trying to check that the weakref is None that I haven't been able to resolve.
PR Checklist: