From 331b667797013e79166fb60fc623b1ed36801a2e Mon Sep 17 00:00:00 2001 From: Abhishek Date: Sun, 21 Jun 2020 00:51:25 -0500 Subject: [PATCH] Imp fixes! --- .../CalendarHandler.swift | 28 +++++++++++++++++++ Clocker/Menu Bar/StatusContainerView.swift | 23 +++++++++++---- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/Clocker/Events and Reminders/CalendarHandler.swift b/Clocker/Events and Reminders/CalendarHandler.swift index 85fa7a4..ab768f7 100644 --- a/Clocker/Events and Reminders/CalendarHandler.swift +++ b/Clocker/Events and Reminders/CalendarHandler.swift @@ -53,6 +53,34 @@ extension EventCenter { return sourcesAndCalendars } + func isThereAnUpcomingCalendarEvent() -> Bool { + if DataStore.shared().shouldDisplay(.showMeetingInMenubar) { + let filteredDates = EventCenter.sharedCenter().eventsForDate + let autoupdatingCal = EventCenter.sharedCenter().autoupdatingCalendar + guard let events = filteredDates[autoupdatingCal.startOfDay(for: Date())] else { + return false + } + + for event in events { + if event.event.startDate.timeIntervalSinceNow > 0, !event.isAllDay { + let timeForEventToStart = event.event.startDate.timeIntervalSinceNow / 60 + + if timeForEventToStart > 30 { + print("Our next event: \(event.event.title ?? "Error") starts in \(timeForEventToStart) mins") + continue + } + + return true + } + } + } + + return false + } + + /* Used for the compact menubar mode. + Returns a tuple with 0 as the header string and 1 as the subtitle string + */ func separateFormat(event: EKEvent) -> (String, String)? { guard let truncateLength = DataStore.shared().retrieve(key: CLTruncateTextLength) as? NSNumber, let eventTitle = event.title else { return nil diff --git a/Clocker/Menu Bar/StatusContainerView.swift b/Clocker/Menu Bar/StatusContainerView.swift index 63bbb27..f71925b 100644 --- a/Clocker/Menu Bar/StatusContainerView.swift +++ b/Clocker/Menu Bar/StatusContainerView.swift @@ -77,8 +77,9 @@ class StatusContainerView: NSView { if let timezoneObject = TimezoneData.customObject(from: timezone) { let precalculatedWidth = Double(compactWidth(for: timezoneObject)) let operationObject = TimezoneDataOperations(with: timezoneObject) - let calculatedSize = compactModeTimeFont.size(operationObject.compactMenuSubtitle(), precalculatedWidth, attributes: timeAttributes) - return result + calculatedSize.width + bufferWidth + let calculatedSubtitleSize = compactModeTimeFont.size(operationObject.compactMenuSubtitle(), precalculatedWidth, attributes: timeAttributes) + let calculatedTitleSize = compactModeTimeFont.size(operationObject.compactMenuTitle(), precalculatedWidth, attributes: timeAttributes) + return result + max(calculatedTitleSize.width, calculatedSubtitleSize.width) + bufferWidth } return result + CGFloat(bufferCalculatedWidth()) @@ -114,10 +115,11 @@ class StatusContainerView: NSView { private func bestWidth(for timezone: TimezoneData) -> Int { let operation = TimezoneDataOperations(with: timezone) + let bestSize = compactModeTimeFont.size(operation.compactMenuSubtitle(), + Double(compactWidth(for: timezone)), attributes: timeAttributes) + let bestTitleSize = compactModeTimeFont.size(operation.compactMenuTitle(), Double(compactWidth(for: timezone)), attributes: timeAttributes) - let bestSize = compactModeTimeFont.size(operation.compactMenuSubtitle(), Double(compactWidth(for: timezone)), attributes: timeAttributes) - - return Int(bestSize.width + bufferWidth) + return Int(max(bestSize.width, bestTitleSize.width) + bufferWidth) } func updateTime() { @@ -126,16 +128,25 @@ class StatusContainerView: NSView { } // See if frame's width needs any adjustment + adjustWidthIfNeccessary() + } + + private func adjustWidthIfNeccessary() { var newWidth: CGFloat = 0 subviews.forEach { - if let statusItem = $0 as? StatusItemView { + if let statusItem = $0 as? StatusItemView, statusItem.isHidden == false { // Determine what's the best width required to display the current string. let newBestWidth = CGFloat(bestWidth(for: statusItem.dataObject)) // Let's note if the current width is too small/correct newWidth += statusItem.frame.size.width != newBestWidth ? newBestWidth : statusItem.frame.size.width + statusItem.frame = CGRect(x: statusItem.frame.origin.x, + y: statusItem.frame.origin.y, + width: newBestWidth, + height: statusItem.frame.size.height) + statusItem.updateTimeInMenubar() } }