mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Remove duplication for getting visible windows
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
parent
d0b9a0d8f0
commit
8f248a7f23
@ -5,23 +5,19 @@
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { beforeQuitOfFrontEndInjectionToken } from "../../../start-main-application/runnable-tokens/before-quit-of-front-end-injection-token";
|
||||
import electronAppInjectable from "../../electron-app.injectable";
|
||||
import { lensWindowInjectionToken } from "../../../start-main-application/lens-window/application-window/lens-window-injection-token";
|
||||
import { pipeline } from "@ogre-tools/fp";
|
||||
import { filter, isEmpty } from "lodash/fp";
|
||||
import { isEmpty } from "lodash/fp";
|
||||
import getVisibleWindowsInjectable from "../../../start-main-application/lens-window/get-visible-windows.injectable";
|
||||
|
||||
const hideDockForLastClosedWindowInjectable = getInjectable({
|
||||
id: "hide-dock-when-there-are-no-windows",
|
||||
|
||||
instantiate: (di) => {
|
||||
const app = di.inject(electronAppInjectable);
|
||||
const getLensWindows = () => di.injectMany(lensWindowInjectionToken);
|
||||
const getVisibleWindows = di.inject(getVisibleWindowsInjectable);
|
||||
|
||||
return {
|
||||
run: () => {
|
||||
const visibleWindows = pipeline(
|
||||
getLensWindows(),
|
||||
filter(window => !!window.isVisible),
|
||||
);
|
||||
const visibleWindows = getVisibleWindows();
|
||||
|
||||
if (isEmpty(visibleWindows)) {
|
||||
app.dock?.hide();
|
||||
|
||||
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { pipeline } from "@ogre-tools/fp";
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { filter } from "lodash/fp";
|
||||
import { lensWindowInjectionToken } from "./application-window/lens-window-injection-token";
|
||||
|
||||
const getVisibleWindowsInjectable = getInjectable({
|
||||
id: "get-visible-windows",
|
||||
|
||||
instantiate: (di) => {
|
||||
const getAllLensWindows = () => di.injectMany(lensWindowInjectionToken);
|
||||
|
||||
return () =>
|
||||
pipeline(
|
||||
getAllLensWindows(),
|
||||
filter((lensWindow) => !!lensWindow.isVisible),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
export default getVisibleWindowsInjectable;
|
||||
@ -2,31 +2,24 @@
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { lensWindowInjectionToken } from "../../start-main-application/lens-window/application-window/lens-window-injection-token";
|
||||
import { pipeline } from "@ogre-tools/fp";
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { filter } from "lodash/fp";
|
||||
import { messageToChannelInjectionToken } from "../../../common/utils/channel/message-to-channel-injection-token";
|
||||
import type { MessageChannel } from "../../../common/utils/channel/message-channel-injection-token";
|
||||
import { tentativeStringifyJson } from "../../../common/utils/tentative-stringify-json";
|
||||
import getVisibleWindowsInjectable from "../../start-main-application/lens-window/get-visible-windows.injectable";
|
||||
|
||||
const messageToChannelInjectable = getInjectable({
|
||||
id: "message-to-channel",
|
||||
|
||||
instantiate: (di) => {
|
||||
const getAllLensWindows = () => di.injectMany(lensWindowInjectionToken);
|
||||
const getVisibleWindows = di.inject(getVisibleWindowsInjectable);
|
||||
|
||||
// TODO: Figure out way to improve typing in internals
|
||||
// Notice that this should be injected using "messageToChannelInjectionToken" which is typed correctly.
|
||||
return (channel: MessageChannel<any>, message?: unknown) => {
|
||||
const stringifiedMessage = tentativeStringifyJson(message);
|
||||
|
||||
const visibleWindows = pipeline(
|
||||
getAllLensWindows(),
|
||||
filter((lensWindow) => !!lensWindow.isVisible),
|
||||
);
|
||||
|
||||
visibleWindows.forEach((lensWindow) =>
|
||||
getVisibleWindows().forEach((lensWindow) =>
|
||||
lensWindow.send({ channel: channel.id, data: stringifiedMessage ? [stringifiedMessage] : [] }),
|
||||
);
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user