fix: shortcuts

This commit is contained in:
Tienson Qin
2025-11-23 22:34:36 +08:00
parent 6d0a8fbe9f
commit 8da04df8c1
2 changed files with 65 additions and 1 deletions

View File

@@ -76,7 +76,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UINavigationControllerDel
// no-op, unless you want to clean anything up
}
private func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem) -> Bool {
@discardableResult
func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem) -> Bool {
switch shortcutItem.type {
case "logseq.quickadd":
donateQuickAddShortcut()

View File

@@ -54,6 +54,8 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// 4) Start observing route changes (your existing logic)
(UIApplication.shared.delegate as? AppDelegate)?.startRouteObservation()
handleInitialSceneOptions(connectionOptions)
}
// Optional, but nice to have:
@@ -62,4 +64,65 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func sceneWillResignActive(_ scene: UIScene) { }
func sceneWillEnterForeground(_ scene: UIScene) { }
func sceneDidEnterBackground(_ scene: UIScene) { }
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
forwardUserActivity(userActivity)
}
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
forwardURLContexts(URLContexts)
}
func windowScene(_ windowScene: UIWindowScene,
performActionFor shortcutItem: UIApplicationShortcutItem,
completionHandler: @escaping (Bool) -> Void) {
let handled = (UIApplication.shared.delegate as? AppDelegate)?
.handleShortcutItem(shortcutItem) ?? false
completionHandler(handled)
}
private func handleInitialSceneOptions(_ options: UIScene.ConnectionOptions) {
if let shortcut = options.shortcutItem {
DispatchQueue.main.async {
_ = (UIApplication.shared.delegate as? AppDelegate)?
.handleShortcutItem(shortcut)
}
}
if let activity = options.userActivities.first {
forwardUserActivity(activity)
}
if !options.urlContexts.isEmpty {
forwardURLContexts(options.urlContexts)
}
}
private func forwardUserActivity(_ userActivity: NSUserActivity) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
_ = appDelegate.application(UIApplication.shared,
continue: userActivity,
restorationHandler: { _ in })
}
private func forwardURLContexts(_ contexts: Set<UIOpenURLContext>) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
for context in contexts {
let sceneOptions = context.options
var appOptions: [UIApplication.OpenURLOptionsKey: Any] = [:]
if let sourceApplication = sceneOptions.sourceApplication {
appOptions[.sourceApplication] = sourceApplication
}
if let annotation = sceneOptions.annotation {
appOptions[.annotation] = annotation
}
appOptions[.openInPlace] = sceneOptions.openInPlace
if let eventAttribution = sceneOptions.eventAttribution {
appOptions[.eventAttribution] = eventAttribution
}
_ = appDelegate.application(UIApplication.shared,
open: context.url,
options: appOptions)
}
}
}