Skip to content

Commit

Permalink
Add content scale options (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanzhong authored Nov 21, 2024
1 parent ac87d8d commit 1c44686
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 56 deletions.
4 changes: 4 additions & 0 deletions LunarBar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
87D65EB62B412A4700E41049 /* AppDefinitionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87D65EB52B412A4700E41049 /* AppDefinitionsTests.swift */; };
87DA5AFF2B3433D400CE2C1A /* WeekdayView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87DA5AFE2B3433D400CE2C1A /* WeekdayView.swift */; };
87F81C542B43EBDE0071CA30 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87F81C532B43EBDE0071CA30 /* main.swift */; };
87FB0ECB2CEDBBE700667C4C /* ScalableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87FB0ECA2CEDBBE300667C4C /* ScalableView.swift */; };
87FF2E822CE89F6700CBC2E0 /* DateDetailsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87FF2E812CE89F6000CBC2E0 /* DateDetailsView.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -86,6 +87,7 @@
87DA5AFE2B3433D400CE2C1A /* WeekdayView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeekdayView.swift; sourceTree = "<group>"; };
87F81C532B43EBDE0071CA30 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
87F862152B33DE1E00857541 /* Build.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Build.xcconfig; sourceTree = "<group>"; };
87FB0ECA2CEDBBE300667C4C /* ScalableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScalableView.swift; sourceTree = "<group>"; };
87FF2E812CE89F6000CBC2E0 /* DateDetailsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateDetailsView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -243,6 +245,7 @@
8782F4822B342413008B1912 /* HeaderView.swift */,
87DA5AFE2B3433D400CE2C1A /* WeekdayView.swift */,
8740E30A2B37F969004A06C2 /* EventView.swift */,
87FB0ECA2CEDBBE300667C4C /* ScalableView.swift */,
87A28A5C2B344D50006655F2 /* DateGridView.swift */,
873424CF2B35357B00C364BF /* DateGridCell.swift */,
87FF2E812CE89F6000CBC2E0 /* DateDetailsView.swift */,
Expand Down Expand Up @@ -391,6 +394,7 @@
8740E30F2B37FB36004A06C2 /* CalendarManager.swift in Sources */,
8714291C2B3DBFDF003FA2CB /* AppMainVC+Menu.swift in Sources */,
8740E30B2B37F969004A06C2 /* EventView.swift in Sources */,
87FB0ECB2CEDBBE700667C4C /* ScalableView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
6 changes: 4 additions & 2 deletions LunarBarMac/Modules/Sources/AppKitControls/ImageButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public final class ImageButton: CustomButton {
addSubview(iconView)

NSLayoutConstraint.activate([
iconView.centerXAnchor.constraint(equalTo: centerXAnchor),
iconView.centerYAnchor.constraint(equalTo: centerYAnchor),
iconView.leadingAnchor.constraint(equalTo: leadingAnchor),
iconView.trailingAnchor.constraint(equalTo: trailingAnchor),
iconView.topAnchor.constraint(equalTo: topAnchor),
iconView.bottomAnchor.constraint(equalTo: bottomAnchor),
])

setFrameSize(CGSize(
Expand Down
102 changes: 102 additions & 0 deletions LunarBarMac/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,57 @@
}
}
},
"Color Scheme" : {
"comment" : "[Menu] Section title for color schemes",
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "颜色偏好"
}
},
"zh-Hant" : {
"stringUnit" : {
"state" : "translated",
"value" : "顏色偏好"
}
}
}
},
"Compact" : {
"comment" : "[Menu] Content scale: compact",
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "紧凑"
}
},
"zh-Hant" : {
"stringUnit" : {
"state" : "translated",
"value" : "緊湊"
}
}
}
},
"Content Scale" : {
"comment" : "[Menu] Section title for content scales",
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "缩放显示"
}
},
"zh-Hant" : {
"stringUnit" : {
"state" : "translated",
"value" : "縮放顯示"
}
}
}
},
"Current Date" : {
"comment" : "[Menu] Use the current date as the menu bar icon",
"localizations" : {
Expand Down Expand Up @@ -1178,6 +1229,23 @@
}
}
},
"Default" : {
"comment" : "[Menu] Content scale: default",
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "默认"
}
},
"zh-Hant" : {
"stringUnit" : {
"state" : "translated",
"value" : "預設"
}
}
}
},
"Default (Mainland China)" : {
"comment" : "[Menu] Default public holidays (Mainland China)",
"localizations" : {
Expand Down Expand Up @@ -1469,6 +1537,23 @@
}
}
},
"Icon" : {
"comment" : "[Menu] Section title for icons",
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "图标"
}
},
"zh-Hant" : {
"stringUnit" : {
"state" : "translated",
"value" : "圖像"
}
}
}
},
"Jingzhe" : {
"comment" : "The 3rd solar term, always in Chinese",
"localizations" : {
Expand Down Expand Up @@ -2379,6 +2464,23 @@
}
}
},
"Roomy" : {
"comment" : "[Menu] Content scale: roomy",
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "宽松"
}
},
"zh-Hant" : {
"stringUnit" : {
"state" : "translated",
"value" : "寬鬆"
}
}
}
},
"Select All" : {
"comment" : "[Menu] Select all calendars",
"localizations" : {
Expand Down
43 changes: 22 additions & 21 deletions LunarBarMac/Sources/Main/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ class AppDelegate: NSObject, NSApplicationDelegate {
DateFormatter.lunarDate.string(from: currentDate).removingLeadingDigits,
].joined(separator: "\n\n")
}

@MainActor
func openPanel() {
guard let sender = statusItem.button else {
return Logger.assertFail("Missing source view to proceed")
}

let popover = AppMainVC.createPopover()
popover.delegate = self
popover.show(relativeTo: sender.bounds, of: sender, preferredEdge: .maxY)
presentedPopover = popover

// Ensure the app is activated and the window is key and ordered front
NSApp.activate(ignoringOtherApps: true)
popover.contentViewController?.view.window?.makeKeyAndOrderFront(nil)

// Keep the button highlighted to mimic the system behavior
sender.highlight(true)

// Clear the tooltip to prevent overlap
sender.toolTip = nil
}
}

// MARK: - NSPopoverDelegate
Expand Down Expand Up @@ -227,25 +249,4 @@ private extension AppDelegate {

return true
}

func openPanel() {
guard let sender = statusItem.button else {
return Logger.assertFail("Missing source view to proceed")
}

let popover = AppMainVC.createPopover()
popover.delegate = self
popover.show(relativeTo: sender.bounds, of: sender, preferredEdge: .maxY)
presentedPopover = popover

// Ensure the app is activated and the window is key and ordered front
NSApp.activate(ignoringOtherApps: true)
popover.contentViewController?.view.window?.makeKeyAndOrderFront(nil)

// Keep the button highlighted to mimic the system behavior
sender.highlight(true)

// Clear the tooltip to prevent overlap
sender.toolTip = nil
}
}
50 changes: 37 additions & 13 deletions LunarBarMac/Sources/Main/AppMainVC+Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,7 @@ private extension AppMainVC {
let menu = NSMenu()

// Icon styles
menu.addItem({
let item = NSMenuItem(title: Localized.UI.menuTitleCalendarIcon)
item.image = AppIconFactory.createCalendarIcon(pointSize: 14)
item.setOn(AppPreferences.General.menuBarIcon == .calendar)

item.addAction {
AppPreferences.General.menuBarIcon = .calendar
}

return item
}())

menu.addItem(withTitle: Localized.UI.menuTitleMenuBarIcon).isEnabled = false
menu.addItem({
let item = NSMenuItem(title: Localized.UI.menuTitleCurrentDate)
item.setOn(AppPreferences.General.menuBarIcon == .date)
Expand All @@ -158,13 +147,26 @@ private extension AppMainVC {
return item
}())

menu.addItem({
let item = NSMenuItem(title: Localized.UI.menuTitleCalendarIcon)
item.image = AppIconFactory.createCalendarIcon(pointSize: 14)
item.setOn(AppPreferences.General.menuBarIcon == .calendar)

item.addAction {
AppPreferences.General.menuBarIcon = .calendar
}

return item
}())

menu.addSeparator()

// Dark mode preferences
menu.addItem(withTitle: Localized.UI.menuTitleColorScheme).isEnabled = false
[
(Localized.UI.menuTitleSystem, Appearance.system),
(Localized.UI.menuTitleLight, Appearance.light),
(Localized.UI.menuTitleDark, Appearance.dark),
(Localized.UI.menuTitleSystem, Appearance.system),
].forEach { (title: String, appearance: Appearance) in
menu.addItem(withTitle: title) { [weak self] in
self?.updateAppearance(appearance)
Expand All @@ -174,6 +176,28 @@ private extension AppMainVC {

menu.addSeparator()

// Content scale preferences
menu.addItem(withTitle: Localized.UI.menuTitleContentScale).isEnabled = false
[
(Localized.UI.menuTitleScaleDefault, ContentScale.default),
(Localized.UI.menuTitleScaleCompact, ContentScale.compact),
(Localized.UI.menuTitleScaleRoomy, ContentScale.roomy),
].forEach { (title: String, scale: ContentScale) in
menu.addItem(withTitle: title) { [weak self] in
AppPreferences.General.contentScale = scale
self?.popover?.close()

if let delegate = NSApp.delegate as? AppDelegate {
delegate.openPanel()
} else {
Logger.assertFail("Unexpected app delegate: \(String(describing: NSApp.delegate))")
}
}
.setOn(AppPreferences.General.contentScale == scale)
}

menu.addSeparator()

// Accessibility options
menu.addItem(withTitle: Localized.UI.menuTitleReduceMotion) { [weak self] in
AppPreferences.Accessibility.reduceMotion.toggle()
Expand Down
15 changes: 12 additions & 3 deletions LunarBarMac/Sources/Main/AppMainVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class AppMainVC: NSViewController {
weak var popover: NSPopover?

// Views
private let scalableView = ScalableView()
private let headerView = HeaderView()
private let weekdayView = WeekdayView()
private let dateGridView = DateGridView()
Expand All @@ -26,7 +27,7 @@ final class AppMainVC: NSViewController {
static func createPopover() -> NSPopover {
let popover = NSPopover()
popover.behavior = .transient
popover.contentSize = Constants.contentSize
popover.contentSize = desiredContentSize
popover.animates = !AppPreferences.Accessibility.reduceMotion

let contentVC = Self()
Expand All @@ -42,7 +43,8 @@ final class AppMainVC: NSViewController {
extension AppMainVC {
override func loadView() {
// Required prior to macOS Sonoma
view = NSView(frame: CGRect(origin: .zero, size: Constants.contentSize))
view = NSView(frame: CGRect(origin: .zero, size: Self.desiredContentSize))
view.addScalableView(scalableView, scale: AppPreferences.General.contentScale.rawValue)
}

override func viewDidLoad() {
Expand Down Expand Up @@ -113,13 +115,20 @@ extension AppMainVC: HeaderViewDelegate {

private extension AppMainVC {
enum Constants {
static let contentSize = CGSize(width: 240, height: 320)
static let headerViewHeight: Double = 40
static let weekdayViewHeight: Double = 17
static let dateGridViewMarginTop: Double = 10
}

@MainActor static var desiredContentSize: CGSize {
CGSize(
width: 240 * AppPreferences.General.contentScale.rawValue,
height: 320 * AppPreferences.General.contentScale.rawValue
)
}

func setUp() {
let view = scalableView.container
headerView.delegate = self
headerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(headerView)
Expand Down
10 changes: 8 additions & 2 deletions LunarBarMac/Sources/Shared/AppDefinitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ enum Localized {
static let menuTitleGotoMonth = String(localized: "Go to Month", comment: "[Menu] Select year and month")
static let menuTitleEnterMonth = String(localized: "Enter Month", comment: "[Menu] Enter a month using date picker")
static let menuTitleAppearance = String(localized: "Appearance", comment: "[Menu] Change dark mode preference")
static let menuTitleCalendarIcon = String(localized: "Calendar Icon", comment: "[Menu] Use a calendar icon as the menu bar icon")
static let menuTitleMenuBarIcon = String(localized: "Icon", comment: "[Menu] Section title for icons")
static let menuTitleCurrentDate = String(localized: "Current Date", comment: "[Menu] Use the current date as the menu bar icon")
static let menuTitleCalendarIcon = String(localized: "Calendar Icon", comment: "[Menu] Use a calendar icon as the menu bar icon")
static let menuTitleColorScheme = String(localized: "Color Scheme", comment: "[Menu] Section title for color schemes")
static let menuTitleSystem = String(localized: "System", comment: "[Menu] Follow the system appearance")
static let menuTitleLight = String(localized: "Light", comment: "[Menu] Use the light appearance")
static let menuTitleDark = String(localized: "Dark", comment: "[Menu] Use the dark appearance")
static let menuTitleSystem = String(localized: "System", comment: "[Menu] Follow the system appearance")
static let menuTitleContentScale = String(localized: "Content Scale", comment: "[Menu] Section title for content scales")
static let menuTitleScaleDefault = String(localized: "Default", comment: "[Menu] Content scale: default")
static let menuTitleScaleCompact = String(localized: "Compact", comment: "[Menu] Content scale: compact")
static let menuTitleScaleRoomy = String(localized: "Roomy", comment: "[Menu] Content scale: roomy")
static let menuTitleReduceMotion = String(localized: "Reduce Motion", comment: "[Menu] Disable animations when presenting the calendar popover")
static let menuTitleReduceTransparency = String(localized: "Reduce Transparency", comment: "[Menu] Reduce transparency of the calendar panel")
static let menuTitleFloatOnTop = String(localized: "Float on Top", comment: "[Menu] Float the popover on top")
Expand Down
Loading

0 comments on commit 1c44686

Please sign in to comment.