Browse Source

Annoying light mode bug fix.

pull/92/head
Abhishek 5 years ago
parent
commit
cd418c8f45
  1. 27
      Clocker/Menu Bar/StatusContainerView.swift
  2. 50
      Clocker/Menu Bar/StatusItemView.swift

27
Clocker/Menu Bar/StatusContainerView.swift

@ -71,14 +71,20 @@ class StatusContainerView: NSView {
} }
} }
let timeBasedAttributes = [
NSAttributedString.Key.font: compactModeTimeFont,
NSAttributedString.Key.backgroundColor: NSColor.clear,
NSAttributedString.Key.paragraphStyle: defaultParagraphStyle,
]
func containerWidth(for timezones: [Data]) -> CGFloat { func containerWidth(for timezones: [Data]) -> CGFloat {
let compressedWidth = timezones.reduce(0.0) { (result, timezone) -> CGFloat in let compressedWidth = timezones.reduce(0.0) { (result, timezone) -> CGFloat in
if let timezoneObject = TimezoneData.customObject(from: timezone) { if let timezoneObject = TimezoneData.customObject(from: timezone) {
let precalculatedWidth = Double(compactWidth(for: timezoneObject)) let precalculatedWidth = Double(compactWidth(for: timezoneObject))
let operationObject = TimezoneDataOperations(with: timezoneObject) let operationObject = TimezoneDataOperations(with: timezoneObject)
let calculatedSubtitleSize = compactModeTimeFont.size(operationObject.compactMenuSubtitle(), precalculatedWidth, attributes: timeAttributes) let calculatedSubtitleSize = compactModeTimeFont.size(operationObject.compactMenuSubtitle(), precalculatedWidth, attributes: timeBasedAttributes)
let calculatedTitleSize = compactModeTimeFont.size(operationObject.compactMenuTitle(), precalculatedWidth, attributes: timeAttributes) let calculatedTitleSize = compactModeTimeFont.size(operationObject.compactMenuTitle(), precalculatedWidth, attributes: timeBasedAttributes)
return result + max(calculatedTitleSize.width, calculatedSubtitleSize.width) + bufferWidth return result + max(calculatedTitleSize.width, calculatedSubtitleSize.width) + bufferWidth
} }
@ -114,10 +120,23 @@ class StatusContainerView: NSView {
} }
private func bestWidth(for timezone: TimezoneData) -> Int { private func bestWidth(for timezone: TimezoneData) -> Int {
var textColor = hasDarkAppearance ? NSColor.white : NSColor.black
if #available(macOS 10.15, *) {
textColor = NSColor.white
}
let timeBasedAttributes = [
NSAttributedString.Key.font: compactModeTimeFont,
NSAttributedString.Key.foregroundColor: textColor,
NSAttributedString.Key.backgroundColor: NSColor.clear,
NSAttributedString.Key.paragraphStyle: defaultParagraphStyle,
]
let operation = TimezoneDataOperations(with: timezone) let operation = TimezoneDataOperations(with: timezone)
let bestSize = compactModeTimeFont.size(operation.compactMenuSubtitle(), let bestSize = compactModeTimeFont.size(operation.compactMenuSubtitle(),
Double(compactWidth(for: timezone)), attributes: timeAttributes) Double(compactWidth(for: timezone)), attributes: timeBasedAttributes)
let bestTitleSize = compactModeTimeFont.size(operation.compactMenuTitle(), Double(compactWidth(for: timezone)), attributes: timeAttributes) let bestTitleSize = compactModeTimeFont.size(operation.compactMenuTitle(), Double(compactWidth(for: timezone)), attributes: timeBasedAttributes)
return Int(max(bestSize.width, bestTitleSize.width) + bufferWidth) return Int(max(bestSize.width, bestTitleSize.width) + bufferWidth)
} }

50
Clocker/Menu Bar/StatusItemView.swift

@ -2,7 +2,7 @@
import Cocoa import Cocoa
private var defaultParagraphStyle: NSMutableParagraphStyle { var defaultParagraphStyle: NSMutableParagraphStyle {
let paragraphStyle = NSMutableParagraphStyle() let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center paragraphStyle.alignment = .center
paragraphStyle.lineBreakMode = .byTruncatingTail paragraphStyle.lineBreakMode = .byTruncatingTail
@ -13,10 +13,39 @@ var compactModeTimeFont: NSFont {
return NSFont.monospacedDigitSystemFont(ofSize: 10, weight: .regular) return NSFont.monospacedDigitSystemFont(ofSize: 10, weight: .regular)
} }
var timeAttributes: [NSAttributedString.Key: AnyObject] { extension NSView {
var textColor = UserDefaults.standard.string(forKey: "AppleInterfaceStyle") == "Dark" ? NSColor.white : NSColor.black var hasDarkAppearance: Bool {
if #available(OSX 10.14, *) {
switch effectiveAppearance.name {
case .darkAqua, .vibrantDark, .accessibilityHighContrastDarkAqua, .accessibilityHighContrastVibrantDark:
return true
default:
return false
}
} else {
switch effectiveAppearance.name {
case .vibrantDark:
return true
default:
return false
}
}
}
}
class StatusItemView: NSView {
// MARK: Private variables
private let locationView: NSTextField = NSTextField(labelWithString: "Hello")
private let timeView: NSTextField = NSTextField(labelWithString: "Mon 19:14 PM")
private var operationsObject: TimezoneDataOperations {
return TimezoneDataOperations(with: dataObject)
}
private var timeAttributes: [NSAttributedString.Key: AnyObject] {
var textColor = hasDarkAppearance ? NSColor.white : NSColor.black
if #available(macOS 10.15, *) { if #available(macOS 10.16, *) {
textColor = NSColor.white textColor = NSColor.white
} }
@ -27,21 +56,12 @@ var timeAttributes: [NSAttributedString.Key: AnyObject] {
NSAttributedString.Key.paragraphStyle: defaultParagraphStyle, NSAttributedString.Key.paragraphStyle: defaultParagraphStyle,
] ]
return attributes return attributes
}
class StatusItemView: NSView {
// MARK: Private variables
private let locationView: NSTextField = NSTextField(labelWithString: "Hello")
private let timeView: NSTextField = NSTextField(labelWithString: "Mon 19:14 PM")
private var operationsObject: TimezoneDataOperations {
return TimezoneDataOperations(with: dataObject)
} }
private var textFontAttributes: [NSAttributedString.Key: Any] { private var textFontAttributes: [NSAttributedString.Key: Any] {
var textColor = UserDefaults.standard.string(forKey: "AppleInterfaceStyle") == "Dark" ? NSColor.white : NSColor.black var textColor = hasDarkAppearance ? NSColor.white : NSColor.black
if #available(macOS 10.15, *) { if #available(macOS 10.16, *) {
textColor = NSColor.white textColor = NSColor.white
} }

Loading…
Cancel
Save