diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index b586100f9..0ac36b2b6 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -2582,6 +2582,71 @@ void _glfwPlatformDestroyWindow(_GLFWwindow* window) window->ns.object = nil; } +static bool +ns_window_level_constant(const char *name, long *val) { +#define C(x) if (strcmp(name, #x) == 0) { *val = (long)x; return true; } + C(NSNormalWindowLevel); C(NSFloatingWindowLevel); C(NSSubmenuWindowLevel); C(NSTornOffMenuWindowLevel); + C(NSMainMenuWindowLevel); C(NSStatusWindowLevel); C(NSModalPanelWindowLevel); C(NSPopUpMenuWindowLevel); + C(NSScreenSaverWindowLevel); + C(kCGBaseWindowLevel); C(kCGMinimumWindowLevel); C(kCGDesktopWindowLevel); C(kCGBackstopMenuLevel); + C(kCGNormalWindowLevel); C(kCGFloatingWindowLevel); C(kCGTornOffMenuWindowLevel); C(kCGDockWindowLevel); + C(kCGMainMenuWindowLevel); C(kCGStatusWindowLevel); C(kCGModalPanelWindowLevel); C(kCGPopUpMenuWindowLevel); + C(kCGDraggingWindowLevel); C(kCGScreenSaverWindowLevel); C(kCGMaximumWindowLevel); C(kCGOverlayWindowLevel); + C(kCGHelpWindowLevel); C(kCGUtilityWindowLevel); C(kCGDesktopIconWindowLevel); C(kCGAssistiveTechHighWindowLevel); + C(kCGCursorWindowLevel); C(kCGNumberOfWindowLevelKeys); +#undef C + return false; +} + +static bool +parse_ns_window_level_term(const char **spec, long *val) { + const char *s = *spec; + while (isspace(*s)) s++; + if (!*s) return false; + if (*s == '+' || *s == '-' || isdigit(*s)) { + errno = 0; + char *end = NULL; + long v = strtol(s, &end, 0); + if (end == s || errno) return false; + *val = v; *spec = end; + return true; + } + char name[96]; size_t i = 0; + while ((isalnum(*s) || *s == '_') && i + 1 < sizeof(name)) name[i++] = *s++; + name[i] = '\0'; + if (!i || !ns_window_level_constant(name, val)) return false; + *spec = s; + return true; +} + +static bool +parse_ns_window_level(const char *spec, NSWindowLevel *level) { + const char *original = spec; + if (!spec) return false; + while (isspace(*spec)) spec++; + if (!*spec || strcmp(spec, "unset") == 0) return false; + long total = 0; + if (!parse_ns_window_level_term(&spec, &total)) goto invalid; + while (true) { + while (isspace(*spec)) spec++; + if (!*spec) { *level = (NSWindowLevel)total; return true; } + char op = *spec++; + if (op != '+' && op != '-') goto invalid; + long term = 0; + if (!parse_ns_window_level_term(&spec, &term)) goto invalid; + total = op == '+' ? total + term : total - term; + } +invalid: + _glfwInputError(GLFW_INVALID_VALUE, "Cocoa: Invalid macOS NSWindow layer expression: %s", original); + return false; +} + +static void +apply_ns_window_layer(_GLFWwindow *window, const char *spec) { + NSWindowLevel level = 0; + if (parse_ns_window_level(spec, &level)) [window->ns.object setLevel:level]; +} + static NSScreen* screen_for_window_center(_GLFWwindow *window) { NSRect windowFrame = [window->ns.object frame]; @@ -2686,9 +2751,10 @@ _glfwPlatformSetLayerShellConfig(_GLFWwindow* window, const GLFWLayerShellConfig double spacing_y = top_edge_spacing + bottom_edge_spacing; const unsigned xsz = config.x_size_in_pixels ? (unsigned)(config.x_size_in_pixels * xscale) : (cell_width * config.x_size_in_cells); const unsigned ysz = config.y_size_in_pixels ? (unsigned)(config.y_size_in_pixels * yscale) : (cell_height * config.y_size_in_cells); + NSRect placement_frame = config.related.use_physical_screen_frame ? screen.frame : screen.visibleFrame; CGFloat dock_height = NSMinY(screen.visibleFrame) - NSMinY(screen.frame); - CGFloat menubar_height = NSHeight(screen.frame) - NSHeight(screen.visibleFrame) - dock_height; - CGFloat x = NSMinX(screen.visibleFrame), y = NSMinY(screen.visibleFrame) - 1, width = NSWidth(screen.visibleFrame), height = NSHeight(screen.visibleFrame) + 2; + CGFloat menubar_height = config.related.use_physical_screen_frame ? 0 : NSHeight(screen.frame) - NSHeight(screen.visibleFrame) - dock_height; + CGFloat x = NSMinX(placement_frame), y = NSMinY(placement_frame) - 1, width = NSWidth(placement_frame), height = NSHeight(placement_frame) + 2; if (config.type == GLFW_LAYER_SHELL_BACKGROUND || config.edge == GLFW_EDGE_CENTER) { x = NSMinX(screen.frame); height = NSHeight(screen.frame) - menubar_height + 1; y = NSMinY(screen.frame); width = NSWidth(screen.frame); } @@ -2729,8 +2795,8 @@ _glfwPlatformSetLayerShellConfig(_GLFWwindow* window, const GLFWLayerShellConfig height = panel_height; width = panel_width; break; } - if (width < 1.) width = NSWidth(screen.visibleFrame); - if (height < 1.) height = NSWidth(screen.visibleFrame); + if (width < 1.) width = NSWidth(placement_frame); + if (height < 1.) height = NSHeight(placement_frame); } if (config.edge != GLFW_EDGE_CENTER_SIZED) { @@ -2740,6 +2806,7 @@ _glfwPlatformSetLayerShellConfig(_GLFWwindow* window, const GLFWLayerShellConfig [nswindow setAnimationBehavior:animation_behavior]; [nswindow setLevel:level]; + apply_ns_window_layer(window, config.related.ns_window_layer); [nswindow setCollectionBehavior: (NSWindowCollectionBehaviorCanJoinAllSpaces | NSWindowCollectionBehaviorStationary | NSWindowCollectionBehaviorIgnoresCycle)]; [nswindow setFrame:NSMakeRect(x, y, width, height) display:YES]; return true; @@ -4039,6 +4106,12 @@ apply_window_corner_curve(_GLFWwindow *window) { +GLFWAPI void glfwCocoaSetWindowLevel(GLFWwindow *w, const char *level_spec) { @autoreleasepool { + _GLFWwindow* window = (_GLFWwindow*)w; + apply_ns_window_layer(window, level_spec); +}} + + GLFWAPI void glfwCocoaSetWindowChrome(GLFWwindow *w, 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) { @autoreleasepool { _GLFWwindow* window = (_GLFWwindow*)w; if (window->ns.layer_shell.is_active) return; diff --git a/glfw/glfw3.h b/glfw/glfw3.h index cbacb7568..2e99fb2e5 100644 --- a/glfw/glfw3.h +++ b/glfw/glfw3.h @@ -1373,7 +1373,8 @@ typedef struct GLFWLayerShellConfig { void (*size_callback)(GLFWwindow *window, float xscale, float yscale, unsigned *cell_width, unsigned *cell_height, double *left_edge_spacing, double *top_edge_spacing, double *right_edge_spacing, double *bottom_edge_spacing); struct { float xscale, yscale; } expected; struct { - float background_opacity; int background_blur, color_space; + float background_opacity; int background_blur, color_space, use_physical_screen_frame; + char ns_window_layer[128]; } related; } GLFWLayerShellConfig; diff --git a/kitty/glfw-wrapper.c b/kitty/glfw-wrapper.c index 37a35891a..91d925a6d 100644 --- a/kitty/glfw-wrapper.c +++ b/kitty/glfw-wrapper.c @@ -497,6 +497,9 @@ load_glfw(const char* path) { *(void **) (&glfwCocoaCycleThroughOSWindows_impl) = dlsym(handle, "glfwCocoaCycleThroughOSWindows"); if (glfwCocoaCycleThroughOSWindows_impl == NULL) dlerror(); // clear error indicator + *(void **) (&glfwCocoaSetWindowLevel_impl) = dlsym(handle, "glfwCocoaSetWindowLevel"); + if (glfwCocoaSetWindowLevel_impl == NULL) dlerror(); // clear error indicator + *(void **) (&glfwCocoaSetWindowChrome_impl) = dlsym(handle, "glfwCocoaSetWindowChrome"); if (glfwCocoaSetWindowChrome_impl == NULL) dlerror(); // clear error indicator diff --git a/kitty/glfw-wrapper.h b/kitty/glfw-wrapper.h index 38ea7ccde..ec4d42933 100644 --- a/kitty/glfw-wrapper.h +++ b/kitty/glfw-wrapper.h @@ -1101,7 +1101,8 @@ typedef struct GLFWLayerShellConfig { void (*size_callback)(GLFWwindow *window, float xscale, float yscale, unsigned *cell_width, unsigned *cell_height, double *left_edge_spacing, double *top_edge_spacing, double *right_edge_spacing, double *bottom_edge_spacing); struct { float xscale, yscale; } expected; struct { - float background_opacity; int background_blur, color_space; + float background_opacity; int background_blur, color_space, use_physical_screen_frame; + char ns_window_layer[128]; } related; } GLFWLayerShellConfig; @@ -2478,6 +2479,10 @@ typedef void (*glfwCocoaCycleThroughOSWindows_func)(bool); GFW_EXTERN glfwCocoaCycleThroughOSWindows_func glfwCocoaCycleThroughOSWindows_impl; #define glfwCocoaCycleThroughOSWindows glfwCocoaCycleThroughOSWindows_impl +typedef void (*glfwCocoaSetWindowLevel_func)(GLFWwindow*, const char*); +GFW_EXTERN glfwCocoaSetWindowLevel_func glfwCocoaSetWindowLevel_impl; +#define glfwCocoaSetWindowLevel glfwCocoaSetWindowLevel_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 diff --git a/kitty/glfw.c b/kitty/glfw.c index c3f31daba..8be1474b1 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -182,6 +182,9 @@ set_layer_shell_config_for(OSWindow *w, GLFWLayerShellConfig *lsc) { lsc->related.background_opacity = effective_os_window_alpha(w); lsc->related.background_blur = OPT(background_blur); lsc->related.color_space = OPT(macos_colorspace); + lsc->related.use_physical_screen_frame = OPT(macos_use_physical_screen_frame); + if (OPT(macos_ns_window_layer)) snprintf(lsc->related.ns_window_layer, sizeof(lsc->related.ns_window_layer), "%s", OPT(macos_ns_window_layer)); + else lsc->related.ns_window_layer[0] = '\0'; w->hide_on_focus_loss = lsc->hide_on_focus_loss; } return glfwSetLayerShellConfig(w->handle, lsc); @@ -1943,6 +1946,9 @@ create_os_window(PyObject UNUSED *self, PyObject *args, PyObject *kw) { if (global_state.is_apple) set_layer_shell_config_for(w, lsc); } else apply_window_chrome_state( w->handle, w->last_window_chrome, width, height, global_state.is_apple ? OPT(hide_window_decorations) != 0 : false); +#ifdef __APPLE__ + if (!w->is_layer_shell) glfwCocoaSetWindowLevel(w->handle, OPT(macos_ns_window_layer)); +#endif // Update window state // We do not call glfwWindowHint to set GLFW_MAXIMIZED before the window is created. // That would cause the window to be set to maximize immediately after creation and use the wrong initial size when restored. diff --git a/kitty/options/definition.py b/kitty/options/definition.py index 6e42a6bfd..5c6d27849 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -2733,6 +2733,27 @@ full display frame instead of leaving space for the notch area. ''' ) +opt('macos_use_physical_screen_frame', 'no', + option_type='to_bool', ctype='bool', + long_text=''' +Use the physical screen frame instead of the visible frame when placing macOS +desktop panels such as those created by :code:`kitty +kitten panel`. This allows +panels to draw in areas normally reserved for the native menu bar or Dock. +''' + ) + +opt('macos_ns_window_layer', 'unset', + option_type='str', ctype='!macos_ns_window_layer', + long_text=''' +Set the macOS NSWindow level for newly created OS windows. The default value +:code:`unset` leaves kitty's normal window-level handling unchanged. Values can +be integer window levels, AppKit/CoreGraphics window-level constant names, or +simple arithmetic expressions combining them with integers. For example: +:code:`NSFloatingWindowLevel`, :code:`kCGBackstopMenuLevel`, +:code:`NSPopUpMenuWindowLevel - 1`. +''' + ) + opt('macos_show_window_title_in', 'all', choices=('all', 'menubar', 'none', 'window'), ctype='window_title_in', long_text=''' diff --git a/kitty/options/parse.py b/kitty/options/parse.py index e062ebf31..56e3e18b1 100644 --- a/kitty/options/parse.py +++ b/kitty/options/parse.py @@ -1112,6 +1112,9 @@ class Parser: def macos_menubar_title_max_length(self, val: str, ans: dict[str, typing.Any]) -> None: ans['macos_menubar_title_max_length'] = positive_int(val) + def macos_ns_window_layer(self, val: str, ans: dict[str, typing.Any]) -> None: + ans['macos_ns_window_layer'] = str(val) + def macos_option_as_alt(self, val: str, ans: dict[str, typing.Any]) -> None: ans['macos_option_as_alt'] = macos_option_as_alt(val) @@ -1135,6 +1138,9 @@ class Parser: def macos_traditional_fullscreen(self, val: str, ans: dict[str, typing.Any]) -> None: ans['macos_traditional_fullscreen'] = to_bool(val) + def macos_use_physical_screen_frame(self, val: str, ans: dict[str, typing.Any]) -> None: + ans['macos_use_physical_screen_frame'] = to_bool(val) + def macos_window_resizable(self, val: str, ans: dict[str, typing.Any]) -> None: ans['macos_window_resizable'] = to_bool(val) diff --git a/kitty/options/to-c-generated.h b/kitty/options/to-c-generated.h index 6d601cc75..0d7d03897 100644 --- a/kitty/options/to-c-generated.h +++ b/kitty/options/to-c-generated.h @@ -1448,6 +1448,32 @@ convert_from_opts_macos_fullscreen_ignore_safe_area_insets(PyObject *py_opts, Op Py_DECREF(ret); } +static void +convert_from_python_macos_use_physical_screen_frame(PyObject *val, Options *opts) { + opts->macos_use_physical_screen_frame = PyObject_IsTrue(val); +} + +static void +convert_from_opts_macos_use_physical_screen_frame(PyObject *py_opts, Options *opts) { + PyObject *ret = PyObject_GetAttrString(py_opts, "macos_use_physical_screen_frame"); + if (ret == NULL) return; + convert_from_python_macos_use_physical_screen_frame(ret, opts); + Py_DECREF(ret); +} + +static void +convert_from_python_macos_ns_window_layer(PyObject *val, Options *opts) { + macos_ns_window_layer(val, opts); +} + +static void +convert_from_opts_macos_ns_window_layer(PyObject *py_opts, Options *opts) { + PyObject *ret = PyObject_GetAttrString(py_opts, "macos_ns_window_layer"); + if (ret == NULL) return; + convert_from_python_macos_ns_window_layer(ret, opts); + Py_DECREF(ret); +} + static void convert_from_python_macos_show_window_title_in(PyObject *val, Options *opts) { opts->macos_show_window_title_in = window_title_in(val); @@ -1724,6 +1750,10 @@ convert_opts_from_python_opts(PyObject *py_opts, Options *opts) { if (PyErr_Occurred()) return false; convert_from_opts_macos_fullscreen_ignore_safe_area_insets(py_opts, opts); if (PyErr_Occurred()) return false; + convert_from_opts_macos_use_physical_screen_frame(py_opts, opts); + if (PyErr_Occurred()) return false; + convert_from_opts_macos_ns_window_layer(py_opts, opts); + if (PyErr_Occurred()) return false; convert_from_opts_macos_show_window_title_in(py_opts, opts); if (PyErr_Occurred()) return false; convert_from_opts_macos_menubar_title_max_length(py_opts, opts); diff --git a/kitty/options/to-c.h b/kitty/options/to-c.h index 84ec7475c..dd1ea8399 100644 --- a/kitty/options/to-c.h +++ b/kitty/options/to-c.h @@ -206,6 +206,9 @@ bell_path(PyObject *src, Options *opts) { STR_SETTER(bell_path); } static inline void bell_theme(PyObject *src, Options *opts) { STR_SETTER(bell_theme); } +static inline void +macos_ns_window_layer(PyObject *src, Options *opts) { STR_SETTER(macos_ns_window_layer); } + static inline void window_logo_path(PyObject *src, Options *opts) { STR_SETTER(default_window_logo); } @@ -604,6 +607,6 @@ free_allocs_in_options(Options *opts) { free_background_images(opts); #define F(x) free(opts->x); opts->x = NULL; F(select_by_word_characters); F(url_excluded_characters); F(select_by_word_characters_forward); - F(bell_path); F(bell_theme); F(default_window_logo); + F(bell_path); F(bell_theme); F(macos_ns_window_layer); F(default_window_logo); #undef F } diff --git a/kitty/options/types.py b/kitty/options/types.py index 3d036ef5d..7569d5a45 100644 --- a/kitty/options/types.py +++ b/kitty/options/types.py @@ -391,12 +391,14 @@ option_names = ( 'macos_fullscreen_ignore_safe_area_insets', 'macos_hide_from_tasks', 'macos_menubar_title_max_length', + 'macos_ns_window_layer', 'macos_option_as_alt', 'macos_quit_when_last_window_closed', 'macos_show_window_title_in', 'macos_thicken_font', 'macos_titlebar_color', 'macos_traditional_fullscreen', + 'macos_use_physical_screen_frame', 'macos_window_resizable', 'map', 'map_timeout', @@ -605,12 +607,14 @@ class Options: macos_fullscreen_ignore_safe_area_insets: bool = False macos_hide_from_tasks: bool = False macos_menubar_title_max_length: int = 0 + macos_ns_window_layer: str = 'unset' macos_option_as_alt: int = 0 macos_quit_when_last_window_closed: bool = False macos_show_window_title_in: choices_for_macos_show_window_title_in = 'all' macos_thicken_font: float = 0 macos_titlebar_color: int = 0 macos_traditional_fullscreen: bool = False + macos_use_physical_screen_frame: bool = False macos_window_resizable: bool = True map_timeout: float = 0 mark1_background: Color = Color(152, 211, 203) diff --git a/kitty/state.h b/kitty/state.h index 2eecb356c..8f01221f2 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -78,11 +78,11 @@ typedef struct Options { bool on_cross, on_drop; } focus_follows_mouse; unsigned int hide_window_decorations; - bool macos_hide_from_tasks, macos_quit_when_last_window_closed, macos_window_resizable, macos_traditional_fullscreen, macos_fullscreen_ignore_safe_area_insets; + bool macos_hide_from_tasks, macos_quit_when_last_window_closed, macos_window_resizable, macos_traditional_fullscreen, macos_fullscreen_ignore_safe_area_insets, macos_use_physical_screen_frame; unsigned int macos_option_as_alt; float macos_thicken_font; WindowTitleIn macos_show_window_title_in; - char *bell_path, *bell_theme; + char *bell_path, *bell_theme, *macos_ns_window_layer; float background_opacity, dim_opacity; ScrollbarVisibilityPolicy scrollbar;