Skip to content
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
5 changes: 4 additions & 1 deletion ImGui.Widgets/ValueSlider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@

private static bool SetValue(ref double value, double target, double min, double max, bool isInteger)
{
double next = Math.Clamp(isInteger ? Math.Round(target, MidpointRounding.AwayFromZero) : target, min, max);
// The value is stored as a float or an int, so it is compared as one: a drag held as a double
// that a float cannot hold exactly would otherwise read as a fresh change on every held frame.
double stored = isInteger ? Math.Round(target, MidpointRounding.AwayFromZero) : (float)target;
double next = Math.Clamp(stored, min, max);
if (IsSameValue(next, value))
{
return false;
Expand All @@ -282,7 +285,7 @@
ImDrawListPtr drawList = ImGui.GetWindowDrawList();
ImGuiStylePtr style = ImGui.GetStyle();
float rounding = style.FrameRounding;
ImGuiCol background = held ? ImGuiCol.FrameBgActive : hovered ? ImGuiCol.FrameBgHovered : ImGuiCol.FrameBg;

Check warning on line 288 in ImGui.Widgets/ValueSlider.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 288 in ImGui.Widgets/ValueSlider.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 288 in ImGui.Widgets/ValueSlider.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 288 in ImGui.Widgets/ValueSlider.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 288 in ImGui.Widgets/ValueSlider.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 288 in ImGui.Widgets/ValueSlider.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 288 in ImGui.Widgets/ValueSlider.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 288 in ImGui.Widgets/ValueSlider.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 288 in ImGui.Widgets/ValueSlider.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 288 in ImGui.Widgets/ValueSlider.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.
drawList.AddRectFilled(frame.Min, frame.Max, ImGui.GetColorU32(background), rounding);

(float start, float end) = ValueSliderState.FillSpan(value, min, max, options.Fill, options.Logarithmic, spec.Epsilon);
Expand Down
36 changes: 36 additions & 0 deletions tests/ImGui.Widgets.UITests/ValueSliderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,42 @@ public void ValueSlider_APrefixIsDrawnButNotTyped()
Assert.AreEqual(30f, value, "A typed value with the prefix in front was not read.");
}

[TestMethod]
public void ValueSlider_AHeldPointerThatStopsReportsNoChange()
{
// 0..1 over 200 px, so a 7 px drag lands on 0.035, which a float cannot hold exactly.
float fraction = 0.5f;
int changes = 0;
Start(() =>
{
ImGui.PushItemWidth(FrameWidth);
if (ImGuiWidgets.ValueSlider(Label, ref fraction, 0f, 1f))
{
changes++;
}

ImGui.PopItemWidth();
});

Rectangle rect = RectOf(Label);
float x = rect.MinX + (FrameWidth / 2f);
float y = rect.MinY + (rect.Height / 2f);
Harness.Mouse.MoveTo(x, y);
Step();
HarnessMouse.Down(0);
Step();
Harness.Mouse.MoveTo(x + 7f, y);
Step(2);

Assert.AreNotEqual(0.5f, fraction, "The drag did not move the value.");
changes = 0;
Step(5);
HarnessMouse.Up(0);
Step();

Assert.AreEqual(0, changes, "The slider reported a change on frames the held pointer did not move.");
}

[TestMethod]
public void ValueSlider_ReportsTheFrameItChangedOn()
{
Expand Down
Loading