From 407db8ae540b477dcf24c973d8223a5679b00934 Mon Sep 17 00:00:00 2001 From: Gnohz Gniy <0x00eeee@gmail.com> Date: Mon, 8 Jan 2024 18:40:15 +0800 Subject: [PATCH] Full-fledged date picker --- .../NSControl+Extension.swift | 1 + LunarBarMac/Sources/Main/AppMainVC+Menu.swift | 40 ++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/LunarBarMac/Modules/Sources/AppKitExtensions/NSControl+Extension.swift b/LunarBarMac/Modules/Sources/AppKitExtensions/NSControl+Extension.swift index 28ae09d..103a8bf 100644 --- a/LunarBarMac/Modules/Sources/AppKitExtensions/NSControl+Extension.swift +++ b/LunarBarMac/Modules/Sources/AppKitExtensions/NSControl+Extension.swift @@ -31,6 +31,7 @@ public extension ClosureActionable { extension NSButton: ClosureActionable {} extension NSMenuItem: ClosureActionable {} +extension NSDatePicker: ClosureActionable {} // MARK: - Private diff --git a/LunarBarMac/Sources/Main/AppMainVC+Menu.swift b/LunarBarMac/Sources/Main/AppMainVC+Menu.swift index c748936..2840116 100644 --- a/LunarBarMac/Sources/Main/AppMainVC+Menu.swift +++ b/LunarBarMac/Sources/Main/AppMainVC+Menu.swift @@ -6,6 +6,7 @@ // import AppKit +import AppKitExtensions import LunarBarKit import ServiceManagement @@ -54,8 +55,8 @@ private extension AppMainVC { let menu = NSMenu() let current = Calendar.solar.year(from: monthDate) - // Generate 20 years before and after the current year - for year in (current - 10)...(current + 10) { + // Quick picker that supports 12 years around the current year + for year in (current - 6)...(current + 6) { let item = menu.addItem(withTitle: String(year)) item.submenu = NSMenu() @@ -75,6 +76,41 @@ private extension AppMainVC { } } + menu.addSeparator() + + // Full-fledged picker that supports any year + menu.addItem({ [weak self] in + let item = NSMenuItem() + let picker = NSDatePicker() + picker.isBezeled = false + picker.isBordered = false + picker.datePickerStyle = .textFieldAndStepper + picker.datePickerElements = .yearMonth + picker.translatesAutoresizingMaskIntoConstraints = false + picker.dateValue = self?.monthDate ?? .now + picker.sizeToFit() + + picker.addAction { [weak picker] in + guard let date = picker?.dateValue else { + return + } + + self?.updateCalendar(targetDate: date) + } + + let wrapper = NSView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: picker.frame.height))) + wrapper.autoresizingMask = .width + wrapper.addSubview(picker) + + NSLayoutConstraint.activate([ + picker.centerXAnchor.constraint(equalTo: wrapper.centerXAnchor), + picker.centerYAnchor.constraint(equalTo: wrapper.centerYAnchor), + ]) + + item.view = wrapper + return item + }()) + let item = NSMenuItem(title: Localized.UI.menuTitleGotoDate) item.submenu = menu return item