Skip to content

Commit df032fc

Browse files
committed
(#265) Add support for setting Button colors
1 parent 018ec39 commit df032fc

File tree

6 files changed

+79
-13
lines changed

6 files changed

+79
-13
lines changed

Help/New-BTButton.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Up to five buttons can be added to a single Toast notification. Buttons may have
2020
| `ActivationType`| Microsoft.Toolkit.Uwp.Notifications.ToastActivationType| Defines the activation type that triggers when the button is pressed. Defaults to Protocol. | No |
2121
| `ImageUri` | String | Path or URI of an image icon to display next to the button label. | No |
2222
| `Id` | String | Specifies an ID associated with another toast control (textbox or selection box). For standard buttons, this aligns the button next to a control; for snooze buttons, associates with a selection box. | No |
23+
| `Color` | String (Green/Red) | If specified as `Green` or `Red`, the button will be visually styled as "Success" (green) or "Critical" (red) where supported. Use for representing positive/primary or destructive actions. | No |
2324

2425
## INPUTS
2526

@@ -74,6 +75,28 @@ New-BTButton -Content 'View Picture' -Arguments $pic -ImageUri $pic
7475

7576
Button with a picture to the left, launches the image file.
7677

78+
## NOTES
79+
80+
To visually distinguish action buttons, you can set the `Color` parameter to `Green` or `Red`. This will render these buttons with a "Success" (green) or "Critical" (red) style in systems that support advanced toast button styling (Windows 11+). Useful for marking approve/submit (green) or destructive (red) actions.
81+
82+
## EXAMPLES
83+
84+
### Example 6
85+
86+
```powershell
87+
New-BTButton -Content 'Approve' -Arguments 'approve' -Color Green
88+
```
89+
90+
Creates a "Success" style (green) button intended for positive actions.
91+
92+
### Example 7
93+
94+
```powershell
95+
New-BTButton -Content 'Delete' -Arguments 'delete' -Color Red
96+
```
97+
98+
Creates a "Critical" style (red) button for destructive actions.
99+
77100
## LINKS
78101

79102
- [New-BTAction](New-BTAction.md)

Help/Submit-BTNotification.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Submits a completed toast notification for display.
99
The `Submit-BTNotification` function submits a completed toast notification to the operating system's notification manager for display.
1010
This function supports advanced scenarios such as event callbacks for user actions or toast dismissal, sequence numbering to ensure correct update order, unique identification for toast replacement, expiration control, direct Action Center delivery, and designating a notification as "Important" (using the `-Urgent` switch) so that it can break through Focus Assist.
1111

12+
If a button in the toast is created using `New-BTButton` with the `-Color` parameter (`Green` or `Red`), Submit-BTNotification will ensure those buttons are visually styled as "Success" (green) or "Critical" (red) in capable environments. This allows you to clearly indicate primary/positive or destructive actions on the notification.
13+
1214
When a script block is supplied for any of the event actions (`ActivatedAction`, `DismissedAction`, or `FailedAction`), the function ensures that only one registration for a logically identical handler is allowed per notification event type. This is accomplished by normalizing and hashing the script block; the resulting hash uniquely identifies the action for event registration purposes. Attempting to register the same handler multiple times for a given event will not create a duplicate subscription, but instead will produce an informative warning.
1315

1416
If the `-ReturnEventData` switch is used and any event action scriptblocks are supplied (`ActivatedAction`, `DismissedAction`, `FailedAction`),

Tests/New-BTButton.Tests.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,12 @@ Describe 'New-BTButton' {
8888
}
8989
}
9090
}
91+
92+
Context 'custom button with color' {
93+
It 'creates a button with green color without throwing' {
94+
{ New-BTButton -Content 'Approve' -Arguments 'approve' -Color Green -WhatIf } | Should -Not -Throw
95+
}
96+
It 'creates a button with red color without throwing' {
97+
{ New-BTButton -Content 'Delete' -Arguments 'delete' -Color Red -WhatIf } | Should -Not -Throw
98+
}
99+
}

Tests/Submit-BTNotification.Tests.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ Describe 'Submit-BTNotification' {
4747
$output | Should -Not -Contain "Duplicate or conflicting OnActivated ScriptBlock event detected"
4848
}
4949
}
50-
Context 'Urgent scenario' {
51-
It 'runs without error with -Urgent' {
52-
$mockContent = [Activator]::CreateInstance([Microsoft.Toolkit.Uwp.Notifications.ToastContent])
53-
{ Submit-BTNotification -Content $mockContent -Urgent -WhatIf } | Should -Not -Throw
50+
Context 'Urgent scenario' {
51+
It 'runs without error with -Urgent' {
52+
$mockContent = [Activator]::CreateInstance([Microsoft.Toolkit.Uwp.Notifications.ToastContent])
53+
{ Submit-BTNotification -Content $mockContent -Urgent -WhatIf } | Should -Not -Throw
54+
}
5455
}
55-
}
5656
}

