forked from compnerd/swift-win32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathView.swift
740 lines (591 loc) · 24.3 KB
/
View.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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/**
* Copyright © 2019 Saleem Abdulrasool <[email protected]>
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
**/
import WinSDK
extension View {
internal func interaction<InteractionType: Interaction>() -> InteractionType? {
// TODO: how do we handle overlapping entries in the `interactions` array?
if let interaction = interactions.first(where: { $0 is InteractionType }) {
return interaction as? InteractionType
}
return nil
}
}
private let SwiftViewProc: SUBCLASSPROC = { (hWnd, uMsg, wParam, lParam, uIdSubclass, dwRefData) in
let view: View? = unsafeBitCast(dwRefData, to: AnyObject.self) as? View
switch uMsg {
case UINT(WM_CONTEXTMENU):
guard let view = view,
let interaction: ContextMenuInteraction = view.interaction() else {
break
}
let x = LOWORD(lParam), y = HIWORD(lParam)
// Clear any existing menu.
view.menu = nil
if let actions = interaction.delegate?
.contextMenuInteraction(interaction,
configurationForMenuAtLocation: Point(x: x, y: y))?
.actionProvider?([]) {
// TODO: handle a possible failure in `CreatePopupMenu`
view.menu = Win32Menu(MenuHandle(owning: CreatePopupMenu()),
items: actions.children)
_ = TrackPopupMenu(view.menu?.hMenu.value, UINT(TPM_RIGHTBUTTON),
Int32(x), Int32(y), 0, view.hWnd, nil)
}
return 0
case UINT(WM_COMMAND):
// TODO: handle menu actions
break
default:
break
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam)
}
internal typealias WindowStyle = (base: DWORD, extended: DWORD)
private func ClientToWindow(size: inout Size, for style: WindowStyle) {
var r: RECT =
RECT(left: 0, top: 0, right: LONG(size.width), bottom: LONG(size.height))
if !AdjustWindowRect(&r, style.base, false) {
log.warning("AdjustWindowRectExForDpi: \(Error(win32: GetLastError()))")
}
size = Size(width: Double(r.right - r.left), height: Double(r.bottom - r.top))
}
private func ScaleClient(rect: inout Rect, for dpi: UINT, _ style: WindowStyle) {
let scale: Double = Double(dpi) / Double(USER_DEFAULT_SCREEN_DPI)
var r: RECT =
RECT(from: rect.applying(AffineTransform(scaleX: scale, y: scale)))
if !AdjustWindowRectExForDpi(&r, style.base, false, style.extended, dpi) {
log.warning("AdjustWindowRectExForDpi: \(Error(win32: GetLastError()))")
}
rect = Rect(from: r)
}
internal func GetRect(hWnd: HWND) -> Rect {
var r: RECT = RECT()
if !GetClientRect(hWnd, &r) {
log.warning("GetClientRect: \(Error(win32: GetLastError()))")
}
_ = withUnsafeMutablePointer(to: &r) { [hWnd] in
$0.withMemoryRebound(to: POINT.self, capacity: 2) {
MapWindowPoints(hWnd, nil, $0, 2)
}
}
return Rect(from: r)
}
extension View {
/// Options to specify how a view adjusts its content when its size changes.
public enum ContentMode: Int {
/// Scale the content to fit the size of itself by changing the aspect ratio
/// of the content if necessary.
case scaleToFill
/// Scale the content to fit the size of the view by maintaining the aspect
/// ratio. Any remaining area of the view's bounds is transparent.
case scaleAspectFill
/// Scale the content to fill the size of the view. Some portion of the
/// content may be clipped to fill the view's bounds.
case redraw
/// center the content in the view's bounds, keeping the proportions the
/// same.
case center
/// Center the content aligned to the top in the view's bounds.
case top
/// Center the content aligned at the bottom in the view's bounds.
case bottom
/// Align the content on the left of the view.
case left
/// Align the content on the right of the view.
case right
/// Align the content in the top-left corner of the view.
case topLeft
/// Align the content in the top-right corner of the view.
case topRight
/// Align the content in the bottom-left corner of the view.
case bottomLeft
/// Align the content in the bottom-right corner of the view.
case bottomRight
}
}
extension View {
/// Options for automatic view resizing.
public struct AutoresizingMask: OptionSet {
public let rawValue: UInt
public init(rawValue: UInt) {
self.rawValue = rawValue
}
}
}
extension View.AutoresizingMask {
public static var none: View.AutoresizingMask {
View.AutoresizingMask(rawValue: 0 << 0)
}
/// Resizing performed by expanding or shrinking a view in the direction of
/// the left margin.
public static var flexibleLeftMargin: View.AutoresizingMask {
View.AutoresizingMask(rawValue: 1 << 0)
}
/// Resizing performed by expanding or shrinking a view's width.
public static var flexibleWidth: View.AutoresizingMask {
View.AutoresizingMask(rawValue: 1 << 1)
}
/// Resizing performed by expanding or shrinking a view in the direction of
/// the right margin.
public static var flexibleRightMargin: View.AutoresizingMask {
View.AutoresizingMask(rawValue: 1 << 2)
}
/// Resizing performed by expanding or shrinking a view in the direction of
/// the top margin.
public static var flexibleTopMargin: View.AutoresizingMask {
View.AutoresizingMask(rawValue: 1 << 3)
}
/// Resizing performed by expanding or shrinking a view's height.
public static var flexibleHeight: View.AutoresizingMask {
View.AutoresizingMask(rawValue: 1 << 4)
}
/// Resizing performed by expanding or shrinking a view in the direction of
/// the bottom margin.
public static var flexibleBottomMargin: View.AutoresizingMask {
View.AutoresizingMask(rawValue: 1 << 5)
}
}
public class View: Responder {
private static let `class`: WindowClass =
WindowClass(hInst: GetModuleHandleW(nil), name: "Swift.View",
style: UInt32(CS_HREDRAW | CS_VREDRAW),
hbrBackground: GetSysColorBrush(COLOR_3DFACE),
hCursor: LoadCursorW(nil, IDC_ARROW))
private static let style: WindowStyle = (base: 0, extended: 0)
internal var hWnd: HWND!
internal var WndClass: WindowClass
internal var GWL_STYLE: LONG {
get { GetWindowLongW(self.hWnd, WinSDK.GWL_STYLE) }
set { _ = SetWindowLongW(self.hWnd, WinSDK.GWL_STYLE, newValue) }
}
internal var GWL_EXSTYLE: LONG {
get { GetWindowLongW(self.hWnd, WinSDK.GWL_EXSTYLE) }
set { _ = SetWindowLongW(self.hWnd, WinSDK.GWL_EXSTYLE, newValue) }
}
internal var font: Font? {
didSet {
SendMessageW(self.hWnd, UINT(WM_SETFONT),
unsafeBitCast(self.font?.hFont.value, to: WPARAM.self),
LPARAM(1))
}
}
internal var menu: Win32Menu? = nil
// MARK - Creating a View Object
// FIXME(compnerd) should this be marked as a convenience initializer?
/// Initializes and returns a newly allocated view object with the specified
/// frame rectangle.
public convenience init(frame: Rect) {
self.init(frame: frame, class: View.class, style: View.style)
}
internal init(frame: Rect, `class`: WindowClass, style: WindowStyle,
parent: HWND? = nil) {
self.WndClass = `class`
_ = self.WndClass.register()
let bOverlappedWindow: Bool =
style.base & DWORD(WS_OVERLAPPEDWINDOW) == DWORD(WS_OVERLAPPEDWINDOW)
var client: Rect = frame
// Convert client area to window rect
ClientToWindow(size: &client.size, for: style)
// TODO(compnerd) Convert client rect into display units
// Only request the window size, not the location, the location will be
// mapped when reparenting.
self.hWnd =
CreateWindowExW(style.extended, self.WndClass.name, nil, style.base,
Int32(bOverlappedWindow ? client.origin.x : 0),
Int32(bOverlappedWindow ? client.origin.y : 0),
Int32(client.size.width),
Int32(client.size.height),
parent, nil, GetModuleHandleW(nil), nil)!
// If `CW_USEDEFAULT` was used, query the actual allocated rect
if frame.origin.x == Double(CW_USEDEFAULT) ||
frame.size.width == Double(CW_USEDEFAULT) {
client = GetRect(hWnd: self.hWnd)
}
// Scale window for DPI
ScaleClient(rect: &client, for: GetDpiForWindow(self.hWnd), style)
// Resize and Position the Window
SetWindowPos(self.hWnd, nil,
CInt(client.origin.x), CInt(client.origin.y),
CInt(client.size.width), CInt(client.size.height),
UINT(SWP_NOZORDER | SWP_FRAMECHANGED))
self.frame = frame
super.init()
_ = SetWindowSubclass(self.hWnd, SwiftViewProc, UINT_PTR.max,
unsafeBitCast(self as AnyObject, to: DWORD_PTR.self))
if !RegisterTouchWindow(self.hWnd, 0) {
log.error("RegisterTouchWindow: \(Error(win32: GetLastError()))")
}
defer { self.font = Font.systemFont(ofSize: Font.systemFontSize) }
}
deinit {
_ = UnregisterTouchWindow(self.hWnd)
_ = DestroyWindow(self.hWnd)
_ = self.WndClass.unregister()
}
// MARK - Configuring a View's Visual Appearance
/// The view's background color.
public var backgroundColor: Color?
/// A boolean that determines if the view is hidden.
public var isHidden: Bool {
get { IsWindowVisible(self.hWnd) }
set(hidden) {
let pEnumFunc: WNDENUMPROC = { (hWnd, lParam) -> WindowsBool in
ShowWindow(hWnd, CInt(lParam))
return true
}
_ = EnumChildWindows(self.hWnd, pEnumFunc,
LPARAM(hidden ? SW_HIDE : SW_RESTORE))
ShowWindow(self.hWnd, hidden ? SW_HIDE : SW_RESTORE)
}
}
// MARK - Configuring the Event-Related Behaviour
/// A boolean value that determines whether user events are ignored and removed
/// from the event queue.
public var isUserInteractionEnabled: Bool {
get { return IsWindowEnabled(self.hWnd) }
set { _ = EnableWindow(self.hWnd, newValue) }
}
// MARK - Configuring the Bounds and Frame Rectangles
/// The frame rectangle, which describes the view's location and size in it's
/// superview's coordinate system.
public var frame: Rect {
didSet {
// Scale window for DPI
var client: Rect = self.frame
ScaleClient(rect: &client, for: GetDpiForWindow(self.hWnd),
WindowStyle(DWORD(bitPattern: self.GWL_STYLE),
DWORD(bitPattern: self.GWL_EXSTYLE)))
// Resize and Position the Window
_ = SetWindowPos(self.hWnd, nil,
CInt(client.origin.x), CInt(client.origin.y),
CInt(client.size.width), CInt(client.size.height),
UINT(SWP_NOZORDER | SWP_FRAMECHANGED))
}
}
/// The center point of the view's frame rectangle
public var center: Point {
get { return Point(x: self.frame.midX, y: self.frame.midY) }
set { self.frame = Rect(origin: Point(x: self.frame.origin.x - newValue.x,
y: self.frame.origin.y - newValue.y),
size: self.frame.size) }
}
// MARK - Managing the View Hierarchy
/// The receiver's superview, or `nil` if it has none.
public private(set) weak var superview: View?
/// The receiver's immediate subviews.
public private(set) var subviews: [View] = []
/// The receiver's window object, or `nil` if it has none.
public private(set) weak var window: Window?
/// Add a subview to the end of the reciever's list of subviews.
public func addSubview(_ view: View) {
self.insertSubview(view, at: self.subviews.endIndex)
}
/// Moves the specified subview so that it appears on top of its siblings.
public func bringSubviewToFront(_ view: View) {
if let index = self.subviews.firstIndex(of: view) {
self.subviews.append(self.subviews.remove(at: index))
}
}
/// Moves the specified subview so that it appears behind its siblings.
public func sendSubviewToBack(_ view: View) {
if let index = self.subviews.lastIndex(of: view) {
self.subviews.insert(self.subviews.remove(at: index),
at: self.subviews.startIndex)
}
}
/// Unlinks the view from its superview and its window, and removes it from
/// the responder chain.
public func removeFromSuperview() {
guard let superview = self.superview else { return }
self.willMove(toSuperview: nil)
superview.willRemoveSubview(self)
// Update the Window style.
self.GWL_STYLE &= ~LONG(bitPattern: WS_POPUP | WS_CAPTION)
self.GWL_STYLE &= ~WS_CHILD
// FIXME(compnerd) can this be avoided somehow?
if self is TextField || self is TextView || self is TableView {
self.GWL_STYLE |= WinSDK.WS_BORDER
self.GWL_EXSTYLE &= ~WS_EX_CLIENTEDGE
}
// Reparent the window.
guard let _ = SetParent(self.hWnd, nil) else {
log.warning("SetParent: \(Error(win32: GetLastError()))")
return
}
// We *must* call `SetWindowPos` after the `SetWindowLong` to have the
// changes take effect.
if !SetWindowPos(self.hWnd, nil, 0, 0, 0, 0,
UINT(SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED)) {
log.warning("SetWindowPos: \(Error(win32: GetLastError()))")
}
self.superview = nil
if let index = superview.subviews.firstIndex(of: self) {
superview.subviews.remove(at: index)
}
self.didMoveToSuperview()
}
/// Inserts a subview at the specified index.
public func insertSubview(_ view: View, at index: Int) {
// Notify the view that it is about to be reparented.
view.willMove(toSuperview: self)
// Notify the old parent that it is about to loose the child.
view.superview?.willRemoveSubview(view)
// MSDN:
// For compatibility reasons, `SetParent` does not modify the `WS_CHILD` or
// `WS_POPUP` window styles of the window whose parent is being changed.
// Therefore, if `hWndNewParent` is `NULL`, you should also clear the
// `WS_CHILD` bit and set the `WS_POPUP` style after calling `SetParent`.
// Conversely, if `hWndNewParent` is not `NULL` and the window was
// previously a child of the desktop, you should clear the `WS_POPUP` style
// and set the `WS_CHILD` style before calling `SetParent`.
//
// When you change the parent, you should synchronize the `UISTATE` of both
// windows. For more information, see `WM_CHANGEUISTATE` and
// `WM_UPDATEUISTATE`.
// Update the window style.
view.GWL_STYLE &= ~LONG(bitPattern: WS_POPUP | WS_CAPTION)
view.GWL_STYLE |= WS_CHILD
// FIXME(compnerd) can this be avoided somehow?
if view is TextField || view is TextView || view is TableView {
view.GWL_STYLE |= WinSDK.WS_BORDER
view.GWL_EXSTYLE &= ~WS_EX_CLIENTEDGE
}
// Reparent the window.
guard let _ = SetParent(view.hWnd, self.hWnd) else {
log.warning("SetParent: \(Error(win32: GetLastError()))")
return
}
// We *must* call `SetWindowPos` after the `SetWindowLong` to have the
// changes take effect.
if !SetWindowPos(view.hWnd, nil, 0, 0, 0, 0,
UINT(SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED)) {
log.warning("SetWindowPos: \(Error(win32: GetLastError()))")
}
// Scale window for DPI
let style: WindowStyle =
WindowStyle(DWORD(bitPattern: view.GWL_STYLE),
DWORD(bitPattern: view.GWL_EXSTYLE))
var client: Rect = view.frame
ScaleClient(rect: &client, for: GetDpiForWindow(view.hWnd), style)
// Resize and Position the Window
_ = SetWindowPos(view.hWnd, nil,
CInt(client.origin.x), CInt(client.origin.y),
CInt(client.size.width), CInt(client.size.height),
UINT(SWP_NOZORDER | SWP_FRAMECHANGED))
view.superview = self
self.subviews.insert(view, at: index)
// Notify any subclassed types for observation.
self.didAddSubview(view)
// Notify the view that it has been reparented.
view.didMoveToSuperview()
}
/// Inserts a view above another view in the view hierarchy.
public func insertSubview(_ view: View, aboveSubview subview: View) {
let index: Array<View>.Index
if let offset = self.subviews.firstIndex(of: subview) {
index = self.subviews.index(after: offset)
} else {
index = self.subviews.endIndex
}
self.insertSubview(view, at: index)
}
/// Inserts a view below another view in the view hierarchy.
public func insertSubview(_ view: View, belowSubview subview: View) {
let index: Array<View>.Index
if let offset = self.subviews.firstIndex(of: subview) {
index = self.subviews.index(before: offset)
} else {
index = self.subviews.endIndex
}
self.insertSubview(view, at: index)
}
/// Exchanges the subviews at the specified indices.
public func exchangeSubview(at index1: Int, withSubviewAt index2: Int) {
self.subviews.swapAt(self.subviews.index(self.subviews.startIndex,
offsetBy: index1),
self.subviews.index(self.subviews.startIndex,
offsetBy: index2))
}
/// Returns a boolean value indicating whether the receiver is a subview of a
/// given view or identical to that view.
public func isDescendant(of view: View) -> Bool {
var parent: View? = self
while parent != nil {
if parent == view { return true }
parent = parent?.superview
}
return false
}
// MARK - Observing View-Related Changes
/// Informs the view that a subview was added.
public func didAddSubview(_ subview: View) {
}
/// Informs the view that a subview is about to be removed.
public func willRemoveSubview(_ subview: View) {
}
/// Informs the view that its superview is about to change to the specified
/// superview.
public func willMove(toSuperview: View?) {
}
/// Informs the view that its superview changed.
public func didMoveToSuperview() {
}
/// Informs the view that its window object is about to change.
public func willMove(toWindow: Window?) {
}
/// Informs the view that its window object changed.
public func diMoveToWindow() {
}
// MARK - Managing the View's Constraints
/// The constraints held by the view.
public private(set) var constraints: [LayoutConstraint] = []
/// Adds a constraint on the layout of the receiving view or its subviews.
public func addConstraint(_ constraint: LayoutConstraint) {
}
/// Removes the specified constraint from the view.
public func removeConstraint(_ constraint: LayoutConstraint) {
}
/// Removes the specified constraints from the view.
public func removeConstraints(_ constraints: [LayoutConstraint]) {
}
// MARK - Create Constraint Using Layout Constraint
/// A layout anchor representing the bottom edge of the view's frame.
public var bottomAnchor: LayoutYAxisAnchor {
LayoutYAxisAnchor(item: self, attribute: .bottom)
}
/// A layout anchor representing the horizontal center of the view's frame.
public var centerXAnchor: LayoutXAxisAnchor {
LayoutXAxisAnchor(item: self, attribute: .centerX)
}
/// A layout anchor representing the vertical center of the view's frame.
public var centerYAnchor: LayoutYAxisAnchor {
LayoutYAxisAnchor(item: self, attribute: .centerY)
}
/// A layout anchor representing the baseline for the topmost line of text in
/// the view.
public var firstBaselineAnchor: LayoutYAxisAnchor {
LayoutYAxisAnchor(item: self, attribute: .firstBaseline)
}
/// A layout anchor representing the height of the view's frame.
public var heightAnchor: LayoutDimension {
LayoutDimension(item: self, attribute: .height)
}
/// A layout anchor representing the baseline for the bottommost line of text
/// in the view.
public var lastBaselineAnchor: LayoutYAxisAnchor {
LayoutYAxisAnchor(item: self, attribute: .lastBaseline)
}
/// A layout anchor representing the leading edge of the view's frame.
public var leadingAnchor: LayoutXAxisAnchor {
LayoutXAxisAnchor(item: self, attribute: .leading)
}
/// A layout anchor representing the left edge of the view's frame.
public var leftAnchor: LayoutXAxisAnchor {
LayoutXAxisAnchor(item: self, attribute: .left)
}
/// A layout anchor representing the right edge of the view's frame.
public var rightAnchor: LayoutXAxisAnchor {
LayoutXAxisAnchor(item: self, attribute: .right)
}
/// A layout anchor representing the top edge of the view's frame.
public var topAnchor: LayoutYAxisAnchor {
LayoutYAxisAnchor(item: self, attribute: .top)
}
/// A layout anchor representing the top edge of the view's frame.
public var trailingAnchor: LayoutXAxisAnchor {
LayoutXAxisAnchor(item: self, attribute: .trailing)
}
/// A layout anchor representing the trailing edge of the view's frame.
public var widthAnchor: LayoutDimension {
LayoutDimension(item: self, attribute: .width)
}
// MARK - Configuring the Resizing Behaviour
// Determine how a view lays out its content when its bounds changes.
public var contentMode: View.ContentMode = .scaleToFill
/// Asks the view to calculate and return the size that best fits the
/// specified size.
public func sizeThatFits(_ size: Size) -> Size {
return self.frame.size
}
/// Resizes and moves the receiver view so it just encloses its subviews.
public func sizeToFit() {
fatalError("\(#function) not yet implemented")
}
/// Determines whether the receiver automatically resizes its subviews when
/// its bounds changes.
public var autoresizesSubviews: Bool = true
/// A bitmask that determines how the receiver resizes itself when its
/// superview's bounds changes.
public var autoresizingMask: View.AutoresizingMask = .none
// MARK - Drawing and Updating the View
/// Draws the receiver's image within the passed-in rectangle.
public func draw(_ rect: Rect) {
fatalError("\(#function) not yet implemented")
}
/// Mark the receiver's entire bounds rectangle as needing to be redrawn.
public func setNeedsDisplay() {
fatalError("\(#function) not yet implemented")
}
/// Marks the specified rectangle of the receiver as needing to be redrawn.
public func setNeedsDisplay(_ rect: Rect) {
fatalError("\(#function) not yet implemented")
}
/// The scale factor applied to the view.
public var contentScaleFactor: Float {
get { 1.0 }
set { fatalError("\(#function) not yet implemented") }
}
// MARK - Adding and Removing Interactions
/// Adds an interaction to the view.
public func addInteraction(_ interaction: Interaction) {
interaction.willMove(to: self)
interaction.view?.interactions.removeAll(where: { $0 === interaction })
interactions.append(interaction)
interaction.didMove(to: self)
}
/// Removes an interaction from the view.
public func removeInteraction(_ interaction: Interaction) {
interaction.willMove(to: nil)
self.interactions.removeAll(where: { $0 === interaction })
interaction.didMove(to: nil)
}
/// The array of interactions for the view.
public var interactions: [Interaction] = []
// MARK - Identifying the View at Runtime
/// An integer that you can use to identify view objects in your application.
public var tag: Int = 0
/// Returns the view whose tag matches the specified value.
public func viewWithTag(_ tag: Int) -> View? {
if self.tag == tag { return self }
// TODO(compnerd) this is a poor equivalent of a level-order traversal of
// the view hierachy. We could implement this properly, but, this provides
// a functional implementation that is brief and is unlikely to be a hot
// path. Convert to a proper level-order traversal.
return self.subviews.first(where: { $0.tag == tag }) ??
self.subviews.lazy.compactMap { $0.viewWithTag(tag) }.first
}
// MARK - Responder Chain
public override var next: Responder? {
if let parent = self.superview { return parent }
return nil
}
// MARK - Trait Environment
// NOTE: this must be in the class to permit deviced types to override the
// notification.
public func traitCollectionDidChange(_ previousTraitCollection: TraitCollection?) {
}
}
extension View: Equatable {
public static func ==(_ lhs: View, _ rhs: View) -> Bool {
return lhs.hWnd == rhs.hWnd
}
}
extension View: TraitEnvironment {
public var traitCollection: TraitCollection {
return self.window?.screen.traitCollection ?? TraitCollection.current
}
}