Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ALL keycode tab and search #230

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/components/inputs/autocomplete-keycode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ const KeycodeLabel = styled.span`
color: var(--color_label);
display: flex;
`;

const Item = styled.div<{$selected?: boolean}>`
box-sizing: border-box;
min-width: 200px;
min-width: 150px;
max-width: 300px;
padding: 5px 10px;
display: flex;
justify-content: space-between;
Expand Down
1 change: 1 addition & 0 deletions src/components/inputs/custom-keycode-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
PromptText,
RowDiv,
} from './dialog-base';
import { getSelectedLanguage } from 'src/store/settingsSlice';

const AutocompleteContainer = styled.ul`
position: fixed;
Expand Down
49 changes: 46 additions & 3 deletions src/components/panes/configure-panes/keycode.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {FC, useState, useEffect, useMemo} from 'react';

Check failure on line 1 in src/components/panes/configure-panes/keycode.tsx

View workflow job for this annotation

GitHub Actions / Build (20.x)

Expected 1-2 arguments, but got 3.
import styled from 'styled-components';
import {Button} from '../../inputs/button';
import {KeycodeModal} from '../../inputs/custom-keycode-modal';
Expand Down Expand Up @@ -42,8 +42,12 @@
disableGlobalHotKeys,
enableGlobalHotKeys,
getDisableFastRemap,
getSelectedLanguage,
} from 'src/store/settingsSlice';
import {getNextKey} from 'src/utils/keyboard-rendering';
import TextInput from 'src/components/inputs/text-input';
import { LANGUAGES } from 'src/utils/languages';

const KeycodeList = styled.div`
display: grid;
grid-template-columns: repeat(auto-fill, 64px);
Expand Down Expand Up @@ -95,6 +99,7 @@
`;

const KeycodeContainer = styled.div`
text-align: center;
padding: 12px;
padding-bottom: 30px;
`;
Expand Down Expand Up @@ -151,10 +156,11 @@
const selectedKeyDefinitions = useAppSelector(getSelectedKeyDefinitions);
const {basicKeyToByte} = useAppSelector(getBasicKeyToByte);
const macroCount = useAppSelector(getMacroCount);

const language = useAppSelector(getSelectedLanguage);

const KeycodeCategories = useMemo(
() => generateKeycodeCategories(basicKeyToByte, macroCount),
[basicKeyToByte, macroCount],
() => generateKeycodeCategories(basicKeyToByte, macroCount, language),
[basicKeyToByte, macroCount, language],
);

// TODO: improve typing so we can get rid of this
Expand Down Expand Up @@ -275,6 +281,13 @@

const renderKeycode = (keycode: IKeycode, index: number) => {
const {code, title, name} = keycode;
const found = code.toLowerCase().includes(search) || name.toLowerCase().includes(search) || title?.toLowerCase().includes(search) || false
if (search !== "" && !found)
{
return (
<></>
)
}
return (
<Keycode
key={code}
Expand All @@ -301,6 +314,8 @@
);
};

const [search, setSearch] = useState("")

const renderSelectedCategory = (
keycodes: IKeycode[],
selectedCategory: string,
Expand Down Expand Up @@ -345,6 +360,33 @@
</KeycodeList>
);
}
case 'all': {
const allKeycodes = []
for (const item of getEnabledMenus())
{
const keycodesList = KeycodeCategories.find(
({id}) => id === item.id,
)?.keycodes as IKeycode[]

// for now skip custom keycodes since those are generated above instead of inside the getKeycodes()
if (item.id === "custom")
{
continue
}

for (const keycode of keycodesList)
{
allKeycodes.push(keycode)
}
}
return (
<KeycodeList>
{allKeycodes.map((keycode, i) =>
renderKeycode(keycode, i),
).concat(renderCustomKeycode())}
</KeycodeList>
)
}
default: {
return <KeycodeList>{keycodeListItems}</KeycodeList>;
}
Expand All @@ -360,6 +402,7 @@
<SubmenuOverflowCell>{renderCategories()}</SubmenuOverflowCell>
<OverflowCell>
<KeycodeContainer>
<TextInput placeholder = "search..." onChange={(e) => {setSearch(e.target.value.toLowerCase())}}/>
{renderSelectedCategory(selectedCategoryKeycodes, selectedCategory)}
</KeycodeContainer>
<KeycodeDesc>{mouseOverDesc}</KeycodeDesc>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import {useAppSelector} from 'src/store/hooks';
import {
getMacroEditorSettings,
setMacroEditorSettings,
getSelectedLanguage,
} from 'src/store/settingsSlice';
import {useDispatch} from 'react-redux';
import { getMacroCount } from 'src/store/macrosSlice';

declare global {
interface Navigator {
Expand Down Expand Up @@ -113,10 +115,6 @@ const componentJoin = (arr: (JSX.Element | null)[], separator: JSX.Element) => {
}, [] as (JSX.Element | null)[]);
};

const KeycodeMap = getKeycodes()
.flatMap((menu) => menu.keycodes)
.reduce((p, n) => ({...p, [n.code]: n}), {} as Record<string, IKeycode>);

