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 visibility through Extension API

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

View File

@ -0,0 +1,78 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { IObservableValue } from "mobx";
import { computed, runInAction, observable } from "mobx";
import type { ApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder";
import { getApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder";
import { getExtensionFakeFor } from "../../renderer/components/test-utils/get-extension-fake";
describe("preferences: extension adding tray items", () => {
describe("when extension with tray items is enabled", () => {
let builder: ApplicationBuilder;
let someObservable: IObservableValue<boolean>;
beforeEach(async () => {
builder = getApplicationBuilder();
await builder.render();
builder.preferences.navigate();
const getExtensionFake = getExtensionFakeFor(builder);
someObservable = observable.box(false);
const testExtension = getExtensionFake({
id: "some-extension-id",
name: "some-extension",
mainOptions: {
trayMenus: [
{
label: "some-controlled-visibility",
click: () => {},
visible: computed(() => someObservable.get()),
},
{
label: "some-uncontrolled-visibility",
click: () => {},
},
],
},
});
builder.extensions.enable(testExtension);
});
it("shows item which doesn't control the visibility", () => {
expect(
builder.tray.get(
"some-uncontrolled-visibility-tray-menu-item-for-extension-some-extension",
),
).not.toBeNull();
});
it("does not show hidden item", () => {
expect(
builder.tray.get(
"some-controlled-visibility-tray-menu-item-for-extension-some-extension",
),
).toBeNull();
});
it("when item becomes visible, shows the item", () => {
runInAction(() => {
someObservable.set(true);
});
expect(
builder.tray.get(
"some-controlled-visibility-tray-menu-item-for-extension-some-extension",
),
).not.toBeNull();
});
});
});

View File

@ -71,7 +71,14 @@ const toItemInjectablesFor = (extension: LensMainExtension, withErrorLoggingFor:
},
enabled: computed(() => registration.enabled ?? true),
visible: computed(() => true),
visible: computed(() => {
if (!registration.visible) {
return true;
}
return registration.visible.get();
}),
}),
injectionToken: trayMenuItemInjectionToken,

View File

@ -3,6 +3,8 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { IComputedValue } from "mobx";
export interface TrayMenuRegistration {
label?: string;
click?: (menuItem: TrayMenuRegistration) => void;
@ -11,4 +13,5 @@ export interface TrayMenuRegistration {
toolTip?: string;
enabled?: boolean;
submenu?: TrayMenuRegistration[];
visible?: IComputedValue<boolean>;
}