Make one of multiple NSWindow persistent across all the windows, including fullscreen windows

45 Views Asked by At

I'm trying to create a window with border which is persistent across all the windows, including fullscreen windows. Something like what zoom does when you're sharing your scereen. Now I do have a code which works well in silo which I have pasted below i.e persists across all the screens including fullscreen apps.

But when I try to expose the same code through node bindings so that my electron application can trigger the initialization of that window, it only shows up on desktop window and the electron window (when forced with orderFront) when in fullscreen but gets hidden on other full screen apps even with orderFront or orderFrontRegardless.

When I list out all the Windows through electron context I see there are 3 windows, 1 being the electron window, 1 the newly created and one more which I'm not sure what is.

I'm new to Objective-C and native macOS development, would be grateful for any help.

@interface GreenBorderView : NSView
@end

@implementation GreenBorderView

- (void)drawRect:(NSRect)dirtyRect {
    // Draw a green border
    [[NSColor greenColor] set];
    [NSBezierPath setDefaultLineWidth:5.0];
    [NSBezierPath strokeRect:NSInsetRect(self.bounds, 5, 5)];
}

@end

@interface YourExistingController : NSObject
@property (nonatomic, strong) NSWindow *window;
- (void)startManagingWindow;

@end

@implementation YourExistingController

- (void)startManagingWindow {
    // Show the windows
    self.window = [[NSWindow alloc] initWithContentRect:[NSScreen mainScreen].frame
                                                        styleMask:NSWindowStyleMaskBorderless
                                                          backing:NSBackingStoreBuffered
                                                            defer:NO];

    [self.window setCollectionBehavior:(NSWindowCollectionBehaviorCanJoinAllSpaces | NSWindowCollectionBehaviorFullScreenAuxiliary)];
    // Create and add the GreenBorderView to the window's contentView
    GreenBorderView *greenBorderView = [[GreenBorderView alloc] initWithFrame:self.window.contentView.bounds];
    [self.window.contentView addSubview:greenBorderView];

    // Set the GreenBorderView as the contentView
    [self.window setContentView:greenBorderView];

    // Configure the window
    [self.window setLevel:NSMainMenuWindowLevel + 1];
    //[window setLevel:NSStatusWindowLevel];
    [self.window setBackgroundColor:[NSColor clearColor]];
    [self.window setOpaque:NO];
    [self.window setTitle: @"Green border"];

    [self.window makeKeyAndOrderFront:nil];

    // Retrieve the list of windows
    NSArray<NSWindow *> *allWindows = [NSApp windows];

    // Log information about each window
    NSString *toMatch = @"UC LOCAL";
    for (NSWindow *windowitr in allWindows) {
        NSString *title = [windowitr title];
        NSLog(@"Window title: %@", title);

    }

    // Schedule a timer to bring the window to front every second
    [NSTimer scheduledTimerWithTimeInterval:1.0
                                     repeats:YES
                                       block:^(NSTimer *timer) {
                                           [self.window orderFrontRegardless];
                                       }];
}

@end


void drawFullScreenBorder() {
    @autoreleasepool {
        YourExistingController *controller = [[YourExistingController alloc] init];
        [controller startManagingWindow];
    }
}
0

There are 0 best solutions below