NetNewsWire/Mac/MainWindow/Sidebar/Cell/SidebarCellLayout.swift

59 lines
1.8 KiB
Swift
Raw Normal View History

//
// SidebarLayout.swift
2018-08-29 07:18:24 +02:00
// NetNewsWire
//
// Created by Brent Simmons on 11/24/17.
// Copyright © 2017 Ranchero Software. All rights reserved.
//
import AppKit
// image - title - unreadCount
2024-03-20 07:05:30 +01:00
@MainActor struct SidebarCellLayout {
let faviconRect: CGRect
let titleRect: CGRect
let unreadCountRect: CGRect
init(appearance: SidebarCellAppearance, cellSize: NSSize, shouldShowImage: Bool, textField: NSTextField, unreadCountView: UnreadCountView) {
let bounds = NSRect(x: 0.0, y: 0.0, width: floor(cellSize.width), height: floor(cellSize.height))
var rFavicon = NSRect.zero
if shouldShowImage {
rFavicon = NSRect(x: 0.0, y: 0.0, width: appearance.imageSize.width, height: appearance.imageSize.height)
2020-01-09 06:40:21 +01:00
rFavicon = rFavicon.centeredVertically(in: bounds)
}
self.faviconRect = rFavicon
let textFieldSize = SingleLineTextFieldSizer.size(for: textField.stringValue, font: textField.font!)
var rTextField = NSRect(x: 0.0, y: 0.0, width: textFieldSize.width, height: textFieldSize.height)
if shouldShowImage {
rTextField.origin.x = NSMaxX(rFavicon) + appearance.imageMarginRight
}
2020-01-09 06:40:21 +01:00
rTextField = rTextField.centeredVertically(in: bounds)
let unreadCountSize = unreadCountView.intrinsicContentSize
let unreadCountIsHidden = unreadCountView.unreadCount < 1
var rUnread = NSRect.zero
if !unreadCountIsHidden {
rUnread.size = unreadCountSize
rUnread.origin.x = NSMaxX(bounds) - unreadCountSize.width
2020-01-09 06:40:21 +01:00
rUnread = rUnread.centeredVertically(in: bounds)
let textFieldMaxX = NSMinX(rUnread) - appearance.unreadCountMarginLeft
if NSMaxX(rTextField) > textFieldMaxX {
rTextField.size.width = textFieldMaxX - NSMinX(rTextField)
}
}
self.unreadCountRect = rUnread
if NSMaxX(rTextField) > NSMaxX(bounds) {
rTextField.size.width = NSMaxX(bounds) - NSMinX(rTextField)
}
self.titleRect = rTextField
}
}