Skip to content

Commit 743d80f

Browse files
author
Bianca Marina Stana
committed
Fixed a few more accessibility issues
1 parent 802daea commit 743d80f

File tree

9 files changed

+108
-95
lines changed

9 files changed

+108
-95
lines changed

Assets/Scripts/Screen Reader/UGUI/AccessibleSlider.cs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using TMPro;
22
using UnityEngine;
33
using UnityEngine.Accessibility;
4+
using UnityEngine.EventSystems;
45
using UnityEngine.UI;
56

67
namespace Unity.Samples.ScreenReader
@@ -10,9 +11,10 @@ namespace Unity.Samples.ScreenReader
1011
/// </summary>
1112
[AddComponentMenu("Accessibility/Accessible Slider"), DisallowMultipleComponent]
1213
[ExecuteAlways]
13-
public sealed class AccessibleSlider : AccessibleElement
14+
public class AccessibleSlider : AccessibleElement
1415
{
15-
Slider m_Slider;
16+
protected Slider m_Slider;
17+
1618
Text m_Text;
1719
TMP_Text m_TMPText;
1820

@@ -49,6 +51,11 @@ protected override void BindToControl()
4951

5052
incremented += OnIncremented;
5153
decremented += OnDecremented;
54+
55+
if (Application.platform == RuntimePlatform.WindowsPlayer)
56+
{
57+
focused += OnSliderFocused;
58+
}
5259
}
5360
}
5461

@@ -60,10 +67,30 @@ protected override void UnbindFromControl()
6067

6168
incremented -= OnIncremented;
6269
decremented -= OnDecremented;
70+
71+
if (Application.platform == RuntimePlatform.WindowsPlayer)
72+
{
73+
focused -= OnSliderFocused;
74+
}
75+
}
76+
}
77+
78+
void OnSliderFocused(bool isFocused)
79+
{
80+
if (isFocused)
81+
{
82+
if (m_Slider != null && m_Slider.IsActive() && m_Slider.IsInteractable())
83+
{
84+
m_Slider.Select();
85+
}
86+
}
87+
else if (!EventSystem.current.alreadySelecting)
88+
{
89+
EventSystem.current.SetSelectedGameObject(null);
6390
}
6491
}
6592

66-
void OnIncremented()
93+
protected virtual void OnIncremented()
6794
{
6895
if (m_Slider.IsActive() && m_Slider.IsInteractable())
6996
{
@@ -73,7 +100,7 @@ void OnIncremented()
73100
}
74101
}
75102

76-
void OnDecremented()
103+
protected virtual void OnDecremented()
77104
{
78105
if (m_Slider.IsActive() && m_Slider.IsInteractable())
79106
{
@@ -83,7 +110,7 @@ void OnDecremented()
83110
}
84111
}
85112

