From bbdd13bd971b7b24d60b05c34020e021b196a743 Mon Sep 17 00:00:00 2001 From: LiJianying Date: Thu, 3 Dec 2026 22:44:55 +0800 Subject: [PATCH] feat(window,screen-capture): add WindowLevel, instant macOS visibility, square frameless, and exclude_own_windows - Add WindowLevel (Normal, Floating, Status, ScreenSaver) with Window::SetLevel/GetLevel across all platforms and unconstrained macOS bounds at Status/ScreenSaver levels (#81) - Disable AppKit hide/show fade-and-shrink animation in macOS Window::Hide/Show/ShowInactive (#82) - Strip NSWindowStyleMaskTitled in macOS Window::SetFrameless(true) for true square corners while preserving key/main focusability (#83) - Add ScreenCaptureOptions::exclude_own_windows on macOS (SCContentFilter excluding own PID) and Windows (WDA_EXCLUDEFROMCAPTURE) (#84) --- src/capi/screen_capturer_c.h | 3 + src/capi/window_c.cpp | 32 ++++ src/capi/window_c.h | 45 +++++ src/platform/android/window_android.cpp | 8 + src/platform/ios/window_ios.mm | 8 + src/platform/linux/window_linux.cpp | 32 ++++ src/platform/macos/screen_capture_macos.mm | 21 ++- src/platform/macos/window_macos.mm | 178 +++++++++++++++--- src/platform/ohos/window_ohos.cpp | 8 + src/platform/wasm/window_wasm.cpp | 6 + .../windows/screen_capture_windows.cpp | 34 ++++ src/platform/windows/window_windows.cpp | 22 +++ src/screen_capturer.h | 2 + src/window.h | 47 +++++ 14 files changed, 421 insertions(+), 25 deletions(-) diff --git a/src/capi/screen_capturer_c.h b/src/capi/screen_capturer_c.h index 66d6fdd..beb84af 100644 --- a/src/capi/screen_capturer_c.h +++ b/src/capi/screen_capturer_c.h @@ -42,6 +42,7 @@ typedef struct { typedef struct { bool include_cursor; + bool exclude_own_windows; bool crop_enabled; native_rectangle_t crop; } native_screen_capture_options_t; @@ -108,6 +109,7 @@ inline nativeapi::CaptureSourceSelectionOptions to_cpp_capture_source_selection_ inline native_screen_capture_options_t to_c_screen_capture_options(const nativeapi::ScreenCaptureOptions& value) { native_screen_capture_options_t result = {}; result.include_cursor = value.include_cursor; + result.exclude_own_windows = value.exclude_own_windows; result.crop_enabled = value.crop_enabled; result.crop = to_c_rectangle(value.crop); return result; @@ -116,6 +118,7 @@ inline native_screen_capture_options_t to_c_screen_capture_options(const nativea inline nativeapi::ScreenCaptureOptions to_cpp_screen_capture_options(const native_screen_capture_options_t& value) { nativeapi::ScreenCaptureOptions result = {}; result.include_cursor = value.include_cursor; + result.exclude_own_windows = value.exclude_own_windows; result.crop_enabled = value.crop_enabled; result.crop = to_cpp_rectangle(value.crop); return result; diff --git a/src/capi/window_c.cpp b/src/capi/window_c.cpp index cecdbab..1b14ffc 100644 --- a/src/capi/window_c.cpp +++ b/src/capi/window_c.cpp @@ -1071,6 +1071,38 @@ bool native_window_is_always_on_top(native_window_t window) { } } +bool native_window_set_level(native_window_t window, native_window_level_t level) { + auto self = nativeapi::HandleTable::GetInstance().Resolve(window); + if (!self) { + return false; + } + try { + return self->SetLevel(to_cpp_window_level(level)); + } catch (const std::exception& e) { + NATIVEAPI_LOG_ERROR("CApi", "native_window_set_level: unexpected exception: " << e.what()); + return false; + } catch (...) { + NATIVEAPI_LOG_ERROR("CApi", "native_window_set_level: unexpected exception"); + return false; + } +} + +native_window_level_t native_window_get_level(native_window_t window) { + auto self = nativeapi::HandleTable::GetInstance().Resolve(window); + if (!self) { + return (native_window_level_t)NATIVE_WINDOW_LEVEL_NORMAL; + } + try { + return to_c_window_level(self->GetLevel()); + } catch (const std::exception& e) { + NATIVEAPI_LOG_ERROR("CApi", "native_window_get_level: unexpected exception: " << e.what()); + return (native_window_level_t)NATIVE_WINDOW_LEVEL_NORMAL; + } catch (...) { + NATIVEAPI_LOG_ERROR("CApi", "native_window_get_level: unexpected exception"); + return (native_window_level_t)NATIVE_WINDOW_LEVEL_NORMAL; + } +} + void native_window_set_always_on_bottom(native_window_t window, bool is_always_on_bottom) { auto self = nativeapi::HandleTable::GetInstance().Resolve(window); if (!self) { diff --git a/src/capi/window_c.h b/src/capi/window_c.h index 01ca7e9..01b02af 100644 --- a/src/capi/window_c.h +++ b/src/capi/window_c.h @@ -55,6 +55,13 @@ typedef enum { NATIVE_WINDOW_CORNER_PREFERENCE_ROUND_SMALL = 3, } native_window_corner_preference_t; +typedef enum { + NATIVE_WINDOW_LEVEL_NORMAL = 0, + NATIVE_WINDOW_LEVEL_FLOATING = 1, + NATIVE_WINDOW_LEVEL_STATUS = 2, + NATIVE_WINDOW_LEVEL_SCREEN_SAVER = 3, +} native_window_level_t; + typedef enum { NATIVE_WINDOW_PROPERTY_TITLE = 0, NATIVE_WINDOW_PROPERTY_RESIZABLE = 1, @@ -363,6 +370,12 @@ void native_window_set_always_on_top(native_window_t window, bool is_always_on_t FFI_PLUGIN_EXPORT bool native_window_is_always_on_top(native_window_t window); +FFI_PLUGIN_EXPORT +bool native_window_set_level(native_window_t window, native_window_level_t level); + +FFI_PLUGIN_EXPORT +native_window_level_t native_window_get_level(native_window_t window); + FFI_PLUGIN_EXPORT void native_window_set_always_on_bottom(native_window_t window, bool is_always_on_bottom); @@ -636,6 +649,8 @@ inline native_window_dock_side_t to_c_window_dock_side(nativeapi::WindowDockSide inline nativeapi::WindowDockSide to_cpp_window_dock_side(native_window_dock_side_t value); inline native_window_corner_preference_t to_c_window_corner_preference(nativeapi::WindowCornerPreference value); inline nativeapi::WindowCornerPreference to_cpp_window_corner_preference(native_window_corner_preference_t value); +inline native_window_level_t to_c_window_level(nativeapi::WindowLevel value); +inline nativeapi::WindowLevel to_cpp_window_level(native_window_level_t value); inline native_window_property_t to_c_window_property(nativeapi::WindowProperty value); inline nativeapi::WindowProperty to_cpp_window_property(native_window_property_t value); inline native_window_occlusion_state_t to_c_window_occlusion_state(nativeapi::WindowOcclusionState value); @@ -723,6 +738,36 @@ inline nativeapi::WindowCornerPreference to_cpp_window_corner_preference(native_ } } +inline native_window_level_t to_c_window_level(nativeapi::WindowLevel value) { + switch (value) { + case nativeapi::WindowLevel::Normal: + return NATIVE_WINDOW_LEVEL_NORMAL; + case nativeapi::WindowLevel::Floating: + return NATIVE_WINDOW_LEVEL_FLOATING; + case nativeapi::WindowLevel::Status: + return NATIVE_WINDOW_LEVEL_STATUS; + case nativeapi::WindowLevel::ScreenSaver: + return NATIVE_WINDOW_LEVEL_SCREEN_SAVER; + default: + return NATIVE_WINDOW_LEVEL_NORMAL; + } +} + +inline nativeapi::WindowLevel to_cpp_window_level(native_window_level_t value) { + switch (value) { + case NATIVE_WINDOW_LEVEL_NORMAL: + return nativeapi::WindowLevel::Normal; + case NATIVE_WINDOW_LEVEL_FLOATING: + return nativeapi::WindowLevel::Floating; + case NATIVE_WINDOW_LEVEL_STATUS: + return nativeapi::WindowLevel::Status; + case NATIVE_WINDOW_LEVEL_SCREEN_SAVER: + return nativeapi::WindowLevel::ScreenSaver; + default: + return static_cast(value); + } +} + inline native_window_property_t to_c_window_property(nativeapi::WindowProperty value) { switch (value) { case nativeapi::WindowProperty::Title: diff --git a/src/platform/android/window_android.cpp b/src/platform/android/window_android.cpp index 6813d27..e8e8dab 100644 --- a/src/platform/android/window_android.cpp +++ b/src/platform/android/window_android.cpp @@ -276,6 +276,14 @@ bool Window::IsAlwaysOnTop() const { return false; } +bool Window::SetLevel(WindowLevel /*level*/) { + return false; +} + +WindowLevel Window::GetLevel() const { + return WindowLevel::Normal; +} + void Window::SetAlwaysOnBottom(bool is_always_on_bottom) { } diff --git a/src/platform/ios/window_ios.mm b/src/platform/ios/window_ios.mm index 1254235..5b178be 100644 --- a/src/platform/ios/window_ios.mm +++ b/src/platform/ios/window_ios.mm @@ -293,6 +293,14 @@ return false; } +bool Window::SetLevel(WindowLevel /*level*/) { + return false; +} + +WindowLevel Window::GetLevel() const { + return WindowLevel::Normal; +} + void Window::SetAlwaysOnBottom(bool is_always_on_bottom) { // Not applicable to iOS } diff --git a/src/platform/linux/window_linux.cpp b/src/platform/linux/window_linux.cpp index 1b9ad5e..6d9b09b 100644 --- a/src/platform/linux/window_linux.cpp +++ b/src/platform/linux/window_linux.cpp @@ -645,6 +645,22 @@ bool Window::IsAlwaysOnTop() const { return pimpl_->always_on_top_; } +bool Window::SetLevel(WindowLevel level) { + if (!pimpl_->widget_) return false; + SetAlwaysOnTop(level != WindowLevel::Normal); + g_object_set_data(G_OBJECT(pimpl_->widget_), "NativeAPIGtk4WindowLevel", + GINT_TO_POINTER(static_cast(level))); + return true; +} + +WindowLevel Window::GetLevel() const { + if (!pimpl_->widget_ || !IsAlwaysOnTop()) return WindowLevel::Normal; + int stored = GPOINTER_TO_INT( + g_object_get_data(G_OBJECT(pimpl_->widget_), "NativeAPIGtk4WindowLevel")); + auto level = static_cast(stored); + return level == WindowLevel::Normal ? WindowLevel::Floating : level; +} + void Window::SetAlwaysOnBottom(bool is_always_on_bottom) { pimpl_->always_on_bottom_ = is_always_on_bottom; if (is_always_on_bottom) { @@ -3028,6 +3044,22 @@ bool Window::IsAlwaysOnTop() const { return state & GDK_WINDOW_STATE_ABOVE; } +bool Window::SetLevel(WindowLevel level) { + if (!pimpl_->gtk_window_) return false; + SetAlwaysOnTop(level != WindowLevel::Normal); + g_object_set_data(G_OBJECT(pimpl_->gtk_window_), "NativeAPIWindowLevel", + GINT_TO_POINTER(static_cast(level))); + return true; +} + +WindowLevel Window::GetLevel() const { + if (!pimpl_->gtk_window_ || !IsAlwaysOnTop()) return WindowLevel::Normal; + int stored = GPOINTER_TO_INT( + g_object_get_data(G_OBJECT(pimpl_->gtk_window_), "NativeAPIWindowLevel")); + auto level = static_cast(stored); + return level == WindowLevel::Normal ? WindowLevel::Floating : level; +} + void Window::SetAlwaysOnBottom(bool is_always_on_bottom) { detail::WindowPropertyScope property_scope(*this); // GDK clears _NET_WM_STATE_ABOVE when setting BELOW and vice versa, so the two diff --git a/src/platform/macos/screen_capture_macos.mm b/src/platform/macos/screen_capture_macos.mm index cb7d934..1bd3f47 100644 --- a/src/platform/macos/screen_capture_macos.mm +++ b/src/platform/macos/screen_capture_macos.mm @@ -14,6 +14,7 @@ #include #include #include +#include #include #include "../../display.h" @@ -296,9 +297,23 @@ CaptureSourceType Type() const override { }]; if (!completion->Wait(cancel, error)) return nullptr; if (kind == CaptureSourceType::Display) { - for (SCDisplay* item in completion->content.get().displays) - if (item.displayID == identity) filter = Obj(Autorelease( - [[SCContentFilter alloc] initWithDisplay:item excludingWindows:@[]])); + NSMutableArray* excluded_apps = [NSMutableArray array]; + if (options.exclude_own_windows) { + const pid_t self_pid = getpid(); + for (SCRunningApplication* app in completion->content.get().applications) { + if (app.processID == self_pid) [excluded_apps addObject:app]; + } + } + for (SCDisplay* item in completion->content.get().displays) { + if (item.displayID == identity) { + filter = Obj(Autorelease( + excluded_apps.count > 0 + ? [[SCContentFilter alloc] initWithDisplay:item + excludingApplications:excluded_apps + exceptingWindows:@[]] + : [[SCContentFilter alloc] initWithDisplay:item excludingWindows:@[]])); + } + } } else { for (SCWindow* item in completion->content.get().windows) if (item.windowID == identity) filter = Obj(Autorelease( diff --git a/src/platform/macos/window_macos.mm b/src/platform/macos/window_macos.mm index 1c908d2..50e2004 100644 --- a/src/platform/macos/window_macos.mm +++ b/src/platform/macos/window_macos.mm @@ -30,6 +30,7 @@ static const void* kWindowMaximizableKey = &kWindowMaximizableKey; static const void* kWindowZoomBypassKey = &kWindowZoomBypassKey; static const void* kWindowFramelessKey = &kWindowFramelessKey; +static const void* kWindowFramelessSavedStyleKey = &kWindowFramelessSavedStyleKey; static const void* kWindowFramelessTitleViewKey = &kWindowFramelessTitleViewKey; static const void* kWindowMovableKey = &kWindowMovableKey; // A shaped window must not inherit AppKit's rounded titled frame. Keep this @@ -89,6 +90,7 @@ @interface NativeApiFullScreenCoordinator : NSObject { NSWindow* __unsafe_unretained window; BOOL pending_hide; BOOL restore_always_on_top; + NSInteger restore_level; } @end @@ -116,8 +118,9 @@ - (instancetype)initWithWindow:(NSWindow*)target { - (void)willEnterFullScreen:(NSNotification*)note { (void)note; if (!window) return; - if ([window level] == NSFloatingWindowLevel) { + if ([window level] >= NSFloatingWindowLevel) { restore_always_on_top = YES; + restore_level = [window level]; [window setLevel:NSNormalWindowLevel]; } } @@ -128,7 +131,7 @@ - (void)didExitFullScreen:(NSNotification*)note { if (restore_always_on_top) { // Raise level before clearing restore_always_on_top so IsAlwaysOnTop() // does not transiently dip to NO while KVO observes the level change. - [window setLevel:NSFloatingWindowLevel]; + [window setLevel:restore_level >= NSFloatingWindowLevel ? restore_level : NSFloatingWindowLevel]; restore_always_on_top = NO; } if (pending_hide) { @@ -174,7 +177,60 @@ static BOOL NativeApiWindowIsAlwaysOnTop(NSWindow* window) { if (auto* coordinator = NativeApiGetFullScreenCoordinator(window, false)) { if (coordinator->restore_always_on_top) return YES; } - return [window level] == NSFloatingWindowLevel; + return [window level] >= NSFloatingWindowLevel; +} + +static NSInteger NativeApiNSWindowLevel(nativeapi::WindowLevel level) { + switch (level) { + case nativeapi::WindowLevel::Normal: + return NSNormalWindowLevel; + case nativeapi::WindowLevel::Floating: + return NSFloatingWindowLevel; + case nativeapi::WindowLevel::Status: + return NSStatusWindowLevel; + case nativeapi::WindowLevel::ScreenSaver: + return CGWindowLevelForKey(kCGScreenSaverWindowLevelKey); + } + return NSNormalWindowLevel; +} + +static nativeapi::WindowLevel NativeApiWindowLevelFromNS(NSInteger level) { + if (level >= CGWindowLevelForKey(kCGScreenSaverWindowLevelKey)) { + return nativeapi::WindowLevel::ScreenSaver; + } + if (level >= NSStatusWindowLevel) { + return nativeapi::WindowLevel::Status; + } + if (level >= NSFloatingWindowLevel) { + return nativeapi::WindowLevel::Floating; + } + return nativeapi::WindowLevel::Normal; +} + +// Allow windows at Status / ScreenSaver level to span the full display (above +// the menu bar and Dock) without AppKit clamping their frame below the menu bar. +static void NativeApiInstallUnconstrainedFrameOverride(NSWindow* window) { + if (!window) return; + static const void* installed_key = &installed_key; + Class cls = object_getClass(window); + if (objc_getAssociatedObject(cls, installed_key)) return; + SEL selector = @selector(constrainFrameRect:toScreen:); + Method method = class_getInstanceMethod(cls, selector); + if (!method) return; + auto original = reinterpret_cast( + method_getImplementation(method)); + IMP replacement = imp_implementationWithBlock(^NSRect(NSWindow* instance, + NSRect frame_rect, + NSScreen* screen) { + if ([instance level] >= NSStatusWindowLevel) { + return frame_rect; + } + return original(instance, selector, frame_rect, screen); + }); + if (!class_addMethod(cls, selector, replacement, method_getTypeEncoding(method))) { + class_replaceMethod(cls, selector, replacement, method_getTypeEncoding(method)); + } + objc_setAssociatedObject(cls, installed_key, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } // The content may paint a backing of its own over the window's background. A view @@ -239,8 +295,10 @@ static void NativeApiApplyTitleBarAppearance(NSWindow* window) { // over, or gives back, the title bar area. const NSRect frame = window.frame; NSNumber* shape_frame = objc_getAssociatedObject(window, kWindowShapeFrameKey); + NSNumber* frameless_saved = objc_getAssociatedObject(window, kWindowFramelessSavedStyleKey); + const BOOL is_frameless = [objc_getAssociatedObject(window, kWindowFramelessKey) boolValue]; auto mask = window.styleMask; - if (shape_frame && NativeApiWindowIsTitleBarHidden(window)) { + if ((shape_frame && NativeApiWindowIsTitleBarHidden(window)) || is_frameless) { mask &= ~(NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskFullSizeContentView); } else { @@ -251,6 +309,12 @@ static void NativeApiApplyTitleBarAppearance(NSWindow* window) { (NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable); objc_setAssociatedObject(window, kWindowShapeFrameKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } + if (frameless_saved) { + mask |= [frameless_saved unsignedLongLongValue] & + (NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable); + objc_setAssociatedObject(window, kWindowFramelessSavedStyleKey, nil, + OBJC_ASSOCIATION_RETAIN_NONATOMIC); + } } if (window.styleMask != mask) window.styleMask = mask; window.titleVisibility = full_size ? NSWindowTitleHidden : NSWindowTitleVisible; @@ -456,6 +520,7 @@ static void NativeApiInstallFocusOverride(NSWindow* window) { auto original = reinterpret_cast(method_getImplementation(method)); IMP replacement = imp_implementationWithBlock(^BOOL(NSWindow* instance) { if (objc_getAssociatedObject(instance, kWindowFocusableKey) || + [objc_getAssociatedObject(instance, kWindowFramelessKey) boolValue] || (NativeApiWindowIsTitleBarHidden(instance) && objc_getAssociatedObject(instance, kWindowShapeFrameKey))) { return NativeApiWindowIsFocusable(instance); @@ -465,6 +530,24 @@ static void NativeApiInstallFocusOverride(NSWindow* window) { if (!class_addMethod(cls, selector, replacement, method_getTypeEncoding(method))) { class_replaceMethod(cls, selector, replacement, method_getTypeEncoding(method)); } + SEL main_selector = @selector(canBecomeMainWindow); + Method main_method = class_getInstanceMethod(cls, main_selector); + if (main_method) { + auto original_main = + reinterpret_cast(method_getImplementation(main_method)); + IMP main_replacement = imp_implementationWithBlock(^BOOL(NSWindow* instance) { + if ([objc_getAssociatedObject(instance, kWindowFramelessKey) boolValue] && + ![instance isKindOfClass:[NSPanel class]]) { + return NativeApiWindowIsFocusable(instance); + } + return original_main(instance, main_selector); + }); + if (!class_addMethod(cls, main_selector, main_replacement, + method_getTypeEncoding(main_method))) { + class_replaceMethod(cls, main_selector, main_replacement, + method_getTypeEncoding(main_method)); + } + } objc_setAssociatedObject(cls, installed_key, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @@ -619,26 +702,36 @@ static void NativeApiObserveWindowProperties(NSWindow* window) { } void Window::Show() { - if (auto* coordinator = NativeApiGetFullScreenCoordinator(pimpl_->ns_window_, false)) { + NSWindow* window = pimpl_->ns_window_; + if (!window) return; + if (auto* coordinator = NativeApiGetFullScreenCoordinator(window, false)) { coordinator->pending_hide = NO; } - if (IsFocusable()) NativeApiRememberFocusReturnTarget(pimpl_->ns_window_); - [pimpl_->ns_window_ setIsVisible:YES]; + if (IsFocusable()) NativeApiRememberFocusReturnTarget(window); + const NSWindowAnimationBehavior saved_anim = window.animationBehavior; + window.animationBehavior = NSWindowAnimationBehaviorNone; + [window setIsVisible:YES]; // Panels receive key focus when shown but should not activate the app. - if (![pimpl_->ns_window_ isKindOfClass:[NSPanel class]]) { + if (![window isKindOfClass:[NSPanel class]]) { [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; } - [pimpl_->ns_window_ makeKeyAndOrderFront:nil]; - NativeApiAttachPendingParentWindow(pimpl_->ns_window_); + [window makeKeyAndOrderFront:nil]; + window.animationBehavior = saved_anim; + NativeApiAttachPendingParentWindow(window); } void Window::ShowInactive() { - if (auto* coordinator = NativeApiGetFullScreenCoordinator(pimpl_->ns_window_, false)) { + NSWindow* window = pimpl_->ns_window_; + if (!window) return; + if (auto* coordinator = NativeApiGetFullScreenCoordinator(window, false)) { coordinator->pending_hide = NO; } - [pimpl_->ns_window_ setIsVisible:YES]; - [pimpl_->ns_window_ orderFrontRegardless]; - NativeApiAttachPendingParentWindow(pimpl_->ns_window_); + const NSWindowAnimationBehavior saved_anim = window.animationBehavior; + window.animationBehavior = NSWindowAnimationBehaviorNone; + [window setIsVisible:YES]; + [window orderFrontRegardless]; + window.animationBehavior = saved_anim; + NativeApiAttachPendingParentWindow(window); } void Window::Hide() { @@ -656,8 +749,16 @@ static void NativeApiObserveWindowProperties(NSWindow* window) { coordinator->pending_hide = NO; } NativeApiDetachFromParentWindow(window, true); + // Suppress AppKit's fade-and-shrink animation for document windows so Hide() + // removes the window within a single frame, matching Windows and Linux. + const NSWindowAnimationBehavior saved_anim = window.animationBehavior; + window.animationBehavior = NSWindowAnimationBehaviorNone; + [CATransaction begin]; + [CATransaction setDisableActions:YES]; [window setIsVisible:NO]; [window orderOut:nil]; + [CATransaction commit]; + window.animationBehavior = saved_anim; } bool Window::IsVisible() const { @@ -743,7 +844,8 @@ static void NativeApiProgrammaticZoom(NSWindow* window) { // Convert from topLeft coordinate system to bottom-left (macOS default) NSRect topLeftRect = NSMakeRect(bounds.x, bounds.y, bounds.width, bounds.height); NSRect nsRect = NSRectExt::bottomLeft(topLeftRect); - [pimpl_->ns_window_ setFrame:nsRect display:YES]; + [pimpl_->ns_window_ setFrame:nsRect display:YES animate:NO]; + [pimpl_->ns_window_.contentView layoutSubtreeIfNeeded]; } Rectangle Window::GetBounds() const { @@ -957,30 +1059,53 @@ static void NativeApiProgrammaticZoom(NSWindow* window) { } void Window::SetAlwaysOnTop(bool is_always_on_top) { + SetLevel(is_always_on_top ? WindowLevel::Floating : WindowLevel::Normal); +} + +bool Window::IsAlwaysOnTop() const { + return NativeApiWindowIsAlwaysOnTop(pimpl_->ns_window_); +} + +bool Window::SetLevel(WindowLevel level) { detail::WindowPropertyScope property_scope(*this); NSWindow* window = pimpl_->ns_window_; - if (!window) return; - if (is_always_on_top) { + if (!window) return false; + const NSInteger target = NativeApiNSWindowLevel(level); + if (target >= NSStatusWindowLevel) { + NativeApiInstallUnconstrainedFrameOverride(window); + } + if (target >= NSFloatingWindowLevel) { auto* coordinator = NativeApiGetFullScreenCoordinator(window, true); if (IsFullScreen()) { coordinator->restore_always_on_top = YES; + coordinator->restore_level = target; [window setLevel:NSNormalWindowLevel]; } else { coordinator->restore_always_on_top = NO; - [window setLevel:NSFloatingWindowLevel]; + coordinator->restore_level = target; + [window setLevel:target]; } } else { if (auto* coordinator = NativeApiGetFullScreenCoordinator(window, false)) { coordinator->restore_always_on_top = NO; + coordinator->restore_level = NSNormalWindowLevel; } - if ([window level] == NSFloatingWindowLevel) { + if ([window level] >= NSFloatingWindowLevel) { [window setLevel:NSNormalWindowLevel]; } } + return true; } -bool Window::IsAlwaysOnTop() const { - return NativeApiWindowIsAlwaysOnTop(pimpl_->ns_window_); +WindowLevel Window::GetLevel() const { + NSWindow* window = pimpl_->ns_window_; + if (!window) return WindowLevel::Normal; + if (auto* coordinator = NativeApiGetFullScreenCoordinator(window, false)) { + if (coordinator->restore_always_on_top) { + return NativeApiWindowLevelFromNS(coordinator->restore_level); + } + } + return NativeApiWindowLevelFromNS([window level]); } // One level below NSNormalWindowLevel: beneath every ordinary window, above the desktop. @@ -1089,15 +1214,24 @@ static void NativeApiProgrammaticZoom(NSWindow* window) { if (!window) return false; if (is_frameless == IsFrameless()) return true; detail::WindowPropertyScope property_scope(*this); - SetTitleBarStyle(is_frameless ? TitleBarStyle::Hidden : TitleBarStyle::Normal); if (is_frameless) { + if (!objc_getAssociatedObject(window, kWindowFramelessSavedStyleKey)) { + objc_setAssociatedObject(window, kWindowFramelessSavedStyleKey, @(window.styleMask), + OBJC_ASSOCIATION_RETAIN_NONATOMIC); + } + NativeApiInstallFocusOverride(window); objc_setAssociatedObject(window, kWindowFramelessKey, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + objc_setAssociatedObject(window, kWindowTitleBarHiddenKey, @YES, + OBJC_ASSOCIATION_RETAIN_NONATOMIC); + NativeApiApplyTitleBarAppearance(window); NSView* title_view = [[window standardWindowButton:NSWindowCloseButton] superview].superview; if (title_view) { objc_setAssociatedObject(window, kWindowFramelessTitleViewKey, title_view, OBJC_ASSOCIATION_RETAIN_NONATOMIC); title_view.hidden = YES; } + } else { + SetTitleBarStyle(TitleBarStyle::Normal); } return true; } diff --git a/src/platform/ohos/window_ohos.cpp b/src/platform/ohos/window_ohos.cpp index 78abaae..8541729 100644 --- a/src/platform/ohos/window_ohos.cpp +++ b/src/platform/ohos/window_ohos.cpp @@ -448,6 +448,14 @@ bool Window::IsAlwaysOnTop() const { return Cached(pimpl_->state_, &ohos::WindowState::always_on_top); } +bool Window::SetLevel(WindowLevel /*level*/) { + return false; +} + +WindowLevel Window::GetLevel() const { + return WindowLevel::Normal; +} + void Window::SetAlwaysOnBottom(bool is_always_on_bottom) { // not supported on OpenHarmony } diff --git a/src/platform/wasm/window_wasm.cpp b/src/platform/wasm/window_wasm.cpp index af0dec7..656de39 100644 --- a/src/platform/wasm/window_wasm.cpp +++ b/src/platform/wasm/window_wasm.cpp @@ -101,6 +101,12 @@ void Window::SetAlwaysOnTop(bool is_always_on_top) {} bool Window::IsAlwaysOnTop() const { return false; } +bool Window::SetLevel(WindowLevel /*level*/) { + return false; +} +WindowLevel Window::GetLevel() const { + return WindowLevel::Normal; +} void Window::SetAlwaysOnBottom(bool is_always_on_bottom) {} bool Window::IsAlwaysOnBottom() const { return false; diff --git a/src/platform/windows/screen_capture_windows.cpp b/src/platform/windows/screen_capture_windows.cpp index 3b27e90..80bd460 100644 --- a/src/platform/windows/screen_capture_windows.cpp +++ b/src/platform/windows/screen_capture_windows.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include "../../display.h" #include "../../foundation/dispatcher.h" @@ -99,6 +100,9 @@ class WindowsStream : public CaptureStream { DirectXPixelFormat::R16G16B16A16Float, 2, size_); session_ = pool_.CreateCaptureSession(item_); session_.IsCursorCaptureEnabled(options.include_cursor); + if (options.exclude_own_windows) { + ApplyOwnWindowExclusion(); + } session_.StartCapture(); return true; } catch (const hresult_error& e) { @@ -193,6 +197,7 @@ class WindowsStream : public CaptureStream { return nullptr; } bool Stop(std::string& error) override { + RestoreOwnWindowExclusion(); try { if (session_) { session_.Close(); session_ = nullptr; } if (pool_) { pool_.Close(); pool_ = nullptr; } @@ -205,6 +210,34 @@ class WindowsStream : public CaptureStream { } ~WindowsStream() override { std::string ignored; Stop(ignored); } private: + void ApplyOwnWindowExclusion() { + struct Context { + DWORD pid; + std::vector>* saved; + } ctx{GetCurrentProcessId(), &excluded_windows_}; + EnumWindows( + [](HWND hwnd, LPARAM lparam) -> BOOL { + auto* c = reinterpret_cast(lparam); + DWORD pid = 0; + GetWindowThreadProcessId(hwnd, &pid); + if (pid == c->pid && IsWindowVisible(hwnd)) { + DWORD prev = WDA_NONE; + if (GetWindowDisplayAffinity(hwnd, &prev) && prev != WDA_EXCLUDEFROMCAPTURE) { + if (SetWindowDisplayAffinity(hwnd, WDA_EXCLUDEFROMCAPTURE)) { + c->saved->push_back({hwnd, prev}); + } + } + } + return TRUE; + }, + reinterpret_cast(&ctx)); + } + void RestoreOwnWindowExclusion() { + for (const auto& [hwnd, prev] : excluded_windows_) { + if (IsWindow(hwnd)) SetWindowDisplayAffinity(hwnd, prev); + } + excluded_windows_.clear(); + } void Resize() { // Recreate alone can leave a static target with no new damage/frame after // the oversized transition frame was discarded. Restart consumption of @@ -230,6 +263,7 @@ class WindowsStream : public CaptureStream { Direct3D11CaptureFramePool pool_{nullptr}; GraphicsCaptureSession session_{nullptr}; winrt::Windows::Graphics::SizeInt32 size_{}; + std::vector> excluded_windows_; }; class WindowsTarget : public CaptureTarget { diff --git a/src/platform/windows/window_windows.cpp b/src/platform/windows/window_windows.cpp index bddd5bc..bf42e2d 100644 --- a/src/platform/windows/window_windows.cpp +++ b/src/platform/windows/window_windows.cpp @@ -2207,6 +2207,28 @@ bool Window::IsAlwaysOnTop() const { return (exStyle & WS_EX_TOPMOST) != 0; } +bool Window::SetLevel(WindowLevel level) { + if (!pimpl_->hwnd_ || !IsWindow(pimpl_->hwnd_)) + return false; + const bool topmost = level != WindowLevel::Normal; + SetAlwaysOnTop(topmost); + SetPropW(pimpl_->hwnd_, L"NativeAPIWindowLevel", + reinterpret_cast(static_cast(level))); + return true; +} + +WindowLevel Window::GetLevel() const { + if (!pimpl_->hwnd_ || !IsWindow(pimpl_->hwnd_)) + return WindowLevel::Normal; + if (!IsAlwaysOnTop()) + return WindowLevel::Normal; + HANDLE prop = GetPropW(pimpl_->hwnd_, L"NativeAPIWindowLevel"); + if (!prop) + return WindowLevel::Floating; + auto level = static_cast(reinterpret_cast(prop)); + return level == WindowLevel::Normal ? WindowLevel::Floating : level; +} + void Window::SetAlwaysOnBottom(bool is_always_on_bottom) { detail::WindowPropertyScope property_scope(*this); pimpl_->always_on_bottom_ = is_always_on_bottom; diff --git a/src/screen_capturer.h b/src/screen_capturer.h index 15b5101..7075dfc 100644 --- a/src/screen_capturer.h +++ b/src/screen_capturer.h @@ -24,6 +24,8 @@ struct CaptureSourceSelectionOptions { struct ScreenCaptureOptions { /** Include the pointer in the captured pixels; unsupported modes fail. */ bool include_cursor = false; + /** Exclude this application's own windows from display captures when supported. */ + bool exclude_own_windows = false; /** Apply crop; when false the rectangle is ignored. */ bool crop_enabled = false; /** Source-local physical pixels, top-left origin, not global logical coordinates. diff --git a/src/window.h b/src/window.h index 8034647..cbdb427 100644 --- a/src/window.h +++ b/src/window.h @@ -147,6 +147,21 @@ enum class WindowCornerPreference { RoundSmall }; +/** + * @brief Stacking level of a top-level window relative to system chrome. + * @see Window::SetLevel() for platform availability. + */ +enum class WindowLevel { + /** Normal application window stacking order. */ + Normal, + /** Above normal windows; equivalent to SetAlwaysOnTop(true). */ + Floating, + /** Above the menu bar / taskbar, e.g. status overlays. */ + Status, + /** Above everything normally visible, including the Dock. */ + ScreenSaver +}; + /** Native window-server number; distinct from nativeapi's WindowId. */ using WindowNumber = int64_t; @@ -1169,6 +1184,38 @@ class Window : public EventEmitter, public NativeObjectProvider, */ bool IsAlwaysOnTop() const; + /** + * @brief Sets the stacking level of the window relative to system chrome. + * + * Higher levels keep the window above the menu bar, Dock, or taskbar. + * On macOS, levels at or above WindowLevel::Status also allow SetBounds() + * to span the full display without being constrained below the menu bar. + * Setting Floating, Status, or ScreenSaver implies always-on-top; setting + * Normal clears it. + * + * @param level The desired stacking level + * @return true if the level was applied, false if unsupported or invalid + * + * @note Platform availability: + * - macOS: ✅ Fully supported - Maps to NSWindow levels (Normal, Floating, + * Status, ScreenSaver) and skips AppKit frame clamping at Status and above. + * - Windows: ✅ Supported - Normal uses HWND_NOTOPMOST; Floating, Status and + * ScreenSaver use HWND_TOPMOST (a borderless topmost window covers the taskbar). + * - Linux: ⚠️ Supported on X11 via _NET_WM_STATE_ABOVE; compositor-dependent on Wayland. + * - Android: ❌ Not applicable - Always returns false + * - iOS: ❌ Not applicable - Always returns false + * - OpenHarmony: ❌ Not applicable - Always returns false + */ + bool SetLevel(WindowLevel level); + + /** + * @brief Gets the current stacking level of the window. + * + * @return The current WindowLevel, or WindowLevel::Normal when unavailable. + * @see SetLevel() for platform availability. + */ + WindowLevel GetLevel() const; + /** * @brief Sets whether the window stays beneath all other normal windows. *