Skip to content

Commit

Permalink
Add "visible" option to mod settings sections (partially addresses #31)
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbl committed Nov 29, 2024
1 parent 5bbbae4 commit 9038e9a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/bridge/ModConfig.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export interface ModConfigBase {
* The unique identifier of the configuration element.
*/
id: string;

/**
* Determines if the field is visible or not.
*/
visible?: Binding<boolean>;
}

/**
Expand Down Expand Up @@ -110,11 +115,6 @@ export interface ModConfigFieldBase extends ModConfigBase {
* The name of the field.
*/
name: string;

/**
* Determines if the field is visible or not.
*/
visible?: Binding<boolean>;
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/renderer/react/settings/ModSettingsSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ModConfigFieldOrSection,
ModConfigSection,
} from 'bridge/ModConfig';
import { parseBinding } from 'renderer/react/BindingsParser';
import { useSetModConfig } from 'renderer/react/context/ModsContext';
import ModSettingsField from 'renderer/react/settings/ModSettingsField';
import { MouseEvent, useCallback } from 'react';
Expand Down Expand Up @@ -93,8 +94,13 @@ export default function ModSettingsSection({
[descendants, mod.config, mod.id, setModConfig],
);

const isShown =
section.visible == null ||
parseBinding<boolean>(section.visible, mod.config);

const areAllDescendantsCheckboxes =
descendants.every((child) => child.type === 'checkbox') ?? false;
descendants.length > 0 &&
(descendants.every((child) => child.type === 'checkbox') ?? false);

const areAllDescendantsChecked =
areAllDescendantsCheckboxes &&
Expand All @@ -118,6 +124,10 @@ export default function ModSettingsSection({
[setModConfig, mod.id, mod.config, descendants, areAllDescendantsChecked],
);

if (!isShown) {
return null;
}

return (
<StyledAccordion
key={section.id ?? 'default'}
Expand Down

0 comments on commit 9038e9a

Please sign in to comment.