Skip to content

Commit 451f8b2

Browse files
authored
Merge pull request #6390 from OliBomby/focus-sliderbar
Allow slider bar to take focus and accept keyboard input while not hovered
2 parents 9c6705e + cc00127 commit 451f8b2

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

osu.Framework.Tests/Visual/UserInterface/TestSceneSliderBar.cs

+27
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public TestSceneSliderBar()
5959
Size = new Vector2(200, 50),
6060
BackgroundColour = Color4.White,
6161
SelectionColour = Color4.Pink,
62+
FocusColour = Color4.OrangeRed,
6263
KeyboardStep = 1,
6364
Current = sliderBarValue
6465
},
@@ -72,6 +73,7 @@ public TestSceneSliderBar()
7273
RangePadding = 20,
7374
BackgroundColour = Color4.White,
7475
SelectionColour = Color4.Pink,
76+
FocusColour = Color4.OrangeRed,
7577
KeyboardStep = 1,
7678
Current = sliderBarValue
7779
},
@@ -85,6 +87,7 @@ public TestSceneSliderBar()
8587
Size = new Vector2(200, 10),
8688
BackgroundColour = Color4.White,
8789
SelectionColour = Color4.Pink,
90+
FocusColour = Color4.OrangeRed,
8891
KeyboardStep = 1,
8992
Current = sliderBarValue
9093
},
@@ -97,6 +100,7 @@ public TestSceneSliderBar()
97100
Size = new Vector2(200, 10),
98101
BackgroundColour = Color4.White,
99102
SelectionColour = Color4.Pink,
103+
FocusColour = Color4.OrangeRed,
100104
KeyboardStep = 1,
101105
Current = sliderBarValue
102106
},
@@ -109,6 +113,8 @@ public TestSceneSliderBar()
109113
{
110114
sliderBar.Current.Disabled = false;
111115
sliderBar.Current.Value = 0;
116+
sliderBar.GetContainingFocusManager()!.ChangeFocus(null);
117+
sliderBarWithNub.GetContainingFocusManager()!.ChangeFocus(null);
112118
});
113119

114120
[Test]
@@ -122,6 +128,7 @@ public void TestVerticalDragHasNoEffect()
122128
() => { InputManager.MoveMouseTo(sliderBar.ToScreenSpace(sliderBar.DrawSize * new Vector2(0.75f, 1f))); });
123129
AddStep("Release Click", () => { InputManager.ReleaseButton(MouseButton.Left); });
124130
checkValue(0);
131+
AddAssert("Slider has no focus", () => !sliderBar.HasFocus);
125132
}
126133

127134
[Test]
@@ -136,6 +143,7 @@ public void TestDragOutReleaseInHasNoEffect()
136143
AddStep("Drag Up", () => { InputManager.MoveMouseTo(sliderBar.ToScreenSpace(sliderBar.DrawSize * new Vector2(0.25f, 0.5f))); });
137144
AddStep("Release Click", () => { InputManager.ReleaseButton(MouseButton.Left); });
138145
checkValue(0);
146+
AddAssert("Slider has focus", () => sliderBar.HasFocus);
139147
}
140148

141149
[Test]
@@ -160,6 +168,23 @@ public void TestKeyboardInput()
160168
InputManager.ReleaseKey(Key.Right);
161169
});
162170
checkValue(1);
171+
172+
AddStep("Click slider", () => InputManager.Click(MouseButton.Left));
173+
checkValue(-5);
174+
175+
AddAssert("Slider has focus", () => sliderBar.HasFocus);
176+
177+
AddStep("move mouse outside", () =>
178+
{
179+
InputManager.MoveMouseTo(sliderBar.ToScreenSpace(sliderBar.DrawSize * new Vector2(2f, 0.5f)));
180+
});
181+
182+
AddStep("Press right arrow key", () =>
183+
{
184+
InputManager.PressKey(Key.Right);
185+
InputManager.ReleaseKey(Key.Right);
186+
});
187+
checkValue(-4);
163188
}
164189

