1

I need to display a modal dialog on the main/frontmost window even if that window is not mine(i.e. the client may be watching a video in fullscreen on Chrome). How can I do that? Currently I'm testing by setting a delay and working in a different app which doesn't work(i.e. the alert is visible only when I switch to the application). I'm looking to develop a kind of system wide plugin thus the reason it needs to be global.

Additional Details: I don't want to move the focus away from the frontmost application. I need something like the UserNotification but with more control over the UI. The user will actually activate the "menu" using a keyboard shortcut.

  DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(10), execute: {
        // Insert code here to initialize your application
        let alert = NSAlert.init()
        alert.messageText = "Hello world"
        alert.informativeText = "Information text"
        alert.addButton(withTitle: "OK")
        alert.addButton(withTitle: "Cancel")
        alert.runModal()
        // Put your code which should be executed with a delay here
    })
themihai
  • 7,903
  • 11
  • 39
  • 61
  • 1
    Isn't `UserNotification` an option? Suddenly moving the focus away from the frontmost application is not a good user experience. – vadian Feb 10 '18 at 11:37
  • UserNotification lacks customisation options to make it useful for my use case. I don't want to move the focus away from the frontmost application. I need something like the UserNotification but with more control over the UI – themihai Feb 10 '18 at 11:46
  • 1
    You can do this by creating a window and setting its `level` and other properties so your window floats above the frontmost application window(s), a la Growl. But you can't satisfy your requirement of "I don't want to move the focus away from the frontmost application" and use an NSAlert window, because `runModal()` makes the window key and starts a modal event loop, which is the ultimate focus "take away." – James Bucanek Feb 10 '18 at 16:32
  • @JamesBucanek the main issue is that I can't make it float over the main window unless I use `NSApp.activate(ignoringOtherApps: true)` and if I do that the app gets the client out of its current workspace/application (i.e. fullscreen watching some video etc). – themihai Feb 10 '18 at 21:52
  • 1
    @themihai, I don't think that's the case. For regular windows, sure, but if you start fiddling with the `level` of the window you can stack that window so it draws on top of all other windows, even windows in other applications. That's how the dock, menu bar, screen savers, notifications, the little indicator that shows when you change the volume, and so on, all work. See [Window Levels](https://developer.apple.com/documentation/appkit/nswindow/window_levels) – James Bucanek Feb 10 '18 at 21:59
  • actually I was about to give the "change volume" indicator as example of the experience I'm trying to develop so I guess I have to study the `Window Levels`. Thanks for your help! – themihai Feb 10 '18 at 22:03
  • 1
    Here was the answer https://stackoverflow.com/a/45184962/613453 – themihai Feb 10 '18 at 22:49

0 Answers0