xpra icon
Bug tracker and wiki

This bug tracker and wiki are being discontinued
please use https://github.com/Xpra-org/xpra instead.


Ticket #490: platform_window_focus.py

File platform_window_focus.py, 1.1 KB (added by Antoine Martin, 7 years ago)

code to force focus on osx

Line 
1import ctypes
2import ctypes.util
3import sys
4
5_is_osx = sys.platform == "darwin"
6
7if _is_osx:
8    appkit = ctypes.cdll.LoadLibrary(ctypes.util.find_library('AppKit'))
9    objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc'))
10
11    objc.objc_getClass.restype = ctypes.c_void_p
12    objc.sel_registerName.restype = ctypes.c_void_p
13    objc.objc_msgSend.restype = ctypes.c_void_p
14    objc.objc_msgSend.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
15
16
17def platform_window_focus(frame):
18    """
19    Focus the window frame
20    """
21
22    # General window raising
23    if frame.IsIconized():
24        frame.Iconize(False)
25    if not frame.IsShown():
26        frame.Show(True)
27    frame.Raise()
28
29    # OSX specific extra to ensure raise
30    if _is_osx:
31        try:
32            NSApplication = ctypes.c_void_p(objc.objc_getClass('NSApplication'))
33            NSApp = ctypes.c_void_p(objc.objc_msgSend(NSApplication, objc.sel_registerName('sharedApplication')))
34            objc.objc_msgSend(NSApp, objc.sel_registerName('activateIgnoringOtherApps:'), True)
35        except:
36            # Failed to bring window to top in OSX
37            pass