mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-10 17:08:39 +00:00
Added ability to click touchbar item and go to calendar
This commit is contained in:
parent
42d6c077f2
commit
292da17aee
@ -67,32 +67,38 @@ class UpNextBarItem: NSCustomTouchBarItem {
|
|||||||
private func updateView() {
|
private func updateView() {
|
||||||
items = []
|
items = []
|
||||||
var upcomingEvents = self.getUpcomingEvents()
|
var upcomingEvents = self.getUpcomingEvents()
|
||||||
|
NSLog("Found \(upcomingEvents.count) events")
|
||||||
upcomingEvents.sort(by: {$0.startDate.compare($1.startDate) == .orderedAscending})
|
upcomingEvents.sort(by: {$0.startDate.compare($1.startDate) == .orderedAscending})
|
||||||
var index = 1
|
var index = 1
|
||||||
for event in upcomingEvents {
|
DispatchQueue.main.async {
|
||||||
// Create UpNextItem
|
for event in upcomingEvents {
|
||||||
let item = UpNextItem(event: event)
|
// Create UpNextItem
|
||||||
item.isBordered = false;
|
let item = UpNextItem(event: event)
|
||||||
items.append(item)
|
item.backgroundColor = self.getBackgroundColor(startDate: event.startDate)
|
||||||
// Check if should display any more
|
// Bind tap event
|
||||||
if (index == self.nthEvent) {
|
item.tapClosure = { [weak self] in
|
||||||
break;
|
self?.switchToApp(event: event)
|
||||||
|
}
|
||||||
|
// Add to view
|
||||||
|
self.items.append(item)
|
||||||
|
// Check if should display any more
|
||||||
|
if (index == self.nthEvent) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
index += 1
|
||||||
}
|
}
|
||||||
index += 1
|
self.reloadData()
|
||||||
}
|
}
|
||||||
self.reloadData()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func reloadData() {
|
private func reloadData() {
|
||||||
NSLog("Displaying \(items.count) items...")
|
NSLog("Displaying \(items.count) items...")
|
||||||
let stackView = NSStackView(views: items.compactMap { $0.view })
|
let stackView = NSStackView(views: items.compactMap { $0.view })
|
||||||
DispatchQueue.main.async {
|
stackView.spacing = 5
|
||||||
stackView.spacing = 1
|
stackView.orientation = .horizontal
|
||||||
stackView.orientation = .horizontal
|
let visibleRect = self.scrollView.documentVisibleRect
|
||||||
let visibleRect = self.scrollView.documentVisibleRect
|
self.scrollView.documentView = stackView
|
||||||
self.scrollView.documentView = stackView
|
stackView.scroll(visibleRect.origin)
|
||||||
stackView.scroll(visibleRect.origin)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func getUpcomingEvents() -> [UpNextEventModel] {
|
private func getUpcomingEvents() -> [UpNextEventModel] {
|
||||||
@ -113,9 +119,29 @@ class UpNextBarItem: NSCustomTouchBarItem {
|
|||||||
return upcomingEvents
|
return upcomingEvents
|
||||||
}
|
}
|
||||||
|
|
||||||
func gotoAppleCalendar() {
|
public func switchToApp(event: UpNextEventModel) {
|
||||||
print("CLICK")
|
var bundleIdentifier: String
|
||||||
NSWorkspace.shared.open(URL(fileURLWithPath: "/Applications/Calendar.app"))
|
switch(event.sourceType) {
|
||||||
|
case .iCalendar:
|
||||||
|
bundleIdentifier = UpNextCalenderSource.bundleIdentifier
|
||||||
|
}
|
||||||
|
|
||||||
|
NSWorkspace.shared.launchApplication(withBundleIdentifier: bundleIdentifier, options: [.default], additionalEventParamDescriptor: nil, launchIdentifier: nil)
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func getBackgroundColor(startDate: Date) -> NSColor {
|
||||||
|
let distance = abs(Date().timeIntervalSinceReferenceDate/60 - startDate.timeIntervalSinceReferenceDate/60) // Get time difference in minutes
|
||||||
|
if(distance < 30 as TimeInterval) { // Less than 30 minutes, backround is red
|
||||||
|
return NSColor.systemRed
|
||||||
|
} else if (distance < 120 as TimeInterval) { // Less than 2 hours, background is yellow
|
||||||
|
return NSColor.systemOrange
|
||||||
|
}
|
||||||
|
return NSColor.clear
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,14 +192,18 @@ struct UpNextEventModel {
|
|||||||
|
|
||||||
// Interface for any event source
|
// Interface for any event source
|
||||||
protocol IUpNextSource {
|
protocol IUpNextSource {
|
||||||
|
static var bundleIdentifier: String { get }
|
||||||
var hasPermission : Bool { get }
|
var hasPermission : Bool { get }
|
||||||
init()
|
init()
|
||||||
func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel]
|
func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel]
|
||||||
}
|
}
|
||||||
|
|
||||||
class UpNextCalenderSource : IUpNextSource {
|
class UpNextCalenderSource : IUpNextSource {
|
||||||
|
static public let bundleIdentifier: String = "com.apple.iCal"
|
||||||
|
|
||||||
public var hasPermission: Bool = false
|
public var hasPermission: Bool = false
|
||||||
private var eventStore = EKEventStore()
|
private var eventStore = EKEventStore()
|
||||||
|
|
||||||
required init() {
|
required init() {
|
||||||
eventStore.requestAccess(to: .event){ granted, error in
|
eventStore.requestAccess(to: .event){ granted, error in
|
||||||
self.hasPermission = granted;
|
self.hasPermission = granted;
|
||||||
@ -190,7 +220,6 @@ class UpNextCalenderSource : IUpNextSource {
|
|||||||
let calendars = self.eventStore.calendars(for: .event)
|
let calendars = self.eventStore.calendars(for: .event)
|
||||||
|
|
||||||
for calendar in calendars {
|
for calendar in calendars {
|
||||||
|
|
||||||
let predicate = self.eventStore.predicateForEvents(withStart: dateLowerBounds, end: dateUpperBounds, calendars: [calendar])
|
let predicate = self.eventStore.predicateForEvents(withStart: dateLowerBounds, end: dateUpperBounds, calendars: [calendar])
|
||||||
|
|
||||||
let events = self.eventStore.events(matching: predicate)
|
let events = self.eventStore.events(matching: predicate)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user