-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathButton.swift
619 lines (549 loc) · 19.3 KB
/
Button.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
import AppKit
// MARK: - Button
/// A fluent styled button, with hover effects and a corner radius.
@objc(MSFButton)
open class Button: NSButton {
/// Initializes a Fluent UI Button with a title only
/// - Parameters:
/// - title: String displayed in the button
/// - size: The ButtonSize, default .large
/// - style: The ButtonStyle, default .primary
/// - accentColor: The accent NSColor, defaulting to the app primary color
@objc public convenience init(
title: String,
size: ButtonSize = .large,
style: ButtonStyle = .primary,
accentColor: NSColor = Colors.primary
) {
let format = ButtonFormat(size: size, style: style, accentColor: accentColor)
self.init(title: title, format: format)
}
/// Initializes a Fluent UI Button with an image only
/// - Parameters:
/// - image: The NSImage to diplay in the button
/// - size: The ButtonSize, default .large
/// - style: The ButtonStyle, default .primary
/// - accentColor: The accent NSColor, defaulting to the app primary color
@objc public convenience init(
image: NSImage,
size: ButtonSize = .large,
style: ButtonStyle = .primary,
accentColor: NSColor = Colors.primary
) {
let format = ButtonFormat(size: size, style: style, accentColor: accentColor)
self.init(image: image, format: format)
}
/// Initializes a Fluent UI Button with a title and image
/// - Parameters:
/// - title: String displayed in the button
/// - image: The NSImage to diplay in the button
/// - imagePosition: The position of the image relative to the title, default .imageLeading
/// - size: The ButtonSize, default .large
/// - style: The ButtonStyle, default .primary
/// - accentColor: The accent NSColor, defaulting to the app primary color
@objc public convenience init(
title: String,
image: NSImage,
imagePosition: NSControl.ImagePosition = .imageLeading,
size: ButtonSize = .large,
style: ButtonStyle = .primary,
accentColor: NSColor = Colors.primary
) {
let format = ButtonFormat(size: size, style: style, accentColor: accentColor)
self.init(title: title, image: image, imagePosition: imagePosition, format: format)
}
/// Initializes a Fluent UI Button with no title or image, and with default formatting
@objc public convenience init() {
self.init(title: "", image: nil, imagePosition: .imageLeading, format: ButtonFormat())
}
@available(*, unavailable)
required public init?(coder decoder: NSCoder) {
preconditionFailure()
}
/// Swift-only designated initializer accepting ButtonFormat struct.
/// - Parameters:
/// - title: String displayed in the button, default empty string
/// - image: The NSImage to diplay in the button, default nil
/// - imagePosition: The position of the image, relative to the title, default imageLeading
/// - format: The ButtonFormat including size, style and accentColor, with all applicable defaults
public init(
title: String = "",
image: NSImage? = nil,
imagePosition: NSControl.ImagePosition = .imageLeading,
format: ButtonFormat = ButtonFormat()
) {
super.init(frame: .zero)
isBordered = false
wantsLayer = true
layer?.contentsScale = window?.backingScaleFactor ?? 1.0
setButtonType(.momentaryChange)
if let cell = cell as? ButtonCell {
// The Button properties `contentTintColorDisabled`,
// `backgroundColorDisabled`, and `borderColorDisabled` fully
// specify the look of the disabled button, and may already include
// a 'disabled' system effect. Don't apply the effect again on top
// of that.
cell.imageDimsWhenDisabled = false
}
self.title = title
self.image = image
self.imagePosition = imagePosition
self.format = format
// Ensure we update backing properties even if high-level style and size
// properties have their default values
let defaultFormat = ButtonFormat()
if style == defaultFormat.style {
setColorValues(forStyle: style, accentColor: accentColor)
}
if size == defaultFormat.size {
setSizeParameters(forSize: size)
}
}
open override class var cellClass: AnyClass? {
get {
return ButtonCell.self
}
set {}
}
/// Image to display in the button.
open override var image: NSImage? {
willSet {
guard wantsLayer == true else {
preconditionFailure("wantsLayer must be set so that the image is rendered on the layer")
}
}
}
/// Title string to display in the button.
public override var title: String {
willSet {
guard wantsLayer == true else {
preconditionFailure("wantsLayer must be set so that the title is rendered on the layer")
}
}
}
/// While the current Button is pressed, its style is temporarily applied to the linkedPrimary button.
/// This emulates an effect seen in the default style of Cocoa buttons, where pressing a secondary
/// button takes the accent color highlighting from a nearby primary button. For best results, the
/// current button should have the `.secondary` style, the linkedPrimary button should have the
/// `.primary` style, and both buttons should have the same accentColor.
@objc public var linkedPrimary: Button? {
didSet {
guard oldValue != linkedPrimary else {
return
}
linkedPrimaryOriginalStyle = linkedPrimary?.style
}
}
private var linkedPrimaryOriginalStyle: ButtonStyle?
public var isPressed: Bool = false {
didSet {
guard isEnabled && oldValue != isPressed else {
return
}
updateContentTintColor()
if let linkedPrimary = linkedPrimary {
if isPressed {
linkedPrimaryOriginalStyle = linkedPrimary.style
linkedPrimary.style = self.style
} else {
linkedPrimary.style = linkedPrimaryOriginalStyle ?? .primary
}
}
needsDisplay = true
}
}
open override func mouseDown(with event: NSEvent) {
isPressed = true
super.mouseDown(with: event)
isPressed = false
}
open override var isEnabled: Bool {
didSet {
guard oldValue != isEnabled else {
return
}
updateContentTintColor()
}
}
open override func updateLayer() {
guard let layer = layer else {
return
}
layer.borderWidth = Button.borderWidth
layer.cornerRadius = cornerRadius
if !isEnabled {
layer.backgroundColor = backgroundColorDisabled?.cgColor
layer.borderColor = borderColorDisabled?.cgColor
} else if isPressed {
layer.backgroundColor = backgroundColorPressed?.cgColor
layer.borderColor = borderColorPressed?.cgColor
} else {
layer.backgroundColor = backgroundColorRest?.cgColor
layer.borderColor = borderColorRest?.cgColor
}
}
public override var wantsUpdateLayer: Bool {
return true
}
open override func drawFocusRingMask() {
// Ensure we draw the focus ring around the entire button bounds
// rather than just around the image or title.
let path = NSBezierPath(roundedRect: bounds, xRadius: cornerRadius, yRadius: cornerRadius)
path.fill()
}
open override func viewDidChangeBackingProperties() {
super.viewDidChangeBackingProperties()
// Update the layer content scales to the current window backingScaleFactor
guard let scale = window?.backingScaleFactor else {
return
}
if let layer = layer {
layer.contentsScale = scale
}
}
public var format: ButtonFormat {
get {
return ButtonFormat(
size: self.size,
style: self.style,
accentColor: self.accentColor
)
}
set {
self.accentColor = newValue.accentColor
self.style = newValue.style
self.size = newValue.size
}
}
/// State-specific colors for foreground, background and border
private var contentTintColorRest: NSColor?
private var contentTintColorPressed: NSColor?
private var contentTintColorDisabled: NSColor?
private var backgroundColorRest: NSColor?
private var backgroundColorPressed: NSColor?
private var backgroundColorDisabled: NSColor?
private var borderColorRest: NSColor?
private var borderColorPressed: NSColor?
private var borderColorDisabled: NSColor?
private func updateContentTintColor() {
if !isEnabled {
contentTintColor = contentTintColorDisabled
} else if isPressed {
contentTintColor = contentTintColorPressed
} else {
contentTintColor = contentTintColorRest
}
}
private func setColorValues(forStyle: ButtonStyle, accentColor: NSColor) {
switch forStyle {
case .primary:
contentTintColorRest = ButtonColor.neutralInverted
contentTintColorPressed = ButtonColor.neutralInverted?.withSystemEffect(.pressed)
contentTintColorDisabled = ButtonColor.brandForegroundDisabled
backgroundColorRest = accentColor
backgroundColorPressed = accentColor.withSystemEffect(.pressed)
backgroundColorDisabled = ButtonColor.brandBackgroundDisabled
borderColorRest = .clear
borderColorPressed = .clear
borderColorDisabled = .clear
case .secondary:
contentTintColorRest = .textColor
contentTintColorPressed = ButtonColor.neutralInverted?.withSystemEffect(.pressed)
contentTintColorDisabled = NSColor.textColor.withSystemEffect(.disabled)
backgroundColorRest = ButtonColor.neutralBackground2
backgroundColorPressed = accentColor.withSystemEffect(.pressed)
backgroundColorDisabled = ButtonColor.neutralBackground2?.withSystemEffect(.disabled)
borderColorRest = ButtonColor.neutralStroke2
borderColorPressed = .clear
borderColorDisabled = ButtonColor.neutralStroke2?.withSystemEffect(.disabled)
case .acrylic:
contentTintColorRest = ButtonColor.neutralForeground3
contentTintColorPressed = ButtonColor.neutralForeground3?.withSystemEffect(.pressed)
contentTintColorDisabled = ButtonColor.neutralForeground3?.withSystemEffect(.disabled)
backgroundColorRest = ButtonColor.neutralBackground3
backgroundColorPressed = ButtonColor.neutralBackground3?.withSystemEffect(.pressed)
backgroundColorDisabled = ButtonColor.neutralBackground3?.withSystemEffect(.disabled)
borderColorRest = .clear
borderColorPressed = .clear
borderColorDisabled = .clear
case .borderless:
contentTintColorRest = accentColor
contentTintColorPressed = accentColor.withSystemEffect(.deepPressed)
contentTintColorDisabled = ButtonColor.brandForegroundDisabled
backgroundColorRest = .clear
backgroundColorPressed = .clear
backgroundColorDisabled = .clear
borderColorRest = .clear
borderColorPressed = .clear
borderColorDisabled = .clear
}
updateContentTintColor()
}
/// This color is used for the background in the primary style, the pressed background in the secondary
/// style, and the content tint (i.e. foreground text/image) color in the borderless style. It is not used
/// for the acrylic style.
@objc public var accentColor: NSColor = Colors.primary {
didSet {
guard oldValue != accentColor else {
return
}
// Recompute relevant state-specific colors appropriate to the style
setColorValues(forStyle: style, accentColor: accentColor)
needsDisplay = true
}
}
/// Any of several pre-set button styles. Setting it determines the content tint
/// (i.e. foreground text/image), background and border color properties for all button states.
@objc public var style: ButtonStyle = .primary {
didSet {
guard oldValue != style else {
return
}
setColorValues(forStyle: style, accentColor: accentColor)
needsDisplay = true
}
}
private var cornerRadius: CGFloat = ButtonSizeParameters.large.cornerRadius
private static let borderWidth: CGFloat = 1
private func setSizeParameters(forSize: ButtonSize) {
let parameters = ButtonSizeParameters.parameters(forSize: size)
font = NSFont.systemFont(ofSize: parameters.fontSize)
cornerRadius = parameters.cornerRadius
guard let cell = cell as? ButtonCell else {
return
}
cell.verticalPadding = parameters.verticalPadding
cell.horizontalPadding = parameters.horizontalPadding
cell.titleVerticalPositionAdjustment = parameters.titleVerticalPositionAdjustment
cell.titleToImageSpacing = parameters.titleToImageSpacing
cell.titleToImageVerticalSpacingAdjustment = parameters.titleToImageVerticalSpacingAdjustment
}
/// Any of several pre-set button sizes. Determines several factors including font size, corner radius,
/// and padding.
@objc public var size: ButtonSize = .large {
didSet {
guard oldValue != size else {
return
}
setSizeParameters(forSize: size)
invalidateIntrinsicContentSize()
needsDisplay = true
}
}
}
// MARK: - ButtonCell
class ButtonCell: NSButtonCell {
var verticalPadding: CGFloat = 0
var horizontalPadding: CGFloat = 0
var titleVerticalPositionAdjustment: CGFloat = 0
var titleToImageSpacing: CGFloat = 0
var titleToImageVerticalSpacingAdjustment: CGFloat = 0
override func imageRect(forBounds rect: NSRect) -> NSRect {
guard
let image = image,
let controlView = controlView,
image.size != .zero,
imagePosition != .noImage
else {
return .zero
}
let layoutDirectionSign = controlView.userInterfaceLayoutDirection == .rightToLeft ? -1 : 1
let titleSize = title.size(withAttributes: [.font: font as Any])
let imageSize = image.size
// Image is either centered, or offset from center by the title
var xOffsetSign = 0
var yOffsetSign = 0
if title.count == 0 {
imagePosition = .imageOnly
}
switch imagePosition {
case .noImage:
preconditionFailure(".noImage case is covered by guard return")
case .imageOnly, .imageOverlaps:
break
case .imageLeft:
xOffsetSign = -1
case .imageLeading:
xOffsetSign = -layoutDirectionSign
case .imageRight:
xOffsetSign = 1
case .imageTrailing:
xOffsetSign = layoutDirectionSign
case .imageBelow:
yOffsetSign = 1
case .imageAbove:
yOffsetSign = -1
@unknown default:
break
}
var x = (rect.width - imageSize.width) / 2
var y = (rect.height - imageSize.height) / 2
if xOffsetSign != 0 {
x += CGFloat(xOffsetSign) * (titleSize.width + titleToImageSpacing) / 2
} else if yOffsetSign != 0 {
y += CGFloat(yOffsetSign) * (titleSize.height + titleToImageSpacing - titleToImageVerticalSpacingAdjustment) / 2
}
return NSRect(
x: x.rounded(.towardZero),
y: y.rounded(.towardZero),
width: imageSize.width.rounded(.awayFromZero),
height: imageSize.height.rounded(.awayFromZero)
)
}
override func titleRect(forBounds rect: NSRect) -> NSRect {
guard
let font = font,
let controlView = controlView,
title.count > 0,
imagePosition != .imageOnly
else {
return .zero
}
let layoutDirectionSign = controlView.userInterfaceLayoutDirection == .rightToLeft ? -1 : 1
let titleSize = title.size(withAttributes: [.font: font])
let imageSize = image?.size ?? .zero
if imageSize == .zero {
imagePosition = .noImage
}
// Title is either centered, or offset from center by the image
var xOffsetSign = 0
var yOffsetSign = 0
switch imagePosition {
case .imageOnly:
preconditionFailure(".imageOnly case is covered by guard return")
case .noImage, .imageOverlaps:
break
case .imageLeft:
xOffsetSign = 1
case .imageLeading:
xOffsetSign = layoutDirectionSign
case .imageRight:
xOffsetSign = -1
case .imageTrailing:
xOffsetSign = -layoutDirectionSign
case .imageBelow:
yOffsetSign = -1
case .imageAbove:
yOffsetSign = 1
@unknown default:
break
}
var x = (rect.width - titleSize.width) / 2
var y = (rect.height - titleSize.height) / 2 + titleVerticalPositionAdjustment
if xOffsetSign != 0 {
x += CGFloat(xOffsetSign) * (imageSize.width + titleToImageSpacing) / 2
} else if yOffsetSign != 0 {
y += CGFloat(yOffsetSign) * (imageSize.height + titleToImageSpacing) / 2
}
return NSRect(
x: x.rounded(.towardZero),
y: y.rounded(.towardZero),
width: titleSize.width.rounded(.awayFromZero),
height: titleSize.height.rounded(.awayFromZero)
)
}
override func drawingRect(forBounds rect: NSRect) -> NSRect {
var width = rect.width - (horizontalPadding * 2)
var height = rect.height - (verticalPadding * 2)
switch imagePosition {
case .imageLeft, .imageLeading, .imageRight, .imageTrailing:
width -= titleToImageSpacing
case .imageBelow, .imageAbove:
height -= titleToImageSpacing
case .noImage, .imageOnly, .imageOverlaps:
break
@unknown default:
break
}
return NSRect(x: 0, y: 0, width: floor(width), height: floor(height))
}
}
// MARK: - Button Formatting Parameters
/// Indicates the size of the button
@objc(MSFButtonSize)
public enum ButtonSize: Int, CaseIterable {
case large
case small
}
/// Indicates what style our button is drawn as
@objc(MSFButtonStyle)
public enum ButtonStyle: Int, CaseIterable {
/// Accent color fill, white text/image.
case primary
/// Light mode: white fill, black text/image, light gray outline.
/// Dark mode: dark gray fill, white text/image, subtle gray outline.
/// Pressed: accent color fill, white text/image, no outline (same as primary style).
case secondary
/// Light mode: light gray fill, black text/image.
/// Dark mode: dark gray fill, white text/image.
case acrylic
/// Accent color text/image, no fill or outline.
case borderless
}
/// Combination of all formatting parameters.
public struct ButtonFormat {
public var size: ButtonSize
public var style: ButtonStyle
public var accentColor: NSColor
public init(
size: ButtonSize = .large,
style: ButtonStyle = .primary,
accentColor: NSColor = Colors.primary
) {
self.size = size
self.style = style
self.accentColor = accentColor
}
}
// MARK: - Semantic Color Constants
@objc(MSFButtonColor)
class ButtonColor: NSObject {
public static let brandForegroundDisabled = NSColor(named: "ButtonColors/brandForegroundDisabled", bundle: FluentUIResources.resourceBundle)
public static let brandBackgroundDisabled = NSColor(named: "ButtonColors/brandBackgroundDisabled", bundle: FluentUIResources.resourceBundle)
public static let neutralInverted = NSColor(named: "ButtonColors/neutralInverted", bundle: FluentUIResources.resourceBundle)
public static let neutralForeground2 = NSColor(named: "ButtonColors/neutralForeground2", bundle: FluentUIResources.resourceBundle)
public static let neutralBackground2 = NSColor(named: "ButtonColors/neutralBackground2", bundle: FluentUIResources.resourceBundle)
public static let neutralStroke2 = NSColor(named: "ButtonColors/neutralStroke2", bundle: FluentUIResources.resourceBundle)
public static let neutralForeground3 = NSColor(named: "ButtonColors/neutralForeground3", bundle: FluentUIResources.resourceBundle)
public static let neutralBackground3 = NSColor(named: "ButtonColors/neutralBackground3", bundle: FluentUIResources.resourceBundle)
}
// MARK: - Size Constants
private struct ButtonSizeParameters {
fileprivate let fontSize: CGFloat
fileprivate let cornerRadius: CGFloat
fileprivate let verticalPadding: CGFloat
fileprivate let horizontalPadding: CGFloat
fileprivate let titleVerticalPositionAdjustment: CGFloat
fileprivate let titleToImageSpacing: CGFloat
fileprivate let titleToImageVerticalSpacingAdjustment: CGFloat
static let large = ButtonSizeParameters(
fontSize: 15, // line height: 19
cornerRadius: 6,
verticalPadding: 4.5, // overall height: 28
horizontalPadding: 36,
titleVerticalPositionAdjustment: -0.25,
titleToImageSpacing: 10,
titleToImageVerticalSpacingAdjustment: 7
)
static let small = ButtonSizeParameters(
fontSize: 13, // line height: 17
cornerRadius: 5,
verticalPadding: 1.5, // overall height: 20
horizontalPadding: 14,
titleVerticalPositionAdjustment: 0,
titleToImageSpacing: 6,
titleToImageVerticalSpacingAdjustment: 7
)
static func parameters(forSize: ButtonSize) -> ButtonSizeParameters {
switch forSize {
case .large:
return .large
case .small:
return .small
}
}
}