-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Label inherits View and clean event handling code #450
Open
yostane
wants to merge
7
commits into
compnerd:main
Choose a base branch
from
yostane:bugfix/442-label-inherit-view
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8c765ec
Label inherits View and clean event handling code
yostane ba61f8c
Attempt to backport event handling code into Label
yostane 87efab7
Remove some test code
yostane 6bc73ec
Reverted code to original position
yostane bdfc68b
Remove extra space
yostane c32ae40
Fix extra space
yostane f9d3ac0
initial code in label+temporary
yostane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,20 +7,7 @@ | |
|
||
import WinSDK | ||
|
||
private let SwiftLabelProc: SUBCLASSPROC = { (hWnd, uMsg, wParam, lParam, uIdSubclass, dwRefData) in | ||
let label: Label? = unsafeBitCast(dwRefData, to: AnyObject.self) as? Label | ||
|
||
switch uMsg { | ||
case UINT(WM_LBUTTONUP): | ||
label?.sendActions(for: .primaryActionTriggered) | ||
default: | ||
break | ||
} | ||
|
||
return DefSubclassProc(hWnd, uMsg, wParam, lParam) | ||
} | ||
|
||
public class Label: Control { | ||
public class Label: View { | ||
private static let `class`: WindowClass = | ||
WindowClass(hInst: GetModuleHandleW(nil), name: "Swift.Label") | ||
private static let style: WindowStyle = (base: WS_TABSTOP, extended: 0) | ||
|
@@ -59,13 +46,14 @@ public class Label: Control { | |
|
||
public init(frame: Rect) { | ||
super.init(frame: frame, class: Label.class, style: Label.style) | ||
_ = SetWindowSubclass(hWnd, SwiftLabelProc, UINT_PTR(1), | ||
unsafeBitCast(self as AnyObject, to: DWORD_PTR.self)) | ||
|
||
let size = self.frame.size | ||
self.hWnd_ = CreateWindowExW(0, WC_STATIC.wide, nil, DWORD(WS_CHILD), | ||
0, 0, CInt(size.width), CInt(size.height), | ||
self.hWnd, nil, GetModuleHandleW(nil), nil)! | ||
|
||
_ = SetWindowSubclass(hWnd, SwiftLabelProc, UINT_PTR(1), | ||
unsafeBitCast(self as AnyObject, to: DWORD_PTR.self)) | ||
// Perform the font setting in `defer` which ensures that the property | ||
// observer is triggered. | ||
defer { self.font = Font.systemFont(ofSize: Font.systemFontSize) } | ||
|
@@ -85,7 +73,130 @@ public class Label: Control { | |
self.font = FontMetrics.default.scaledFont(for: self.font, | ||
compatibleWith: traitCollection) | ||
} | ||
|
||
|
||
// MARK - Control API that should be removed when GestureRecognizer is implemented | ||
private var actions: [Control.Event:[ControlEventCallable]] = | ||
Dictionary<Control.Event, [ControlEventCallable]>(minimumCapacity: 16) | ||
|
||
@inline(__always) | ||
private func addAction(_ callable: ControlEventCallable, | ||
for controlEvents: Control.Event) { | ||
for event in Label.Event.semanticEvents { | ||
if controlEvents.rawValue & event.rawValue == event.rawValue { | ||
self.actions[event, default: []].append(callable) | ||
} | ||
} | ||
} | ||
|
||
func sendActions(for controlEvents: Control.Event) { | ||
for event in Label.Event.semanticEvents { | ||
if controlEvents.rawValue & event.rawValue == event.rawValue { | ||
_ = self.actions[event]?.map { $0(sender: self, event: controlEvents) } | ||
} | ||
} | ||
} | ||
|
||
public func addTarget<Target: AnyObject>(_ target: Target, | ||
action: @escaping (Target) -> () -> Void, | ||
for controlEvents: Control.Event) { | ||
self.addAction(ControlEventCallback<Self, Target>(binding: action, on: target), | ||
for: controlEvents) | ||
} | ||
|
||
/// Associates a target object and action method with the control. | ||
public func addTarget<Source: Label, Target: AnyObject>(_ target: Target, | ||
action: @escaping (Target) -> (_: Source) -> Void, | ||
for controlEvents: Control.Event) { | ||
self.addAction(ControlEventCallback(binding: action, on: target), | ||
for: controlEvents) | ||
} | ||
|
||
/// Returns the events for which the control has associated actions. | ||
public private(set) var allControlEvents: Control.Event = | ||
Control.Event(rawValue: 0) | ||
|
||
/// Associates a target object and action method with the control. | ||
public func addTarget<Source: Label, Target: AnyObject>(_ target: Target, | ||
action: @escaping (Target) -> (_: Source, _: Control.Event) -> Void, | ||
for controlEvents: Control.Event) { | ||
self.addAction(ControlEventCallback<Source, Target>(binding: action, on: target), | ||
for: controlEvents) | ||
} | ||
} | ||
|
||
extension Label: ContentSizeCategoryAdjusting { | ||
} | ||
|
||
|
||
private protocol ControlEventCallable { | ||
func callAsFunction(sender: Label, event: Control.Event) | ||
} | ||
|
||
private struct ControlEventCallback<Source: Label, Target: AnyObject>: ControlEventCallable { | ||
private unowned(safe) let instance: Target | ||
private let method: (Target) -> (_: Source, _: Control.Event) -> Void | ||
|
||
public init(binding: @escaping (Target) -> (_: Source, _: Control.Event) -> Void, | ||
on: Target) { | ||
self.instance = on | ||
self.method = binding | ||
} | ||
|
||
public init(binding: @escaping (Target) -> (_: Source) -> Void, on: Target) { | ||
self.instance = on | ||
self.method = { (target: Target) in { (sender: Source, _: Control.Event) in | ||
binding(target)(sender) | ||
} | ||
} | ||
} | ||
|
||
public init(binding: @escaping (Target) -> () -> Void, on: Target) { | ||
self.instance = on | ||
self.method = { (target: Target) in { (_: Source, _: Control.Event) in | ||
binding(target)() | ||
} | ||
} | ||
} | ||
|
||
public func callAsFunction(sender: Label, event: Control.Event) { | ||
self.method(instance)(sender as! Source, event) | ||
} | ||
} | ||
|
||
private let SwiftLabelProc: SUBCLASSPROC = { (hWnd, uMsg, wParam, lParam, uIdSubclass, dwRefData) in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you move this below the class? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oups, I put it back in the wrong location 😁 |
||
let label: Label? = unsafeBitCast(dwRefData, to: AnyObject.self) as? Label | ||
|
||
switch uMsg { | ||
case UINT(WM_LBUTTONUP): | ||
label?.sendActions(for: .primaryActionTriggered) | ||
default: | ||
break | ||
} | ||
|
||
return DefSubclassProc(hWnd, uMsg, wParam, lParam) | ||
} | ||
extension Label { | ||
/// Constants describing the types of events possible for controls. | ||
public struct Event: Equatable, Hashable, RawRepresentable { | ||
public typealias RawValue = Int | ||
|
||
public let rawValue: RawValue | ||
|
||
public init(rawValue: RawValue) { | ||
self.rawValue = rawValue | ||
} | ||
} | ||
} | ||
extension Label.Event { | ||
/// A semantic action triggered by buttons. | ||
public static var primaryActionTriggered: Control.Event { | ||
Control.Event(rawValue: 1 << 13) | ||
} | ||
|
||
fileprivate static var semanticEvents: [Control.Event] { | ||
return [ | ||
.primaryActionTriggered | ||
] | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was there a reason for this move?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oups, I put it back in the wrong location 😁