Skip to content

Commit

Permalink
UI: add containing View for each Label
Browse files Browse the repository at this point in the history
`WC_STATIC` control doesn't receive events like `WM_CONTEXTMENU`. To be able to handle those events, let's create containing views for `WC_STATIC` instances.

Co-authored-by: Saleem Abdulrasool <[email protected]>
  • Loading branch information
egorzhdan and compnerd committed Apr 3, 2021
1 parent 3078755 commit 84c7495
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions Sources/SwiftWin32/Views and Controls/Label.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,47 @@ private let SwiftLabelProc: SUBCLASSPROC = { (hWnd, uMsg, wParam, lParam, uIdSub
return DefSubclassProc(hWnd, uMsg, wParam, lParam)
}

public class Label: Control {
fileprivate class LabelView: View {
private static let `class`: WindowClass = WindowClass(named: WC_STATIC)
private static let style: WindowStyle = (base: WS_TABSTOP | DWORD(SS_NOTIFY), extended: 0)
private static let style: WindowStyle = (base: 0, extended: 0)

@_Win32WindowText
public var text: String?

public init(frame: Rect) {
super.init(frame: frame, class: LabelView.class, style: LabelView.style)
}
}

public class Label: Control {
private static let `class`: WindowClass =
WindowClass(hInst: GetModuleHandleW(nil), name: "Swift.Label")
private static let style: WindowStyle = (base: WS_TABSTOP, extended: 0)

private let view: LabelView

public override var frame: Rect {
didSet {
self.view.frame = Rect(origin: .zero, size: frame.size)
}
}

public override var font: Font! {
get { return super.font }
set(value) { super.font = value }
get { return self.view.font }
set(value) { self.view.font = value }
}

@_Win32WindowText
public var text: String?
public var text: String? {
get { return self.view.text }
set(value) { self.view.text = value }
}

public init(frame: Rect) {
self.view = LabelView(frame: Rect(origin: .zero, size: frame.size))
super.init(frame: frame, class: Label.class, style: Label.style)
_ = SetWindowSubclass(hWnd, SwiftLabelProc, UINT_PTR(1),
unsafeBitCast(self as AnyObject, to: DWORD_PTR.self))
self.addSubview(self.view)
}

// ContentSizeCategoryAdjusting
Expand Down

0 comments on commit 84c7495

Please sign in to comment.