IDEA-163208: refactoring - simplify api

This commit is contained in:
Artem Bochkarev
2018-06-12 19:00:12 +07:00
parent 9d3f38dd90
commit 81bb385439
6 changed files with 105 additions and 71 deletions
@@ -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);
});
@@ -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; }
@@ -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);
@@ -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);
@@ -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()) {
@@ -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<TBItemScrubber.ItemData> 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<TBItemScrubber.ItemData> 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;