From 81bb38543971407d579f57529ff3f3873ee60104 Mon Sep 17 00:00:00 2001 From: Artem Bochkarev Date: Thu, 7 Jun 2018 15:11:32 +0700 Subject: [PATCH] IDEA-163208: refactoring - simplify api --- .../execution/actions/StopAction.java | 4 +- .../com/intellij/ui/mac/touchbar/TBItem.java | 4 +- .../ui/mac/touchbar/TBItemButton.java | 91 +++++++++++++++---- .../intellij/ui/mac/touchbar/TouchBar.java | 40 ++------ .../ui/mac/touchbar/TouchBarsManager.java | 17 ++-- .../ui/mac/touchbar/TouchbarTest.java | 20 ++-- 6 files changed, 105 insertions(+), 71 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/execution/actions/StopAction.java b/platform/lang-impl/src/com/intellij/execution/actions/StopAction.java index 9b42e70a4d47..fae59b8ae8c2 100644 --- a/platform/lang-impl/src/com/intellij/execution/actions/StopAction.java +++ b/platform/lang-impl/src/com/intellij/execution/actions/StopAction.java @@ -269,8 +269,8 @@ public class StopAction extends DumbAwareAction implements AnAction.TransparentU final TouchBar tb; try (NSAutoreleaseLock lock = new NSAutoreleaseLock()) { tb = new TouchBar("select_running_to_stop", true, true); - tb.addButton(null, "Stop all", () -> { - for (RunContentDescriptor sd : stoppableDescriptors) + tb.addButton().setText("Stop all").setAction(() -> { + for (RunContentDescriptor sd: stoppableDescriptors) ExecutionManagerImpl.stopProcess(sd); TouchBarsManager.closeTouchBar(tb, true); }); diff --git a/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TBItem.java b/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TBItem.java index 0a9cdde273e8..51237958a722 100644 --- a/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TBItem.java +++ b/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TBItem.java @@ -6,8 +6,8 @@ import com.intellij.ui.mac.foundation.ID; import org.jetbrains.annotations.NotNull; abstract class TBItem { - final String myUid; - protected ID myNativePeer = ID.NIL; // java wrapper holds native object + final @NotNull String myUid; + protected @NotNull ID myNativePeer = ID.NIL; // java wrapper holds native object protected boolean myIsVisible = true; TBItem(@NotNull String uid) { myUid = uid; } diff --git a/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TBItemButton.java b/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TBItemButton.java index 80d71224f88c..322cf55f9f6a 100644 --- a/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TBItemButton.java +++ b/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TBItemButton.java @@ -12,34 +12,89 @@ public class TBItemButton extends TBItem { protected @Nullable NSTLibrary.Action myAction; protected @Nullable Icon myIcon; protected @Nullable String myText; - protected int myWidth; - protected int myFlags; + protected int myWidth = -1; + protected int myFlags = 0; private int myUpdateOptions; - protected TBItemButton(@NotNull String uid) { this(uid, null, null, null, -1, 0); } + TBItemButton(@NotNull String uid) { super(uid); } - TBItemButton(@NotNull String uid, Icon icon, String text, NSTLibrary.Action action) { this(uid, icon, text, action, -1, 0); } + public TBItemButton setIcon(Icon icon) { + if (!_equals(icon, myIcon)) { + myIcon = icon; + if (myNativePeer != ID.NIL) { + myUpdateOptions |= NSTLibrary.BUTTON_UPDATE_IMG; + _updateNativePeer(); + } + } - TBItemButton(@NotNull String uid, Icon icon, String text, NSTLibrary.Action action, int buttWidth) { this(uid, icon, text, action, buttWidth, 0); } - - TBItemButton(@NotNull String uid, Icon icon, String text, NSTLibrary.Action action, int buttWidth, int buttonFlags) { - super(uid); - myAction = action; - myIcon = icon; - myText = text; - myWidth = buttWidth; - myFlags = buttonFlags; + return this; } - void setWidth(int width) { _update(myIcon, myText, myAction, width, myFlags); } + public TBItemButton setText(String text) { + if (!Comparing.equal(text, myText)) { + myText = text; + if (myNativePeer != ID.NIL) { + myUpdateOptions |= NSTLibrary.BUTTON_UPDATE_TEXT; + _updateNativePeer(); + } + } + + return this; + } + + public TBItemButton setAction(NSTLibrary.Action action) { + if (action != myAction) { + myAction = action; + if (myNativePeer != ID.NIL) { + myUpdateOptions |= NSTLibrary.BUTTON_UPDATE_ACTION; + _updateNativePeer(); + } + } + + return this; + } + + public TBItemButton setWidth(int width) { + if (width != myWidth) { + myWidth = width; + if (myNativePeer != ID.NIL) { + myUpdateOptions |= NSTLibrary.BUTTON_UPDATE_WIDTH; + _updateNativePeer(); + } + } + + return this; + } // [-128, 127], 0 is the default value - void setPriority(byte prio) { _update(myIcon, myText, myAction, myWidth, myFlags | NSTLibrary.priority2mask(prio)); } + public TBItemButton setPriority(byte prio) { + final int flags = myFlags | NSTLibrary.priority2mask(prio); + if (flags != myFlags) { + myFlags = flags; + if (myNativePeer != ID.NIL) { + myUpdateOptions |= NSTLibrary.BUTTON_UPDATE_FLAGS; + _updateNativePeer(); + } + } + + return this; + } + + public TBItemButton setFlags(boolean isSelected, boolean isDisabled) { + int flags = _applyFlag(myFlags, isSelected, NSTLibrary.BUTTON_FLAG_SELECTED); + flags = _applyFlag(flags, isDisabled, NSTLibrary.BUTTON_FLAG_DISABLED); + if (flags != myFlags) { + myFlags = flags; + if (myNativePeer != ID.NIL) { + myUpdateOptions |= NSTLibrary.BUTTON_UPDATE_FLAGS; + _updateNativePeer(); + } + } + + return this; + } - void update(Icon icon, String text, NSTLibrary.Action action) { _update(icon, text, action, myWidth, myFlags); } - void update(Icon icon, String text) { _update(icon, text, myAction, myWidth, myFlags); } - void update(Icon icon) { _update(icon, myText, myAction, myWidth, myFlags); } void update(Icon icon, String text, boolean isSelected, boolean isDisabled) { int flags = _applyFlag(myFlags, isSelected, NSTLibrary.BUTTON_FLAG_SELECTED); flags = _applyFlag(flags, isDisabled, NSTLibrary.BUTTON_FLAG_DISABLED); diff --git a/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TouchBar.java b/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TouchBar.java index 0de8890664bf..957cf5f1662e 100644 --- a/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TouchBar.java +++ b/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TouchBar.java @@ -2,7 +2,6 @@ package com.intellij.ui.mac.touchbar; import com.intellij.icons.AllIcons; -import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.diagnostic.Logger; import com.intellij.ui.mac.foundation.ID; import org.jetbrains.annotations.NotNull; @@ -38,7 +37,11 @@ public class TouchBar implements NSTLibrary.ItemCreator { public TouchBar(@NotNull String touchbarName, boolean replaceEsc, boolean releaseOnClose) { myName = touchbarName; - myCustomEsc = replaceEsc ? new TBItemButton(genNewID("esc"), AllIcons.Actions.Cancel, null, this::_closeSelf) : null; + if (replaceEsc) + myCustomEsc = new TBItemButton(genNewID("esc")).setIcon(AllIcons.Actions.Cancel).setAction(this::_closeSelf); + else + myCustomEsc = null; + myNativePeer = NST.createTouchBar(touchbarName, this, myCustomEsc != null ? myCustomEsc.myUid : null); myReleaseOnClose = releaseOnClose; } @@ -78,31 +81,7 @@ public class TouchBar implements NSTLibrary.ItemCreator { // public TBItemButton addButton() { - final TBItemButton butt = new TBItemButton(genNewID("button"), null, null, null); - myItems.add(butt); - return butt; - } - - public TBItemButton addButton(Icon icon, String text, NSTLibrary.Action action) { - final TBItemButton butt = new TBItemButton(genNewID("button"), icon, text, action); - myItems.add(butt); - return butt; - } - - public TBItemButton addButton(Icon icon, String text, NSTLibrary.Action action, int buttonFlags) { - final TBItemButton butt = new TBItemButton(genNewID("button"), icon, text, action, -1, buttonFlags); - myItems.add(butt); - return butt; - } - - public TBItemButton addButton(Icon icon, String text, String actionId) { - final TBItemButton butt = new TBItemButton(genNewID("button"), icon, text, new PlatformAction(actionId)); - myItems.add(butt); - return butt; - } - - public TBItemButton addButton(Icon icon, String text, AnAction act) { - final TBItemButton butt = new TBItemButton(genNewID("button"), icon, text, new PlatformAction(act)); + final TBItemButton butt = new TBItemButton(genNewID("button")); myItems.add(butt); return butt; } @@ -119,14 +98,13 @@ public class TouchBar implements NSTLibrary.ItemCreator { return popover; } - public TBItemScrubber addScrubber(int width) { - final TBItemScrubber scrubber = new TBItemScrubber(genNewID("scrubber"), width); + public TBItemScrubber addScrubber() { + final int defaultScrubberWidth = 500; + final TBItemScrubber scrubber = new TBItemScrubber(genNewID("scrubber"), defaultScrubberWidth); myItems.add(scrubber); return scrubber; } - public TBItemScrubber addScrubber() { return addScrubber(500); } - public void addSpacing(boolean large) { final SpacingItem spacing = new SpacingItem(large ? "static_touchbar_item_large_space" : "static_touchbar_item_small_space"); myItems.add(spacing); diff --git a/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TouchBarsManager.java b/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TouchBarsManager.java index 092449e9b0fb..c2b6f39736a1 100644 --- a/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TouchBarsManager.java +++ b/platform/platform-impl/src/com/intellij/ui/mac/touchbar/TouchBarsManager.java @@ -275,18 +275,19 @@ public class TouchBarsManager { defIndex = c; continue; } - groupButtons.add(new TBItemButton( - "message_dlg_bar_group_item_" + c, - null, DialogWrapper.extractMnemonic(sb).second, NSTLibrary.run2act(actions[c]), -1, 0 - )); + groupButtons.add( + new TBItemButton("message_dlg_bar_group_item_" + c) + .setText(DialogWrapper.extractMnemonic(sb).second) + .setAction(NSTLibrary.run2act(actions[c]))); } Collections.reverse(groupButtons); if (defIndex >= 0) - groupButtons.add(new TBItemButton( - "message_dlg_bar_group_item_default", - null, DialogWrapper.extractMnemonic(buttons[defIndex]).second, NSTLibrary.run2act(actions[defIndex]), -1, NSTLibrary.BUTTON_FLAG_COLORED - )); + groupButtons.add( + new TBItemButton("message_dlg_bar_group_item_default") + .setText(DialogWrapper.extractMnemonic(buttons[defIndex]).second) + .setAction(NSTLibrary.run2act(actions[defIndex])) + .setFlags(true, false)); final TouchBar tb; try (NSAutoreleaseLock lock = new NSAutoreleaseLock()) { diff --git a/platform/platform-tests/testSrc/com/intellij/ui/mac/touchbar/TouchbarTest.java b/platform/platform-tests/testSrc/com/intellij/ui/mac/touchbar/TouchbarTest.java index 95477eaa0f3e..e8af3e7c6396 100644 --- a/platform/platform-tests/testSrc/com/intellij/ui/mac/touchbar/TouchbarTest.java +++ b/platform/platform-tests/testSrc/com/intellij/ui/mac/touchbar/TouchbarTest.java @@ -33,9 +33,9 @@ public class TouchbarTest { private static TouchBar _createTestButtonsTouchbar() { final TouchBar testTB = new TouchBar("test", false); - testTB.addButton(null, "test1", createPrintTextCallback("pressed test1 button")); - testTB.addButton(null, "test2", createPrintTextCallback("pressed test2 button")); - testTB.addButton(AllIcons.Toolwindows.ToolWindowRun, null, createPrintTextCallback("pressed image button")); + testTB.addButton().setText("test1").setAction(createPrintTextCallback("pressed test1 button")); + testTB.addButton().setText("test2").setAction(createPrintTextCallback("pressed test2 button")); + testTB.addButton().setIcon(AllIcons.Toolwindows.ToolWindowRun).setAction(createPrintTextCallback("pressed image button")); return testTB; } @@ -43,7 +43,7 @@ public class TouchbarTest { final TouchBar testTB = new TouchBar("test", false); testTB.addSpacing(true); - final TBItemScrubber scrubber = testTB.addScrubber(450); + final TBItemScrubber scrubber = testTB.addScrubber(); List scrubberItems = new ArrayList<>(); for (int c = 0; c < 11; ++c) { String txt; @@ -61,18 +61,18 @@ public class TouchbarTest { private static TouchBar _createTestAllTouchbar() { final TouchBar testTB = new TouchBar("test", false); testTB.addSpacing(true); - testTB.addButton(null, "test1", createPrintTextCallback("pressed test1 button")); - testTB.addButton(null, "test2", createPrintTextCallback("pressed test2 button")); + testTB.addButton().setText("test1").setAction(createPrintTextCallback("pressed test1 button")); + testTB.addButton().setText("test2").setAction(createPrintTextCallback("pressed test2 button")); testTB.addSpacing(false); - testTB.addButton(AllIcons.Toolwindows.ToolWindowRun, null, createPrintTextCallback("pressed image button")); + testTB.addButton().setIcon(AllIcons.Toolwindows.ToolWindowRun).setAction(createPrintTextCallback("pressed image button")); final TouchBar tapHoldTB = new TouchBar("test_popover_tap_and_hold", false); final TouchBar expandTB = new TouchBar("test_configs_popover_expand", false); final int configPopoverWidth = 143; testTB.addPopover(AllIcons.Toolwindows.ToolWindowBuild, "test-popover", configPopoverWidth, expandTB, tapHoldTB); - expandTB.addButton(AllIcons.Toolwindows.ToolWindowDebugger, null, createPrintTextCallback("pressed pimage button")); - final TBItemScrubber scrubber = expandTB.addScrubber(450); + expandTB.addButton().setIcon(AllIcons.Toolwindows.ToolWindowDebugger).setAction(createPrintTextCallback("pressed popover-image button")); + final TBItemScrubber scrubber = expandTB.addScrubber(); List scrubberItems = new ArrayList<>(); for (int c = 0; c < 15; ++c) { String txt; @@ -84,7 +84,7 @@ public class TouchbarTest { } expandTB.selectVisibleItemsToShow(); - tapHoldTB.addButton(AllIcons.Toolwindows.ToolWindowPalette, null, createPrintTextCallback("pressed pimage button")); + tapHoldTB.addButton().setIcon(AllIcons.Toolwindows.ToolWindowPalette).setAction(createPrintTextCallback("pressed tap-hold-image button")); tapHoldTB.selectVisibleItemsToShow(); return testTB;