How to Highlight the active window and dim the rest windows cocoa app programmatically?

故事扮演 提交于 2019-12-02 18:11:55

问题


In my Cocoa app I want to Highlight which ever the window is active and dim the rest windows. something like window focus application. Below block is my code.

NSNotificationCenter* center = [[NSWorkspace sharedWorkspace] notificationCenter];
[center addObserver:self selector:@selector(newApplicationDidActive:) name:NSWorkspaceDidActivateApplicationNotification object:nil];

- (void)newApplicationDidActive:(NSNotification *)notification {
    NSDictionary* userInfo = notification.userInfo;
    NSLog(@"did Active %@", [userInfo objectForKey:NSWorkspaceApplicationKey ]
    if ([[NSUserDefaults standardUserDefaults] boolForKey:kUDKeyWindowFocus]) {
        NSLog(@"window focus");
        [[FocusWindow defaultHandler].window orderWindow:NSWindowAbove relativeTo:0];
    }
}

Update : Added below code partially working but not accurate can some suggest any ideas or views.

NSPoint mouseLocation = [NSEvent mouseLocation];
NSInteger windowNumber = [NSWindow windowNumberAtPoint:mouseLocation belowWindowWithWindowNumber:0];
NSLog(@"windowNumber %lu",windowNumber);
[focusWindowController.window orderWindow:NSWindowBelow relativeTo:windowNumber];
[app activateWithOptions:NSApplicationActivateIgnoringOtherApps];

Instead of

[[FocusWindow defaultHandler].window orderWindow:NSWindowAbove relativeTo:0];

In First Block windowNumberAtPoint:belowWindowWithWindowNumber which gives Frontmost window number with that i am ordering my dimming window below orderWindow:relativeTo the Frontmost window.

来源:https://stackoverflow.com/questions/51946754/how-to-highlight-the-active-window-and-dim-the-rest-windows-cocoa-app-programmat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!