Skip to content

Commit 55c9805

Browse files
committed
Merge branch 'ActionEnv'
2 parents 3ef0010 + 74ef798 commit 55c9805

File tree

7 files changed

+59
-50
lines changed

7 files changed

+59
-50
lines changed

Examples/counter/Sources/counter/Actions/DecrementAction.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,12 @@ class DecrementAction: KeyAction {
2727

2828
static var userTitleEnabled: Bool? = false
2929

30-
var context: String
31-
32-
var coordinates: StreamDeck.Coordinates?
30+
let context: String
3331

3432
@GlobalSetting(\.count) var count
3533

36-
required init(context: String, coordinates: StreamDeck.Coordinates?) {
34+
required init(context: String) {
3735
self.context = context
38-
self.coordinates = coordinates
3936
}
4037

4138
func willAppear(device: String, payload: AppearEvent<Settings>) {

Examples/counter/Sources/counter/Actions/IncrementAction.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,12 @@ class IncrementAction: KeyAction {
2727

2828
static var userTitleEnabled: Bool? = false
2929

30-
var context: String
31-
32-
var coordinates: StreamDeck.Coordinates?
30+
let context: String
3331

3432
@GlobalSetting(\.count) var count
3533

36-
required init(context: String, coordinates: StreamDeck.Coordinates?) {
34+
required init(context: String) {
3735
self.context = context
38-
self.coordinates = coordinates
3936
}
4037

4138
func willAppear(device: String, payload: AppearEvent<Settings>) {

Examples/counter/Sources/counter/Actions/RotaryAction.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,14 @@ class RotaryAction: EncoderAction {
2424
rotate: "Count",
2525
push: "Reset")
2626

27-
var context: String
28-
29-
var coordinates: StreamDeck.Coordinates?
30-
3127
static var userTitleEnabled: Bool? = false
3228

29+
let context: String
30+
3331
@GlobalSetting(\.count) var count: Int
3432

35-
required init(context: String, coordinates: StreamDeck.Coordinates?) {
33+
required init(context: String) {
3634
self.context = context
37-
self.coordinates = coordinates
3835
}
3936

4037
func willAppear(device: String, payload: AppearEvent<Settings>) {

Sources/StreamDeck/StreamDeck Plugin/Action/Action.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ public protocol Action {
101101

102102
/// The context value for the instance.
103103
var context: String { get }
104-
105-
/// The coordinates of the instance.
106-
var coordinates: Coordinates? { get }
107-
104+
108105
/// The duration a button needs to be held down in order to be considered a long press.
109106
var longPressDuration: TimeInterval { get }
110107

@@ -113,7 +110,7 @@ public protocol Action {
113110

114111
/// Create a new instance with the specified context and coordinates.
115112
init(context: String, coordinates: Coordinates?)
116-
113+
117114
// MARK: - Events
118115
func didReceiveGlobalSettings()
119116

Sources/StreamDeck/StreamDeck Plugin/PluginCommunication.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public final actor PluginCommunication {
8484
type(of: plugin).actions.first { $0.uuid == uuid }
8585
}
8686

87-
public func registerInstance(_ event: ActionEvent<InstanceAppearEvent>) {
87+
public func registerInstance(_ event: ActionEvent<InstanceAppearEvent>) {
8888
// Check if the instance already exists
8989
guard instances[event.context] == nil else {
9090
log.log("This instance has already been registered.")
@@ -97,8 +97,8 @@ public final actor PluginCommunication {
9797
return
9898
}
9999

100-
// Register the controller type
101-
ActionControllerRegistry.shared.store(event.context, controller: event.payload.controller, device: event.device)
100+
// Register the action metadata
101+
ActionControllerRegistry.shared.store(event: event)
102102

103103
// Initialize a new instance
104104
instances[event.context] = actionType.init(context: event.context, coordinates: event.payload.coordinates)
@@ -127,13 +127,11 @@ public final actor PluginCommunication {
127127
// Find all actions on the device
128128
guard let actions = ActionControllerRegistry.shared.devices[device] else { return [] }
129129

130-
return actions.compactMap { context in
131-
instances[context] as? A
130+
return actions.compactMap { info in
131+
instances[info.context] as? A
132132
}
133-
134133
}
135134

136-
137135
// MARK: - WebSocket Methods
138136
/// Continually receive messages from the socket.
139137
func monitorSocket() async {
@@ -149,8 +147,8 @@ public final actor PluginCommunication {
149147
// Parse the message outside of the WebSocket loop.
150148
// An action may take a long time to respond, especially
151149
// if an AppleEvent takes longer than expected, so we
152-
// want to kick that off and get back to receiving messages
153-
// as soon as possible.
150+
// want to kick that off and get back to receiving
151+
// messages as soon as possible.
154152
Task.detached(priority: .userInitiated) {
155153
await self.parseMessage(message)
156154
}
@@ -353,12 +351,11 @@ public final actor PluginCommunication {
353351
log.log("Received first event, requesting global settings")
354352
// Get the initial global settings
355353
await PluginCommunication.shared.sendEvent(.getGlobalSettings,
356-
context: PluginCommunication.shared.uuid,
357-
payload: nil)
354+
context: PluginCommunication.shared.uuid,
355+
payload: nil)
358356
shouldLoadSettings = false
359357
}
360358

361-
362359
#if DEBUG
363360
if let json = String(data: data, encoding: .utf8) {
364361
log.debug("\(json, privacy: .public)")

Sources/StreamDeck/Support Models/ActionControllerRegistry.swift

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,56 @@ class ActionControllerRegistry {
1212
static let shared = ActionControllerRegistry()
1313

1414
/// A dictionary of actions and their controller type.
15-
var actions: [String: ControllerType] = [:]
15+
var actions: [String: ActionInfo] = [:]
1616

1717
/// A dictionary of devices and their actions
18-
var devices: [String: [String]] = [:]
18+
var devices: [String: [ActionInfo]] {
19+
let infos: [ActionInfo] = Array(actions.values)
20+
return Dictionary(grouping: infos) { $0.device }
21+
}
1922

20-
func store(_ actionContext: String, controller: ControllerType, device: String) {
21-
actions[actionContext] = controller
22-
devices[device, default: []].append(actionContext)
23+
func store(event: ActionEvent<InstanceAppearEvent>) {
24+
actions[event.context] = ActionInfo(event)
2325
}
2426

2527
func remove(_ actionContext: String) {
2628
actions[actionContext] = nil
27-
devices[actionContext] = nil
2829
}
2930

30-
subscript <A: Action>(_ action: A) -> ControllerType? {
31-
get {
32-
actions[action.context]
33-
}
34-
35-
set {
36-
actions[action.context] = newValue
37-
}
31+
subscript <A: Action>(_ action: A) -> ActionInfo? {
32+
actions[action.context]
3833
}
3934

4035
}
4136

37+
/// Information about where the action is displayed.
38+
struct ActionInfo {
39+
let context: String
40+
let device: String
41+
let controllerType: ControllerType
42+
let coordinates: Coordinates?
43+
44+
init(_ event: ActionEvent<InstanceAppearEvent>) {
45+
self.context = event.context
46+
self.device = event.device
47+
self.controllerType = event.payload.controller
48+
self.coordinates = event.payload.coordinates
49+
}
50+
}
51+
4252
extension Action {
4353
/// The type of controller the action instance was instantiated on.
4454
public var controllerType: ControllerType {
45-
ActionControllerRegistry.shared[self]!
55+
ActionControllerRegistry.shared[self]!.controllerType
56+
}
57+
58+
/// The type of controller the action instance was instantiated on.
59+
public var coordinates: Coordinates? {
60+
ActionControllerRegistry.shared[self]!.coordinates
61+
}
62+
63+
/// The type of controller the action instance was instantiated on.
64+
public var device: String {
65+
ActionControllerRegistry.shared[self]!.device
4666
}
4767
}

Sources/StreamDeck/Support Models/PluginManifest/Coordinates.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
import Foundation
99

1010
/// The coordinates of the action triggered.
11-
public struct Coordinates: Decodable, Hashable, Sendable {
12-
11+
public struct Coordinates: Decodable, Hashable, Sendable, CustomStringConvertible {
12+
1313
/// The column.
1414
public let column: Int
1515

1616
/// The row.
1717
public let row: Int
18+
19+
public var description: String {
20+
"Coord (\(row), \(column))"
21+
}
1822
}

0 commit comments

Comments
 (0)