Skip to content

Commit a9fe23a

Browse files
committed
better taskbar progress implementation
1 parent a7ed494 commit a9fe23a

1 file changed

Lines changed: 234 additions & 76 deletions

File tree

FlashpointSecurePlayer/ProgressManager.cs

Lines changed: 234 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ bool fFullscreen
5151
private static readonly ITaskbarList3 taskbarList = (ITaskbarList3)new ITaskbarList();
5252
private static readonly bool taskbarListSupported = Environment.OSVersion.Version >= new Version(6, 1);
5353
private static bool taskbarListInitialized = false;
54-
55-
const int VALUE_COMPLETE = 100;
54+
55+
const int PROGRESS_FORM_VALUE_COMPLETE = 100;
5656

5757
private static ProgressBar progressBar = null;
5858
private static Form progressForm = null;
59+
private static ulong progressFormValue = 0;
60+
private static TaskbarProgressBarState progressFormState = TaskbarProgressBarState.Normal;
5961
private static ProgressBarStyle style = ProgressBarStyle.Marquee;
6062
private static int value = 0;
6163
private static IntPtr state = PBST_NORMAL;
@@ -218,7 +220,7 @@ private static void Show() {
218220
double reciprocal = 1;
219221
double multiplier = 0;
220222

221-
for (int i = 0;i < goalsArray.Length;i++) {
223+
for (int i = 0; i < goalsArray.Length; i++) {
222224
for (int j = 0; j < i; j++) {
223225
size = goalsArray[j].Size;
224226

@@ -291,9 +293,67 @@ public static ProgressBar ProgressBar {
291293
return;
292294
}
293295

294-
Style = ProgressManager.style;
295-
Value = ProgressManager.value;
296-
State = ProgressManager.state;
296+
ProgressBarStyle = ProgressManager.style;
297+
ProgressBarValue = ProgressManager.value;
298+
ProgressBarState = ProgressManager.state;
299+
}
300+
}
301+
302+
private static ProgressBarStyle ProgressBarStyle {
303+
/*
304+
get {
305+
if (ProgressBar == null) {
306+
return ProgressManager.style;
307+
}
308+
return ProgressBar.Style;
309+
}
310+
*/
311+
312+
set {
313+
if (ProgressBar == null) {
314+
return;
315+
}
316+
317+
ProgressBar.Style = value;
318+
}
319+
}
320+
321+
private static int ProgressBarValue {
322+
/*
323+
get {
324+
if (ProgressBar == null) {
325+
return ProgressManager.value;
326+
}
327+
return ProgressBar.Value;
328+
}
329+
*/
330+
331+
set {
332+
if (ProgressBar == null) {
333+
return;
334+
}
335+
336+
ProgressBar.Value = value;
337+
}
338+
}
339+
340+
private static IntPtr ProgressBarState {
341+
/*
342+
get {
343+
return ProgressManager.state;
344+
}
345+
*/
346+
347+
set {
348+
if (ProgressBar == null) {
349+
return;
350+
}
351+
352+
if (!ProgressBar.IsHandleCreated || ProgressBar.Handle == IntPtr.Zero) {
353+
return;
354+
}
355+
356+
SendMessage(ProgressBar.Handle, PBM_SETSTATE, value, IntPtr.Zero);
297357
}
298358
}
299359

@@ -313,112 +373,210 @@ public static Form ProgressForm {
313373
try {
314374
taskbarList.HrInit();
315375
taskbarListInitialized = true;
316-
} catch { }
376+
} catch (Exception ex) {
377+
LogExceptionToLauncher(ex);
378+
}
317379
}
318380

319-
Style = ProgressManager.style;
320-
Value = ProgressManager.value;
321-
State = ProgressManager.state;
381+
ProgressFormStyle = ProgressManager.style;
382+
ProgressFormValue = ProgressManager.value;
383+
ProgressFormState = ProgressManager.state;
322384
}
323385
}
324386