165190
[TestCase(false)]
@@ -246,6 +271,7 @@ public void TestAbsoluteDrag()
246271
() => { InputManager.MoveMouseTo(sliderBarWithNub.ToScreenSpace(sliderBarWithNub.DrawSize * new Vector2(0.4f, 1f))); });
247272
AddStep("Release Click", () => { InputManager.ReleaseButton(MouseButton.Left); });
248273
checkValue(-2);
274+
AddAssert("Slider has focus", () => sliderBarWithNub.HasFocus);
249275
}
250276

251277
[Test]
@@ -259,6 +285,7 @@ public void TestRelativeDrag()
259285
() => { InputManager.MoveMouseTo(sliderBarWithNub.ToScreenSpace(sliderBarWithNub.DrawSize * new Vector2(0.75f, 1f))); });
260286
AddStep("Release Click", () => { InputManager.ReleaseButton(MouseButton.Left); });
261287
checkValue(3);
288+
AddAssert("Slider has focus", () => sliderBarWithNub.HasFocus);
262289
}
263290

264291
[Test]

osu.Framework/Graphics/UserInterface/BasicSliderBar.cs

+41-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Numerics;
55
using osuTK.Graphics;
66
using osu.Framework.Graphics.Shapes;
7+
using osu.Framework.Input.Events;
78
using Vector2 = osuTK.Vector2;
89

910
namespace osu.Framework.Graphics.UserInterface
@@ -23,6 +24,18 @@ public Color4 SelectionColour
2324
set => SelectionBox.Colour = value;
2425
}
2526

27+
private Color4 focusColour = FrameworkColour.YellowGreen;
28+
29+
public Color4 FocusColour
30+
{
31+
get => focusColour;
32+
set
33+
{
34+
focusColour = value;
35+
updateFocus();
36+
}
37+
}
38+
2639
protected readonly Box SelectionBox;
2740
protected readonly Box Box;
2841

@@ -37,10 +50,37 @@ public BasicSliderBar()
3750
},
3851
SelectionBox = new Box
3952
{
40-
RelativeSizeAxes = Axes.Both,
4153
Colour = FrameworkColour.Yellow,
54+
RelativeSizeAxes = Axes.Both,
4255
}
4356
};
57+
58+
Masking = true;
59+
}
60+
61+
protected override void OnFocus(FocusEvent e)
62+
{
63+
updateFocus();
64+
base.OnFocus(e);
65+
}
66+
67+
protected override void OnFocusLost(FocusLostEvent e)
68+
{
69+
updateFocus();
70+
base.OnFocusLost(e);
71+
}
72+
73+
private void updateFocus()
74+
{
75+
if (HasFocus)
76+
{
77+
BorderThickness = 3;
78+
BorderColour = FocusColour;
79+
}
80+
else
81+
{
82+
BorderThickness = 0;
83+
}
4484
}
4585

4686
protected override void UpdateValue(float value)

osu.Framework/Graphics/UserInterface/SliderBar.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,21 @@ protected override bool OnDragStart(DragStartEvent e)
166166
return false;
167167
}
168168

169+
GetContainingFocusManager()?.ChangeFocus(this);
169170
handleMouseInput(e);
170171
return true;
171172
}
172173

173174
protected override void OnDragEnd(DragEndEvent e) => Commit();
174175

176+
public override bool AcceptsFocus => true;
177+
175178
protected override bool OnKeyDown(KeyDownEvent e)
176179
{
177180
if (currentNumberInstantaneous.Disabled)
178181
return false;
179182

180-
if (!IsHovered)
183+
if (!IsHovered && !HasFocus)
181184
return false;
182185

183186
float step = KeyboardStep != 0 ? KeyboardStep : (Convert.ToSingle(currentNumberInstantaneous.MaxValue) - Convert.ToSingle(currentNumberInstantaneous.MinValue)) / 20;

0 commit comments

Comments
 (0)