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.

141 lines
4.7 KiB

// Copyright © 2015 Abhishek Banthia
import Cocoa
var defaultParagraphStyle: NSMutableParagraphStyle {
6 years ago
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
paragraphStyle.lineBreakMode = .byTruncatingTail
return paragraphStyle
}
var compactModeTimeFont: NSFont {
return NSFont.monospacedDigitSystemFont(ofSize: 10, weight: .regular)
}
extension NSView {
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
6 years ago
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)
}
6 years ago
private var timeAttributes: [NSAttributedString.Key: AnyObject] {
var textColor = hasDarkAppearance ? NSColor.white : NSColor.black
if #available(OSX 11.0, *) {
textColor = NSColor.white
}
let attributes = [
NSAttributedString.Key.font: compactModeTimeFont,
NSAttributedString.Key.foregroundColor: textColor,
NSAttributedString.Key.backgroundColor: NSColor.clear,
NSAttributedString.Key.paragraphStyle: defaultParagraphStyle,
]
return attributes
}
private var textFontAttributes: [NSAttributedString.Key: Any] {
var textColor = hasDarkAppearance ? NSColor.white : NSColor.black
if #available(OSX 11.0, *) {
textColor = NSColor.white
}
6 years ago
let textFontAttributes = [
NSAttributedString.Key.font: NSFont.boldSystemFont(ofSize: 10),
NSAttributedString.Key.foregroundColor: textColor,
NSAttributedString.Key.backgroundColor: NSColor.clear,
6 years ago
NSAttributedString.Key.paragraphStyle: defaultParagraphStyle,
]
return textFontAttributes
}
6 years ago
6 years ago
// MARK: Public
var dataObject: TimezoneData! {
didSet {
initialSetup()
}
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
6 years ago
[timeView, locationView].forEach {
$0.wantsLayer = true
$0.applyDefaultStyle()
$0.translatesAutoresizingMaskIntoConstraints = false
addSubview($0)
}
6 years ago
timeView.disableWrapping()
6 years ago
NSLayoutConstraint.activate([
locationView.leadingAnchor.constraint(equalTo: leadingAnchor),
locationView.trailingAnchor.constraint(equalTo: trailingAnchor),
locationView.topAnchor.constraint(equalTo: topAnchor, constant: 7),
6 years ago
locationView.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 0.35),
])
6 years ago
NSLayoutConstraint.activate([
timeView.leadingAnchor.constraint(equalTo: leadingAnchor),
timeView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0),
timeView.topAnchor.constraint(equalTo: locationView.bottomAnchor),
6 years ago
timeView.bottomAnchor.constraint(equalTo: bottomAnchor),
])
}
6 years ago
@available(OSX 10.14, *)
override func viewDidChangeEffectiveAppearance() {
super.viewDidChangeEffectiveAppearance()
updateTimeInMenubar()
}
func updateTimeInMenubar() {
locationView.attributedStringValue = NSAttributedString(string: operationsObject.compactMenuTitle(), attributes: textFontAttributes)
timeView.attributedStringValue = NSAttributedString(string: operationsObject.compactMenuSubtitle(), attributes: timeAttributes)
}
6 years ago
private func initialSetup() {
locationView.attributedStringValue = NSAttributedString(string: operationsObject.compactMenuTitle(), attributes: textFontAttributes)
timeView.attributedStringValue = NSAttributedString(string: operationsObject.compactMenuSubtitle(), attributes: timeAttributes)
}
6 years ago
6 years ago
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
6 years ago
override func mouseDown(with event: NSEvent) {
super.mouseDown(with: event)
guard let mainDelegate = NSApplication.shared.delegate as? AppDelegate else {
return
}
6 years ago
mainDelegate.togglePanel(event)
}
}