Skip to content
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

Keyboard focus can activate top component #4603

Merged
merged 1 commit into from Oct 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -36,13 +36,17 @@
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.AWTEventListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.netbeans.core.windows.ModeImpl;
import org.netbeans.core.windows.Switches;
import org.netbeans.core.windows.view.ui.slides.SlideBar;
Expand Down Expand Up @@ -82,6 +86,8 @@ public TabbedHandler(ModeView modeView, int kind, Tabbed tbd) {
activationManager = new ActivationManager();
Toolkit.getDefaultToolkit().addAWTEventListener(
activationManager, AWTEvent.MOUSE_EVENT_MASK);
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addPropertyChangeListener("focusOwner", activationManager);
}
}
tabbed = tbd;
Expand Down Expand Up @@ -464,11 +470,33 @@ public static void handleMaximization(TabActionEvent tae) {

/** Well, we can't totally get rid of AWT event listeners - this is what
* keeps track of the activated mode. */
private static class ActivationManager implements AWTEventListener {
private static class ActivationManager implements AWTEventListener, PropertyChangeListener {
@Override
public void eventDispatched(AWTEvent e) {
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
handleActivation((MouseEvent) e);
handleActivation(e.getSource());
}
}

/**
* Keyboard focus change event handler. Handle situation where
* active TopComponent was in a different window and window
* changed without a mouse event.
* See
* Editor with Keyboard focus is not active TopComponent
* https://github.com/apache/netbeans/issues/4437
*/
@Override
public void propertyChange(PropertyChangeEvent e) {
Frame mainWindowNB = WindowManagerImpl.getInstance().getMainWindow();
Window activeWindowKB = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
TopComponent currentTC = TopComponent.getRegistry().getActivated();
// Only do something if focus to the main window and
// active TC is not in the main window. Note that focus changes
// to detached windows handled in DefaultSeparateContainer.
if(mainWindowNB == activeWindowKB
&& mainWindowNB != SwingUtilities.getRoot(currentTC)) {
handleActivation(e.getNewValue());
}
}

Expand All @@ -490,12 +518,11 @@ public void eventDispatched(AWTEvent e) {
* components. This behavior is compatible with all window managers I can
* imagine.
*/
private void handleActivation(MouseEvent evt) {
Object obj = evt.getSource();
if (!(obj instanceof Component)) {
private void handleActivation(Object evtObject) {
if (!(evtObject instanceof Component)) {
return;
}
Component comp = (Component) obj;
Component comp = (Component) evtObject;

while (comp != null && !(comp instanceof ModeComponent)) {
if (comp instanceof JComponent) {
Expand Down