From 0f587a40a3fdc1ad85712b5b2669331fd843afbb Mon Sep 17 00:00:00 2001 From: Flanker-MSFT Date: Mon, 12 Jun 2023 14:24:34 +0800 Subject: [PATCH] Support toggle action visible status based on action place --- .../toolkit/lib/common/action/Action.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/azure-toolkit-libs/azure-toolkit-common-lib/src/main/java/com/microsoft/azure/toolkit/lib/common/action/Action.java b/azure-toolkit-libs/azure-toolkit-common-lib/src/main/java/com/microsoft/azure/toolkit/lib/common/action/Action.java index 4449a62d4..6a00b959b 100644 --- a/azure-toolkit-libs/azure-toolkit-common-lib/src/main/java/com/microsoft/azure/toolkit/lib/common/action/Action.java +++ b/azure-toolkit-libs/azure-toolkit-common-lib/src/main/java/com/microsoft/azure/toolkit/lib/common/action/Action.java @@ -7,8 +7,6 @@ import com.microsoft.azure.toolkit.lib.common.bundle.AzureString; import com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException; -import com.microsoft.azure.toolkit.lib.common.model.AbstractAzResource; -import com.microsoft.azure.toolkit.lib.common.model.AbstractAzResourceModule; import com.microsoft.azure.toolkit.lib.common.model.AzResource; import com.microsoft.azure.toolkit.lib.common.model.AzResourceModule; import com.microsoft.azure.toolkit.lib.common.model.Emulatable; @@ -50,11 +48,13 @@ public class Action extends OperationBase implements Cloneable { public static final Action.Id OPEN_AZURE_SETTINGS = Action.Id.of("user/common.open_azure_settings"); public static final Action.Id DISABLE_AUTH_CACHE = Action.Id.of("user/account.disable_auth_cache"); + public static final String COMMON = "common"; + @Nonnull private final Id id; @Nonnull private Predicate enableWhen = o -> true; - private Predicate visibleWhen = o -> true; + private BiPredicate visibleWhen = (o, place) -> true; private Function iconProvider; private Function labelProvider; private Function titleProvider; @@ -64,6 +64,7 @@ public class Action extends OperationBase implements Cloneable { private List> titleParamProviders = new ArrayList<>(); private D source; + private String place; @Setter private Predicate authRequiredProvider = Action::isAuthRequiredForAzureResource; @@ -87,11 +88,15 @@ public String getId() { return this.id.id; } - @Nonnull public IView.Label getView(D s) { + return getView(s, COMMON); + } + + @Nonnull + public IView.Label getView(D s, final String place) { final D source = Optional.ofNullable(this.source).orElse(s); try { - final boolean visible = this.visibleWhen.test(source); + final boolean visible = this.visibleWhen.test(source, place); if (visible) { final String label = this.labelProvider.apply(source); final String icon = Optional.ofNullable(this.iconProvider).map(p -> p.apply(source)).orElse(null); @@ -110,7 +115,7 @@ public IView.Label getView(D s) { @SuppressWarnings("unchecked") public BiConsumer getHandler(D s, Object e) { final D source = Optional.ofNullable(this.source).orElse(s); - if (!this.visibleWhen.test(source) && !this.enableWhen.test(source)) { + if (!this.visibleWhen.test(source, COMMON) && !this.enableWhen.test(source)) { return null; } for (int i = this.handlers.size() - 1; i >= 0; i--) { @@ -203,6 +208,11 @@ public Action enableWhen(@Nonnull Predicate enableWhen) { } public Action visibleWhen(@Nonnull Predicate visibleWhen) { + this.visibleWhen = (object, ignore) -> visibleWhen.test(object); + return this; + } + + public Action visibleWhen(@Nonnull BiPredicate visibleWhen) { this.visibleWhen = visibleWhen; return this; }