const optimizeKeycodeSequence = (sequence: RawKeycodeSequence) => {
return pipeline(
sequence,
Expand Down Expand Up @@ -158,6 +156,11 @@ export const MacroRecorder: React.FC<{
isRecording,
recordDelaysEnabled,
);

const numMacros = useAppSelector(getMacroCount)
const KeycodeMap = getKeycodes(numMacros)
.flatMap((menu) => menu.keycodes)
.reduce((p, n) => ({...p, [n.code]: n}), {} as Record<string, IKeycode>);
const macroSequenceRef = useRef<HTMLDivElement>(null);
const recordingToggleChange = useCallback(
async (isRecording: boolean) => {
Expand Down
24 changes: 24 additions & 0 deletions src/components/panes/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
updateThemeName,
getRenderMode,
updateRenderMode,
getLanguage,
updateLanguage
} from 'src/store/settingsSlice';
import {AccentSelect} from '../inputs/accent-select';
import {THEMES} from 'src/utils/themes';
Expand All @@ -35,6 +37,7 @@ import {faToolbox} from '@fortawesome/free-solid-svg-icons';
import {getSelectedConnectedDevice} from 'src/store/devicesSlice';
import {ErrorMessage} from '../styled';
import {webGLIsAvailable} from 'src/utils/test-webgl';
import {LANGUAGES} from 'src/utils/languages';

const Container = styled.div`
display: flex;
Expand All @@ -61,6 +64,7 @@ export const Settings = () => {
const themeName = useAppSelector(getThemeName);
const renderMode = useAppSelector(getRenderMode);
const selectedDevice = useAppSelector(getSelectedConnectedDevice);
const language = useAppSelector(getLanguage);

const [showDiagnostics, setShowDiagnostics] = useState(false);

Expand All @@ -72,6 +76,14 @@ export const Settings = () => {
(opt) => opt.value === themeName,
);

const languageSelectOptions = Object.keys(LANGUAGES).map((k) => ({
label: k.replaceAll('_', ' '),
value: k,
}));
const defaultLanguageValue = languageSelectOptions.find(
(opt) => opt.value === language
);

const renderModeOptions = webGLIsAvailable
? [
{
Expand Down Expand Up @@ -129,6 +141,18 @@ export const Settings = () => {
/>
</Detail>
</ControlRow>
<ControlRow>
<Label>Language</Label>
<Detail>
<AccentSelect
defaultValue={defaultLanguageValue}
options={languageSelectOptions}
onChange={(option: any) => {
option && dispatch(updateLanguage(option.value));
}}
/>
</Detail>
</ControlRow>
<ControlRow>
<Label>Keycap Theme</Label>
<Detail>
Expand Down
12 changes: 12 additions & 0 deletions src/store/settingsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {makeSRGBTheme} from 'src/utils/keyboard-rendering';
import {updateCSSVariables} from 'src/utils/color-math';
import {webGLIsAvailable} from 'src/utils/test-webgl';
import {DefinitionVersion} from '@the-via/reader';
import { LANGUAGES } from 'src/utils/languages';
import { updateKeycodesList } from '../utils/key';

// TODO: why are these settings mixed? Is it because we only want some of them cached? SHould we rename to "CachedSettings"?
type SettingsState = Settings & {
Expand Down Expand Up @@ -98,6 +100,10 @@ const settingsSlice = createSlice({
enableGlobalHotKeys: (state) => {
state.allowGlobalHotKeys = true;
},
updateLanguage: (state, action: PayloadAction<string>) => {
state.language = action.payload;
setSettings(state);
},
},
});

Expand All @@ -113,6 +119,7 @@ export const {
updateRenderMode,
updateThemeName,
updateDesignDefinitionVersion,
updateLanguage
} = settingsSlice.actions;

export default settingsSlice.reducer;
Expand Down Expand Up @@ -140,6 +147,11 @@ export const getThemeName = (state: RootState) => state.settings.themeName;
export const getSelectedTheme = createSelector(getThemeName, (themeName) => {
return THEMES[themeName as keyof typeof THEMES];
});
export const getLanguage = (state: RootState) => state.settings.language;
export const getSelectedLanguage = createSelector(getLanguage, (language) => {
updateKeycodesList();
return LANGUAGES[language as keyof typeof LANGUAGES];
});

export const getSelectedSRGBTheme = createSelector(
getSelectedTheme,
Expand Down
1 change: 1 addition & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export type Settings = {
macroEditor: MacroEditorSettings;
testKeyboardSoundsSettings: TestKeyboardSoundsSettings;
designDefinitionVersion: DefinitionVersion;
language: string;
};

export type CommonMenusMap = {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/device-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const defaultStoreData = {
mode: TestKeyboardSoundsMode.WickiHayden,
transpose: 0,
},
language: 'ENGLISH',
},
};

Expand Down Expand Up @@ -177,3 +178,7 @@ export const getSettings = (): Settings => deviceStore.get('settings');
export const setSettings = (settings: Settings) => {
deviceStore.set('settings', current(settings));
};

export const getLanguageFromStore = () => {
return deviceStore.get('settings')?.language;
};
Loading
Loading