Skip to content

Commit b7ee6b4

Browse files
committed
Control: functional handling for event triggering
Filter the triggering events first then invoke the handlers. This is slightly slower than doing it interspersed and may take a little more memory (which can be handled by using `lazy`), but makes it easier to reason about. It hoists the event list out as Action bindings result in a third use.
1 parent 742432c commit b7ee6b4

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

Sources/SwiftWin32/Views and Controls/Control.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ private struct ControlEventCallback<Source: Control, Target: AnyObject>: Control
3636
}
3737
}
3838

39+
@inline(__always)
40+
private var kTriggers: [Control.Event] {
41+
Control.Event.touchEvents + Control.Event.semanticEvents + Control.Event.editingEvents
42+
}
43+
3944
/// The base class for controls, which are visual elements that convey a
4045
/// specific action or intention in response to user interactions.
4146
public class Control: View {
@@ -47,11 +52,8 @@ public class Control: View {
4752
@inline(__always)
4853
private func addAction(_ callable: ControlEventCallable,
4954
for controlEvents: Control.Event) {
50-
for event in Control.Event.touchEvents + Control.Event.semanticEvents + Control.Event.editingEvents {
51-
if controlEvents.rawValue & event.rawValue == event.rawValue {
52-
self.actions[event, default: []].append(callable)
53-
}
54-
}
55+
kTriggers.filter { $0.rawValue & controlEvents.rawValue == $0.rawValue }
56+
.forEach { self.actions[$0, default: []].append(callable) }
5557
}
5658

5759
/// Associates a target object and action method with the control.
@@ -86,11 +88,8 @@ public class Control: View {
8688

8789
/// Calls the action methods associated with the specified events.
8890
func sendActions(for controlEvents: Control.Event) {
89-
for event in Control.Event.touchEvents + Control.Event.semanticEvents + Control.Event.editingEvents {
90-
if controlEvents.rawValue & event.rawValue == event.rawValue {
91-
_ = self.actions[event]?.map { $0(sender: self, event: controlEvents) }
92-
}
93-
}
91+
kTriggers.filter { $0.rawValue & controlEvents.rawValue == $0.rawValue }
92+
.forEach { self.actions[$0]?.forEach { $0(sender: self, event: controlEvents) } }
9493
}
9594
}
9695

0 commit comments

Comments
 (0)