one
Version:
One is a React framework for web, iOS, and Android apps.
494 lines (489 loc) • 19 kB
text/typescript
// generated by bun run generate from the SwiftUI SDK and codegen/catalog.ts.
// edit the generator or catalog, then regenerate.
import type { NativeMenuItem } from './specs/OneNativeMenuNativeComponent'
import type { MenuItem } from './types'
import { assertSwiftUIValue } from './generated/swiftui'
export function flattenMenuItems(
items: readonly MenuItem[],
iosVersion = 17
): NativeMenuItem[] {
const result: NativeMenuItem[] = []
const ids = new Set<string>()
const append = (
items: readonly MenuItem[],
parentId: string,
disabled: boolean,
hidden: boolean
) => {
for (const item of items) {
const input = item as unknown as Record<string, unknown>
switch (item.type) {
case 'action': {
for (const name of Object.keys(input)) {
if (
name !== 'type' &&
name !== 'id' &&
name !== 'title' &&
name !== 'systemImage' &&
name !== 'role' &&
name !== 'disabled' &&
name !== 'hidden' &&
name !== 'help' &&
name !== 'menuActionDismissBehavior'
)
throw new Error(
'Unsupported Swift.Menu ' + item.type + ' property: ' + name
)
}
const id = input.id ?? ''
if (input.id == null)
throw new Error('Swift.Menu ' + item.type + ' requires id')
if (typeof id !== 'string') throw new Error('Invalid Swift.Menu id')
const title = input.title ?? ''
if (input.title == null)
throw new Error('Swift.Menu ' + item.type + ' requires title')
if (typeof title !== 'string') throw new Error('Invalid Swift.Menu title')
const systemImage = input.systemImage ?? ''
if (typeof systemImage !== 'string')
throw new Error('Invalid Swift.Menu systemImage')
const role = input.role ?? ''
if (typeof role !== 'string') throw new Error('Invalid Swift.Menu role')
if (role !== '') assertSwiftUIValue('ButtonRole', role, iosVersion)
const disabledValue = input.disabled ?? false
if (typeof disabledValue !== 'boolean')
throw new Error('Invalid Swift.Menu disabled')
const hiddenValue = input.hidden ?? false
if (typeof hiddenValue !== 'boolean')
throw new Error('Invalid Swift.Menu hidden')
const help = input.help ?? ''
if (typeof help !== 'string') throw new Error('Invalid Swift.Menu help')
const menuActionDismissBehavior = input.menuActionDismissBehavior ?? ''
if (typeof menuActionDismissBehavior !== 'string')
throw new Error('Invalid Swift.Menu menuActionDismissBehavior')
if (menuActionDismissBehavior !== '')
assertSwiftUIValue(
'MenuActionDismissBehavior',
menuActionDismissBehavior,
iosVersion
)
if (!item.id || ids.has(item.id))
throw new Error(
'Swift.Menu requires unique, nonempty item ids: "' + item.id + '"'
)
ids.add(item.id)
result.push({
id,
title,
systemImage,
role,
disabled: disabled || disabledValue,
hidden: hidden || hiddenValue,
help,
controlGroupStyle: 'automatic',
values: [],
selection: '',
menuOrder: '',
menuActionDismissBehavior,
parentId,
type: item.type,
})
break
}
case 'toggle': {
for (const name of Object.keys(input)) {
if (
name !== 'type' &&
name !== 'id' &&
name !== 'title' &&
name !== 'systemImage' &&
name !== 'values' &&
name !== 'disabled' &&
name !== 'hidden' &&
name !== 'help' &&
name !== 'menuActionDismissBehavior'
)
throw new Error(
'Unsupported Swift.Menu ' + item.type + ' property: ' + name
)
}
const id = input.id ?? ''
if (input.id == null)
throw new Error('Swift.Menu ' + item.type + ' requires id')
if (typeof id !== 'string') throw new Error('Invalid Swift.Menu id')
const title = input.title ?? ''
if (input.title == null)
throw new Error('Swift.Menu ' + item.type + ' requires title')
if (typeof title !== 'string') throw new Error('Invalid Swift.Menu title')
const systemImage = input.systemImage ?? ''
if (typeof systemImage !== 'string')
throw new Error('Invalid Swift.Menu systemImage')
const values = input.values ?? []
if (input.values == null)
throw new Error('Swift.Menu ' + item.type + ' requires values')
if (
!Array.isArray(values) ||
!values.length ||
values.some((v) => typeof v !== 'boolean')
)
throw new Error('Swift.Menu toggle values must be a nonempty boolean array')
const disabledValue = input.disabled ?? false
if (typeof disabledValue !== 'boolean')
throw new Error('Invalid Swift.Menu disabled')
const hiddenValue = input.hidden ?? false
if (typeof hiddenValue !== 'boolean')
throw new Error('Invalid Swift.Menu hidden')
const help = input.help ?? ''
if (typeof help !== 'string') throw new Error('Invalid Swift.Menu help')
const menuActionDismissBehavior = input.menuActionDismissBehavior ?? ''
if (typeof menuActionDismissBehavior !== 'string')
throw new Error('Invalid Swift.Menu menuActionDismissBehavior')
if (menuActionDismissBehavior !== '')
assertSwiftUIValue(
'MenuActionDismissBehavior',
menuActionDismissBehavior,
iosVersion
)
if (!item.id || ids.has(item.id))
throw new Error(
'Swift.Menu requires unique, nonempty item ids: "' + item.id + '"'
)
ids.add(item.id)
result.push({
id,
title,
systemImage,
role: '',
disabled: disabled || disabledValue,
hidden: hidden || hiddenValue,
help,
controlGroupStyle: 'automatic',
values,
selection: '',
menuOrder: '',
menuActionDismissBehavior,
parentId,
type: item.type,
})
break
}
case 'submenu': {
for (const name of Object.keys(input)) {
if (
name !== 'type' &&
name !== 'id' &&
name !== 'title' &&
name !== 'systemImage' &&
name !== 'disabled' &&
name !== 'hidden' &&
name !== 'help' &&
name !== 'menuOrder' &&
name !== 'menuActionDismissBehavior' &&
name !== 'children'
)
throw new Error(
'Unsupported Swift.Menu ' + item.type + ' property: ' + name
)
}
const id = input.id ?? ''
if (input.id == null)
throw new Error('Swift.Menu ' + item.type + ' requires id')
if (typeof id !== 'string') throw new Error('Invalid Swift.Menu id')
const title = input.title ?? ''
if (input.title == null)
throw new Error('Swift.Menu ' + item.type + ' requires title')
if (typeof title !== 'string') throw new Error('Invalid Swift.Menu title')
const systemImage = input.systemImage ?? ''
if (typeof systemImage !== 'string')
throw new Error('Invalid Swift.Menu systemImage')
const disabledValue = input.disabled ?? false
if (typeof disabledValue !== 'boolean')
throw new Error('Invalid Swift.Menu disabled')
const hiddenValue = input.hidden ?? false
if (typeof hiddenValue !== 'boolean')
throw new Error('Invalid Swift.Menu hidden')
const help = input.help ?? ''
if (typeof help !== 'string') throw new Error('Invalid Swift.Menu help')
const menuOrder = input.menuOrder ?? ''
if (typeof menuOrder !== 'string')
throw new Error('Invalid Swift.Menu menuOrder')
if (menuOrder !== '') assertSwiftUIValue('MenuOrder', menuOrder, iosVersion)
const menuActionDismissBehavior = input.menuActionDismissBehavior ?? ''
if (typeof menuActionDismissBehavior !== 'string')
throw new Error('Invalid Swift.Menu menuActionDismissBehavior')
if (menuActionDismissBehavior !== '')
assertSwiftUIValue(
'MenuActionDismissBehavior',
menuActionDismissBehavior,
iosVersion
)
if (!item.id || ids.has(item.id))
throw new Error(
'Swift.Menu requires unique, nonempty item ids: "' + item.id + '"'
)
ids.add(item.id)
result.push({
id,
title,
systemImage,
role: '',
disabled: disabled || disabledValue,
hidden: hidden || hiddenValue,
help,
controlGroupStyle: 'automatic',
values: [],
selection: '',
menuOrder,
menuActionDismissBehavior,
parentId,
type: item.type,
})
if (!Array.isArray(input.children))
throw new Error('Swift.Menu ' + item.type + ' requires children')
append(
input.children,
item.id,
Boolean(disabled || disabledValue),
Boolean(hidden || hiddenValue)
)
break
}
case 'section': {
for (const name of Object.keys(input)) {
if (
name !== 'type' &&
name !== 'id' &&
name !== 'title' &&
name !== 'hidden' &&
name !== 'children'
)
throw new Error(
'Unsupported Swift.Menu ' + item.type + ' property: ' + name
)
}
const id = input.id ?? ''
if (input.id == null)
throw new Error('Swift.Menu ' + item.type + ' requires id')
if (typeof id !== 'string') throw new Error('Invalid Swift.Menu id')
const title = input.title ?? ''
if (typeof title !== 'string') throw new Error('Invalid Swift.Menu title')
const hiddenValue = input.hidden ?? false
if (typeof hiddenValue !== 'boolean')
throw new Error('Invalid Swift.Menu hidden')
if (!item.id || ids.has(item.id))
throw new Error(
'Swift.Menu requires unique, nonempty item ids: "' + item.id + '"'
)
ids.add(item.id)
result.push({
id,
title,
systemImage: '',
role: '',
disabled: disabled || false,
hidden: hidden || hiddenValue,
help: '',
controlGroupStyle: 'automatic',
values: [],
selection: '',
menuOrder: '',
menuActionDismissBehavior: '',
parentId,
type: item.type,
})
if (!Array.isArray(input.children))
throw new Error('Swift.Menu ' + item.type + ' requires children')
append(
input.children,
item.id,
Boolean(disabled || false),
Boolean(hidden || hiddenValue)
)
break
}
case 'controlGroup': {
for (const name of Object.keys(input)) {
if (
name !== 'type' &&
name !== 'id' &&
name !== 'title' &&
name !== 'systemImage' &&
name !== 'disabled' &&
name !== 'hidden' &&
name !== 'controlGroupStyle' &&
name !== 'children'
)
throw new Error(
'Unsupported Swift.Menu ' + item.type + ' property: ' + name
)
}
const id = input.id ?? ''
if (input.id == null)
throw new Error('Swift.Menu ' + item.type + ' requires id')
if (typeof id !== 'string') throw new Error('Invalid Swift.Menu id')
const title = input.title ?? ''
if (typeof title !== 'string') throw new Error('Invalid Swift.Menu title')
const systemImage = input.systemImage ?? ''
if (typeof systemImage !== 'string')
throw new Error('Invalid Swift.Menu systemImage')
const disabledValue = input.disabled ?? false
if (typeof disabledValue !== 'boolean')
throw new Error('Invalid Swift.Menu disabled')
const hiddenValue = input.hidden ?? false
if (typeof hiddenValue !== 'boolean')
throw new Error('Invalid Swift.Menu hidden')
const controlGroupStyle = input.controlGroupStyle ?? 'automatic'
if (typeof controlGroupStyle !== 'string')
throw new Error('Invalid Swift.Menu controlGroupStyle')
assertSwiftUIValue('ControlGroupStyle', controlGroupStyle, iosVersion)
if (!item.id || ids.has(item.id))
throw new Error(
'Swift.Menu requires unique, nonempty item ids: "' + item.id + '"'
)
ids.add(item.id)
result.push({
id,
title,
systemImage,
role: '',
disabled: disabled || disabledValue,
hidden: hidden || hiddenValue,
help: '',
controlGroupStyle,
values: [],
selection: '',
menuOrder: '',
menuActionDismissBehavior: '',
parentId,
type: item.type,
})
if (!Array.isArray(input.children))
throw new Error('Swift.Menu ' + item.type + ' requires children')
append(
input.children,
item.id,
Boolean(disabled || disabledValue),
Boolean(hidden || hiddenValue)
)
break
}
case 'picker': {
for (const name of Object.keys(input)) {
if (
name !== 'type' &&
name !== 'id' &&
name !== 'title' &&
name !== 'systemImage' &&
name !== 'selection' &&
name !== 'disabled' &&
name !== 'hidden' &&
name !== 'help' &&
name !== 'children'
)
throw new Error(
'Unsupported Swift.Menu ' + item.type + ' property: ' + name
)
}
const id = input.id ?? ''
if (input.id == null)
throw new Error('Swift.Menu ' + item.type + ' requires id')
if (typeof id !== 'string') throw new Error('Invalid Swift.Menu id')
const title = input.title ?? ''
if (input.title == null)
throw new Error('Swift.Menu ' + item.type + ' requires title')
if (typeof title !== 'string') throw new Error('Invalid Swift.Menu title')
const systemImage = input.systemImage ?? ''
if (typeof systemImage !== 'string')
throw new Error('Invalid Swift.Menu systemImage')
const selection = input.selection ?? ''
if (input.selection == null)
throw new Error('Swift.Menu ' + item.type + ' requires selection')
if (typeof selection !== 'string')
throw new Error('Invalid Swift.Menu selection')
const disabledValue = input.disabled ?? false
if (typeof disabledValue !== 'boolean')
throw new Error('Invalid Swift.Menu disabled')
const hiddenValue = input.hidden ?? false
if (typeof hiddenValue !== 'boolean')
throw new Error('Invalid Swift.Menu hidden')
const help = input.help ?? ''
if (typeof help !== 'string') throw new Error('Invalid Swift.Menu help')
if (!item.id || ids.has(item.id))
throw new Error(
'Swift.Menu requires unique, nonempty item ids: "' + item.id + '"'
)
ids.add(item.id)
result.push({
id,
title,
systemImage,
role: '',
disabled: disabled || disabledValue,
hidden: hidden || hiddenValue,
help,
controlGroupStyle: 'automatic',
values: [],
selection,
menuOrder: '',
menuActionDismissBehavior: '',
parentId,
type: item.type,
})
if (!Array.isArray(input.children))
throw new Error('Swift.Menu ' + item.type + ' requires children')
if (
!input.children.length ||
input.children.some((child: any) => child?.type !== 'action')
)
throw new Error('Swift.Menu picker children must be actions')
if (!input.children.some((child: any) => child.id === input.selection))
throw new Error('Swift.Menu picker selection must match a child id')
append(
input.children,
item.id,
Boolean(disabled || disabledValue),
Boolean(hidden || hiddenValue)
)
break
}
case 'divider': {
for (const name of Object.keys(input)) {
if (name !== 'type' && name !== 'id')
throw new Error(
'Unsupported Swift.Menu ' + item.type + ' property: ' + name
)
}
const id = input.id ?? ''
if (input.id == null)
throw new Error('Swift.Menu ' + item.type + ' requires id')
if (typeof id !== 'string') throw new Error('Invalid Swift.Menu id')
if (!item.id || ids.has(item.id))
throw new Error(
'Swift.Menu requires unique, nonempty item ids: "' + item.id + '"'
)
ids.add(item.id)
result.push({
id,
title: '',
systemImage: '',
role: '',
disabled: disabled || false,
hidden: hidden || false,
help: '',
controlGroupStyle: 'automatic',
values: [],
selection: '',
menuOrder: '',
menuActionDismissBehavior: '',
parentId,
type: item.type,
})
break
}
default:
throw new Error('Unknown Swift.Menu item type: ' + input.type)
}
}
}
append(items, '', false, false)
return result
}