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

Window grab API rework #2379

Merged
merged 13 commits into from
Oct 15, 2023
Merged

Conversation

yunline
Copy link
Contributor

@yunline yunline commented Aug 5, 2023

  • grab -> grab_mouse
  • Add grab_keyboard
  • Add mouse_grabbed
  • Add keyboard_grabbed

We already have grab_mouse/keyboard, why add mouse/keyboard_grabbed?

These attribute are for different uses.
grab_mouse means whether the window will try to grab the mouse.
mouse_grabbed means whether the mouse is grabbed by this window.
See docs for details.

TestCode

import sys

import pygame
from pygame._sdl2 import video

w1 = video.Window("w1", position=(200, 200))
w2 = video.Window("w2", position=(600, 300))
w1.grab_keyboard = True
w2.grab_keyboard = True

pygame.key.stop_text_input()

print(
    "This is a test script for 'grab_keyboard' and 'keyboard_grabbed'.s\n"
    "Try press 's' or change the focus of the two windows."
)

while 1:
    for event in pygame.event.get():
        if event.type == pygame.WINDOWFOCUSGAINED:
            print(
                f"w1 grabbed:{w1.keyboard_grabbed}, "
                f"w2 grabbed:{w2.keyboard_grabbed}"
            )
        if event.type == pygame.KEYDOWN and event.unicode == "s":
            if (win := event.window) is not None:
                win.grab_keyboard = not win.grab_keyboard
                print(
                    f"[{win.title}] grab mode {'on' if win.grab_keyboard else 'off'}. "
                    f"`Win + D` should {'not ' if win.grab_keyboard else ''}work.\n"
                    f"w1 grabbed:{w1.keyboard_grabbed}, "
                    f"w2 grabbed:{w2.keyboard_grabbed}"
                )
        if event.type == pygame.WINDOWCLOSE:
            sys.exit()

@yunline yunline added New API This pull request may need extra debate as it adds a new class or function to pygame _sdl2 pygame._sdl2 labels Aug 5, 2023
@yunline yunline marked this pull request as ready for review August 22, 2023 06:50
@yunline yunline requested a review from a team as a code owner August 22, 2023 06:50
Copy link
Member

@MyreMylar MyreMylar left a comment

Choose a reason for hiding this comment

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

All works on my machine, I can grab the mouse and the keyboard shortcuts with the Window class all day long 👍 🎉

I added a couple of small documentation suggestion fixes along with this review. Feel free to fix them yourself or whoever ends up 2nd review and merging this can do so.

docs/reST/ref/sdl2_video.rst Outdated Show resolved Hide resolved
docs/reST/ref/sdl2_video.rst Outdated Show resolved Hide resolved
docs/reST/ref/sdl2_video.rst Outdated Show resolved Hide resolved
src_c/doc/sdl2_video_doc.h Outdated Show resolved Hide resolved
yunline and others added 6 commits October 15, 2023 07:45
Co-authored-by: Dan Lawrence <danintheshed@gmail.com>
Co-authored-by: Dan Lawrence <danintheshed@gmail.com>
Co-authored-by: Dan Lawrence <danintheshed@gmail.com>
Co-authored-by: Dan Lawrence <danintheshed@gmail.com>
Copy link
Member

@zoldalma999 zoldalma999 left a comment

Choose a reason for hiding this comment

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

Looks good to me! Used your test app (and modified it to make sure mouse grabbing works), and it did what it should. I also tested passing in grab_keyboard as an argument, that works too.

@MyreMylar MyreMylar merged commit e3d34b4 into pygame-community:main Oct 15, 2023
30 checks passed
@ankith26 ankith26 added this to the 2.4.0 milestone Oct 15, 2023
MyreMylar added a commit that referenced this pull request Oct 16, 2023
@yunline yunline deleted the window-grab-rework branch October 17, 2023 05:21
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 _sdl2 pygame._sdl2 window
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants