sclass BackgroundProcessesUI is IBackgroundProcesses { Set processes = setWithNotify(syncLinkedHashSet(), l0 updateCount); SimpleLiveValue lvCount = new SimpleLiveValue(Int, 0); record Entry(S name) is IBackgroundProcess { settable JMenuItem menuItem; settable Runnable interruptAction; toString { ret name; } close { processes.remove(this); } } JLabel shortLabel() { var lbl = bindToolTipToTransformedLiveValue( n -> makeToolTip(), lvCount, simpleTransformedLiveValueLabel(n -> n2(n), lvCount)); onMouseDown_anyButton(lbl, e -> { var l = cloneList(processes); // make sure title is displayed fully JPopupMenu menu = new;/* { public Dimension getMinimumSize() { TitledBorder border = optCast TitledBorder(getBorder()); _print(+border); ret maxDimension(super.getMinimumSize(), border?.getMinimumSize(this)); } };*/ int n = componentCount(menu); for (Entry a : l) addMenuItem(menu, processToMenuItem(a)); if (componentCount(menu) != n) { var border = jRaisedSectionBorder("Background Processes"); setBorder(menu, border); showPopupMenu(menu, e); var size = menu.getSize(); var borderSize = dimensionPlus(10 /* hack */, 0, border.getMinimumSize(menu)); printVars(+size, +borderSize); menu.setPopupSize(maxDimension(size, borderSize)); } }); ret lbl; } void add(Entry process) { processes.add(process); } void remove(Entry process) { processes.remove(process); } void addOrRemove(bool add, Entry process) { if (add) add(process); else remove(process); } public Entry tempAdd(S name) { ret tempAdd(new Entry(or2(name, "Unnamed process"))); } public Entry tempAdd(Entry process) { if (process == null) null; processes.add(process); ret process; } void updateCount { lvCount.set(l(processes)); } S makeToolTip() { var l = listProcesses(); ret empty(l) ? "No background processes" : n2(l, "background process", "background processes") + ": " + joinWithComma(processes); } swappable JMenuItem processToMenuItem(Entry process) { try object process.getMenuItem(); if (process.interruptAction() != null) ret jMenuItem("Interrupt " + process, rThread(process.interruptAction())); ret jMenuItem(process + " [no action available]"); } L listProcesses() { ret cloneList(processes); } Cl processesNamedIC(S name) { ret filter(listProcesses(), entry -> eqic(entry.name, name)); } }