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

Add Window.focused #3079

Merged
merged 3 commits into from
Aug 27, 2024
Merged

Add Window.focused #3079

merged 3 commits into from
Aug 27, 2024

Conversation

damusss
Copy link
Contributor

@damusss damusss commented Aug 22, 2024

stubs, docs, test included.

Can it be done with events?

Yes. but you need to keep an extra variable and it's more annoying. If there is a simpler way, why not have it?

Is this different from the other focus-related api?

Yes. Other functions such as event.get_grab, mouse/key.get_focused return True if the state is true for any open window. This attribute only returns true for the current Window object, which is important if you need to redirect interactions to one window only (for example, get_pos returns the relative pos of the current window, so you need a way to decide which window can use the get_pos or get_pressed results. I have a real life example if needed)

Why Window.focused?

I am only following the attribute convention of Window such as mouse_grabbed, keyboard_grabbed.

Test code

import pygame

pygame.init()
win = pygame.Window("focused test", (500, 500))

while True:
    for e in pygame.event.get():
        if e.type == pygame.WINDOWCLOSE and e.window == win:
            pygame.quit()
            raise SystemExit

    print(win.focused)
    win.flip()

    # play around with the window, focus other windows, check the terminal

@damusss damusss added New API This pull request may need extra debate as it adds a new class or function to pygame window labels Aug 22, 2024
@damusss damusss requested a review from a team as a code owner August 22, 2024 18:06
Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks 🎉

@ankith26 ankith26 added this to the 2.5.2 milestone Aug 23, 2024
docs/reST/ref/window.rst Outdated Show resolved Hide resolved
Copy link
Contributor

@bilhox bilhox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

@bilhox bilhox merged commit 4a4814f into pygame-community:main Aug 27, 2024
26 checks passed
@damusss damusss deleted the Window-get_focus branch August 27, 2024 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New API This pull request may need extra debate as it adds a new class or function to pygame window
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants