Swiftui:在单击应用程序的码头图标时,如何使您的应用程序进入前面?

发布于 2025-01-21 20:19:33 字数 963 浏览 3 评论 0原文

当将SwiftUI应用程序最小化并单击码头图标时。该应用不会像其他应用程序那样被杀死并将其放在前面。

import SwiftUI

@main
struct MyApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            MainView()
        }
    }
}

class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
        // THIS IS NEVER CALLED!!!
        if !flag {
            for window: AnyObject in sender.windows {
                window.makeKeyAndOrderFront(self)
            }
        }
        
        return true
    }
}

其他委托方法(例如ApplicationDidLaunch)确实被调用,因此它不是链接问题。有人知道如何使它起作用吗?

评论asperi

When a SwiftUI app is minimized and the dock icon is clicked. The app won't be deminimized and put to the front just like other apps do.

import SwiftUI

@main
struct MyApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            MainView()
        }
    }
}

class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
        // THIS IS NEVER CALLED!!!
        if !flag {
            for window: AnyObject in sender.windows {
                window.makeKeyAndOrderFront(self)
            }
        }
        
        return true
    }
}

Other delegate methods like applicationDidLaunch do get called so its not a linking issue. Does anyone know how to get this to work?

Comment on Asperi
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尘世孤行 2025-01-28 20:19:33

2022

将显示应用程序被隐藏或最小化的应用程序!

func applicationDidBecomeActive(_ notification: Notification) {
    NSApp.unhide(self)
    
    if let wnd = NSApp.windows.first {
        wnd.makeKeyAndOrderFront(self)
        wnd.setIsVisible(true)
    }
}

2022

Will display app in case of it is hidden or minimized both!

func applicationDidBecomeActive(_ notification: Notification) {
    NSApp.unhide(self)
    
    if let wnd = NSApp.windows.first {
        wnd.makeKeyAndOrderFront(self)
        wnd.setIsVisible(true)
    }
}
你曾走过我的故事 2025-01-28 20:19:33

在单击Dock图标时被调用的委托方法,请尝试调用

NSApp.activate(ignoringOtherApps: true)

此应用程序应将您的应用程序带到前景

In delegate methods that get called when clicking on the dock icon try calling

NSApp.activate(ignoringOtherApps: true)

This should bring your app to the foreground

不必你懂 2025-01-28 20:19:33

使用确实成为主动回调,被测试为Xcode 13.3/MacOS 12.2.1

类似,

func applicationDidBecomeActive(_ notification: Notification) {
    if NSApp.windows.compactMap({ $0.isVisible ? Optional(true) : nil }).isEmpty {
         NSApp.windows.first?.makeKeyAndOrderFront(self)
    }
}

项目中的测试模块在此处

注意:如果有活动应用程序,则在单击dock图标中唯一的application(will/dod)更新

Use did become active callback, tested as worked with Xcode 13.3 / macOS 12.2.1

Like,

func applicationDidBecomeActive(_ notification: Notification) {
    if NSApp.windows.compactMap({ $0.isVisible ? Optional(true) : nil }).isEmpty {
         NSApp.windows.first?.makeKeyAndOrderFront(self)
    }
}

Test module in project is here

Note: in case of active application the only application(Will/Did)Update are called in click Dock icon.

若言繁花未落 2025-01-28 20:19:33

我找到了这个解决方案。

在您的@Main输入类应用程序中添加此属性:

@NSApplicationDelegateAdaptor var appDelegate: AppDelegate

现在应该看起来像这样:

import SwiftUI

@main
struct TestApp: App {
    @NSApplicationDelegateAdaptor var appDelegate: AppDelegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

现在,创建AppDelegate类(您可以创建一个新的独立的AppDelegate.swift文件,或者在App struct关闭后继续键入:

class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidChangeOcclusionState(_ notification: Notification) {
        if let window = NSApp.windows.first, window.isMiniaturized {
            NSApp.hide(self)
        }
    }
    
    func applicationDidBecomeActive(_ notification: Notification) {
        NSApp.windows.first?.makeKeyAndOrderFront(self)
    }
}

现在它正在最小化,最小化, 最大化,关闭和重新开放(Ex。Safari,Mail等)。

与其他应用程序一样,

I found this solution.

In your @main entry class App add this property:

@NSApplicationDelegateAdaptor var appDelegate: AppDelegate

It now should looks like this:

import SwiftUI

@main
struct TestApp: App {
    @NSApplicationDelegateAdaptor var appDelegate: AppDelegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Now, create the AppDelegate class (you can create a new separate AppDelegate.swift file or just continue typing after the App struct closed:

class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidChangeOcclusionState(_ notification: Notification) {
        if let window = NSApp.windows.first, window.isMiniaturized {
            NSApp.hide(self)
        }
    }
    
    func applicationDidBecomeActive(_ notification: Notification) {
        NSApp.windows.first?.makeKeyAndOrderFront(self)
    }
}

Now it is minimizing, maximizing, closing and reopening again as normal as other apps (ex. Safari, Mail, etc.).

This works perfectly on Monterey 12.4.

最美不过初阳 2025-01-28 20:19:33

这是围绕我有用的临时工作

class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
    // disable tabs ...
    NSWindow.allowsAutomaticWindowTabbing = false
}

// Quit if the main window is closed
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
    BzLogger[Self.self].info("")
    return true
}

func applicationDidUpdate(_ notification: Notification) {
    if NSApplication.shared.mainWindow == nil,
       let event = NSApplication.shared.currentEvent {
        if event.type == .systemDefined {
            // FIX-ME: kdeda
            // This is hack since Apple broke this whole thing on macOS
            
            // BzLogger[Self.self].info("NSApplication.shared.currentEvent: \(event)")
            // BzLogger[Self.self].info("NSApplication.shared.currentEvent: \(event.subtype)")
            // BzLogger[Self.self].info("NSApplication.shared.pressedMouseButtons: \(NSEvent.pressedMouseButtons)")
            // BzLogger[Self.self].info("NSApplication.shared.isActive: \(NSApplication.shared.isActive)")
            if NSEvent.pressedMouseButtons == 1 {
                // mouse down, maybe there is a better way
                if NSApp.windows.compactMap({ $0.isVisible ? Optional(true) : nil }).isEmpty {
                    NSApp.windows.first?.makeKeyAndOrderFront(self)
                }
            }
        }
    }
}

}

This is a temporary work around that works for me

class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
    // disable tabs ...
    NSWindow.allowsAutomaticWindowTabbing = false
}

// Quit if the main window is closed
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
    BzLogger[Self.self].info("")
    return true
}

func applicationDidUpdate(_ notification: Notification) {
    if NSApplication.shared.mainWindow == nil,
       let event = NSApplication.shared.currentEvent {
        if event.type == .systemDefined {
            // FIX-ME: kdeda
            // This is hack since Apple broke this whole thing on macOS
            
            // BzLogger[Self.self].info("NSApplication.shared.currentEvent: \(event)")
            // BzLogger[Self.self].info("NSApplication.shared.currentEvent: \(event.subtype)")
            // BzLogger[Self.self].info("NSApplication.shared.pressedMouseButtons: \(NSEvent.pressedMouseButtons)")
            // BzLogger[Self.self].info("NSApplication.shared.isActive: \(NSApplication.shared.isActive)")
            if NSEvent.pressedMouseButtons == 1 {
                // mouse down, maybe there is a better way
                if NSApp.windows.compactMap({ $0.isVisible ? Optional(true) : nil }).isEmpty {
                    NSApp.windows.first?.makeKeyAndOrderFront(self)
                }
            }
        }
    }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文