@@ -56,9 +56,7 @@ internal void SetProgress(float progressValue)
56
56
}
57
57
58
58
private void OnConsoleCancelEvent ( object sender , ConsoleCancelEventArgs e )
59
- {
60
- Dispose ( ) ;
61
- }
59
+ => Dispose ( ) ;
62
60
63
61
public void Dispose ( )
64
62
{
@@ -104,9 +102,7 @@ private interface ITaskbarList3
104
102
[ Guid ( "56FDF344-FD6D-11d0-958A-006097C9A090" ) ]
105
103
[ ClassInterface ( ClassInterfaceType . None ) ]
106
104
[ ComImport ]
107
- private class TaskbarInstance
108
- {
109
- }
105
+ private class TaskbarInstance { }
110
106
111
107
[ DllImport ( "kernel32.dll" ) ]
112
108
private static extern IntPtr GetConsoleWindow ( ) ;
@@ -141,17 +137,13 @@ private Com(IntPtr handle)
141
137
}
142
138
143
139
internal void SetState ( TaskbarProgressState taskbarState )
144
- {
145
- taskbarInstance . SetProgressState ( consoleWindowHandle , taskbarState ) ;
146
- }
140
+ => taskbarInstance . SetProgressState ( consoleWindowHandle , taskbarState ) ;
147
141
148
142
/// <summary>
149
143
/// Sets the progress value out of 100.
150
144
/// </summary>
151
145
internal void SetValue ( uint progressValue )
152
- {
153
- taskbarInstance . SetProgressValue ( consoleWindowHandle , progressValue , 100 ) ;
154
- }
146
+ => taskbarInstance . SetProgressValue ( consoleWindowHandle , progressValue , 100 ) ;
155
147
}
156
148
157
149
private sealed class Terminal
@@ -185,11 +177,10 @@ private enum ConsoleModes : uint
185
177
private const int STD_OUTPUT_HANDLE = - 11 ;
186
178
187
179
private readonly IntPtr consoleHandle ;
180
+ private uint currentProgress ;
188
181
189
182
private Terminal ( IntPtr handle )
190
- {
191
- consoleHandle = handle ;
192
- }
183
+ => consoleHandle = handle ;
193
184
194
185
internal static Terminal ? MaybeCreateInstanceAndSetInitialState ( TaskbarProgressState initialTaskbarState )
195
186
{
@@ -206,52 +197,61 @@ private Terminal(IntPtr handle)
206
197
// If we try to write without VT mode, the sequence will be printed for the user to see, which clutters the output.
207
198
return null ;
208
199
}
209
- SetStateThenRevertConsoleMode ( handle , initialTaskbarState , previousConsoleMode ) ;
210
- return new Terminal ( handle ) ;
200
+ var terminal = new Terminal ( handle ) ;
201
+ terminal . WriteStateSequence ( initialTaskbarState ) ;
202
+ SetConsoleMode ( handle , previousConsoleMode ) ;
203
+ return terminal ;
211
204
}
212
205
213
206
internal void SetState ( TaskbarProgressState taskbarState )
214
207
{
215
208
GetConsoleMode ( consoleHandle , out ConsoleModes previousConsoleMode ) ;
216
209
SetConsoleMode ( consoleHandle , ConsoleModes . ENABLE_VIRTUAL_TERMINAL_PROCESSING | ConsoleModes . ENABLE_PROCESSED_OUTPUT ) ;
217
- SetStateThenRevertConsoleMode ( consoleHandle , taskbarState , previousConsoleMode ) ;
210
+ WriteStateSequence ( taskbarState ) ;
211
+ SetConsoleMode ( consoleHandle , previousConsoleMode ) ;
218
212
}
219
213
220
- private static void SetStateThenRevertConsoleMode ( IntPtr handle , TaskbarProgressState taskbarState , ConsoleModes previousConsoleMode )
214
+ private void WriteStateSequence ( TaskbarProgressState taskbarState )
221
215
{
222
216
// Write progress state to console for Windows Terminal (https://github.com/microsoft/terminal/issues/6700).
223
217
switch ( taskbarState )
224
218
{
225
219
case TaskbarProgressState . NoProgress :
220
+ currentProgress = 100 ;
226
221
Console . Write ( "\x1b ]9;4;0;0\x1b \\ " ) ;
227
222
break ;
228
223
case TaskbarProgressState . Indeterminate :
224
+ currentProgress = 100 ;
229
225
Console . Write ( "\x1b ]9;4;3;0\x1b \\ " ) ;
230
226
break ;
231
227
case TaskbarProgressState . Normal :
232
- // Do nothing, this is set automatically when SetValue is called (and WT has no documented way to set this).
228
+ // Normal state is set when progress is set.
229
+ WriteProgressSequence ( currentProgress ) ;
233
230
break ;
234
231
case TaskbarProgressState . Error :
235
- Console . Write ( "\x1b ]9;4;2;0 \x1b \\ " ) ;
232
+ Console . Write ( $ "\x1b ]9;4;2;{ currentProgress } \x1b \\ ") ;
236
233
break ;
237
234
case TaskbarProgressState . Warning :
238
- Console . Write ( "\x1b ]9;4;4;0 \x1b \\ " ) ;
235
+ Console . Write ( $ "\x1b ]9;4;4;{ currentProgress } \x1b \\ ") ;
239
236
break ;
240
237
}
241
- SetConsoleMode ( handle , previousConsoleMode ) ;
242
238
}
243
239
244
240
/// <summary>
245
241
/// Sets the progress value out of 100.
246
242
/// </summary>
247
243
internal void SetValue ( uint progressValue )
248
244
{
245
+ currentProgress = progressValue ;
249
246
// Write progress sequence to console for Windows Terminal (https://github.com/microsoft/terminal/discussions/14268).
250
247
GetConsoleMode ( consoleHandle , out ConsoleModes previousConsoleMode ) ;
251
248
SetConsoleMode ( consoleHandle , ConsoleModes . ENABLE_VIRTUAL_TERMINAL_PROCESSING | ConsoleModes . ENABLE_PROCESSED_OUTPUT ) ;
252
- Console . Write ( $ " \x1b ]9;4;1; { progressValue } \x1b \\ " ) ;
249
+ WriteProgressSequence ( progressValue ) ;
253
250
SetConsoleMode ( consoleHandle , previousConsoleMode ) ;
254
251
}
252
+
253
+ private static void WriteProgressSequence ( uint progressValue )
254
+ => Console . Write ( $ "\x1b ]9;4;1;{ progressValue } \x1b \\ ") ;
255
255
}
256
256
}
257
257
}
0 commit comments