325-
private static ProgressBarStyle Style {
387+
private static ProgressBarStyle ProgressFormStyle {
388+
/*
326389
get {
327-
return ProgressManager.style;
390+
return ProgressManager.progressFormState == TaskbarProgressBarState.Indeterminate ? ProgressBarStyle.Marquee : ProgressBarStyle.Blocks;
328391
}
392+
*/
329393

330394
set {
331-
ProgressManager.style = value;
395+
if (ProgressManager.progressFormValue >= PROGRESS_FORM_VALUE_COMPLETE) {
396+
return;
397+
}
332398

333-
if (ProgressBar != null) {
334-
ProgressBar.Style = ProgressManager.style;
335-
}
336-
337-
if (ProgressForm != null) {
338-
if (ProgressForm.IsHandleCreated && ProgressForm.Handle != IntPtr.Zero) {
339-
if (taskbarListInitialized) {
340-
if (ProgressManager.style == ProgressBarStyle.Marquee) {
341-
// error/paused states take priority over marquee style
342-
if (ProgressManager.value < VALUE_COMPLETE && ProgressManager.state == PBST_NORMAL) {
343-
taskbarList.SetProgressState(ProgressForm.Handle, TaskbarProgressBarState.Indeterminate);
344-
}
345-
} else {
346-
Value = ProgressManager.value;
347-
State = ProgressManager.state;
348-
}
349-
}
350-
}
399+
if (value != ProgressBarStyle.Marquee) {
400+
ProgressFormValue = ProgressManager.value;
401+
ProgressFormState = ProgressManager.state;
402+
return;
403+
}
404+
405+
// normal state does not take priority over indeterminate state
406+
if (ProgressManager.progressFormState != TaskbarProgressBarState.Normal) {
407+
return;
408+
}
409+
410+
ProgressManager.progressFormState = TaskbarProgressBarState.Indeterminate;
411+
412+
if (ProgressForm == null) {
413+
return;
414+
}
415+
416+
if (!ProgressForm.IsHandleCreated || ProgressForm.Handle == IntPtr.Zero) {
417+
return;
418+
}
419+
420+
if (!taskbarListInitialized) {
421+
return;
351422
}
423+
424+
taskbarList.SetProgressState(ProgressForm.Handle, ProgressManager.progressFormState);
352425
}
353426
}
354427

355-
private static int Value {
428+
private static int ProgressFormValue {
429+
/*
356430
get {
357-
return ProgressManager.value;
431+
return (int)ProgressManager.progressFormValue;
358432
}
433+
*/
359434

360435
set {
361-
ProgressManager.value = Math.Min(100, Math.Max(0, value));
436+
ProgressManager.progressFormValue = (ulong)value;
362437

363-
if (ProgressBar != null) {
364-
ProgressBar.Value = ProgressManager.value;
365-
}
438+
bool completed = false;
366439

367-
if (ProgressForm != null) {
368-
if (ProgressForm.IsHandleCreated && ProgressForm.Handle != IntPtr.Zero) {
369-
if (taskbarListInitialized) {
370-
// marquee style takes priority over value (setting the value disables marquee style)
371-
if (ProgressManager.style != ProgressBarStyle.Marquee || ProgressManager.state != PBST_NORMAL) {
372-
// when reset to zero, update the state so the value can be set
373-
if (ProgressManager.value == 0) {
374-
State = ProgressManager.state;
375-
}
440+
if (ProgressManager.progressFormValue >= PROGRESS_FORM_VALUE_COMPLETE) {
441+
completed = true;
442+
}
376443

377-
taskbarList.SetProgressValue(ProgressForm.Handle, (ulong)ProgressManager.value, VALUE_COMPLETE);
378-
}
444+
if (completed) {
445+
// if we have completed, ignore the value in the no progress state
446+
if (ProgressManager.progressFormState == TaskbarProgressBarState.NoProgress) {
447+
return;
448+
}
379449

380-
// it is required to set the state to No Progress when completed
381-
if (ProgressManager.value >= VALUE_COMPLETE) {
382-
taskbarList.SetProgressState(ProgressForm.Handle, TaskbarProgressBarState.NoProgress);
383-
}
384-
}
450+
ProgressManager.progressFormState = TaskbarProgressBarState.NoProgress;
451+
} else {
452+
// if we haven't completed, ignore the value in the indeterminate state
453+
if (ProgressManager.progressFormState == TaskbarProgressBarState.Indeterminate) {
454+
return;
385455
}
386456
}
457+
458+
if (ProgressForm == null) {
459+
return;
460+
}
461+
462+
if (!ProgressForm.IsHandleCreated || ProgressForm.Handle == IntPtr.Zero) {
463+
return;
464+
}
465+
466+
if (!taskbarListInitialized) {
467+
return;
468+
}
469+
470+
taskbarList.SetProgressValue(ProgressForm.Handle, ProgressManager.progressFormValue, PROGRESS_FORM_VALUE_COMPLETE);
471+
472+
// it is required to set the state to No Progress when completed
473+
if (completed) {
474+
taskbarList.SetProgressState(ProgressForm.Handle, ProgressManager.progressFormState);
475+
return;
476+
}
477+
478+
// if we previously completed, update the state
479+
if (ProgressManager.progressFormState == TaskbarProgressBarState.NoProgress) {
480+
ProgressFormState = ProgressManager.state;
481+
}
387482
}
388483
}
389484

390-
private static IntPtr State {
485+
private static IntPtr ProgressFormState {
486+
/*
391487
get {
392-
return ProgressManager.state;
488+
if (ProgressManager.progressFormState == TaskbarProgressBarState.Error) {
489+
return PBST_ERROR;
490+
} else if (ProgressManager.progressFormState == TaskbarProgressBarState.Paused) {
491+
return PBST_PAUSED;
492+
} else {
493+
return PBST_NORMAL;
494+
}
393495
}
496+
*/
394497

395498
set {
396-
ProgressManager.state = value;
499+
if (ProgressManager.progressFormValue >= PROGRESS_FORM_VALUE_COMPLETE) {
500+
return;
501+
}
397502

398-
if (ProgressBar != null) {
399-
if (ProgressBar.IsHandleCreated && ProgressBar.Handle != IntPtr.Zero) {
400-
SendMessage(ProgressBar.Handle, PBM_SETSTATE, ProgressManager.state, IntPtr.Zero);
503+
if (value == PBST_ERROR) {
504+
if (ProgressManager.progressFormState == TaskbarProgressBarState.Error) {
505+
return;
401506
}
402-
}
403507

404-
if (ProgressForm != null) {
405-
if (ProgressForm.IsHandleCreated && ProgressForm.Handle != IntPtr.Zero) {
406-
if (taskbarListInitialized) {
407-
if (ProgressManager.value < VALUE_COMPLETE) {
408-
if (ProgressManager.state == PBST_NORMAL) {
409-
// normal state does not take priority over marquee style
410-
if (ProgressManager.style != ProgressBarStyle.Marquee) {
411-
taskbarList.SetProgressState(ProgressForm.Handle, TaskbarProgressBarState.Normal);
412-
}
413-
} else if (ProgressManager.state == PBST_ERROR) {
414-
taskbarList.SetProgressState(ProgressForm.Handle, TaskbarProgressBarState.Error);
415-
} else if (ProgressManager.state == PBST_PAUSED) {
416-
taskbarList.SetProgressState(ProgressForm.Handle, TaskbarProgressBarState.Paused);
417-
}
418-
}
419-
}
508+
ProgressManager.progressFormState = TaskbarProgressBarState.Error;
509+
} else if (value == PBST_PAUSED) {
510+
if (ProgressManager.progressFormState == TaskbarProgressBarState.Paused) {
511+
return;
512+
}
513+
514+
ProgressManager.progressFormState = TaskbarProgressBarState.Paused;
515+
} else {
516+
if (ProgressManager.progressFormState == TaskbarProgressBarState.Normal) {
517+
return;
518+
}
519+
520+
// normal state does not take priority over indeterminate state
521+
if (ProgressManager.progressFormState == TaskbarProgressBarState.Indeterminate) {
522+
return;
420523
}
524+
525+
ProgressManager.progressFormState = TaskbarProgressBarState.Normal;
526+
}
527+
528+
if (ProgressForm == null) {
529+
return;
530+
}
531+
532+
if (!ProgressForm.IsHandleCreated || ProgressForm.Handle == IntPtr.Zero) {
533+
return;
534+
}
535+
536+
if (!taskbarListInitialized) {
537+
return;
421538
}
539+
540+
taskbarList.SetProgressState(ProgressForm.Handle, ProgressManager.progressFormState);
541+
}
542+
}
543+
544+
private static ProgressBarStyle Style {
545+
get {
546+
return ProgressManager.style;
547+
}
548+
549+
set {
550+
ProgressManager.style = value;
551+
552+
ProgressBarStyle = ProgressManager.style;
553+
ProgressFormStyle = ProgressManager.style;
554+
}
555+
}
556+
557+
private static int Value {
558+
get {
559+
return ProgressManager.value;
560+
}
561+
562+
set {
563+
ProgressManager.value = Math.Min(100, Math.Max(0, value));
564+
565+
ProgressBarValue = ProgressManager.value;
566+
ProgressFormValue = ProgressManager.value;
567+
}
568+
}
569+
570+
private static IntPtr State {
571+
get {
572+
return ProgressManager.state;
573+
}
574+
575+
set {
576+
ProgressManager.state = value;
577+
578+
ProgressBarState = ProgressManager.state;
579+
ProgressFormState = ProgressManager.state;
422580
}
423581
}
424582

0 commit comments

Comments
 (0)