Skip to content

Commit

Permalink
Full-fledged date picker
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanzhong committed Jan 8, 2024
1 parent 6c6bf33 commit 407db8a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public extension ClosureActionable {

extension NSButton: ClosureActionable {}
extension NSMenuItem: ClosureActionable {}
extension NSDatePicker: ClosureActionable {}

// MARK: - Private

Expand Down
40 changes: 38 additions & 2 deletions LunarBarMac/Sources/Main/AppMainVC+Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import AppKit
import AppKitExtensions
import LunarBarKit
import ServiceManagement

Expand Down Expand Up @@ -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()

Expand All @@ -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
Expand Down

0 comments on commit 407db8a

Please sign in to comment.