src/Public/New-BTButton.ps1

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
function New-BTButton {
22
<#
33
.SYNOPSIS
4-
Creates a new clickable button for a Toast Notification.
4+
Creates a new clickable button for a Toast Notification, with optional color styling.
55
66
.DESCRIPTION
77
The New-BTButton function creates a new button for a Toast Notification. Up to five buttons can be added to a single Toast notification.
8-
Buttons may have display text, an icon, an optional activation type, argument string, or serve as system managed Dismiss/Snooze buttons.
8+
Buttons may have display text, an icon, an optional activation type, argument string, serve as system managed Dismiss/Snooze buttons, and may optionally be rendered with colored button styles by specifying -Color.
9+
10+
If -Color is set to 'Green' or 'Red', the button will be displayed with a "Success" (green) or "Critical" (red) style in supported environments.
911
1012
.PARAMETER Snooze
1113
Switch. Creates a system-handled snooze button. When paired with a selection box on the toast, the snooze time is customizable.
@@ -28,6 +30,9 @@
2830
.PARAMETER Id
2931
String. Specifies an ID associated with another toast control (textbox or selection box). For standard buttons, this aligns the button next to a control, for snooze buttons it associates with a selection box.
3032
33+
.PARAMETER Color
34+
String. Optional. If specified as 'Green' or 'Red', the button will be visually styled as "Success" (green) or "Critical" (red) where supported. Use for indicating primary/positive or destructive actions.
35+
3136
.INPUTS
3237
None. You cannot pipe input to this function.
3338
@@ -57,6 +62,14 @@
5762
New-BTButton -Content 'View Picture' -Arguments $pic -ImageUri $pic
5863
Button with a picture to the left, launches the image file.
5964
65+
.EXAMPLE
66+
New-BTButton -Content 'Approve' -Arguments 'approve' -Color 'Green'
67+
Creates a button with a green "Success" style intended for positive actions like approval.
68+
69+
.EXAMPLE
70+
New-BTButton -Content 'Delete' -Arguments 'delete' -Color 'Red'
71+
Creates a button with a red "Critical" style indicating a destructive action.
72+
6073
.LINK
6174
https://github.com/Windos/BurntToast/blob/main/Help/New-BTButton.md
6275
#>
@@ -97,7 +110,10 @@
97110
[Parameter(ParameterSetName = 'Button')]
98111
[Parameter(ParameterSetName = 'Snooze')]
99112
[alias('TextBoxId', 'SelectionBoxId')]
100-
[string] $Id
113+
[string] $Id,
114+
115+
[ValidateSet('Green', 'Red')]
116+
[string] $Color
101117
)
102118

103119
switch ($PsCmdlet.ParameterSetName) {
@@ -125,7 +141,7 @@
125141
if ($Id) {
126142
$Button.SelectionBoxId = $Id
127143
}
128-
144+
129145
if ($ImageUri) {
130146
$Button.ImageUri = $ImageUri
131147
}
@@ -139,6 +155,10 @@
139155
}
140156
}
141157

158+
if ($Color) {
159+
$Button = $Button.SetHintActionId($Color)
160+
}
161+
142162
switch ($Button.GetType().Name) {
143163
ToastButton { if($PSCmdlet.ShouldProcess("returning: [$($Button.GetType().Name)]:Content=$($Button.Content):Arguments=$($Button.Arguments):ActivationType=$($Button.ActivationType):ImageUri=$($Button.ImageUri):TextBoxId=$($Button.TextBoxId)")) { $Button }}
144164
ToastButtonSnooze { if($PSCmdlet.ShouldProcess("returning: [$($Button.GetType().Name)]:CustomContent=$($Button.CustomContent):ImageUri=$($Button.ImageUri):SelectionBoxId=$($Button.SelectionBoxId)")) { $Button } }

src/Public/Submit-BTNotification.ps1

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
.DESCRIPTION
77
The Submit-BTNotification function submits a completed toast notification to the operating system's notification manager for display.
88
This function supports advanced scenarios such as event callbacks for user actions or toast dismissal, sequence numbering to ensure correct update order, unique identification for toast replacement, expiration control, and direct Action Center delivery.
9+
Supports colored action buttons: when a button generated via New-BTButton includes the -Color parameter
10+
('Green' or 'Red'), the notification will style those buttons as "Success" (green) or "Critical" (red)
11+
to visually distinguish positive or destructive actions where supported.
912
1013
When an action ScriptBlock is supplied (Activated, Dismissed, or Failed), a normalized SHA256 hash of its content is used to generate a unique SourceIdentifier for event registration.
1114
This prevents duplicate handler registration for the same ScriptBlock, warning if a duplicate registration is attempted.
@@ -100,10 +103,19 @@
100103
$ToastXml.LoadXml($ToastXmlContent)
101104

102105
if ($Urgent) {
103-
try {
104-
$ToastXml.GetElementsByTagName('toast')[0].SetAttribute('scenario', 'urgent')
105-
} catch {
106-
# We don't actually want to capture these errors, but rather suppress them.
106+
try {$ToastXml.GetElementsByTagName('toast')[0].SetAttribute('scenario', 'urgent')} catch {}
107+
}
108+
109+
if ($ToastXml.GetXml() -match 'hint-actionId="(Red|Green)"') {
110+
try {$ToastXml.GetElementsByTagName('toast').SetAttribute('useButtonStyle', 'true')} catch {}
111+
112+
foreach ($ActionElement in $ToastXml.GetElementsByTagName('actions')[0].ChildNodes) {
113+
if ($ActionElement.GetAttribute('hint-actionId') -eq 'Red') {
114+
$ActionElement.SetAttribute('hint-buttonStyle', 'Critical')
115+
}
116+
if ($ActionElement.GetAttribute('hint-actionId') -eq 'Green') {
117+
$ActionElement.SetAttribute('hint-buttonStyle', 'Success')
118+
}
107119
}
108120
}
109121

0 commit comments

Comments
 (0)