Skip to content

Commit

Permalink
Fix shortcut keys on commands so they are according to Cogs standard
Browse files Browse the repository at this point in the history
  • Loading branch information
nilscognite committed Aug 20, 2024
1 parent e8cab8c commit 97fcba3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
11 changes: 6 additions & 5 deletions react-components/src/architecture/base/commands/BaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,19 @@ export abstract class BaseCommand {
return translate(key, fallback);
}

public getShortCutKeys(): string | undefined {
public getShortCutKeys(): string[] | undefined {
const key = this.shortCutKey;
if (key === undefined) {
return undefined;
}
let result = '';
const keys: string[] = [];
if (this.shortCutKeyOnCtrl) {
result += 'Ctrl+';
keys.push('Ctrl');
}
if (this.shortCutKeyOnShift) {
result += 'Shift+';
keys.push('Shift');
}
return result + key;
keys.push(key);
return keys;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ export const CommandButton = ({
}
const placement = getTooltipPlacement(isHorizontal);
const label = command.getLabel(t);
const shortcut = command.getShortCutKeys();

return (
<CogsTooltip
content={<LabelWithShortcut label={label} shortcut={shortcut} />}
content={<LabelWithShortcut label={label} command={command} />}
disabled={label === undefined}
appendTo={document.body}
placement={placement}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export const FilterButton = ({
}
const placement = getTooltipPlacement(isHorizontal);
const label = usedInSettings ? undefined : command.getLabel(t);
const shortcut = command.getShortCutKeys();
const flexDirection = getFlexDirection(isHorizontal);

const children = command.children;
Expand All @@ -102,7 +101,7 @@ export const FilterButton = ({
}
return (
<CogsTooltip
content={<LabelWithShortcut label={label} shortcut={shortcut} />}
content={<LabelWithShortcut label={label} command={command} />}
disabled={usedInSettings || label === undefined}
appendTo={document.body}
placement={placement}>
Expand Down
13 changes: 10 additions & 3 deletions react-components/src/components/Architecture/LabelWithShortcut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@ import React from 'react';
import styled from 'styled-components';

import { Shortcut } from '@cognite/cogs.js';
import { type BaseCommand } from '../../architecture/base/commands/BaseCommand';

type LabelWithShortcutProps = {
label?: string;
shortcut?: string;
command?: BaseCommand;
inverted?: boolean;
};

export const LabelWithShortcut: React.FC<LabelWithShortcutProps> = ({ label, shortcut }) => {
export const LabelWithShortcut: React.FC<LabelWithShortcutProps> = ({
label,
command,
inverted = true
}) => {
if (label === undefined) {
return <></>;
}
const keys = command?.getShortCutKeys();
return (
<Container key={label}>
<Label>{label}</Label>
{shortcut !== undefined && <Shortcut keys={[shortcut]} inverted />}
{keys !== undefined && <Shortcut keys={keys} inverted={inverted} />}
</Container>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,13 @@ export const OptionButton = ({
}
const placement = getTooltipPlacement(isHorizontal);
const label = usedInSettings ? undefined : command.getLabel(t);
const shortcut = command.getShortCutKeys();
const flexDirection = getFlexDirection(isHorizontal);
const children = command.children;
const selectedLabel = command.selectedChild?.getLabel(t);

return (
<CogsTooltip
content={<LabelWithShortcut label={label} shortcut={shortcut} />}
content={<LabelWithShortcut label={label} command={command} />}
disabled={usedInSettings || label === undefined}
appendTo={document.body}
placement={placement}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ export const SettingsButton = ({
}
const placement = getTooltipPlacement(isHorizontal);
const label = command.getLabel(t);
const shortcut = command.getShortCutKeys();
const flexDirection = getFlexDirection(isHorizontal);
const children = command.children;

return (
<CogsTooltip
content={<LabelWithShortcut label={label} shortcut={shortcut} />}
content={<LabelWithShortcut label={label} command={command} />}
disabled={label === undefined}
appendTo={document.body}
placement={placement}>
Expand Down Expand Up @@ -157,7 +156,6 @@ function createButton(command: BaseCommand, t: TranslateDelegate): ReactElement
return <></>;
}
const label = command.getLabel(t);
const shortcut = command.getShortCutKeys();
return (
<Menu.Item
key={command.uniqueId}
Expand All @@ -170,7 +168,7 @@ function createButton(command: BaseCommand, t: TranslateDelegate): ReactElement
command.invoke();
setChecked(command.isChecked);
}}>
<LabelWithShortcut label={label} shortcut={shortcut} />
<LabelWithShortcut label={label} command={command} inverted={false} />
</Menu.Item>
);
}
Expand Down

0 comments on commit 97fcba3

Please sign in to comment.