AndroidStudio在使用快捷键关闭一个tab后,窗口失去焦点的问题
创始人
2024-10-12 01:31:01
0

我们可以编写一段监听器,当tab关闭时触发该监听器,强制将焦点移回到Android Studio窗口。

以下是具体代码示例:

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.actionSystem.impl.SimpleDataContext;
import com.intellij.openapi.actionSystem.impl.Utils;
import com.intellij.openapi.keymap.KeymapUtil;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.ui.tabs.JBTabs;
import com.intellij.ui.tabs.impl.JBTabsImpl;
import com.intellij.util.ui.UIUtil;

import javax.swing.*;
import java.awt.*;

public class FocusFixAction {

    public static void install() {
        JBTabsImpl tabs = UIUtil.uiTraverser(WindowManager.getInstance().getIdeFrame(null).getComponent())
                .getNext(JBTabsImpl.class);
        tabs.addListener(new FocusFixListener());
    }

    private static class FocusFixListener extends JBTabs.Listener.Adapter {
        @Override
        public void tabRemoved(TabInfo info) {
            JComponent c = info.getComponent();
            if (c == null) return;

            if (c.getParent() instanceof JLayeredPane) {
                c = c.getParent();
            }

            Rectangle cBounds = c.getBounds();

            cBounds.y += 2;
            if (SystemInfo.isOSX) {
                // move down one more pixel to work around window manager bugs on macOS
                cBounds.y += 1;
            }

            IdeFrame frame = IdeFrame.getActiveFrame();
            if (frame == null) return;

            JLayeredPane rootPane = frame.getRootPane();
            if (rootPane == null) return;

            JComponent focusOwner = (JComponent) KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
            boolean isToolbar = c instanceof ActionToolbarImpl;

            // if the current focused component is the main editor, focus the project tree view instead.
            if (isToolbar || focusOwner == null || Utils.getComponentParent(focusOwner, JBTabs.class) == null) {
                DataContext dataContext;
                if (isToolbar) {
                    dataContext = new SimpleDataContext(DataKeys.PROJECT.getName(), null, DataKeys.CONTEXT_COMPONENT.getName(), c);
                } else {
                    dataContext = DataManager.getInstance().getDataContext(rootPane);
                }

                ProjectView view = LangDataKeys.PROJECT_VIEW.getData(dataContext);

                if (view != null) {
                    view.select(null, null, false);
                    IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> view.select(null, info.getProject(), true));

相关内容

热门资讯

透视真的"wepok... 透视真的"wepoker透视脚本免费下载"详细辅助2025新版教程(有辅助器神器);大神普及一款德州...
透视ai代打"wep... 透视ai代打"wepoker有透视功能吗"详细辅助教你攻略(黑侠辅助器神器);wepoker有透视功...
透视透视"aapok... 透视透视"aapoker怎么选牌"详细辅助高科技教程(可以开透视辅助);原来确实真的有挂(需添加指定...
透视软件"hhpok... 透视软件"hhpoker真能买到挂吗"详细辅助AI教程(有插件透明)1、很好的工具软件,可以解锁游戏...
透视有挂"wepok... 透视有挂"wepokerplus透视脚本免费"详细辅助安装教程(到底有透视技巧)科技教程也叫必备教程...
透视有挂"wepok... 透视有挂"wepoker辅助器下载"详细辅助介绍教程(h5破解攻略);玩家必备必赢加哟《136704...
透视美元局"wepo... 透视美元局"wepoker怎么挂飞机"详细辅助规律教程(辅助脚本攻略)1、许多玩家不知道wepoke...
透视黑科技"uupo... 透视黑科技"uupoker有透视吗"详细辅助扑克教程(万能挂攻略);原来确实真的有挂(需添加指定薇1...
透视最新"智星德州可... 透视最新"智星德州可以透视吗"详细辅助可靠技巧(透视脚本苹果版软件);原来确实真的有挂(需添加指定薇...
透视软件"德州局hh... 透视软件"德州局hhpoker"详细辅助2025新版总结(透视脚本是有挂攻略);最新版2024是一款...