From 65fcc70fbb0dd801ca2b2f7487658b47e6ccd624 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Sat, 19 Oct 2024 14:49:08 +0200 Subject: [PATCH] feat: added calendar reset and update mechanism when opening the popup view --- Modules/Clock/popup.swift | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Modules/Clock/popup.swift b/Modules/Clock/popup.swift index 95814014..4ca56146 100644 --- a/Modules/Clock/popup.swift +++ b/Modules/Clock/popup.swift @@ -84,7 +84,7 @@ internal class Popup: PopupWrapper { } } - override func settings() -> NSView? { + public override func settings() -> NSView? { let view = SettingsContainerView() view.addArrangedSubview(PreferencesSection([ @@ -99,6 +99,12 @@ internal class Popup: PopupWrapper { return view } + public override func appear() { + if self.calendarState { + self.calendarView?.checkCurrentDay() + } + } + private func rearrange() { let views = self.subviews.filter{ $0 is ClockView }.compactMap{ $0 as? ClockView } views.forEach{ $0.removeFromSuperview() } @@ -124,6 +130,7 @@ private class CalendarView: NSStackView { private var year: Int private var month: Int + private var day: Int private var currentYear: Int { Calendar.current.component(.year, from: Date()) @@ -155,6 +162,7 @@ private class CalendarView: NSStackView { ) self.year = Calendar.current.component(.year, from: Date()) self.month = Calendar.current.component(.month, from: Date()) + self.day = Calendar.current.component(.day, from: Date()) super.init(frame: NSRect(x: 0, y: 0, width: width, height: width - 32)) @@ -177,6 +185,16 @@ private class CalendarView: NSStackView { fatalError("init(coder:) has not been implemented") } + public func checkCurrentDay() { + guard self.day != self.currentDay || self.month != self.currentMonth || self.year != self.currentYear else { return } + + self.year = self.currentYear + self.month = self.currentMonth + self.day = self.currentDay + + self.setup() + } + override func updateLayer() { self.layer?.backgroundColor = (isDarkMode ? NSColor(red: 17/255, green: 17/255, blue: 17/255, alpha: 0.25) : NSColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1)).cgColor }