Skip to content

Commit

Permalink
Fix issue #3 and #4
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaogdgenuine committed Apr 19, 2022
1 parent 3538006 commit 2f6175e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Doll.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
3A05F079FD8ED95600C00E09 /* NSEventExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A05FE1708B1B3E267E75950 /* NSEventExtensions.swift */; };
3A05F0D44C56AF54405AF0C8 /* README.md in Sources */ = {isa = PBXBuildFile; fileRef = 3A05F569906D4FC793ED3B11 /* README.md */; };
3A05F16312AEFBD25670304B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3A05FE513B0F76D50EE9A90B /* Assets.xcassets */; };
3A05F319D3B6EACC3882AF88 /* UserDefaultSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A05F135761A1F95B691AC9F /* UserDefaultSettings.swift */; };
Expand Down Expand Up @@ -34,6 +35,7 @@
3A05F569906D4FC793ED3B11 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = Doll/README.md; sourceTree = "<group>"; };
3A05F6A27D4869EB06B6E98D /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
3A05F88D951B0284A8B90FF0 /* Doll.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Doll.app; sourceTree = BUILT_PRODUCTS_DIR; };
3A05FE1708B1B3E267E75950 /* NSEventExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSEventExtensions.swift; sourceTree = "<group>"; };
3A05FE513B0F76D50EE9A90B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
3A05FF26C7DEDD4C5CBF1E98 /* Doll.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Doll.entitlements; sourceTree = "<group>"; };
89CB806427A311400065CA70 /* MonitorCore */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = MonitorCore; sourceTree = "<group>"; };
Expand Down Expand Up @@ -120,6 +122,7 @@
children = (
89CB807527A312F10065CA70 /* ViewExtensions.swift */,
3A05F135761A1F95B691AC9F /* UserDefaultSettings.swift */,
3A05FE1708B1B3E267E75950 /* NSEventExtensions.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -251,6 +254,7 @@
3A05F319D3B6EACC3882AF88 /* UserDefaultSettings.swift in Sources */,
3A05FBC07A5CF3CCC2701FBC /* AppSettings.swift in Sources */,
3A05F0D44C56AF54405AF0C8 /* README.md in Sources */,
3A05F079FD8ED95600C00E09 /* NSEventExtensions.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
12 changes: 12 additions & 0 deletions Doll/AppDelegate.swift
Expand Up @@ -2,10 +2,22 @@ import SwiftUI

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var appActivated = false

func applicationDidFinishLaunching(_ aNotification: Notification) {
MonitorEngine.setup()
}

func applicationWillBecomeActive(_ notification: Notification) {
// https://github.com/xiaogdgenuine/Doll/issues/4
// Create new instance if the app relaunch again from Spotlight or manually open by user
if appActivated {
MonitorEngine.createNewInstanceIfNecessary()
}

appActivated = true
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
Expand Down
6 changes: 2 additions & 4 deletions Doll/ContentView.swift
Expand Up @@ -102,10 +102,8 @@ struct ContentView: View {
}
}
HStack {
if let _ = statusBar.monitoredApp {
Button("Stop monitor") {
statusBar.destroy()
}
Button("Stop monitor") {
statusBar.destroy()
}

Spacer()
Expand Down
34 changes: 23 additions & 11 deletions Doll/Source/MonitorEngine.swift
Expand Up @@ -21,15 +21,15 @@ class MonitorEngine {
}

if monitoredApps.count == 0 {
return firstTimeSetup()
return createNewInstance()
}

monitoredApps.forEach { app in
let statusBar = createNewStatusBar()
statusBar.monitorApp(app: app)
}
} else {
firstTimeSetup()
createNewInstance()
}
}

Expand Down Expand Up @@ -64,27 +64,39 @@ class MonitorEngine {
saveSelectedApps()

if statusBars.count == 0 {
firstTimeSetup()
createNewInstance()
}
}

private static func saveSelectedApps() {
let data = statusBars.compactMap {
$0.monitoredApp?.bundleId
}
.joined(separator: ",")
UserDefaults.standard.set(data, forKey: SETTING_MONITORED_APP_IDS)
UserDefaults.standard.synchronize()
static func createNewInstanceIfNecessary() {
if let unAssignedMonitor = (statusBars.first {
$0.monitoredApp == nil
}) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
unAssignedMonitor.showPopover()
}
} else {
createNewInstance()
}
}

private static func firstTimeSetup() {
static func createNewInstance() {
let newStatusBar = createNewStatusBar()

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
newStatusBar.showPopover()
}
}

private static func saveSelectedApps() {
let data = statusBars.compactMap {
$0.monitoredApp?.bundleId
}
.joined(separator: ",")
UserDefaults.standard.set(data, forKey: SETTING_MONITORED_APP_IDS)
UserDefaults.standard.synchronize()
}

private static func createNewStatusBar() -> StatusBarController {
let popover = NSPopover()
popover.contentSize = NSSize(width: 600, height: 500)
Expand Down
9 changes: 9 additions & 0 deletions Doll/Utils/NSEventExtensions.swift
@@ -0,0 +1,9 @@
import AppKit

extension NSEvent {
var isRightClick: Bool {
let rightClick = (self.type == .rightMouseUp)
let controlClick = self.modifierFlags.contains(.control)
return rightClick || controlClick
}
}
11 changes: 8 additions & 3 deletions Doll/Views/StatusBarController.swift
Expand Up @@ -18,6 +18,7 @@ class StatusBarController {
statusItem = statusBar.statusItem(withLength: iconSize)

if let statusBarButton = statusItem.button {
statusBarButton.sendAction(on: [.leftMouseUp, .rightMouseUp])
statusBarButton.image = defaultIcon
statusBarButton.image?.size = NSSize(width: iconSize, height: iconSize)
statusBarButton.image?.isTemplate = false
Expand All @@ -38,8 +39,9 @@ class StatusBarController {

let noAppSelected = statusItem.button?.image == defaultIcon
let isOptionKeyHolding = NSEvent.modifierFlags.contains(.option)
let isRightClick = NSApp.currentEvent?.isRightClick == true

if noAppSelected || isOptionKeyHolding {
if noAppSelected || isOptionKeyHolding || isRightClick {
if (mainPopover.isShown) {
hidePopover()
} else {
Expand All @@ -65,8 +67,11 @@ class StatusBarController {
guard let appFullPath = NSWorkspace.shared.urlForApplication(withBundleIdentifier: app.bundleId)?.absoluteURL.path else {
return
}

guard let targetBundle = Bundle(path: appFullPath),
let appName = targetBundle.object(forInfoDictionaryKey: kCFBundleNameKey as String) as? String else {
let appName = (
targetBundle.object(forInfoDictionaryKey: "CFBundleDisplayName") ??
targetBundle.object(forInfoDictionaryKey: kCFBundleNameKey as String)) as? String else {
return
}

Expand Down Expand Up @@ -130,8 +135,8 @@ class StatusBarController {
if let monitoredApp = monitoredApp {
MonitorService.unObserve(appName: monitoredApp.appName)
MonitorEngine.unMonitor(app: monitoredApp)
statusBar.removeStatusItem(statusItem)
}
statusBar.removeStatusItem(statusItem)
}

private func createNotificationPopover(newText: String) -> NSPopover {
Expand Down
1 change: 0 additions & 1 deletion MonitorCore/Sources/Monitor/MonitorService.swift
Expand Up @@ -45,7 +45,6 @@ public struct MonitorService {

public static func unObserve(appName: String) {
observedAppInfos.removeValue(forKey: appName)
print(observedAppInfos.count)
}

public static func openMonitoredApp(appName: String) {
Expand Down

0 comments on commit 2f6175e

Please sign in to comment.