perf: remove normalize-subview to avoid appkit work
This commit is contained in:
1 parent
13ed78bc23
commit
ce2dfc1f19
11 files changed
+26
-42
No files matched your search
@@ -123,27 +123,16 @@ extension NSView {
|
||||
}
|
||||
|
||||
func setSubviews(_ views: [NSView]) {
|
||||
for view in views {
|
||||
normalizeSubview(view)
|
||||
}
|
||||
subviews = views
|
||||
}
|
||||
|
||||
func addSubviews(_ views: [NSView]) {
|
||||
for view in views {
|
||||
normalizeSubview(view)
|
||||
}
|
||||
subviews = subviews + views
|
||||
}
|
||||
|
||||
func setSubviewAbove(_ view: NSView) {
|
||||
normalizeSubview(view)
|
||||
addSubview(view, positioned: .above, relativeTo: nil)
|
||||
}
|
||||
|
||||
private func normalizeSubview(_ view: NSView) {
|
||||
view.translatesAutoresizingMaskIntoConstraints = false
|
||||
}
|
||||
}
|
||||
|
||||
extension Collection {
|
||||
|
||||
@@ -29,6 +29,7 @@ class FeedbackWindow: NSWindow {
|
||||
|
||||
private func setupView() {
|
||||
let appIcon = LightImageView()
|
||||
appIcon.translatesAutoresizingMaskIntoConstraints = false
|
||||
appIcon.updateContents(.cgImage(App.appIcon), NSSize(width: 80, height: 80))
|
||||
appIcon.fit(80, 80)
|
||||
let appText = StackView([
|
||||
|
||||
@@ -14,7 +14,6 @@ class LightImageView: NSView {
|
||||
init(frame frameRect: NSRect = .zero, withTransparencyChecks: Bool = false) {
|
||||
self.withTransparencyChecks = withTransparencyChecks
|
||||
super.init(frame: frameRect)
|
||||
translatesAutoresizingMaskIntoConstraints = false
|
||||
wantsLayer = true
|
||||
// configure the layer for efficient GPU-scaling
|
||||
layer!.contentsGravity = .resize
|
||||
|
||||
@@ -13,7 +13,6 @@ class TrafficLightButton: NSButton {
|
||||
self.type = type
|
||||
target = self
|
||||
action = #selector(onClick)
|
||||
fit(TrafficLightButton.size, TrafficLightButton.size)
|
||||
addTrackingArea(NSTrackingArea(rect: bounds, options: [.mouseEnteredAndExited, .activeInKeyWindow], owner: self, userInfo: nil))
|
||||
toolTip = tooltip
|
||||
appearance = NSAppearance(named: .aqua)
|
||||
|
||||
@@ -32,7 +32,7 @@ class ThumbnailFontIconView: ThumbnailTitleView {
|
||||
attributedStringValue = initialAttributedString
|
||||
textColor = color
|
||||
toolTip = tooltip
|
||||
addOrUpdateConstraint(widthAnchor, cell!.cellSize.width)
|
||||
frame.size = cell!.cellSize
|
||||
}
|
||||
|
||||
// number should be in the interval [0-50]
|
||||
@@ -69,14 +69,14 @@ class ThumbnailFontIconView: ThumbnailTitleView {
|
||||
class ThumbnailFilledFontIconView: NSView {
|
||||
convenience init(_ thumbnailFontIconView: ThumbnailFontIconView, backgroundColor: NSColor, size: CGFloat) {
|
||||
self.init(frame: .zero)
|
||||
translatesAutoresizingMaskIntoConstraints = false
|
||||
let backgroundView = ThumbnailFontIconView(symbol: .filledCircled, size: size - 3, color: backgroundColor)
|
||||
addSubview(backgroundView)
|
||||
addSubview(thumbnailFontIconView, positioned: .above, relativeTo: nil)
|
||||
backgroundView.centerXAnchor.constraint(equalTo: thumbnailFontIconView.centerXAnchor).isActive = true
|
||||
let offset = ((thumbnailFontIconView.cell!.cellSize.width - backgroundView.cell!.cellSize.width) / 2).rounded()
|
||||
backgroundView.topAnchor.constraint(equalTo: thumbnailFontIconView.topAnchor, constant: offset).isActive = true
|
||||
widthAnchor.constraint(equalTo: thumbnailFontIconView.widthAnchor).isActive = true
|
||||
heightAnchor.constraint(equalTo: thumbnailFontIconView.heightAnchor).isActive = true
|
||||
let fgSize = thumbnailFontIconView.frame.size
|
||||
let bgSize = backgroundView.frame.size
|
||||
let offset = ((fgSize.width - bgSize.width) / 2).rounded()
|
||||
thumbnailFontIconView.frame.origin = .zero
|
||||
backgroundView.frame.origin = NSPoint(x: (fgSize.width - bgSize.width) / 2, y: offset)
|
||||
frame.size = fgSize
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ import Cocoa
|
||||
|
||||
class ThumbnailTitleView: NSTextField {
|
||||
private var currentWidth: CGFloat = -1
|
||||
private var widthConstraint: NSLayoutConstraint?
|
||||
|
||||
// we set their size manually; override this to remove wasteful appkit-side work
|
||||
override var intrinsicContentSize: NSSize {
|
||||
@@ -10,30 +9,26 @@ class ThumbnailTitleView: NSTextField {
|
||||
}
|
||||
|
||||
convenience init(font: NSFont) {
|
||||
self.init(labelWithString: "")
|
||||
self.init(frame: .zero)
|
||||
stringValue = ""
|
||||
isEditable = false
|
||||
isSelectable = false
|
||||
isBezeled = false
|
||||
drawsBackground = false
|
||||
self.font = font
|
||||
textColor = Appearance.fontColor
|
||||
// drawsBackground = true
|
||||
// backgroundColor = .red
|
||||
lineBreakMode = .byTruncatingTail
|
||||
allowsDefaultTighteningForTruncation = false
|
||||
translatesAutoresizingMaskIntoConstraints = false
|
||||
}
|
||||
|
||||
func fixHeight() {
|
||||
heightAnchor.constraint(equalToConstant: fittingSize.height).isActive = true
|
||||
frame.size.height = cell!.cellSize.height
|
||||
}
|
||||
|
||||
func setWidth(_ width: CGFloat) {
|
||||
guard currentWidth != width else { return }
|
||||
currentWidth = width
|
||||
frame.size.width = width
|
||||
if let widthConstraint {
|
||||
widthConstraint.constant = width
|
||||
} else {
|
||||
let constraint = widthAnchor.constraint(equalToConstant: width)
|
||||
constraint.isActive = true
|
||||
widthConstraint = constraint
|
||||
}
|
||||
}
|
||||
|
||||
override func mouseMoved(with event: NSEvent) {
|
||||
|
||||
@@ -182,7 +182,6 @@ class ThumbnailView: FlippedView {
|
||||
}
|
||||
|
||||
private func setupView() {
|
||||
translatesAutoresizingMaskIntoConstraints = false
|
||||
setAccessibilityChildren([])
|
||||
wantsLayer = true
|
||||
layer!.masksToBounds = false // without this, label will be clipped in app-icons style since its larger than its parentView
|
||||
@@ -196,7 +195,6 @@ class ThumbnailView: FlippedView {
|
||||
thumbnail.shadow = shadow
|
||||
appIcon.shadow = shadow
|
||||
dockLabelIcon.shadow = shadow
|
||||
appIcon.translatesAutoresizingMaskIntoConstraints = false
|
||||
appIcon.setSubviewAbove(dockLabelIcon)
|
||||
label.fixHeight()
|
||||
vStackView.wantsLayer = true
|
||||
@@ -552,7 +550,7 @@ class ThumbnailView: FlippedView {
|
||||
static func widthOfComfortableReadability() -> CGFloat? {
|
||||
let labTitleView = ThumbnailTitleView(font: Appearance.font)
|
||||
labTitleView.stringValue = "abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz" + extraTextForPadding
|
||||
return labTitleView.fittingSize.width
|
||||
return labTitleView.cell!.cellSize.width
|
||||
}
|
||||
|
||||
static func widthOfLongestTitle() -> CGFloat? {
|
||||
@@ -561,7 +559,7 @@ class ThumbnailView: FlippedView {
|
||||
for window in Windows.list {
|
||||
guard window.shouldShowTheUser else { continue }
|
||||
labTitleView.stringValue = window.title + extraTextForPadding
|
||||
let width = labTitleView.fittingSize.width
|
||||
let width = labTitleView.cell!.cellSize.width
|
||||
if width > maxWidth {
|
||||
maxWidth = width
|
||||
}
|
||||
|
||||
@@ -19,11 +19,11 @@ class ThumbnailsView {
|
||||
|
||||
static func updateCachedSizes() {
|
||||
guard let firstView = ThumbnailsView.recycledViews.first else { return }
|
||||
layoutCache.labelHeight = firstView.label.fittingSize.height
|
||||
let iconFittingSize = firstView.windowIndicatorIcons.first!.fittingSize
|
||||
layoutCache.iconWidth = iconFittingSize.width
|
||||
layoutCache.iconHeight = iconFittingSize.height
|
||||
layoutCache.dockLabelSize = firstView.dockLabelIcon.fittingSize
|
||||
layoutCache.labelHeight = firstView.label.cell!.cellSize.height
|
||||
let iconCellSize = firstView.windowIndicatorIcons.first!.cell!.cellSize
|
||||
layoutCache.iconWidth = iconCellSize.width
|
||||
layoutCache.iconHeight = iconCellSize.height
|
||||
layoutCache.dockLabelSize = firstView.dockLabelIcon.frame.size
|
||||
layoutCache.comfortableReadabilityWidth = ThumbnailView.widthOfComfortableReadability()
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ class PermissionsWindow: NSWindow {
|
||||
|
||||
private func setupView() {
|
||||
let appIcon = LightImageView()
|
||||
appIcon.translatesAutoresizingMaskIntoConstraints = false
|
||||
appIcon.updateContents(.cgImage(App.appIcon), NSSize(width: 80, height: 80))
|
||||
appIcon.fit(80, 80)
|
||||
let appText = TitleLabel(NSLocalizedString("AltTab needs some permissions", comment: ""))
|
||||
|
||||
@@ -134,6 +134,7 @@ class LabelAndControl: NSObject {
|
||||
onMouseEntered: EventClosure? = nil,
|
||||
onMouseExited: EventClosure? = nil) -> ClickHoverImageView {
|
||||
let imageView = ThumbnailFontIconView(symbol: .circledInfo, size: size, color: .labelColor)
|
||||
imageView.translatesAutoresizingMaskIntoConstraints = false
|
||||
let view = ClickHoverImageView(infoCircle: imageView)
|
||||
view.onClick = onClick
|
||||
view.onMouseEntered = onMouseEntered
|
||||
|
||||
@@ -3,6 +3,7 @@ import Cocoa
|
||||
class AboutTab {
|
||||
static func initTab() -> NSView {
|
||||
let appIcon = LightImageView()
|
||||
appIcon.translatesAutoresizingMaskIntoConstraints = false
|
||||
appIcon.updateContents(.cgImage(App.appIcon), NSSize(width: 256, height: 256))
|
||||
appIcon.fit(256, 256)
|
||||
let appText = StackView([
|
||||
|
||||
Reference in new issue
Block a user