forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessibilityUtils.java
More file actions
128 lines (114 loc) · 4.4 KB
/
Copy pathAccessibilityUtils.java
File metadata and controls
128 lines (114 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.utils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.accessibility.AccessibleAdapter;
import org.eclipse.swt.accessibility.AccessibleEvent;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import com.microsoft.copilot.eclipse.ui.swt.CssConstants;
/**
* Utility class for adding accessibility properties to UI components.
*/
public class AccessibilityUtils {
/**
* Adds an accessibility name to the given UI component.
*/
public static void addAccessibilityNameForUiComponent(Control control, String name) {
addAccessibilityPropertiesForUiComponent(control, name, null);
}
/**
* Adds an accessibility description to the given UI component.
*/
public static void addAccessibilityDescriptionForUiComponent(Control control, String description) {
addAccessibilityPropertiesForUiComponent(control, null, description);
}
/**
* Adds accessibility name and description to the given UI component.
*/
public static void addAccessibilityPropertiesForUiComponent(Control control, String name, String description) {
control.getAccessible().addAccessibleListener(new AccessibleAdapter() {
@Override
public void getName(AccessibleEvent e) {
e.result = name;
}
public void getDescription(AccessibleEvent e) {
e.result = description;
}
});
}
/**
* Adds focus border styling to a Composite widget. When the widget gains focus via keyboard (Tab), a colored border
* is drawn around it to provide a visual hint. The border is not shown when focus is gained via mouse click. Also
* enables tab traversal for the widget.
*
* @param composite the composite widget to add focus border styling and tab traversal to
*/
public static void addFocusBorderToComposite(Composite composite) {
final boolean[] showFocusBorder = { false };
final boolean[] mousePressed = { false };
// Enable tab traversal
composite.addTraverseListener(e -> {
if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
e.doit = true;
}
});
// Track mouse press to distinguish keyboard vs mouse focus
composite.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
mousePressed[0] = true;
}
});
composite.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
// Defer the border decision to allow mouseDown event to be processed first,
// since focusGained fires before mouseDown when clicking
composite.getDisplay().asyncExec(() -> {
if (composite.isDisposed() || !composite.isFocusControl()) {
return;
}
if (!mousePressed[0]) {
// Traverse up to find parent ScrolledComposite and ensure focused widget is visible,
// only auto scroll when focus is gained via keyboard
Composite parent = composite.getParent();
while (parent != null) {
if (parent instanceof ScrolledComposite scrolledComposite) {
scrolledComposite.showControl(composite);
break;
}
parent = parent.getParent();
}
}
// Only show border if focus was gained via keyboard (not mouse click)
showFocusBorder[0] = !mousePressed[0];
mousePressed[0] = false;
composite.redraw();
});
}
@Override
public void focusLost(FocusEvent e) {
showFocusBorder[0] = false;
mousePressed[0] = false;
composite.redraw();
}
});
composite.addPaintListener(e -> {
if (showFocusBorder[0]) {
Rectangle clientArea = composite.getClientArea();
Color focusBorderColor = CssConstants.getWidgetFocusBorderColor(composite.getDisplay());
e.gc.setForeground(focusBorderColor);
e.gc.setLineWidth(1);
e.gc.drawRectangle(0, 0, clientArea.width - 1, clientArea.height - 1);
}
});
}
}