You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.7 KiB

// Copyright © 2015 Abhishek Banthia
import Cocoa
func bufferCalculatedWidth() -> Int {
var totalWidth = 55
5 years ago
if DataStore.shared().shouldShowDateInMenubar() {
totalWidth += 8
}
5 years ago
if DataStore.shared().shouldDisplay(.twelveHour) {
totalWidth += 18
}
5 years ago
if DataStore.shared().shouldDisplay(.seconds) {
totalWidth += 15
}
5 years ago
return totalWidth
}
func compactWidth(for timezone: TimezoneData) -> Int {
var totalWidth = 55
let timeFormat = timezone.timezoneFormat()
5 years ago
if DataStore.shared().shouldShowDateInMenubar() {
totalWidth += 8
}
5 years ago
if timeFormat == DateFormat.twelveHour || timeFormat == DateFormat.twelveHourWithSeconds {
totalWidth += 18
} else if timeFormat == DateFormat.twentyFourHour || timeFormat == DateFormat.twentyFourHourWithSeconds {
totalWidth += 0
}
5 years ago
if timezone.shouldShowSeconds() {
// Slight buffer needed when the Menubar supplementary text was Mon 9:27:58 AM
totalWidth += 15
}
5 years ago
return totalWidth
}
// Test with Sat 12:46 AM
let bufferWidth: CGFloat = 9.5
class StatusContainerView: NSView {
private var previousX: Int = 0
5 years ago
override func awakeFromNib() {
super.awakeFromNib()
wantsLayer = true
layer?.backgroundColor = NSColor.clear.cgColor
}
5 years ago
init(with timezones: [Data]) {
func addSubviews() {
timezones.forEach {
if let timezoneObject = TimezoneData.customObject(from: $0) {
addTimezone(timezoneObject)
}
}
}
5 years ago
func containerWidth(for timezones: [Data]) -> CGFloat {
let compressedWidth = timezones.reduce(0.0) { (result, timezone) -> CGFloat in
5 years ago
if let timezoneObject = TimezoneData.customObject(from: timezone) {
let precalculatedWidth = Double(compactWidth(for: timezoneObject))
let operationObject = TimezoneDataOperations(with: timezoneObject)
let calculatedSize = compactModeTimeFont.size(operationObject.compactMenuHeader(), precalculatedWidth, attributes: timeAttributes)
return result + calculatedSize.width + bufferWidth
}
5 years ago
return result + CGFloat(bufferCalculatedWidth())
}
5 years ago
let calculatedWidth = min(compressedWidth,
CGFloat(timezones.count * bufferCalculatedWidth()))
return calculatedWidth
}
5 years ago
let statusItemWidth = containerWidth(for: timezones)
let frame = NSRect(x: 0, y: 0, width: statusItemWidth, height: 30)
super.init(frame: frame)
5 years ago
addSubviews()
}
5 years ago
5 years ago
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
5 years ago
func addTimezone(_ timezone: TimezoneData) {
let calculatedWidth = bestWidth(for: timezone)
5 years ago
let frame = NSRect(x: previousX, y: 0, width: calculatedWidth, height: 30)
5 years ago
let statusItemView = StatusItemView(frame: frame)
statusItemView.dataObject = timezone
5 years ago
addSubview(statusItemView)
5 years ago
previousX += calculatedWidth
}
5 years ago
private func bestWidth(for timezone: TimezoneData) -> Int {
let operation = TimezoneDataOperations(with: timezone)
5 years ago
let bestSize = compactModeTimeFont.size(operation.compactMenuHeader(), Double(compactWidth(for: timezone)), attributes: timeAttributes)
5 years ago
return Int(bestSize.width + bufferWidth)
}
5 years ago
func updateTime() {
if subviews.isEmpty {
assertionFailure("Subviews count should > 0")
}
5 years ago
subviews.forEach {
if let statusItem = $0 as? StatusItemView {
statusItem.updateTimeInMenubar()
}
}
}
}