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.

147 lines
5.9 KiB

// Copyright © 2015 Abhishek Banthia
3 years ago
import AppKit
import Foundation
class UpcomingEventViewItem: NSCollectionViewItem {
static let reuseIdentifier = NSUserInterfaceItemIdentifier("UpcomingEventViewItem")
3 years ago
3 years ago
@IBOutlet var calendarColorView: NSView!
@IBOutlet var leadingConstraint: NSLayoutConstraint!
@IBOutlet var eventTitleLabel: NSTextField!
@IBOutlet var eventSubtitleButton: NSButton!
3 years ago
@IBOutlet var supplementaryButtonWidthConstraint: NSLayoutConstraint!
3 years ago
@IBOutlet var zoomButton: NSButton!
3 years ago
private static let SupplementaryButtonWidth: CGFloat = 24.0
private static let EventLeadingConstraint: CGFloat = 10.0
3 years ago
private var meetingLink: URL?
private weak var panelDelegate: UpcomingEventPanelDelegate?
override func viewDidLoad() {
zoomButton.target = self
zoomButton.action = #selector(zoomButtonAction(_:))
}
override func prepareForReuse() {
zoomButton.image = nil
eventTitleLabel.stringValue = ""
eventSubtitleButton.stringValue = ""
3 years ago
meetingLink = nil
}
override var acceptsFirstResponder: Bool {
return false
}
// MARK: Setup UI
3 years ago
3 years ago
func setup(_ title: String,
_ subtitle: String,
_ color: NSColor,
_ link: URL?,
_ delegate: UpcomingEventPanelDelegate?,
_ isCancelled: Bool)
{
if leadingConstraint.constant != UpcomingEventViewItem.EventLeadingConstraint / 2 {
leadingConstraint.animator().constant = UpcomingEventViewItem.EventLeadingConstraint / 2
3 years ago
}
panelDelegate = delegate
meetingLink = link
setupLabels(title, isCancelled)
setupSupplementaryButton(link, cancellationState: isCancelled)
setCalendarButtonTitle(buttonTitle: subtitle, cancellationState: isCancelled)
calendarColorView.layer?.backgroundColor = color.cgColor
}
private func setupLabels(_ title: String, _ cancellationState: Bool) {
var sanitizedTitle = title
if (cancellationState) {
let offendingString = "Canceled: "
sanitizedTitle = sanitizedTitle.replacingOccurrences(of: offendingString, with: "")
}
let attributes: [NSAttributedString.Key: Any] = cancellationState ? [NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue,
NSAttributedString.Key.strikethroughColor: NSColor.gray] : [:]
let attributedString = NSAttributedString(string: sanitizedTitle, attributes: attributes)
eventTitleLabel.attributedStringValue = attributedString
3 years ago
3 years ago
eventTitleLabel.toolTip = title
}
3 years ago
private func setupSupplementaryButton(_ meetingURL: URL?, cancellationState: Bool) {
guard meetingURL != nil, cancellationState == false else {
3 years ago
zoomButton.image = nil
supplementaryButtonWidthConstraint.constant = 0.0
return
3 years ago
}
zoomButton.isHidden = false
zoomButton.image = Themer.shared().videoCallImage()
if supplementaryButtonWidthConstraint.constant != UpcomingEventViewItem.SupplementaryButtonWidth {
supplementaryButtonWidthConstraint.constant = UpcomingEventViewItem.SupplementaryButtonWidth
3 years ago
}
}
3 years ago
func setupUndeterminedState(_ delegate: UpcomingEventPanelDelegate?) {
3 years ago
panelDelegate = delegate
setAlternateState(NSLocalizedString("See your next Calendar event here.", comment: "Next Event Label for no Calendar access"),
NSLocalizedString("Click here to start.", comment: "Button Title for no Calendar access"),
NSColor.systemBlue,
Themer.shared().removeImage())
3 years ago
}
3 years ago
3 years ago
func setupEmptyState() {
3 years ago
let subtitle = NSCalendar.autoupdatingCurrent.isDateInWeekend(Date()) ? "Happy Weekend.".localized() : "Great going.".localized()
3 years ago
setAlternateState(NSLocalizedString("No upcoming events for today!", comment: "Next Event Label with no upcoming event"),
3 years ago
subtitle,
NSColor.systemGreen,
nil)
}
private func setAlternateState(_ title: String, _ buttonTitle: String, _ color: NSColor, _ image: NSImage? = nil) {
if leadingConstraint.constant != UpcomingEventViewItem.EventLeadingConstraint {
leadingConstraint.animator().constant = UpcomingEventViewItem.EventLeadingConstraint
3 years ago
}
eventTitleLabel.stringValue = title
setCalendarButtonTitle(buttonTitle: buttonTitle, cancellationState: false)
calendarColorView.layer?.backgroundColor = color.cgColor
zoomButton.image = image
3 years ago
}
3 years ago
private func setCalendarButtonTitle(buttonTitle: String, cancellationState: Bool) {
3 years ago
let style = NSMutableParagraphStyle()
style.alignment = .left
style.lineBreakMode = .byTruncatingTail
3 years ago
3 years ago
if let boldFont = NSFont(name: "Avenir", size: 11) {
let sanitizedString = cancellationState ? "Canceled." : buttonTitle
3 years ago
let attributes = [NSAttributedString.Key.foregroundColor: NSColor.gray, NSAttributedString.Key.paragraphStyle: style, NSAttributedString.Key.font: boldFont]
let attributedString = NSAttributedString(string: sanitizedString, attributes: attributes)
3 years ago
eventSubtitleButton.attributedTitle = attributedString
eventSubtitleButton.toolTip = buttonTitle
3 years ago
}
}
// MARK: Button Actions
3 years ago
@IBAction func calendarButtonAction(_ sender: NSButton) {
3 years ago
panelDelegate?.didClickSupplementaryButton(sender)
3 years ago
}
3 years ago
@objc func zoomButtonAction(_ sender: NSButton) {
guard sender.image != nil else { return }
3 years ago
3 years ago
if let meetingURL = meetingLink {
NSWorkspace.shared.open(meetingURL)
} else {
3 years ago
panelDelegate?.didRemoveCalendarView()
3 years ago
}
}
}