86-
void UpdateValue(float newValue)
113+
protected virtual void UpdateValue(float newValue)
87114
{
88115
value = $"{newValue:P0}";
89116
SetNodeProperties();

Assets/Scripts/Screen Reader/UGUI/AccessibleSliderWithSteps.cs

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using TMPro;
21
using UnityEngine;
3-
using UnityEngine.Accessibility;
4-
using UnityEngine.UI;
52

63
namespace Unity.Samples.ScreenReader
74
{
@@ -10,76 +7,25 @@ namespace Unity.Samples.ScreenReader
107
/// </summary>
118
[AddComponentMenu("Accessibility/Accessible Slider With Steps"), DisallowMultipleComponent]
129
[ExecuteAlways]
13-
public sealed class AccessibleSliderWithSteps : AccessibleElement
10+
public sealed class AccessibleSliderWithSteps : AccessibleSlider
1411
{
15-
Slider m_Slider;
16-
Text m_Text;
17-
TMP_Text m_TMPText;
18-
19-
void Start()
20-
{
21-
#if UNITY_2023_3_OR_NEWER
22-
role = AccessibilityRole.Slider;
23-
#endif // UNITY_2023_3_OR_NEWER
24-
25-
m_Slider = GetComponentInChildren<Slider>();
26-
m_Text = GetComponentInChildren<Text>();
27-
m_TMPText = GetComponentInChildren<TMP_Text>();
28-
29-
if (m_Text != null)
30-
{
31-
label ??= m_Text.text;
32-
}
33-
else if (m_TMPText != null)
34-
{
35-
label ??= m_TMPText.text;
36-
}
37-
38-
if (m_Slider != null)
39-
{
40-
UpdateValue(m_Slider.value);
41-
}
42-
}
43-
44-
protected override void BindToControl()
45-
{
46-
if (m_Slider != null)
47-
{
48-
m_Slider.onValueChanged.AddListener(UpdateValue);
49-
50-
incremented += OnIncremented;
51-
decremented += OnDecremented;
52-
}
53-
}
54-
55-
protected override void UnbindFromControl()
56-
{
57-
if (m_Slider != null)
58-
{
59-
m_Slider.onValueChanged.RemoveListener(UpdateValue);
60-
61-
incremented -= OnIncremented;
62-
decremented -= OnDecremented;
63-
}
64-
}
65-
66-
void OnIncremented()
12+
protected override void OnIncremented()
6713
{
6814
if (m_Slider.IsActive() && m_Slider.IsInteractable() && m_Slider.value < 2)
6915
{
7016
m_Slider.value++;
7117
}
7218
}
7319

74-
void OnDecremented()
20+
protected override void OnDecremented()
7521
{
7622
if (m_Slider.IsActive() && m_Slider.IsInteractable() && m_Slider.value > 0)
7723
{
7824
m_Slider.value--;
7925
}
8026
}
8127

82-
void UpdateValue(float newValue)
28+
protected override void UpdateValue(float newValue)
8329
{
8430
value = newValue switch
8531
{

Assets/Scripts/Screen Reader/UITk/Handlers/ButtonHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ButtonHandler()
2121

2222
public override string GetLabel()
2323
{
24-
return (ownerElement as Button)?.text;
24+
return ownerElement is Button button ? button.text : base.GetLabel();
2525
}
2626

2727
public override AccessibilityRole GetRole() => AccessibilityRole.Button;

Assets/Scripts/Screen Reader/UITk/Handlers/LabelHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class LabelHandler : VisualElementAccessibilityHandler
99
{
1010
public override string GetLabel()
1111
{
12-
return (ownerElement as Label)?.text;
12+
return ownerElement is Label lbl ? lbl.text : base.GetLabel();
1313
}
1414

1515
public override AccessibilityRole GetRole() => AccessibilityRole.StaticText;

Assets/Scripts/Screen Reader/UITk/Handlers/ScrollViewHandler.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public class ScrollViewHandler : VisualElementAccessibilityHandler
1010
{
1111
#if UNITY_6000_3_OR_NEWER
1212
public override AccessibilityRole GetRole() => AccessibilityRole.ScrollView;
13+
#else // UNITY_6000_3_OR_NEWER
14+
public override bool IsActive() =>
15+
Application.platform != RuntimePlatform.Android &&
16+
Application.platform != RuntimePlatform.IPhonePlayer;
1317
#endif // UNITY_6000_3_OR_NEWER
1418

1519
protected override void BindToElement(VisualElement ve)

Assets/Scripts/Screen Reader/UITk/Handlers/SliderHandler.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using UnityEngine;
12
using UnityEngine.Accessibility;
23
using UnityEngine.Scripting;
34
using UnityEngine.UIElements;
@@ -9,6 +10,23 @@ class BaseSliderHandler<TValue> : BaseFieldHandler<TValue>
910
{
1011
public BaseSliderHandler()
1112
{
13+
if (Application.platform == RuntimePlatform.WindowsPlayer)
14+
{
15+
focused += focused =>
16+
{
17+
var slider = ownerElement as Slider;
18+
19+
if (focused)
20+
{
21+
slider?.Focus();
22+
}
23+
else
24+
{
25+
slider?.Blur();
26+
}
27+
};
28+
}
29+
1230
incremented += () => Step(true);
1331
decremented += () => Step(false);
1432
}
@@ -30,9 +48,14 @@ public void Step(bool incr)
3048
slider.value += step;
3149
}
3250

51+
public override string GetLabel()
52+
{
53+
return ownerElement is Slider slider ? slider.label : base.GetLabel();
54+
}
55+
3356
public override string GetValue()
3457
{
35-
return ownerElement is Slider field ? $"{field.value:P0}" : "";
58+
return ownerElement is Slider slider ? $"{slider.value:P0}" : base.GetValue();
3659
}
3760

3861
#if UNITY_2023_3_OR_NEWER

Assets/Scripts/Screen Reader/UITk/Handlers/TextFieldHandler.cs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,35 @@ class TextFieldFieldHandler : BaseFieldHandler<string>
1010
{
1111
public TextFieldFieldHandler()
1212
{
13-
focused += focused =>
13+
if (Application.platform == RuntimePlatform.OSXPlayer ||
14+
Application.platform == RuntimePlatform.WindowsPlayer)
1415
{
15-
var textField = ownerElement as TextField;
16-
17-
if (focused)
18-
{
19-
textField?.Focus();
20-
}
21-
else
16+
focused += focused =>
2217
{
23-
textField?.Blur();
24-
}
25-
};
18+
var textField = ownerElement as TextField;
19+
20+
if (focused)
21+
{
22+
textField?.Focus();
23+
}
24+
else
25+
{
26+
textField?.Blur();
27+
}
28+
};
29+
}
2630
}
2731

28-
public override string GetValue()
32+
public override string GetLabel()
2933
{
30-
var textField = ownerElement as TextField;
31-
32-
if (string.IsNullOrEmpty(textField?.value))
33-
{
34-
return ownerElement is TextField tf ? tf.textEdition.placeholder : null;
35-
}
34+
return ownerElement is TextField textField ? textField.label : base.GetLabel();
35+
}
3636

37-
return base.GetValue();
37+
public override string GetValue()
38+
{
39+
return ownerElement is TextField textField ?
40+
string.IsNullOrEmpty(textField.value) ? textField.textEdition.placeholder : textField.value :
41+
base.GetValue();
3842
}
3943

4044
#if UNITY_6000_3_OR_NEWER

Assets/Scripts/Screen Reader/UITk/Handlers/ToggleHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public ToggleHandler()
1919
};
2020
}
2121

22+
public override string GetLabel()
23+
{
24+
return ownerElement is Toggle toggle ? toggle.label : base.GetLabel();
25+
}
26+
2227
public override string GetValue() => "";
2328

2429
#if UNITY_2023_3_OR_NEWER

Assets/Scripts/Screen Reader/UITk/Handlers/VisualElementAccessibilityHandler.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,26 @@ public bool isActive
7171
{
7272
get
7373
{
74-
if (m_OwnerElement != null)
74+
if (m_OwnerElement == null)
7575
{
76-
// If the node is disabled or hidden, then it is set as inactive regardless of
77-
// AccessibilityProperties.isActive.
78-
if (IsElementVisible(m_OwnerElement))
79-
{
80-
var acc = m_OwnerElement.GetAccessibleProperties();
81-
82-
return acc is not { m_IsActive: { defined: true } } || acc.active;
83-
}
76+
return false;
8477
}
8578

86-
return false;
79+
// If the node is disabled or hidden, then it is set as inactive regardless of
80+
// AccessibilityProperties.isActive.
81+
if (!IsElementVisible(m_OwnerElement))
82+
{
83+
return false;
84+
}
85+
86+
var acc = m_OwnerElement.GetAccessibleProperties();
87+
88+
return (acc is not { m_IsActive: { defined: true } } || acc.active) && IsActive();
8789
}
8890
}
8991

92+
public virtual bool IsActive() => true;
93+
9094
public bool isIgnored
9195
{
9296
get

0 commit comments

Comments
 (0)