some work, still need to fix the race condition on emitting

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2021-04-12 11:51:18 -04:00
1 parent 99a464c61d
commit a38cb10720
17 files changed
+4341 -108

No files matched your search

@@ -45,25 +45,26 @@ export class KubernetesCluster implements CatalogEntity {
}
async onContextMenuOpen(context: CatalogEntityContextMenuContext) {
context.menuItems = [
context.menuItems.push(
{
icon: "settings",
icon: "Settings",
title: "Settings",
onClick: async () => context.navigate(`/cluster/${this.metadata.uid}/settings`)
},
{
icon: "delete",
icon: "Delete",
title: "Delete",
onClick: async () => clusterStore.removeById(this.metadata.uid),
confirm: {
message: `Remove Kubernetes Cluster "${this.metadata.name} from Lens?`
}
},
];
);
if (this.status.active) {
context.menuItems.unshift({
icon: "link_off",
icon: "LinkOff",
title: "Disconnect",
onClick: async () => {
clusterStore.deactivate(this.metadata.uid);
@@ -72,9 +73,9 @@ export class KubernetesCluster implements CatalogEntity {
});
}
const category = catalogCategoryRegistry.getCategoryForEntity<KubernetesClusterCategory>(this);
if (category) category.emit("contextMenuOpen", this, context);
catalogCategoryRegistry
.getCategoryForEntity<KubernetesClusterCategory>(this)
?.emit("contextMenuOpen", );
}
}
+4 -2
View File
@@ -1,3 +1,5 @@
import Icons from "@material-ui/icons";
export interface CatalogCategoryVersion {
name: string;
entityClass: { new(data: CatalogEntityData): CatalogEntity };
@@ -43,7 +45,7 @@ export interface CatalogEntityActionContext {
}
export type CatalogEntityContextMenu = {
icon: string;
icon: keyof typeof Icons;
title: string;
onClick: () => Promise<void>;
confirm?: {
@@ -53,7 +55,7 @@ export type CatalogEntityContextMenu = {
export interface CatalogEntityContextMenuContext {
navigate: (url: string) => void;
menuItems: CatalogEntityContextMenu[];
menuItems: Promise<CatalogEntityContextMenu[] | CatalogEntityContextMenu>[];
}
export type CatalogEntityData = {
+1 -3
View File
@@ -1,10 +1,8 @@
import { computed } from "mobx";
import { CatalogEntity } from "../../common/catalog-entity";
import { catalogEntityRegistry as registry } from "../../common/catalog-entity-registry";
export class CatalogEntityRegistry {
@computed getItemsForApiKind<T extends CatalogEntity>(apiVersion: string, kind: string): T[] {
getItemsForApiKind<T extends CatalogEntity>(apiVersion: string, kind: string): T[] {
return registry.getItemsForApiKind<T>(apiVersion, kind);
}
}
+1 -1
View File
@@ -85,7 +85,7 @@ export class Cluster implements ClusterModel, ClusterState {
whenReady = when(() => this.ready);
/**
* Is cluster object initializinng on-going
* Is cluster object initializing on-going
*
* @observable
*/
+41
View File
@@ -0,0 +1,41 @@
import React from "react";
import { remote } from "electron";
import * as Icons from "@material-ui/icons";
import { render, cleanup } from "@testing-library/react";
type IconName = keyof typeof Icons;
export class NativeImageCache {
private static cache = new Map<IconName, Electron.NativeImage>();
static fromName(name: IconName): Electron.NativeImage {
const prev = NativeImageCache.cache.get(name);
if (prev) {
return prev;
}
const Icon = Icons[name];
if (!Icon) {
throw new Error(`Invalid icon name: ${name}`);
}
cleanup();
const svgPath = render(<Icon />).baseElement.querySelector("svg").querySelector("path").getAttribute("d");
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.width = 24;
canvas.height = 24;
ctx.fillStyle = remote.nativeTheme.shouldUseDarkColors ? "white" : "black"; // swap for contrast
ctx.fill(new Path2D(svgPath));
const png = canvas.toDataURL("image/png");
const icon = remote.nativeImage.createFromDataURL(png).resize({ width: 16, height: 16 });
NativeImageCache.cache.set(name, icon);
return icon;
}
}
+1
View File
@@ -0,0 +1 @@
export * from "./from-material-ui";
+21 -23
View File
@@ -7,8 +7,6 @@ import { CatalogEntityItem, CatalogEntityStore } from "./catalog-entity.store";
import { navigate } from "../../navigation";
import { kebabCase } from "lodash";
import { PageLayout } from "../layout/page-layout";
import { MenuItem, MenuActions } from "../menu";
import { Icon } from "../icon";
import { CatalogEntityContextMenu, CatalogEntityContextMenuContext, catalogEntityRunContext } from "../../api/catalog-entity";
import { Badge } from "../badge";
import { hotbarStore } from "../../../common/hotbar-store";
@@ -124,27 +122,27 @@ export class Catalog extends React.Component {
@autobind()
renderItemMenu(item: CatalogEntityItem) {
const onOpen = async () => {
await item.onContextMenuOpen(this.contextMenu);
};
// const onOpen = async () => {
// await item.onContextMenuOpen(this.contextMenu);
// };
return (
<MenuActions onOpen={() => onOpen()}>
<MenuItem key="add-to-hotbar" onClick={() => this.addToHotbar(item) }>
<Icon material="add" small interactive={true} title="Add to hotbar"/> Add to Hotbar
</MenuItem>
<MenuItem key="remove-from-hotbar" onClick={() => this.removeFromHotbar(item) }>
<Icon material="clear" small interactive={true} title="Remove from hotbar"/> Remove from Hotbar
</MenuItem>
{ this.contextMenu.menuItems.map((menuItem, index) => {
return (
<MenuItem key={index} onClick={() => this.onMenuItemClick(menuItem)}>
<Icon material={menuItem.icon} small interactive={true} title={menuItem.title}/> {menuItem.title}
</MenuItem>
);
})}
</MenuActions>
);
// return (
// <MenuActions onOpen={() => onOpen()}>
// <MenuItem key="add-to-hotbar" onClick={() => this.addToHotbar(item) }>
// <Icon material="add" small interactive={true} title="Add to hotbar"/> Add to Hotbar
// </MenuItem>
// <MenuItem key="remove-from-hotbar" onClick={() => this.removeFromHotbar(item) }>
// <Icon material="clear" small interactive={true} title="Remove from hotbar"/> Remove from Hotbar
// </MenuItem>
// { this.contextMenu.menuItems.map((menuItem, index) => {
// return (
// <MenuItem key={index} onClick={() => this.onMenuItemClick(menuItem)}>
// <Icon material={menuItem.icon} small interactive={true} title={menuItem.title}/> {menuItem.title}
// </MenuItem>
// );
// })}
// </MenuActions>
// );
}
render() {
@@ -183,7 +181,7 @@ export class Catalog extends React.Component {
{ title: item.phase, className: kebabCase(item.phase) }
]}
onDetails={(item: CatalogEntityItem) => this.onDetails(item) }
renderItemMenu={this.renderItemMenu}
// renderItemMenu={this.renderItemMenu}
addRemoveButtons={{
addTooltip: "Add Kubernetes Cluster",
onAdd: () => navigate(addClusterURL()),
+42 -45
View File
@@ -6,12 +6,11 @@ import { cssNames, IClassName } from "../../utils";
import { Tooltip } from "../tooltip";
import { Avatar } from "@material-ui/core";
import { CatalogEntity, CatalogEntityContextMenu, CatalogEntityContextMenuContext } from "../../../common/catalog-entity";
import { Menu, MenuItem } from "../menu";
import { Icon } from "../icon";
import { observable } from "mobx";
import { navigate } from "../../navigation";
import { hotbarStore } from "../../../common/hotbar-store";
import { ConfirmDialog } from "../confirm-dialog";
import contextMenu from "electron-context-menu";
import { navigate } from "../../navigation";
import { NativeImageCache } from "../../Icons";
interface Props extends DOMAttributes<HTMLElement> {
entity: CatalogEntity;
@@ -23,17 +22,7 @@ interface Props extends DOMAttributes<HTMLElement> {
@observer
export class HotbarIcon extends React.Component<Props> {
@observable.deep private contextMenu: CatalogEntityContextMenuContext;
@observable menuOpen = false;
componentDidMount() {
this.contextMenu = {
menuItems: [],
navigate: (url: string) => navigate(url)
};
}
get iconString() {
get iconContent() {
let splittedName = this.props.entity.metadata.name.split(" ");
if (splittedName.length === 1) {
@@ -55,10 +44,6 @@ export class HotbarIcon extends React.Component<Props> {
}
}
toggleMenu() {
this.menuOpen = !this.menuOpen;
}
removeFromHotbar(item: CatalogEntity) {
const hotbar = hotbarStore.getByName("default"); // FIXME
@@ -86,6 +71,30 @@ export class HotbarIcon extends React.Component<Props> {
}
}
onAvatarClick = async () => {
const menuContext: CatalogEntityContextMenuContext = {
menuItems: [],
navigate: (url: string) => navigate(url)
};
await this.props.entity.onContextMenuOpen(menuContext);
contextMenu({
menu: () => ([
{
label: "Remove from Hotbar",
icon: NativeImageCache.fromName("Clear"),
click: () => this.removeFromHotbar(this.props.entity),
},
...menuItems.map(item => ({
label: item.title,
icon: NativeImageCache.fromName(item.icon),
click: () => item.onClick(),
})),
]),
});
};
render() {
const {
entity, errorClass, isActive,
@@ -96,35 +105,23 @@ export class HotbarIcon extends React.Component<Props> {
interactive: true,
active: isActive,
});
const onOpen = async () => {
await entity.onContextMenuOpen(this.contextMenu);
this.toggleMenu();
};
const avatarClassName = isActive ? "active" : "default";
return (
<div className={className}>
<Tooltip targetId={entityIconId}>{entity.metadata.name}</Tooltip>
<Avatar {...elemProps} id={entityIconId} variant="square" className={isActive ? "active" : "default"}>{this.iconString}</Avatar>
<Menu
usePortal={false}
htmlFor={entityIconId}
className="HotbarIconMenu"
isOpen={this.menuOpen}
toggleEvent="contextmenu"
position={{right: true, bottom: true }} // FIXME: position does not work
open={() => onOpen()}
close={() => this.toggleMenu()}>
<MenuItem key="remove-from-hotbar" onClick={() => this.removeFromHotbar(entity) }>
<Icon material="clear" small interactive={true} title="Remove from hotbar"/> Remove from Hotbar
</MenuItem>
{ this.contextMenu && this.contextMenu.menuItems.map((menuItem) => {
return (
<MenuItem key={menuItem.title} onClick={() => this.onMenuItemClick(menuItem) }>
<Icon material={menuItem.icon} small interactive={true} title={menuItem.title}/> {menuItem.title}
</MenuItem>
);
})}
</Menu>
<Tooltip targetId={entityIconId}>
{entity.metadata.name}
</Tooltip>
<div onContextMenu={this.onAvatarClick}>
<Avatar
{...elemProps}
id={entityIconId}
variant="square"
className={avatarClassName}
>
{this.iconContent}
</Avatar>
</div>
{children}
</div>
);