Skip to content

Commit f2005f9

Browse files
committed
Allow dynamically set all Extension preferences
Allow dynamically set all Extension preferences: - SF Symbol (${extension_id}_symbol) - Action Type (${extension_id}_action_type) - Action (${extension_id}_action) Values set here take precedence over MDM managed values
1 parent 1112d34 commit f2005f9

1 file changed

Lines changed: 48 additions & 16 deletions

File tree

src/Support/Views/ButtonTemplateViews/ItemExtension.swift

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ struct ItemExtension: View {
6161
@AppStorage private var extensionValue: String?
6262
@AppStorage private var extensionLoading: Bool?
6363
@AppStorage private var extensionAlert: Bool?
64+
@AppStorage private var extensionSymbol: String?
65+
@AppStorage private var extensionActionType: String?
66+
@AppStorage private var extensionAction: String?
6467

6568
init(title: String,
6669
subtitle: String? = nil,
@@ -97,8 +100,37 @@ struct ItemExtension: View {
97100
self._extensionValue = AppStorage(extensionIdentifier, store: .standard)
98101
self._extensionLoading = AppStorage("\(extensionIdentifier)_loading", store: .standard)
99102
self._extensionAlert = AppStorage("\(extensionIdentifier)_alert", store: .standard)
103+
self._extensionSymbol = AppStorage("\(extensionIdentifier)_symbol", store: .standard)
104+
self._extensionActionType = AppStorage("\(extensionIdentifier)_action_type", store: .standard)
105+
self._extensionAction = AppStorage("\(extensionIdentifier)_action", store: .standard)
106+
}
107+
108+
// Show dynamic SF Symbol or MDM managed SF Symbol
109+
var activeExtensionSymbol: String {
110+
if let extensionSymbol {
111+
return extensionSymbol
112+
} else {
113+
return image
114+
}
100115
}
101116

117+
// Dynamic action type or MDM managed action type
118+
var activeExtensionActionType: String? {
119+
if let extensionActionType {
120+
return extensionActionType
121+
} else {
122+
return linkType
123+
}
124+
}
125+
126+
// Dynamic action or MDM managed action
127+
var activeExtensionAction: String? {
128+
if let extensionAction {
129+
return extensionAction
130+
} else {
131+
return link
132+
}
133+
}
102134

103135
var body: some View {
104136

@@ -120,7 +152,7 @@ struct ItemExtension: View {
120152
Ellipse()
121153
.foregroundColor(.white)
122154
.overlay(
123-
Image(systemName: image)
155+
Image(systemName: activeExtensionSymbol)
124156
.foregroundColor(symbolColor)
125157
.font(.system(size: 18))
126158
)
@@ -226,10 +258,10 @@ struct ItemExtension: View {
226258
.accessibilityHidden(true)
227259
} else {
228260
Ellipse()
229-
.foregroundColor(hoverView && link != "" ? .primary : symbolColor)
261+
.foregroundColor(hoverView && activeExtensionAction != "" ? .primary : symbolColor)
230262
.overlay(
231-
Image(systemName: image)
232-
.foregroundColor(hoverView && link != "" ? Color("hoverColor") : Color.white)
263+
Image(systemName: activeExtensionSymbol)
264+
.foregroundColor(hoverView && activeExtensionAction != "" ? Color("hoverColor") : Color.white)
233265
)
234266
.frame(width: 26, height: 26)
235267
.padding(.leading, 10)
@@ -264,7 +296,7 @@ struct ItemExtension: View {
264296
}
265297
.frame(width: Constants.largeItemWidth, height: Constants.itemLegacyHeight)
266298
.accessibilityLabel(title + ", " + (subtitle ?? ""))
267-
.background(hoverView && hoverEffectEnable && link != "" ? EffectsView(material: NSVisualEffectView.Material.windowBackground, blendingMode: NSVisualEffectView.BlendingMode.withinWindow) : EffectsView(material: NSVisualEffectView.Material.popover, blendingMode: NSVisualEffectView.BlendingMode.withinWindow))
299+
.background(hoverView && hoverEffectEnable && activeExtensionAction != "" ? EffectsView(material: NSVisualEffectView.Material.windowBackground, blendingMode: NSVisualEffectView.BlendingMode.withinWindow) : EffectsView(material: NSVisualEffectView.Material.popover, blendingMode: NSVisualEffectView.BlendingMode.withinWindow))
268300
.cornerRadius(10)
269301
// Apply gray and black border in Dark Mode to better view the buttons like Control Center
270302
.modifier(DarkModeBorder())
@@ -318,35 +350,35 @@ struct ItemExtension: View {
318350

319351
func tapGesture() {
320352
// Don't do anything when no link is specified
321-
guard link != "" else {
353+
guard activeExtensionAction != "" else {
322354
logger.debug("No link specified for \(title, privacy: .public), button disabled...")
323355
return
324356
}
325357

326-
if linkType == "App" {
358+
if activeExtensionActionType == "App" {
327359
openApp()
328-
} else if linkType == "URL" {
360+
} else if activeExtensionActionType == "URL" {
329361
openLink()
330-
} else if linkType == "Command" {
362+
} else if activeExtensionActionType == "Command" {
331363
runCommand()
332364
// MARK: - DistributedNotification is deprecated, use PrivilegedScript instead
333-
} else if linkType == "DistributedNotification" || linkType == "PrivilegedScript" {
334-
guard let link else {
365+
} else if activeExtensionActionType == "DistributedNotification" || activeExtensionActionType == "PrivilegedScript" {
366+
guard let activeExtensionAction else {
335367
return
336368
}
337369
Task {
338-
await runPrivilegedCommand(command: link, key: "Action")
370+
await runPrivilegedCommand(command: activeExtensionAction, key: "Action")
339371
}
340372
} else {
341373
self.showingAlert.toggle()
342-
logger.error("Invalid Link Type: \(linkType!)")
374+
logger.error("Invalid Link Type: \(activeExtensionActionType!)")
343375
}
344376
}
345377

346378
// Open application with given Bundle Identifier
347379
func openApp() {
348380

349-
guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: link ?? "")
381+
guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: activeExtensionAction ?? "")
350382
// Show alert when there is an error
351383
else {
352384
self.showingAlert.toggle()
@@ -361,7 +393,7 @@ struct ItemExtension: View {
361393

362394
// Open URL
363395
func openLink() {
364-
guard let url = URL(string: link ?? "")
396+
guard let url = URL(string: activeExtensionAction ?? "")
365397
// Show alert when there is an error
366398
else {
367399
self.showingAlert.toggle()
@@ -381,7 +413,7 @@ struct ItemExtension: View {
381413
let task = Process()
382414
let pipe = Pipe()
383415

384-
let command = link?.replaceLocalVariables(computerInfo: computerinfo, userInfo: userinfo)
416+
let command = activeExtensionAction?.replaceLocalVariables(computerInfo: computerinfo, userInfo: userinfo)
385417

386418
task.standardOutput = pipe
387419
task.standardError = pipe

0 commit comments

Comments
 (0)