diff --git a/Mos.xcodeproj/project.pbxproj b/Mos.xcodeproj/project.pbxproj index eb89aa9..26d12de 100644 --- a/Mos.xcodeproj/project.pbxproj +++ b/Mos.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 22003C6E1E8555F0001220BC /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22003C6D1E8555F0001220BC /* Utils.swift */; }; 22142DA41E25344E00E4BFBF /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22142DA31E25344E00E4BFBF /* AppDelegate.swift */; }; 22142DA61E25344E00E4BFBF /* ScrollMonitorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22142DA51E25344E00E4BFBF /* ScrollMonitorViewController.swift */; }; 22142DA81E25344E00E4BFBF /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 22142DA71E25344E00E4BFBF /* Assets.xcassets */; }; @@ -27,7 +28,6 @@ 22937B091E3B657600A45C1D /* PreferencesDonateViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22937B081E3B657600A45C1D /* PreferencesDonateViewController.swift */; }; 22A32B331E334B2E00A5CF38 /* PreferencesAboutViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22A32B321E334B2E00A5CF38 /* PreferencesAboutViewController.swift */; }; 22A32B381E334B4300A5CF38 /* PreferencesUpdatesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22A32B361E334B4300A5CF38 /* PreferencesUpdatesViewController.swift */; }; - 22DFD03B1E840DFA00B1E366 /* LaunchStarter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22DFD03A1E840DFA00B1E366 /* LaunchStarter.swift */; }; 22F4EFEF1E325C36009EED31 /* PreferencesTabViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22F4EFEE1E325C36009EED31 /* PreferencesTabViewController.swift */; }; /* End PBXBuildFile section */ @@ -100,6 +100,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 22003C6D1E8555F0001220BC /* Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = ""; }; 22142DA01E25344E00E4BFBF /* Mos.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Mos.app; sourceTree = BUILT_PRODUCTS_DIR; }; 22142DA31E25344E00E4BFBF /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 22142DA51E25344E00E4BFBF /* ScrollMonitorViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrollMonitorViewController.swift; sourceTree = ""; }; @@ -134,7 +135,6 @@ 2295D8271E40542B00054A5C /* zh-Hant */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hant"; path = "zh-Hant.lproj/Main.strings"; sourceTree = ""; }; 22A32B321E334B2E00A5CF38 /* PreferencesAboutViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferencesAboutViewController.swift; sourceTree = ""; }; 22A32B361E334B4300A5CF38 /* PreferencesUpdatesViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferencesUpdatesViewController.swift; sourceTree = ""; }; - 22DFD03A1E840DFA00B1E366 /* LaunchStarter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LaunchStarter.swift; sourceTree = ""; }; 22F4EFEE1E325C36009EED31 /* PreferencesTabViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferencesTabViewController.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -190,7 +190,7 @@ children = ( 22142DA31E25344E00E4BFBF /* AppDelegate.swift */, 2291A8A21E2A5400004181B8 /* ScrollCore.swift */, - 22DFD03A1E840DFA00B1E366 /* LaunchStarter.swift */, + 22003C6D1E8555F0001220BC /* Utils.swift */, 22A32B3B1E334B7A00A5CF38 /* ScrollMonitorPanel */, 22A32B3A1E334B5900A5CF38 /* PreferencesPanel */, 22A32B3C1E334B9700A5CF38 /* StatusMenu */, @@ -474,9 +474,9 @@ 22A32B381E334B4300A5CF38 /* PreferencesUpdatesViewController.swift in Sources */, 226A2E6E1E2E68620035F1A6 /* MouseEvent.m in Sources */, 22937B091E3B657600A45C1D /* PreferencesDonateViewController.swift in Sources */, - 22DFD03B1E840DFA00B1E366 /* LaunchStarter.swift in Sources */, 22816CFF1E2B31020020D6B1 /* ScrollMonitorWindowController.swift in Sources */, 22F4EFEF1E325C36009EED31 /* PreferencesTabViewController.swift in Sources */, + 22003C6E1E8555F0001220BC /* Utils.swift in Sources */, 22142DA61E25344E00E4BFBF /* ScrollMonitorViewController.swift in Sources */, 2278EE991E3DF770000AB25B /* PreferencesIgnoreViewController.swift in Sources */, 22816D011E2B47FB0020D6B1 /* StatusMenuController.swift in Sources */, diff --git a/Mos/Info.plist b/Mos/Info.plist index 7ba35cd..ac0406e 100644 --- a/Mos/Info.plist +++ b/Mos/Info.plist @@ -19,7 +19,7 @@ CFBundleShortVersionString 1.5.0 CFBundleVersion - 20170303 + 20170324 LSApplicationCategoryType public.app-category.utilities LSMinimumSystemVersion diff --git a/Mos/ScrollCore.swift b/Mos/ScrollCore.swift index 0325a7a..2729e2d 100644 --- a/Mos/ScrollCore.swift +++ b/Mos/ScrollCore.swift @@ -482,35 +482,56 @@ class ScrollCore: NSObject { } return false } - // TODO: 节流处理 // 判断 LaunchPad 是否激活 + static var launchpadActiveCache = false + static var launchpadLastDetectTime = 0.0 static func launchpadIsActive() -> Bool { - let windowInfoList = CGWindowListCopyWindowInfo(CGWindowListOption.optionOnScreenOnly, CGWindowID(0)) as [AnyObject]! - for windowInfo in windowInfoList! { - let windowName = windowInfo[kCGWindowName]! - if windowName != nil && windowName as! String == "LPSpringboard" { - return true + // 如果距离上次检测时间大于500ms, 则重新检测一遍, 否则直接返回上次的结果 + let nowTime = NSDate().timeIntervalSince1970 + if nowTime - missioncontrolLastDetectTime > 0.5 { + ScrollCore.missioncontrolLastDetectTime = nowTime + let windowInfoList = CGWindowListCopyWindowInfo(CGWindowListOption.optionOnScreenOnly, CGWindowID(0)) as [AnyObject]! + for windowInfo in windowInfoList! { + let windowName = windowInfo[kCGWindowName]! + if windowName != nil && windowName as! String == "LPSpringboard" { + ScrollCore.launchpadActiveCache = true + return true + } } + ScrollCore.launchpadActiveCache = false + return false + } else { + ScrollCore.missioncontrolLastDetectTime = nowTime + return ScrollCore.launchpadActiveCache } - return false } - // TODO: 节流处理 // 判断 MissionControl 是否激活 + static var missioncontrolActiveCache = false + static var missioncontrolLastDetectTime = 0.0 static func missioncontrolIsActive() -> Bool { - let windowInfoList = CGWindowListCopyWindowInfo(CGWindowListOption.optionOnScreenOnly, CGWindowID(0)) as [AnyObject]! - for windowInfo in windowInfoList! { - let windowOwnerName = windowInfo[kCGWindowOwnerName]! - if windowOwnerName != nil && windowOwnerName as! String == "Dock" { - if windowInfo[kCGWindowName]! == nil { - return true + // 如果距离上次检测时间大于500ms, 则重新检测一遍, 否则直接返回上次的结果 + let nowTime = NSDate().timeIntervalSince1970 + if nowTime - missioncontrolLastDetectTime > 0.5 { + ScrollCore.missioncontrolLastDetectTime = nowTime + let windowInfoList = CGWindowListCopyWindowInfo(CGWindowListOption.optionOnScreenOnly, CGWindowID(0)) as [AnyObject]! + for windowInfo in windowInfoList! { + let windowOwnerName = windowInfo[kCGWindowOwnerName]! + if windowOwnerName != nil && windowOwnerName as! String == "Dock" { + if windowInfo[kCGWindowName]! == nil { + ScrollCore.missioncontrolActiveCache = true + return true + } } } + ScrollCore.missioncontrolActiveCache = false + return false + } else { + ScrollCore.missioncontrolLastDetectTime = nowTime + return ScrollCore.missioncontrolActiveCache } - return false } - // 打印Log static func printLog(of event: CGEvent) { print(ScrollCore.getScrollLog(of: event)) diff --git a/Mos/LaunchStarter.swift b/Mos/Utils.swift similarity index 95% rename from Mos/LaunchStarter.swift rename to Mos/Utils.swift index b48378f..dc5c17c 100644 --- a/Mos/LaunchStarter.swift +++ b/Mos/Utils.swift @@ -1,21 +1,27 @@ // -// LaunchStarter.swift +// Utils.swift // Mos -// 用于管理开机启动项 -// Created by Cb on 2017/3/23. +// 常用工具类 +// Created by Cb on 2017/3/24. // Copyright © 2017年 Cb. All rights reserved. // -// With help from: https://gist.github.com/plapier/f8e1dde1b1624dfbb3e4 import Foundation + +// +// 用于管理开机启动项 +// Created by Cb on 2017/3/23. +// Copyright © 2017年 Cb. All rights reserved. +// With help from: https://gist.github.com/plapier/f8e1dde1b1624dfbb3e4 +// @available(*, deprecated, message:"LSSharedFileList will deprecated in feature.") class LaunchStarter { static func applicationIsInStartUpItems() -> Bool { return (LaunchStarter.itemReferencesInLoginItems().existingReference != nil) } - + static func itemReferencesInLoginItems() -> (existingReference: LSSharedFileListItem?, lastReference: LSSharedFileListItem?) { if let appURL : NSURL = NSURL.fileURL(withPath: Bundle.main.bundlePath) as NSURL? { if let loginItemsRef = LSSharedFileListCreate(nil, kLSSharedFileListSessionLoginItems.takeRetainedValue(), nil).takeRetainedValue() as LSSharedFileList? { @@ -35,7 +41,7 @@ class LaunchStarter { } return (nil, nil) } - + // 切换是否开机加入LoginItems static func toggleLaunchAtStartup() { let itemReferences = LaunchStarter.itemReferencesInLoginItems() @@ -56,7 +62,7 @@ class LaunchStarter { } } } - + // 从LoginItems移除 static func disableLaunchAtStartup() { let itemReferences = LaunchStarter.itemReferencesInLoginItems()