Move the code to cycle through OS Windows into glfw

This commit is contained in:
Kovid Goyal
2025-11-12 12:02:38 +05:30
parent 7fe38ae579
commit 81f429d52b
7 changed files with 39 additions and 26 deletions

View File

@@ -3480,6 +3480,36 @@ glfwGetCocoaKeyEquivalent(uint32_t glfw_key, int glfw_mods, int *cocoa_mods) {
GLFWAPI bool glfwIsLayerShellSupported(void) { return true; }
GLFWAPI void
glfwCocoaCycleThroughOSWindows(bool backwards) {
NSArray *allWindows = [NSApp windows];
if (allWindows.count < 2) return;
NSMutableArray<NSWindow *> *filteredWindows = [NSMutableArray array];
for (NSWindow *window in allWindows) {
NSRect windowFrame = [window frame];
// Exclude zero size windows which are likely zombie windows from the Tahoe bug
// if ([obj isMemberOfClass:[MyClass class]]) {
if (
windowFrame.size.width > 0 && windowFrame.size.height > 0 && \
!window.isMiniaturized && window.isVisible && \
[window isMemberOfClass:[GLFWWindow class]]
) [filteredWindows addObject:window];
}
if (filteredWindows.count < 2) return;
NSWindow *keyWindow = [NSApp keyWindow];
NSUInteger index = [filteredWindows indexOfObject:keyWindow];
NSUInteger nextIndex = 0;
if (index != NSNotFound) {
if (backwards) {
nextIndex = (index == 0) ? [filteredWindows count] - 1 : index - 1;
} else nextIndex = (index + 1) % filteredWindows.count;
}
NSWindow *nextWindow = filteredWindows[nextIndex];
[nextWindow makeKeyAndOrderFront:nil];
}
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////

View File

@@ -313,6 +313,7 @@ def generate_wrappers(glfw_header: str) -> None:
void* glfwGetX11Display(void)
unsigned long glfwGetX11Window(GLFWwindow* window)
void glfwSetPrimarySelectionString(GLFWwindow* window, const char* string)
void glfwCocoaCycleThroughOSWindows(bool backwards)
void glfwCocoaSetWindowChrome(GLFWwindow* window, unsigned int color, bool use_system_color, unsigned int system_color,\
int background_blur, unsigned int hide_window_decorations, bool show_text_in_titlebar, int color_space, float background_opacity, bool resizable)
const char* glfwGetPrimarySelectionString(GLFWwindow* window, void)

View File

@@ -52,7 +52,6 @@ void cocoa_system_beep(const char*);
void cocoa_set_activation_policy(bool);
bool cocoa_alt_option_key_pressed(unsigned long);
void cocoa_toggle_secure_keyboard_entry(void);
void cocoa_cycle_through_os_windows(bool);
void cocoa_hide(void);
void cocoa_clear_global_shortcuts(void);
void cocoa_hide_others(void);

View File

@@ -927,30 +927,6 @@ cocoa_toggle_secure_keyboard_entry(void) {
[[NSUserDefaults standardUserDefaults] setBool:k.isDesired forKey:@"SecureKeyboardEntry"];
}
void
cocoa_cycle_through_os_windows(bool backwards) {
NSArray *allWindows = [NSApp windows];
if (allWindows.count < 2) return;
NSMutableArray<NSWindow *> *filteredWindows = [NSMutableArray array];
for (NSWindow *window in allWindows) {
NSRect windowFrame = [window frame];
// Exclude zero size windows which are likely zombie windows from the Tahoe bug
if (windowFrame.size.width > 0 && windowFrame.size.height > 0 && !window.isMiniaturized && window.isVisible) [filteredWindows addObject:window];
}
if (filteredWindows.count < 2) return;
NSWindow *keyWindow = [NSApp keyWindow];
NSUInteger index = [filteredWindows indexOfObject:keyWindow];
NSUInteger nextIndex = 0;
if (index != NSNotFound) {
if (backwards) {
nextIndex = (index == 0) ? [filteredWindows count] - 1 : index - 1;
} else nextIndex = (index + 1) % filteredWindows.count;
}
NSWindow *nextWindow = filteredWindows[nextIndex];
[nextWindow makeKeyAndOrderFront:nil];
}
void
cocoa_hide(void) {
[[NSApplication sharedApplication] performSelectorOnMainThread:@selector(hide:) withObject:nil waitUntilDone:NO];

3
kitty/glfw-wrapper.c generated
View File

@@ -473,6 +473,9 @@ load_glfw(const char* path) {
*(void **) (&glfwSetPrimarySelectionString_impl) = dlsym(handle, "glfwSetPrimarySelectionString");
if (glfwSetPrimarySelectionString_impl == NULL) dlerror(); // clear error indicator
*(void **) (&glfwCocoaCycleThroughOSWindows_impl) = dlsym(handle, "glfwCocoaCycleThroughOSWindows");
if (glfwCocoaCycleThroughOSWindows_impl == NULL) dlerror(); // clear error indicator
*(void **) (&glfwCocoaSetWindowChrome_impl) = dlsym(handle, "glfwCocoaSetWindowChrome");
if (glfwCocoaSetWindowChrome_impl == NULL) dlerror(); // clear error indicator

4
kitty/glfw-wrapper.h generated
View File

@@ -2330,6 +2330,10 @@ typedef void (*glfwSetPrimarySelectionString_func)(GLFWwindow*, const char*);
GFW_EXTERN glfwSetPrimarySelectionString_func glfwSetPrimarySelectionString_impl;
#define glfwSetPrimarySelectionString glfwSetPrimarySelectionString_impl
typedef void (*glfwCocoaCycleThroughOSWindows_func)(bool);
GFW_EXTERN glfwCocoaCycleThroughOSWindows_func glfwCocoaCycleThroughOSWindows_impl;
#define glfwCocoaCycleThroughOSWindows glfwCocoaCycleThroughOSWindows_impl
typedef void (*glfwCocoaSetWindowChrome_func)(GLFWwindow*, unsigned int, bool, unsigned int, int, unsigned int, bool, int, float, bool);
GFW_EXTERN glfwCocoaSetWindowChrome_func glfwCocoaSetWindowChrome_impl;
#define glfwCocoaSetWindowChrome glfwCocoaSetWindowChrome_impl

View File

@@ -1924,7 +1924,7 @@ toggle_secure_input(PYNOARG) {
static PyObject*
macos_cycle_through_os_windows(PyObject *self UNUSED, PyObject *backwards) {
#ifdef __APPLE__
cocoa_cycle_through_os_windows(PyObject_IsTrue(backwards));
glfwCocoaCycleThroughOSWindows(PyObject_IsTrue(backwards));
#else
(void)backwards;
#endif