using System;
namespace Terminal.Gui {
///
/// A Progress Bar view that can indicate progress of an activity visually.
///
///
///
/// can operate in two modes, percentage mode, or
/// activity mode. The progress bar starts in percentage mode and
/// setting the Fraction property will reflect on the UI the progress
/// made so far. Activity mode is used when the application has no
/// way of knowing how much time is left, and is started when the method is called.
/// Call repeatedly as progress is made.
///
///
public class ProgressBar : View {
bool isActivity;
int activityPos, delta;
///
/// Initializes a new instance of the class, starts in percentage mode with an absolute position and size.
///
/// Rect.
public ProgressBar (Rect rect) : base (rect)
{
CanFocus = false;
fraction = 0;
}
///
/// Initializes a new instance of the class, starts in percentage mode and uses relative layout.
///
public ProgressBar () : base ()
{
CanFocus = false;
fraction = 0;
}
float fraction;
///
/// Gets or sets the fraction to display, must be a value between 0 and 1.
///
/// The fraction representing the progress.
public float Fraction {
get => fraction;
set {
fraction = value;
isActivity = false;
SetNeedsDisplay ();
}
}
///
/// Notifies the that some progress has taken place.
///
///
/// If the is is percentage mode, it switches to activity
/// mode. If is in activity mode, the marker is moved.
///
public void Pulse ()
{
if (!isActivity) {
isActivity = true;
activityPos = 0;
delta = 1;
} else {
activityPos += delta;
if (activityPos < 0) {
activityPos = 1;
delta = 1;
} else if (activityPos >= Frame.Width) {
activityPos = Frame.Width - 2;
delta = -1;
}
}
SetNeedsDisplay ();
}
///
public override void Redraw(Rect region)
{
Driver.SetAttribute (ColorScheme.Normal);
int top = Frame.Width;
if (isActivity) {
Move (0, 0);
for (int i = 0; i < top; i++)
if (i == activityPos)
Driver.AddRune (Driver.Stipple);
else
Driver.AddRune (' ');
} else {
Move (0, 0);
int mid = (int)(fraction * top);
int i;
for (i = 0; i < mid; i++)
Driver.AddRune (Driver.Stipple);
for (; i < top; i++)
Driver.AddRune (' ');
}
}
}
}