1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-11 17:38:38 +00:00
This commit is contained in:
ad 2018-04-16 15:36:15 +03:00
parent a37bc94588
commit 3d7bcd2d47

View File

@ -20,10 +20,6 @@
import Cocoa import Cocoa
let activateKeyIndex = "ActivateKeyIndex"
let appScrubberOrderIndex = "AppScrubberOrderIndex"
let appScrubberModeIndex = "AppScrubberModeIndex"
class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrubberDataSource { class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrubberDataSource {
var scrubber: NSScrubber! var scrubber: NSScrubber!
@ -36,9 +32,9 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
scrubber = NSScrubber().then { scrubber = NSScrubber().then {
$0.delegate = self $0.delegate = self
$0.dataSource = self $0.dataSource = self
$0.mode = UserDefaults.standard.appScrubberMode $0.mode = .free // .fixed
let layout = NSScrubberFlowLayout().then { let layout = NSScrubberFlowLayout().then {
$0.itemSize = NSSize(width: 50, height: 30) $0.itemSize = NSSize(width: 44, height: 30)
} }
$0.scrubberLayout = layout $0.scrubberLayout = layout
$0.selectionBackgroundStyle = .roundedBackground $0.selectionBackgroundStyle = .roundedBackground
@ -50,7 +46,6 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didLaunchApplicationNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didLaunchApplicationNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didTerminateApplicationNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didTerminateApplicationNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didActivateApplicationNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didActivateApplicationNotification, object: nil)
UserDefaults.standard.addObserver(self, forKeyPath: appScrubberOrderIndex, context: nil)
updateRunningApplication(animated: false) updateRunningApplication(animated: false)
} }
@ -68,7 +63,7 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
} }
func updateRunningApplication(animated: Bool) { func updateRunningApplication(animated: Bool) {
let isDockOrder = UserDefaults.standard.integer(forKey: appScrubberOrderIndex) != 0 let isDockOrder = false
let newApplications = (isDockOrder ? dockPersistentApplications() : launchedApplications()).filter { let newApplications = (isDockOrder ? dockPersistentApplications() : launchedApplications()).filter {
!$0.isTerminated && $0.bundleIdentifier != nil !$0.isTerminated && $0.bundleIdentifier != nil
} }
@ -78,12 +73,10 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
} }
if animated { if animated {
scrubber.performSequentialBatchUpdates { scrubber.performSequentialBatchUpdates {
// print("-----update-----")
for (index, app) in newApplications.enumerated() { for (index, app) in newApplications.enumerated() {
while runningApplications[safe:index].map(newApplications.contains) == false { while runningApplications[safe:index].map(newApplications.contains) == false {
scrubber.removeItems(at: [index]) scrubber.removeItems(at: [index])
let r = runningApplications.remove(at: index) let r = runningApplications.remove(at: index)
// print("remove \(r.localizedName!) at \(index)")
} }
if let oldIndex = runningApplications.index(of: app) { if let oldIndex = runningApplications.index(of: app) {
guard oldIndex != index else { guard oldIndex != index else {
@ -91,11 +84,9 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
} }
scrubber.moveItem(at: oldIndex, to: index) scrubber.moveItem(at: oldIndex, to: index)
runningApplications.move(at: oldIndex, to: index) runningApplications.move(at: oldIndex, to: index)
// print("move \(app.localizedName!) at \(oldIndex) to \(index)")
} else { } else {
scrubber.insertItems(at: [index]) scrubber.insertItems(at: [index])
runningApplications.insert(app, at: index) runningApplications.insert(app, at: index)
// print("insert \(app.localizedName!) to \(index)")
} }
} }
assert(runningApplications == newApplications) assert(runningApplications == newApplications)
@ -107,8 +98,6 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
scrubber.selectedIndex = index ?? 0 scrubber.selectedIndex = index ?? 0
} }
// MARK: - NSScrubberDataSource
public func numberOfItems(for scrubber: NSScrubber) -> Int { public func numberOfItems(for scrubber: NSScrubber) -> Int {
return runningApplications.count return runningApplications.count
} }
@ -123,16 +112,11 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
} }
public func didFinishInteracting(with scrubber: NSScrubber) { public func didFinishInteracting(with scrubber: NSScrubber) {
guard scrubber.selectedIndex > 0 else { runningApplications[scrubber.selectedIndex].activate(options: [ .activateIgnoringOtherApps ])
return
}
runningApplications[scrubber.selectedIndex].activate(options: .activateIgnoringOtherApps)
} }
} }
// MARK: - Applications
private func launchedApplications() -> [NSRunningApplication] { private func launchedApplications() -> [NSRunningApplication] {
let asns = _LSCopyApplicationArrayInFrontToBackOrder(~0)?.takeRetainedValue() let asns = _LSCopyApplicationArrayInFrontToBackOrder(~0)?.takeRetainedValue()
return (0..<CFArrayGetCount(asns)).flatMap { index in return (0..<CFArrayGetCount(asns)).flatMap { index in
@ -174,26 +158,12 @@ private func dockPersistentApplications() -> [NSRunningApplication] {
public protocol Then {} public protocol Then {}
extension Then where Self: Any { extension Then where Self: Any {
/// Makes it available to set properties with closures just after initializing and copying the value types.
///
/// let frame = CGRect().with {
/// $0.origin.x = 10
/// $0.size.width = 100
/// }
public func with(_ block: (inout Self) throws -> Void) rethrows -> Self { public func with(_ block: (inout Self) throws -> Void) rethrows -> Self {
var copy = self var copy = self
try block(&copy) try block(&copy)
return copy return copy
} }
/// Makes it available to execute something with closures.
///
/// UserDefaults.standard.do {
/// $0.set("devxoul", forKey: "username")
/// $0.set("devxoul@gmail.com", forKey: "email")
/// $0.synchronize()
/// }
public func `do`(_ block: (Self) throws -> Void) rethrows { public func `do`(_ block: (Self) throws -> Void) rethrows {
try block(self) try block(self)
} }
@ -201,14 +171,6 @@ extension Then where Self: Any {
} }
extension Then where Self: AnyObject { extension Then where Self: AnyObject {
/// Makes it available to set properties with closures just after initializing.
///
/// let label = UILabel().then {
/// $0.textAlignment = .Center
/// $0.textColor = UIColor.blackColor()
/// $0.text = "Hello, World!"
/// }
public func then(_ block: (Self) throws -> Void) rethrows -> Self { public func then(_ block: (Self) throws -> Void) rethrows -> Self {
try block(self) try block(self)
return self return self
@ -223,28 +185,11 @@ extension CGRect: Then {}
extension CGSize: Then {} extension CGSize: Then {}
extension CGVector: Then {} extension CGVector: Then {}
extension UserDefaults {
var activateKey: NSEvent.ModifierFlags? {
let keys: [NSEvent.ModifierFlags?] = [.command, .option, .control, .shift, nil]
let index = integer(forKey: activateKeyIndex)
return keys[index]
}
var appScrubberMode: NSScrubber.Mode {
let actions: [NSScrubber.Mode] = [.fixed, .free]
let index = integer(forKey: appScrubberModeIndex)
return actions[index]
}
}
extension NSUserInterfaceItemIdentifier { extension NSUserInterfaceItemIdentifier {
static let scrubberApplicationsItem = NSUserInterfaceItemIdentifier("ScrubberApplicationsItemReuseIdentifier") static let scrubberApplicationsItem = NSUserInterfaceItemIdentifier("ScrubberApplicationsItemReuseIdentifier")
} }
extension RangeReplaceableCollection { extension RangeReplaceableCollection {
mutating func move(at oldIndex: Self.Index, to newIndex: Self.Index) { mutating func move(at oldIndex: Self.Index, to newIndex: Self.Index) {
guard oldIndex != newIndex else { guard oldIndex != newIndex else {
return return
@ -255,7 +200,6 @@ extension RangeReplaceableCollection {
} }
extension Collection { extension Collection {
subscript(safe index: Self.Index) -> Self.Iterator.Element? { subscript(safe index: Self.Index) -> Self.Iterator.Element? {
guard index < endIndex else { guard index < endIndex else {
return nil return nil