Skip to content

Commit 30f7077

Browse files
committed
feat: Add limits for usages in the list, re-render fixes.
1 parent 3690bfd commit 30f7077

File tree

5 files changed

+49
-31
lines changed

5 files changed

+49
-31
lines changed

src/behaviors/BehaviorBindingPicker.stories.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const meta = {
2020
args: {
2121
layerNames: ["Main", "Fn"],
2222
onBindingChanged: fn(),
23-
// onPhysicalLayoutClicked: fn(),
2423
},
2524
} satisfies Meta<typeof BehaviorBindingPicker>;
2625

@@ -34,7 +33,7 @@ export const Example: Story = {
3433
{
3534
id: 0,
3635
displayName: "Key Press",
37-
metadata: [{ param1: [{ name: "Key", hidUsage: {} }], param2: [] }],
36+
metadata: [{ param1: [{ name: "Key", hidUsage: { consumerMax: 0, keyboardMax: 0 } }], param2: [] }],
3837
},
3938
],
4039
},

src/behaviors/HidUsagePicker.stories.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ type Story = StoryObj<typeof meta>;
2121

2222
export const Keyboard: Story = {
2323
args: {
24-
usagePages: [7],
24+
usagePages: [{ id: 7 }],
2525
},
2626
};
2727

2828
export const KeyboardAndConsumer: Story = {
2929
args: {
30-
usagePages: [7, 12],
30+
usagePages: [{ id: 7 }, { id: 12 }],
3131
},
3232
};

src/behaviors/HidUsagePicker.tsx

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,56 @@ import {
1717
} from "../hid-usages";
1818
import { useCallback, useMemo } from "react";
1919

20+
export interface HidUsagePage {
21+
id: number;
22+
min?: number;
23+
max?: number;
24+
}
25+
2026
export interface HidUsagePickerProps {
27+
label?: string;
2128
value?: number;
22-
usagePages: number[];
29+
usagePages: HidUsagePage[];
2330
onValueChanged: (value?: number) => void;
2431
}
2532

33+
type UsageSectionProps = HidUsagePage;
34+
35+
const UsageSection = ({id, min, max}: UsageSectionProps) => {
36+
const info = useMemo(() => hid_usage_page_get_ids(id), [id]);
37+
38+
let usages = useMemo(() => {
39+
let usages = info?.UsageIds || [];
40+
if (max || min) {
41+
usages = usages.filter(i => i.Id <= (max || Number.MAX_SAFE_INTEGER) && i.Id >= (min || 0));
42+
}
43+
44+
return usages;
45+
}, [id, min, max, info]);
46+
47+
return (
48+
<Section id={id}>
49+
<Header className="text-text-base/50">{info?.Name}</Header>
50+
<Collection items={usages}>
51+
{(i) => (
52+
<ListBoxItem
53+
className="rac-hover:text-accent pl-3 relative rac-focus:text-accent cursur-default select-none rac-selected:before:content-['✔'] before:absolute before:left-[0] before:top-[0]"
54+
id={hid_usage_from_page_and_id(id, i.Id)}
55+
>
56+
{i.Name}
57+
</ListBoxItem>
58+
)}
59+
</Collection>
60+
</Section>
61+
);
62+
}
63+
2664
export const HidUsagePicker = ({
65+
label,
2766
value,
2867
usagePages,
2968
onValueChanged,
3069
}: HidUsagePickerProps) => {
31-
let pageObjects = useMemo(() => {
32-
return usagePages
33-
.map((p) => ({ id: p, info: hid_usage_page_get_ids(p) }))
34-
.filter((i) => !!i.info);
35-
}, [usagePages]);
36-
3770
const selectionChanged = useCallback(
3871
(e: Key | null) => {
3972
let value = typeof e == "number" ? e : undefined;
@@ -44,7 +77,7 @@ export const HidUsagePicker = ({
4477

4578
return (
4679
<ComboBox selectedKey={value} onSelectionChange={selectionChanged}>
47-
<Label>HID Usage</Label>
80+
{ label && <Label>{label}</Label> }
4881
<div>
4982
<Input className="p-1 rounded" />
5083
<Button className="ml-[-1.75em] px-[0.25em] py-0 rounded rounded-md bg-accent w-6 h-6">
@@ -53,25 +86,11 @@ export const HidUsagePicker = ({
5386
</div>
5487
<Popover className="w-[var(--trigger-width)] max-h-4 border rounded border-text-base bg-bg-base">
5588
<ListBox
56-
items={pageObjects}
89+
items={usagePages}
5790
className="block max-h-[30vh] min-h-[unset] overflow-auto m-2"
5891
selectionMode="single"
5992
>
60-
{(p) => (
61-
<Section id={p.id}>
62-
<Header className="text-text-base/50">{p.info?.Name}</Header>
63-
<Collection items={p.info?.UsageIds}>
64-
{(i) => (
65-
<ListBoxItem
66-
className="rac-hover:text-accent ml-2 rac-focus:text-accent cursur-default select-none"
67-
id={hid_usage_from_page_and_id(p.id, i.Id)}
68-
>
69-
{i.Name}
70-
</ListBoxItem>
71-
)}
72-
</Collection>
73-
</Section>
74-
)}
93+
{({id, min, max}) => <UsageSection id={id} min={min} max={max} />}
7594
</ListBox>
7695
</Popover>
7796
</ComboBox>

src/behaviors/ParameterValuePicker.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const Range: Story = {
4343

4444
export const HID: Story = {
4545
args: {
46-
values: [{ name: "Key", hidUsage: {} }],
46+
values: [{ name: "Key", hidUsage: { consumerMax: 0, keyboardMax: 0 } }],
4747
},
4848
};
4949

src/behaviors/ParameterValuePicker.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export const ParameterValuePicker = ({
1717
if (values.length == 0) {
1818
return <></>;
1919
} else if (values.every((v) => v.constant !== undefined)) {
20-
console.log("Loading a constant list wtih", value, values);
2120
return (
2221
<div>
2322
<select
@@ -48,8 +47,9 @@ export const ParameterValuePicker = ({
4847
return (
4948
<HidUsagePicker
5049
onValueChanged={onValueChanged}
50+
label={values[0].name}
5151
value={value}
52-
usagePages={[7, 12]}
52+
usagePages={[{ id: 7, min: 4, max: values[0].hidUsage.keyboardMax }, { id: 12, max: values[0].hidUsage.consumerMax }]}
5353
/>
5454
);
5555
} else if (values[0].layerIndex) {

0 commit comments

Comments
 (0)