1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Expose a way to control tray item enableablity through Extension API

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-07-06 09:26:54 +03:00
parent 5253df0647
commit 7d7ae86245
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
3 changed files with 90 additions and 7 deletions

View File

@ -11,7 +11,8 @@ import { getExtensionFakeFor } from "../../renderer/components/test-utils/get-ex
describe("preferences: extension adding tray items", () => { describe("preferences: extension adding tray items", () => {
describe("when extension with tray items is enabled", () => { describe("when extension with tray items is enabled", () => {
let builder: ApplicationBuilder; let builder: ApplicationBuilder;
let someObservable: IObservableValue<boolean>; let someObservableForVisibility: IObservableValue<boolean>;
let someObservableForEnabled: IObservableValue<boolean>;
beforeEach(async () => { beforeEach(async () => {
builder = getApplicationBuilder(); builder = getApplicationBuilder();
@ -22,7 +23,8 @@ describe("preferences: extension adding tray items", () => {
const getExtensionFake = getExtensionFakeFor(builder); const getExtensionFake = getExtensionFakeFor(builder);
someObservable = observable.box(false); someObservableForVisibility = observable.box(false);
someObservableForEnabled = observable.box(false);
const testExtension = getExtensionFake({ const testExtension = getExtensionFake({
id: "some-extension-id", id: "some-extension-id",
@ -33,13 +35,36 @@ describe("preferences: extension adding tray items", () => {
{ {
label: "some-controlled-visibility", label: "some-controlled-visibility",
click: () => {}, click: () => {},
visible: computed(() => someObservable.get()), visible: computed(() => someObservableForVisibility.get()),
}, },
{ {
label: "some-uncontrolled-visibility", label: "some-uncontrolled-visibility",
click: () => {}, click: () => {},
}, },
{
label: "some-controlled-enabled",
click: () => {},
enabled: computed(() => someObservableForEnabled.get()),
},
{
label: "some-uncontrolled-enabled",
click: () => {},
},
{
label: "some-statically-enabled",
click: () => {},
enabled: true,
},
{
label: "some-statically-disabled",
click: () => {},
enabled: false,
},
], ],
}, },
}); });
@ -65,7 +90,7 @@ describe("preferences: extension adding tray items", () => {
it("when item becomes visible, shows the item", () => { it("when item becomes visible, shows the item", () => {
runInAction(() => { runInAction(() => {
someObservable.set(true); someObservableForVisibility.set(true);
}); });
expect( expect(
@ -74,5 +99,52 @@ describe("preferences: extension adding tray items", () => {
), ),
).not.toBeNull(); ).not.toBeNull();
}); });
it("given item does not have enabled status, item is enabled by default", () => {
const item = builder.tray.get(
"some-uncontrolled-enabled-tray-menu-item-for-extension-some-extension",
);
expect(item?.enabled).toBe(true);
});
describe("given item has controlled enabled status and is disabled", () => {
it("is disabled", () => {
const item = builder.tray.get(
"some-controlled-enabled-tray-menu-item-for-extension-some-extension",
);
expect(item?.enabled).toBe(false);
});
it("when item becomes enabled, items is enabled", () => {
runInAction(() => {
someObservableForEnabled.set(true);
});
const item = builder.tray.get(
"some-controlled-enabled-tray-menu-item-for-extension-some-extension",
);
expect(item?.enabled).toBe(true);
});
});
it("given item is statically enabled, item is enabled", () => {
const item = builder.tray.get(
"some-statically-enabled-tray-menu-item-for-extension-some-extension",
);
expect(item?.enabled).toBe(true);
});
it("given item is statically disabled, item is disabled", () => {
const item = builder.tray.get(
"some-statically-disabled-tray-menu-item-for-extension-some-extension",
);
expect(item?.enabled).toBe(false);
});
}); });
}); });

View File

@ -16,6 +16,7 @@ import { withErrorSuppression } from "../../../common/utils/with-error-suppressi
import type { WithErrorLoggingFor } from "../../../common/utils/with-error-logging/with-error-logging.injectable"; import type { WithErrorLoggingFor } from "../../../common/utils/with-error-logging/with-error-logging.injectable";
import withErrorLoggingInjectable from "../../../common/utils/with-error-logging/with-error-logging.injectable"; import withErrorLoggingInjectable from "../../../common/utils/with-error-logging/with-error-logging.injectable";
import getRandomIdInjectable from "../../../common/utils/get-random-id.injectable"; import getRandomIdInjectable from "../../../common/utils/get-random-id.injectable";
import { isBoolean } from "../../../common/utils";
const trayMenuItemRegistratorInjectable = getInjectable({ const trayMenuItemRegistratorInjectable = getInjectable({
id: "tray-menu-item-registrator", id: "tray-menu-item-registrator",
@ -64,13 +65,23 @@ const toItemInjectablesFor = (extension: LensMainExtension, withErrorLoggingFor:
// TODO: Find out how to improve typing so that instead of // TODO: Find out how to improve typing so that instead of
// x => withErrorSuppression(x) there could only be withErrorSuppression // x => withErrorSuppression(x) there could only be withErrorSuppression
x => withErrorSuppression(x), (x) => withErrorSuppression(x),
); );
return decorated(registration); return decorated(registration);
}, },
enabled: computed(() => registration.enabled ?? true), enabled: computed(() => {
if (registration.enabled === undefined) {
return true;
}
if (isBoolean(registration.enabled)) {
return registration.enabled;
}
return registration.enabled.get();
}),
visible: computed(() => { visible: computed(() => {
if (!registration.visible) { if (!registration.visible) {

View File

@ -11,7 +11,7 @@ export interface TrayMenuRegistration {
id?: string; id?: string;
type?: "normal" | "separator" | "submenu"; type?: "normal" | "separator" | "submenu";
toolTip?: string; toolTip?: string;
enabled?: boolean; enabled?: boolean | IComputedValue<boolean>;
submenu?: TrayMenuRegistration[]; submenu?: TrayMenuRegistration[];
visible?: IComputedValue<boolean>; visible?: IComputedValue<boolean>;
} }