Skip to content

Gtk: Fix incrementing ProgressBar.Value by one #2759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions src/Eto.Gtk/Forms/Controls/ProgressBarHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ namespace Eto.GtkSharp.Forms.Controls
{
public class ProgressBarHandler : GtkControl<Gtk.ProgressBar, ProgressBar, ProgressBar.ICallback>, ProgressBar.IHandler
{
int minValue;
int maxValue = 100;
bool indeterminate;
UITimer timer;
int _value;
int _minValue;
int _maxValue = 100;
bool _indeterminate;
UITimer _timer;
public static double UpdateInterval = 0.2;
public static double PulseStep = 0.1;

Expand Down Expand Up @@ -41,62 +42,63 @@ public void TimerElapsed(object sender, EventArgs e)

public bool Indeterminate
{
get { return indeterminate; }
get { return _indeterminate; }
set
{
indeterminate = value;
if (indeterminate)
_indeterminate = value;
if (_indeterminate)
{
if (timer == null)
if (_timer == null)
{
timer = new UITimer();
timer.Elapsed += Connector.TimerElapsed;
_timer = new UITimer();
_timer.Elapsed += Connector.TimerElapsed;
}
timer.Interval = UpdateInterval;
_timer.Interval = UpdateInterval;
Control.PulseStep = PulseStep;
timer.Start();
_timer.Start();
}
else if (timer != null)
timer.Stop();
else if (_timer != null)
_timer.Stop();
}
}

public int MaxValue
{
get { return maxValue; }
get { return _maxValue; }
set
{
var val = Value;
maxValue = value;
_maxValue = value;
Value = val;
}
}

public int MinValue
{
get { return minValue; }
get { return _minValue; }
set
{
var val = Value;
minValue = value;
_minValue = value;
Value = val;
}
}

public int Value
{
get { return (int)((Control.Fraction * MaxValue) + MinValue); }
get => _value;
set
{
_value = value;
Control.Fraction = Math.Max(0, Math.Min(1, ((double)value - MinValue) / (double)MaxValue));
}
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (timer != null)
timer.Stop();
if (_timer != null)
_timer.Stop();
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions test/Eto.Test/UnitTests/Forms/Controls/ProgressBarTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using NUnit.Framework;
namespace Eto.Test.UnitTests.Forms.Controls
{
[TestFixture]
public class ProgressBarTests : TestBase
{
[Test]
public void IncrementingByOneShouldGetToTheEnd()
{
Invoke(() =>
{
var progressBar = new ProgressBar();
progressBar.MaxValue = 100;
progressBar.MinValue = 0;
progressBar.Value = 0;
for (int i = 0; i < 100; i++)
{
progressBar.Value++;
}
Assert.That(progressBar.Value, Is.EqualTo(100), "#1");
});
}
}
}
Loading