From 199871b897041b59a88791021c170d2058af5477 Mon Sep 17 00:00:00 2001 From: TheMoonThatRises <58153205+TheMoonThatRises@users.noreply.github.com> Date: Sun, 31 Dec 2023 16:33:54 -0700 Subject: [PATCH] Fix opening through document type (#753) * fix: open document type while Whisky is closed * fix: crash with zero bottles --- Whisky/AppDelegate.swift | 10 ++++++++++ Whisky/Views/FileOpenView.swift | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Whisky/AppDelegate.swift b/Whisky/AppDelegate.swift index 25524cc30..0d5366aec 100644 --- a/Whisky/AppDelegate.swift +++ b/Whisky/AppDelegate.swift @@ -22,6 +22,16 @@ import SwiftUI class AppDelegate: NSObject, NSApplicationDelegate { @AppStorage("hasShownMoveToApplicationsAlert") private var hasShownMoveToApplicationsAlert = false + func application(_ application: NSApplication, open urls: [URL]) { + // Test if automatic window tabbing is enabled + // as it is disabled when ContentView appears + if NSWindow.allowsAutomaticWindowTabbing, let url = urls.first { + // Reopen the file after Whisky has been opened + // so that the `onOpenURL` handler is actually called + NSWorkspace.shared.open(url) + } + } + func applicationDidFinishLaunching(_ notification: Notification) { if !hasShownMoveToApplicationsAlert && !AppDelegate.insideAppsFolder { DispatchQueue.main.asyncAfter(deadline: .now()) { diff --git a/Whisky/Views/FileOpenView.swift b/Whisky/Views/FileOpenView.swift index 562b7ac0a..2e87af283 100644 --- a/Whisky/Views/FileOpenView.swift +++ b/Whisky/Views/FileOpenView.swift @@ -57,12 +57,14 @@ struct FileOpenView: View { } .frame(minWidth: 400, minHeight: 115) .onAppear { - selection = bottles.first(where: { $0.url == currentBottle })?.url ?? bottles[0].url - if bottles.count <= 1 { // If the user only has one bottle // there's nothing for them to select run() + } else if bottles.count > 0 { + // Makes sure there are more than 0 bottles. + // Otherwise, it will crash on the nil cascade + selection = bottles.first(where: { $0.url == currentBottle })?.url ?? bottles[0].url } } }