-
Notifications
You must be signed in to change notification settings - Fork 209
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
JBR-4478 Implement support for native accessible caret events on Windows #456
Open
dmitrii-drobotov
wants to merge
1
commit into
JetBrains:jbr21
Choose a base branch
from
dmitrii-drobotov:ddrobotov/JBR-4478
base: jbr21
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
src/java.desktop/windows/classes/sun/awt/windows/AccessibleCaretLocationNotifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* | ||
* Copyright 2024 JetBrains s.r.o. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
package sun.awt.windows; | ||
|
||
import sun.awt.AWTAccessor; | ||
import sun.java2d.SunGraphicsEnvironment; | ||
|
||
import javax.swing.*; | ||
import javax.swing.event.CaretEvent; | ||
import javax.swing.event.CaretListener; | ||
import javax.swing.text.JTextComponent; | ||
import java.awt.*; | ||
import java.awt.im.InputMethodRequests; | ||
import java.beans.PropertyChangeEvent; | ||
import java.beans.PropertyChangeListener; | ||
import java.lang.ref.WeakReference; | ||
|
||
/** | ||
* Provides caret tracking support for assistive tools that don't work with Java Access Bridge. | ||
* Specifically, it's targeted for the built-in Windows Magnifier. | ||
* This class listens to caret change events of a currently focused JTextComponent and forwards them to the native code, | ||
* which in turn sends them as Win32 IAccessible events. | ||
* <p> | ||
* A typical high-level scenario of interaction with the magnifier: | ||
* <ol> | ||
* <li>Magnifier sends a WM_GETOBJECT window message to get accessible content of the window.</li> | ||
* <li>The message is handled in AwtComponent native class (awt_Component.cpp), | ||
* which calls {@link #startCaretNotifier}.</li> | ||
* <li>We start listening for keyboard focus change events.</li> | ||
* <li>If at some point focus gets to a {@link JTextComponent}, we subscribe to its caret events.</li> | ||
* <li>When caret changes, we need to move the magnifier viewport to the new caret location. | ||
* To achieve this, we create a Win32 IAccessible object for the caret (see AccessibleCaret.cpp), | ||
* and send an event that its location was changed (EVENT_OBJECT_LOCATIONCHANGE).</li> | ||
* <li>Magnifier receives this event and sends WM_GETOBJECT message with OBJID_CARET argument | ||
* to get the caret object and its location property. After that, it moves the viewport to the returned location. | ||
* </li> | ||
* <li>When the {@link JTextComponent} loses focus, we stop listening to caret events | ||
* and release the IAccessible caret object.</li> | ||
* </ol> | ||
* </p> | ||
* <p> | ||
* The feature is enabled by default | ||
* and can be disabled by setting sun.awt.windows.use.native.caret.accessibility.events property to false. | ||
* </p> | ||
*/ | ||
@SuppressWarnings("unused") // Used from the native through JNI. | ||
class AccessibleCaretLocationNotifier implements PropertyChangeListener, CaretListener { | ||
private static AccessibleCaretLocationNotifier caretNotifier; | ||
private static final boolean nativeCaretEventsEnabled = | ||
Boolean.parseBoolean(System.getProperty("sun.awt.windows.use.native.caret.accessibility.events", "true")); | ||
|
||
private WeakReference<JTextComponent> currentFocusedComponent; | ||
private long currentHwnd; | ||
|
||
@SuppressWarnings("unused") // Called from the native through JNI. | ||
public static void startCaretNotifier(long hwnd) { | ||
if (nativeCaretEventsEnabled && caretNotifier == null) { | ||
SwingUtilities.invokeLater(() -> { | ||
caretNotifier = new AccessibleCaretLocationNotifier(hwnd); | ||
KeyboardFocusManager cfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); | ||
cfm.addPropertyChangeListener("focusOwner", caretNotifier); | ||
if (cfm.getFocusOwner() instanceof JTextComponent textComponent) { | ||
caretNotifier.propertyChange(new PropertyChangeEvent(caretNotifier, "focusOwner", null, textComponent)); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public AccessibleCaretLocationNotifier(long hwnd) { | ||
currentHwnd = hwnd; | ||
} | ||
|
||
private static native void updateNativeCaretLocation(long hwnd, int x, int y, int width, int height); | ||
private static native void releaseNativeCaret(long hwnd); | ||
private static native void notifyFocusedWindowChanged(long hwnd); | ||
|
||
@Override | ||
public void propertyChange(PropertyChangeEvent e) { | ||
Window w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow(); | ||
if (w != null) { | ||
WWindowPeer wp = AWTAccessor.getComponentAccessor().getPeer(w); | ||
if (wp != null) { | ||
long hwnd = wp.getHWnd(); | ||
if (currentHwnd != hwnd) { | ||
currentHwnd = hwnd; | ||
notifyFocusedWindowChanged(currentHwnd); | ||
} | ||
} | ||
} | ||
|
||
Object newFocusedComponent = e.getNewValue(); | ||
if (currentFocusedComponent != null) { | ||
JTextComponent currentComponentStrong = currentFocusedComponent.get(); | ||
if (currentComponentStrong != null && newFocusedComponent != currentComponentStrong) { | ||
currentComponentStrong.removeCaretListener(this); | ||
currentFocusedComponent.clear(); | ||
currentFocusedComponent = null; | ||
releaseNativeCaret(currentHwnd); | ||
} | ||
} | ||
|
||
if (newFocusedComponent instanceof JTextComponent textComponent) { | ||
currentFocusedComponent = new WeakReference<>(textComponent); | ||
textComponent.addCaretListener(this); | ||
// Trigger caret event when the text component receives focus to notify about the initial caret location. | ||
caretUpdate(new CaretEvent(textComponent) { | ||
// Dot and mark won't be used, so we can set any values. | ||
@Override | ||
public int getDot() { return 0; } | ||
|
||
@Override | ||
public int getMark() { return 0; } | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public void caretUpdate(CaretEvent e) { | ||
if (!(e.getSource() instanceof JTextComponent textComponent)) { | ||
return; | ||
} | ||
|
||
SwingUtilities.invokeLater(() -> { | ||
dmitrii-drobotov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!textComponent.isShowing()) return; | ||
InputMethodRequests imr = textComponent.getInputMethodRequests(); | ||
if (imr == null) return; | ||
Rectangle caretRectangle = imr.getTextLocation(null); | ||
if (caretRectangle == null) return; | ||
caretRectangle.width = 1; | ||
|
||
Container parent = textComponent.getParent(); | ||
if (parent != null && parent.isShowing()) { | ||
// Make sure we don't go outside of parent bounds, which can happen in the case of scrollable components. | ||
Rectangle parentBounds = parent.getBounds(); | ||
parentBounds.setLocation(parent.getLocationOnScreen()); | ||
|
||
if (!parentBounds.contains(caretRectangle)) { | ||
caretRectangle = parentBounds.intersection(caretRectangle); | ||
if (caretRectangle.isEmpty()) return; | ||
} | ||
} | ||
|
||
caretRectangle = SunGraphicsEnvironment.toDeviceSpaceAbs(caretRectangle); | ||
|
||
updateNativeCaretLocation(AccessibleCaretLocationNotifier.this.currentHwnd, | ||
(int) caretRectangle.getX(), (int) caretRectangle.getY(), | ||
(int) caretRectangle.getWidth(), (int) caretRectangle.getHeight()); | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
caretNotifier
is read and written from different threads, we should:caretNotifier
is stillnull
on the EDT