Skip to content

Commit

Permalink
Detect reopeing events correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaulomatic committed May 8, 2024
1 parent 41e6f2c commit d5a83ce
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
4 changes: 2 additions & 2 deletions AirPods Sanity.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1.0.4;
CURRENT_PROJECT_VERSION = 1.0.4.1;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"AirPods Sanity/Preview Content\"";
DEVELOPMENT_TEAM = 2P9YZ8CQSH;
Expand Down Expand Up @@ -395,7 +395,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1.0.4;
CURRENT_PROJECT_VERSION = 1.0.4.1;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"AirPods Sanity/Preview Content\"";
DEVELOPMENT_TEAM = 2P9YZ8CQSH;
Expand Down
Binary file not shown.
50 changes: 41 additions & 9 deletions AirPods Sanity/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ import SimplyCoreAudio

class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject
{
private let notificationName = Notification.Name("eu.punke.AirPods-Sanity.AppLaunched")
private let NotificationName = Notification.Name("eu.punke.AirPods-Sanity.AppLaunched")

private let _Preferences: Preferences

private var _Devices: DevicesObserver!
private var _MenuBar: MenuBar!
private var _Subscription: IDisposable!

override init()
{
self._Preferences = Preferences.Instance
}

func applicationDidFinishLaunching(_ notification: Notification)
{
Expand All @@ -29,10 +36,27 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject
SetupApplication()
}
}
func applicationDidBecomeActive(_ notification: Notification)
{
}

func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool
{
false
// https://stackoverflow.com/a/59003304/2239781
let __AppleEvent: NSAppleEventDescriptor? = NSAppleEventManager.shared().currentAppleEvent
let __SenderName: String = __AppleEvent?.attributeDescriptor(forKeyword: keyAddressAttr)?.stringValue ?? ""
// let __SenderPID: Int32? = __AppleEvent?.attributeDescriptor(forKeyword: keySenderPIDAttr)?.int32Value ?? 0

if __SenderName != "Dock"
{
self.PerformSecondLaunchActions()
}
else if !self._Preferences.ShowInDock
{
self.PerformSecondLaunchActions()
}

return false
}

func applicationWillTerminate(_ notification: Notification)
Expand All @@ -53,12 +77,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject

private func SendLaunchNotification()
{
DistributedNotificationCenter.default().post(name: notificationName, object: nil)
DistributedNotificationCenter.default().post(name: NotificationName, object: nil)
}

private func SetupLaunchNotificationListener()
{
DistributedNotificationCenter.default().addObserver(self, selector: #selector(HandleSecondLaunch), name: notificationName, object: nil)
DistributedNotificationCenter.default().addObserver(self, selector: #selector(HandleSecondLaunch), name: NotificationName, object: nil)
}

@objc private func HandleSecondLaunch()
Expand All @@ -73,16 +97,24 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject
return
}

let __ShowInSeconds = 1.0
DispatchQueue.main.asyncAfter(deadline: .now() + __ShowInSeconds)
self._MenuBar.Show()

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0)
{
self._MenuBar.Show()
}

DispatchQueue.main.asyncAfter(deadline: .now() + 2.0)
{
self._MenuBar.Show()
}

let __HideInSeconds = 10.0
DispatchQueue.main.asyncAfter(deadline: .now() + __HideInSeconds)
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0)
{
self._MenuBar.HideWhenNeccessary()
if !self._Preferences.ShowInMenuBar
{
self._MenuBar.Hide()
}
}
}

Expand Down
5 changes: 0 additions & 5 deletions AirPods Sanity/MenuBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ class MenuBar
self._StatusBarItem.isVisible = false;
}

public func HideWhenNeccessary()
{
self.SetShowInMenuBar();
}

private func SetShowInMenuBar()
{
self._StatusBarItem.isVisible = self._Preferences.ShowInMenuBar
Expand Down

0 comments on commit d5a83ce

Please sign in to comment.