void () { // cancel prev task if (myNativeUpdateTask != null) { myNativeUpdateTask.cancel(); } // ensure that top of stack has current touchbar final @Nullable ComponentActions ca = myStack.peek(); if (ca == null) { // stack is empty myLastShownTouchbar = null; NST.setTouchBar(myWindow != null ? myWindow.get() : null, null); return; } // the current touchbar wasn't set yet, do it now if (ca.getCurrent() == null) { ca.setCurrent(lastModifiersEx); if (ca.getCurrent() == null) { ca.setCurrent(0); } } // schedule a new task myNativeUpdateTask = SimpleTimer.getInstance().setUp(() -> { final @Nullable ComponentActions topCA = myStack.peek(); final @Nullable TBPanel tb = topCA != null ? topCA.getCurrent() : null; if (tb == myLastShownTouchbar) { return; } if (myLastShownTouchbar instanceof TBPanelActionGroup) { ((TBPanelActionGroup)myLastShownTouchbar).stopUpdateTimer(); } myLastShownTouchbar = tb; if (myLastShownTouchbar instanceof TBPanelActionGroup atb) { atb.startUpdateTimer(); // timer can "sleep" sometimes (when user doesn't send input, for example) // so always do force update before showing ApplicationManager.getApplication().invokeLater(atb::updateActionItems); } Window window = myWindow != null ? myWindow.get() : null; if (myLastShownTouchbar != null) { myLastShownTouchbar.setTo(window); } else { NST.setTouchBar(window, ID.NIL); } }, CHANGE_DELAY); }