using WPFluent.Interop;
namespace WPFluent.Taskbar;
///
/// Allows to change the status of the displayed notification in the application icon on the TaskBar.
///
public static class TaskBarProgress
{
///
/// Gets a value indicating whether the current operating system supports task bar manipulation.
///
private static bool IsSupported() { return Win32.Utilities.IsOSWindows7OrNewer; }
///
/// Allows to change the status of the progress bar in the task bar.
///
/// Window to manipulate.
/// State of the progress indicator.
public static bool SetState(Window? window, TaskBarProgressState taskBarProgressState)
{
if(window is null)
{
return false;
}
if(window.IsLoaded)
{
return SetState(new WindowInteropHelper(window).Handle, taskBarProgressState);
}
window.Loaded += (_, _) =>
{
_ = SetState(new WindowInteropHelper(window).Handle, taskBarProgressState);
};
return true;
}
///
/// Allows to change the status of the progress bar in the task bar.
///
/// Window handle.
/// State of the progress indicator.
public static bool SetState(IntPtr hWnd, TaskBarProgressState taskBarProgressState)
{
if(!IsSupported())
{
throw new Exception("Taskbar functions not available.");
}
return UnsafeNativeMethods.SetTaskbarState(hWnd, UnsafeReflection.Cast(taskBarProgressState));
}
///
/// Allows to change the fill of the task bar.
///
/// Window to manipulate.
/// Progress sate to set.
/// Current value to display
public static bool SetValue(Window window, TaskBarProgressState taskBarProgressState, int current)
{
if(current > 100)
{
current = 100;
}
if(current < 0)
{
current = 0;
}
return SetValue(window, taskBarProgressState, current, 100);
}
///
/// Allows to change the fill of the task bar.
///
/// Window handle.
/// Progress sate to set.
/// Current value to display
public static bool SetValue(IntPtr hWnd, TaskBarProgressState taskBarProgressState, int current)
{
if(current > 100)
{
current = 100;
}
if(current < 0)
{
current = 0;
}
return SetValue(hWnd, taskBarProgressState, current, 100);
}
///
/// Allows to change the fill of the task bar.
///
/// Window to manipulate.
/// Progress sate to set.
/// Current value to display
/// Total number for division.
public static bool SetValue(Window? window, TaskBarProgressState taskBarProgressState, int current, int total)
{
if(window is null)
{
return false;
}
if(window.IsLoaded)
{
return SetValue(new WindowInteropHelper(window).Handle, taskBarProgressState, current, total);
}
window.Loaded += (_, _) =>
{
_ = SetValue(new WindowInteropHelper(window).Handle, taskBarProgressState, current, total);
};
return false;
}
///
/// Allows to change the fill of the task bar.
///
/// Window handle.
/// Progress sate to set.
/// Current value to display
/// Total number for division.
public static bool SetValue(IntPtr hWnd, TaskBarProgressState taskBarProgressState, int current, int total)
{
if(!IsSupported())
{
throw new Exception("Taskbar functions not available.");
}
return UnsafeNativeMethods.SetTaskbarValue(hWnd, UnsafeReflection.Cast(taskBarProgressState), current, total);
}
}