IDEA-194199: hide search-buttons panel when editor-search loses focus

This commit is contained in:
Artem Bochkarev
2018-06-26 12:38:26 +07:00
parent b0f1df1eb2
commit 4fa8963aa7
4 changed files with 64 additions and 17 deletions
@@ -34,6 +34,9 @@ class BarContainer {
_updateTouchBarsParents();
}
@Override
public String toString() { return myMain.toString(); }
void set(@NotNull TouchBar main, Map<Long, TouchBar> alts) {
myMain = main;
myKeyMask2Alt = alts;
@@ -140,7 +140,7 @@ class ProjectData {
return false;
}
@Nullable BarContainer findByComponent(Component child) {
@Nullable EditorData findEditorDataByComponent(Component child) {
ApplicationManager.getApplication().assertIsDispatchThread();
for (EditorData editorData : myEditors.values()) {
@@ -151,10 +151,16 @@ class ProjectData {
LOG.error("focused header of editor: " + editorData.editor + ", but BarContainer wasn't created, header: " + ecmp);
continue;
}
return editorData.containerSearch;
return editorData;
}
}
return null;
}
@Nullable BarContainer findDebugToolWindowByComponent(Component child) {
ApplicationManager.getApplication().assertIsDispatchThread();
if (myActiveDebugSessions.get() <= 0)
return null;
@@ -272,12 +278,16 @@ class ProjectData {
int getDbgSessions() { return myActiveDebugSessions.get(); }
static long getUsedKeyMask() { return InputEvent.ALT_DOWN_MASK | InputEvent.META_DOWN_MASK | InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK; }
private static long _str2mask(@NotNull String modifierId) {
if (!modifierId.contains(".")) {
if (modifierId.equalsIgnoreCase("alt"))
return InputEvent.ALT_DOWN_MASK;
if (modifierId.equalsIgnoreCase("cmd"))
return InputEvent.META_DOWN_MASK;
if (modifierId.equalsIgnoreCase("ctrl"))
return InputEvent.CTRL_DOWN_MASK;
if (modifierId.equalsIgnoreCase("shift"))
return InputEvent.SHIFT_DOWN_MASK;
return 0;
@@ -15,10 +15,13 @@ public class StackTouchBars {
private long myCurrentKeyMask;
// static String changeReason; // for debugging only
void updateKeyMask(long newMask) {
if (myCurrentKeyMask != newMask) {
synchronized (this) {
// System.out.printf("change current mask: 0x%X -> 0x%X\n", myCurrentKeyMask, e.getModifiersEx());
// changeReason = String.format("change current mask: 0x%X -> 0x%X", myCurrentKeyMask, newMask);
myCurrentKeyMask = newMask;
_setTouchBarFromTopContainer();
}
@@ -40,6 +43,7 @@ public class StackTouchBars {
if (condition != null && !condition.value(top))
return;
// System.out.println("removeContainer [POP]: " + top);
myContainersStack.pop();
_setTouchBarFromTopContainer();
}
@@ -62,6 +66,7 @@ public class StackTouchBars {
if (top == bar)
return;
// System.out.println("showContainer: " + bar);
myContainersStack.remove(bar);
myContainersStack.push(bar);
_setTouchBarFromTopContainer();
@@ -72,6 +77,7 @@ public class StackTouchBars {
if (tb == null || myContainersStack.isEmpty())
return;
// System.out.println("removeContainer: " + tb);
tb.onHide();
BarContainer top = myContainersStack.peek();
@@ -123,6 +129,9 @@ public class StackTouchBars {
synchronized void setTouchBar(TouchBar bar) {
// the usual event sequence "focus lost -> show underlay bar -> focus gained" produces annoying flicker
// use slightly deferred update to skip "showing underlay bar"
// System.out.printf("schedule next TouchBar: %s | reason '%s'\n", bar, changeReason);
// changeReason = null;
myNextBar = bar;
final Timer timer = new Timer(100, (event)->{
_setNextTouchBar();
@@ -141,6 +150,7 @@ public class StackTouchBars {
return;
}
// System.out.println("set next: " + myNextBar);
if (myCurrentBar != null)
myCurrentBar.onHide();
myCurrentBar = myNextBar;
@@ -124,20 +124,20 @@ public class TouchBarsManager {
if (e instanceof MouseWheelEvent)
return;
ourStack.updateKeyMask(e.getModifiersEx());
ourStack.updateKeyMask(e.getModifiersEx() & ProjectData.getUsedKeyMask());
}
public static void onFocusEvent(AWTEvent e) {
if (!isTouchBarAvailable())
return;
if (!(e.getSource() instanceof Container))
return;
final Container src = (Container)e.getSource();
// NOTE: WindowEvent.WINDOW_GAINED_FOCUS can be fired when frame focused
if (e.getID() == FocusEvent.FOCUS_GAINED) {
if (!(e.getSource() instanceof Container))
return;
final Container src = (Container)e.getSource();
if (_hasPopup()) {
// System.out.println("skip focus event processing because popup exists: " + e);
return;
@@ -146,6 +146,7 @@ public class TouchBarsManager {
if (_hasNonModalDialog()) {
final BarContainer barForParent = _findByParentComponent(src, ourTemporaryBars.values(), null);
if (barForParent != null) {
// StackTouchBars.changeReason = "non-modal dialog gained focus";
barForParent.show();
return;
}
@@ -155,27 +156,47 @@ public class TouchBarsManager {
if (pd.isDisposed())
continue;
if (pd.checkToolWindowContents((Component)e.getSource())) {
if (pd.checkToolWindowContents(src)) {
// System.out.println("tool window gained focus: " + e);
return;
}
final BarContainer parent = pd.findByComponent((Component)e.getSource());
if (parent != null) {
// System.out.println("component gained focus: " + e);
ourStack.showContainer(parent);
final ProjectData.EditorData ed = pd.findEditorDataByComponent(src);
if (ed != null && ed.containerSearch != null) {
// System.out.println("editor-component gained focus: " + e);
// StackTouchBars.changeReason = "editor-search gained focus";
ourStack.showContainer(ed.containerSearch);
return;
}
final BarContainer twbc = pd.findDebugToolWindowByComponent(src);
if (twbc != null) {
// System.out.println("debugger component gained focus: " + e);
// StackTouchBars.changeReason = "tool-window gained focus";
ourStack.showContainer(twbc);
return;
}
}
} else if (e.getID() == FocusEvent.FOCUS_LOST) {
if (!(e.getSource() instanceof Container))
return;
final Container src = (Container)e.getSource();
final BarContainer nonModalDialogParent = _findByParentComponent(src, ourTemporaryBars.values(), bc -> bc.isNonModalDialog());
if (nonModalDialogParent != null) {
// System.out.println("non-modal dialog window '" + nonModalDialogParent.getParentComponent() + "' lost focus: " + e);
// StackTouchBars.changeReason = "non-modal dialog lost focus";
nonModalDialogParent.hide();
return;
}
for (ProjectData pd: ourProjectData.values()) {
if (pd.isDisposed())
continue;
final ProjectData.EditorData ed = pd.findEditorDataByComponent(src);
if (ed != null && ed.containerSearch != null) {
// System.out.println("editor-component lost focus: " + e);
// StackTouchBars.changeReason = "editor-component lost focus";
ourStack.removeContainer(ed.containerSearch);
return;
}
}
}
}
@@ -208,6 +229,7 @@ public class TouchBarsManager {
final boolean hasDebugSession = pd.getDbgSessions() > 0;
if (!hasDebugSession) {
// System.out.println("elevate default because editor window gained focus: " + editor);
// StackTouchBars.changeReason = "elevate default because editor gained focus";
ourStack.elevateContainer(pd.get(BarType.DEFAULT));
}
}
@@ -255,6 +277,8 @@ public class TouchBarsManager {
return;
}
// System.out.printf("onUpdateEditorHeader: editor='%s', header='%s'\n", editor, header);
final ActionGroup actions = header instanceof DataProvider ? TouchbarDataKeys.ACTIONS_KEY.getData((DataProvider)header) : null;
if (header == null) {
// System.out.println("set null header");