Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On GTK, OptionContainer current_tab returns wrong index #2703

Closed
steve725 opened this issue Jul 10, 2024 · 7 comments · Fixed by #2713
Closed

On GTK, OptionContainer current_tab returns wrong index #2703

steve725 opened this issue Jul 10, 2024 · 7 comments · Fixed by #2713
Labels
bug A crash or error in behavior. linux The issue relates Linux support.

Comments

@steve725
Copy link

Describe the bug

The property "current_tab" of an OptionContainer-Object returns the wrong index of the current tab.
This happens because the first click on a tab is obviously not registered.
As a consequence the previous index is returned.

Steps to reproduce

  1. Go to 'examples/optioncontainer/optioncontainer'
  2. Run 'python app.py' or 'python -m app'
  3. Click on one of the three tabs ('Option 0, Option 1, Option 2) of the OptionContainer
  4. See output above the tabs

Expected behavior

OptionContainer.current_tab.index should return the current index of the clicked tab not the previous index

Screenshots

No response

Environment

  • Operating System: OpenSuse Tumbleweed 20240704; KDE Plasma 6.1.2
  • Python version: 3.12.4 (miniforge; conda version : 23.11.0)
  • Software versions:
    • Briefcase:
    • Toga: 0.4.5

Logs


Additional context

No response

@steve725 steve725 added the bug A crash or error in behavior. label Jul 10, 2024
@mhsmith mhsmith changed the title OptionContainer current_tab returns wrong index On GTK, OptionContainer current_tab returns wrong index Jul 13, 2024
@mhsmith mhsmith added the linux The issue relates Linux support. label Jul 13, 2024
@mhsmith
Copy link
Member

mhsmith commented Jul 13, 2024

I can't reproduce this with the Toga main branch on Cocoa, so I assume it's specific to GTK. But I can't currently run Toga on GTK because my Linux distribution is too old. @freakboy3742: Can you reproduce it on GTK?

@steve725
Copy link
Author

Yes, it seems to be a GTK specific issue, on Windows the OptionContainer works correct.
Maybe it's due to that I didn't create a MainWindow in my app and therefore Gtk.Notebook() [toga-main/gtk/src/toga_gtk/widgets/optioncontainer.py] does not show at startup?
I don't use a MainWindow because I want to avoid the native menu on Linux and Windows.

@mhsmith
Copy link
Member

mhsmith commented Jul 13, 2024

Maybe it's due to that I didn't create a MainWindow in my app

You said it happens with examples/optioncontainer, which does create a MainWindow.

@steve725
Copy link
Author

I'm using toga 0.4.5 so I adapted the example using only the Window-class instead of MainWindow.
Trying to install the main-branch always gives me an error:

pip install https://github.com/beeware/toga/archive/refs/heads/main.zip -> error: Multiple top-level packages discovered in a flat-layout: ['gtk', 'iOS', 'web', 'core', 'demo', 'toga', 'cocoa', 'dummy', 'android', 'changes', 'testbed', 'textual', 'winforms']

Maybe I need to change pyproject.toml.

@mhsmith
Copy link
Member

mhsmith commented Jul 14, 2024

Toga 0.4.5 is the current stable version, which requires every app to have a MainWindow. We've removed this restriction on the main branch. To use it, edit your pyproject.toml file to replace thetoga-gtk line with the following:

"git+https://github.com/beeware/toga#subdirectory=core",
"git+https://github.com/beeware/toga#subdirectory=gtk",

And similarly for the other toga packages if you want to run your app on other platforms.

I think it's unlikely that the window type will make any difference to the OptionContainer behavior, but it would be easy enough for you to check.

@steve725
Copy link
Author

I checked the example with the code of the main branch and the problem still remains. So you are right: the OptionContainer behavior does not depend on the window type.
Maybe someone else can reproduce the wrong behavior on a Linux system.

@freakboy3742
Copy link
Member

I can confirm I'm seeing this on GTK with main - the callback appears to be invoked with the "old" index, not the newly selected index.

The issue is event timing. When the GTK switch-page event is fired, it provided the index of the newly selected page; but whatever is returned by current_page() hasn't been updated yet. As a result, the on_select handler of the example program is returning the "old" tab index.

Unfortunately, there doesn't appear to be a "switch has completed" event - the only event that seems to do anything in response to a tab change is "switch-page".

One workaround is to defer the firing of the on_select event; if that event is put onto the event loop, rather than firing immediately, the "switch-page" event completes, and current_page() returns the updated index.

Another workaround would be to avoid current_page() entirely, and internally reproduce that functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A crash or error in behavior. linux The issue relates Linux support.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants