mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 09:28:38 +00:00
* code cleanup
This commit is contained in:
parent
69b3f9d238
commit
9db9df5641
@ -29,16 +29,15 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
|
|||||||
override init(identifier: NSTouchBarItem.Identifier) {
|
override init(identifier: NSTouchBarItem.Identifier) {
|
||||||
super.init(identifier: identifier)
|
super.init(identifier: identifier)
|
||||||
|
|
||||||
scrubber = NSScrubber().then {
|
scrubber = NSScrubber();
|
||||||
$0.delegate = self
|
scrubber.delegate = self
|
||||||
$0.dataSource = self
|
scrubber.dataSource = self
|
||||||
$0.mode = .free // .fixed
|
scrubber.mode = .free // .fixed
|
||||||
let layout = NSScrubberFlowLayout().then {
|
let layout = NSScrubberFlowLayout();
|
||||||
$0.itemSize = NSSize(width: 44, height: 30)
|
layout.itemSize = NSSize(width: 44, height: 30)
|
||||||
}
|
scrubber.scrubberLayout = layout
|
||||||
$0.scrubberLayout = layout
|
scrubber.selectionBackgroundStyle = .roundedBackground
|
||||||
$0.selectionBackgroundStyle = .roundedBackground
|
|
||||||
}
|
|
||||||
view = scrubber
|
view = scrubber
|
||||||
|
|
||||||
scrubber.register(NSScrubberImageItemView.self, forItemIdentifier: .scrubberApplicationsItem)
|
scrubber.register(NSScrubberImageItemView.self, forItemIdentifier: .scrubberApplicationsItem)
|
||||||
@ -93,13 +92,25 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem, NSScrubberDelegate, NSScrub
|
|||||||
|
|
||||||
public func didFinishInteracting(with scrubber: NSScrubber) {
|
public func didFinishInteracting(with scrubber: NSScrubber) {
|
||||||
runningApplications[scrubber.selectedIndex].activate(options: [ .activateIgnoringOtherApps ])
|
runningApplications[scrubber.selectedIndex].activate(options: [ .activateIgnoringOtherApps ])
|
||||||
|
|
||||||
|
// NB: if you can't open app which on another space, try to check mark
|
||||||
|
// "When switching to an application, switch to a Space with open windows for the application"
|
||||||
|
// in Mission control settings
|
||||||
|
|
||||||
|
// TODO: deminiaturize app
|
||||||
|
// if let info = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID) as? [[ String : Any]] {
|
||||||
|
// for dict in info {
|
||||||
|
// if dict["kCGWindowOwnerName"] as! String == runningApplications[scrubber.selectedIndex].localizedName {
|
||||||
|
// print(dict["kCGWindowNumber"], dict["kCGWindowOwnerName"])
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)).compactMap { index in
|
||||||
let asn = CFArrayGetValueAtIndex(asns, index)
|
let asn = CFArrayGetValueAtIndex(asns, index)
|
||||||
let pid = pidFromASN(asn)
|
let pid = pidFromASN(asn)
|
||||||
return NSRunningApplication(processIdentifier: pid)
|
return NSRunningApplication(processIdentifier: pid)
|
||||||
@ -113,77 +124,22 @@ private func dockPersistentApplications() -> [NSRunningApplication] {
|
|||||||
|
|
||||||
guard let dockDefaults = UserDefaults(suiteName: "com.apple.dock"),
|
guard let dockDefaults = UserDefaults(suiteName: "com.apple.dock"),
|
||||||
let persistentApps = dockDefaults.array(forKey: "persistent-apps") as [AnyObject]?,
|
let persistentApps = dockDefaults.array(forKey: "persistent-apps") as [AnyObject]?,
|
||||||
let bundleIDs = persistentApps.flatMap({ $0.value(forKeyPath: "tile-data.bundle-identifier") }) as? [String] else {
|
let bundleIDs = persistentApps.compactMap({ $0.value(forKeyPath: "tile-data.bundle-identifier") }) as? [String] else {
|
||||||
return apps
|
return apps
|
||||||
}
|
}
|
||||||
|
|
||||||
return apps.sorted { (lhs, rhs) in
|
return apps.sorted { (lhs, rhs) in
|
||||||
if lhs.bundleIdentifier == "com.apple.finder" {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if rhs.bundleIdentifier == "com.apple.finder" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
switch ((bundleIDs.index(of: lhs.bundleIdentifier!)), bundleIDs.index(of: rhs.bundleIdentifier!)) {
|
switch ((bundleIDs.index(of: lhs.bundleIdentifier!)), bundleIDs.index(of: rhs.bundleIdentifier!)) {
|
||||||
case (nil, _):
|
case (nil, _):
|
||||||
return false;
|
return false;
|
||||||
case (_?, nil):
|
case (_?, nil):
|
||||||
return true
|
return true
|
||||||
case let (i1?, i2?):
|
case let (i1?, i2?):
|
||||||
return i1 < i2;
|
return i1 < i2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol Then {}
|
|
||||||
|
|
||||||
extension Then where Self: Any {
|
|
||||||
public func with(_ block: (inout Self) throws -> Void) rethrows -> Self {
|
|
||||||
var copy = self
|
|
||||||
try block(©)
|
|
||||||
return copy
|
|
||||||
}
|
|
||||||
|
|
||||||
public func `do`(_ block: (Self) throws -> Void) rethrows {
|
|
||||||
try block(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Then where Self: AnyObject {
|
|
||||||
public func then(_ block: (Self) throws -> Void) rethrows -> Self {
|
|
||||||
try block(self)
|
|
||||||
return self
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
extension NSObject: Then {}
|
|
||||||
|
|
||||||
extension CGPoint: Then {}
|
|
||||||
extension CGRect: Then {}
|
|
||||||
extension CGSize: Then {}
|
|
||||||
extension CGVector: Then {}
|
|
||||||
|
|
||||||
extension NSUserInterfaceItemIdentifier {
|
extension NSUserInterfaceItemIdentifier {
|
||||||
static let scrubberApplicationsItem = NSUserInterfaceItemIdentifier("ScrubberApplicationsItemReuseIdentifier")
|
static let scrubberApplicationsItem = NSUserInterfaceItemIdentifier("ScrubberApplicationsItemReuseIdentifier")
|
||||||
}
|
}
|
||||||
|
|
||||||
extension RangeReplaceableCollection {
|
|
||||||
mutating func move(at oldIndex: Self.Index, to newIndex: Self.Index) {
|
|
||||||
guard oldIndex != newIndex else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
let item = remove(at: oldIndex)
|
|
||||||
insert(item, at: newIndex)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Collection {
|
|
||||||
subscript(safe index: Self.Index) -> Self.Iterator.Element? {
|
|
||||||
guard index < endIndex else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return self[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user