File size: 3,631 Bytes
2d8be8f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | <script>
import {
IconMenuItem,
CheckMenuItem,
PredefinedMenuItem,
MenuItem
} from '@tauri-apps/api/menu'
let { newItem, itemClick } = $props()
let kind = $state('Normal')
let text = $state('')
let icon = $state('')
/** @type {import('@tauri-apps/api/menu').PredefinedMenuItemOptions['item'] | undefined} */
let predefinedItem = undefined
let checked = $state(true)
const itemKinds = ['Normal', 'Icon', 'Check', 'Predefined']
const predefinedOptions = [
'Separator',
'Copy',
'Cut',
'Paste',
'SelectAll',
'Undo',
'Redo',
'Minimize',
'Maximize',
'Fullscreen',
'Hide',
'HideOthers',
'ShowAll',
'CloseWindow',
'Quit',
'Services'
]
function onKindChange(event) {
kind = event.currentTarget.value
}
function onPredefinedChange(event) {
predefinedItem = event.currentTarget.value
}
async function create() {
let options = null
let item = null
const t = text
switch (kind) {
case 'Normal':
options = {
text,
action: (id) => itemClick({ id, text: t })
}
item = await MenuItem.new(options)
break
case 'Icon':
options = {
text,
icon,
action: (id) => itemClick({ id, text: t })
}
item = await IconMenuItem.new(options)
break
case 'Check':
options = {
text,
checked,
action: (id) => itemClick({ id, text: t })
}
item = await CheckMenuItem.new(options)
break
case 'Predefined':
options = {
item: predefinedItem
}
item = await PredefinedMenuItem.new(options)
break
}
newItem({ item, options })
text = ''
predefinedItem = undefined
}
</script>
<div class="flex flex-row gap-2 flex-grow-0">
<div class="flex flex-col">
{#each itemKinds as itemKind, i}
<div class="flex gap-1">
<input
id="{itemKind}Input"
checked={kind === itemKind}
onchange={onKindChange}
type="radio"
name="kind"
value={itemKinds[i]}
/>
<label for="{itemKind}Input">{itemKind}</label>
</div>
{/each}
</div>
<div class="bg-gray/30 dark:bg-white/5 w-1px flex-shrink-0"></div>
<div class="flex flex-col gap-2">
{#if kind == 'Normal' || kind == 'Icon' || kind == 'Check'}
<input class="input" type="text" placeholder="Text" bind:value={text} />
{/if}
{#if kind == 'Icon'}
<input class="input" type="icon" placeholder="Icon" bind:value={icon} />
{:else if kind == 'Check'}
<div class="flex gap-1">
<input
id="checkItemCheckedInput"
type="checkbox"
class="checkbox"
bind:checked
/>
<label for="checkItemCheckedInput">Enabled</label>
</div>
{:else if kind == 'Predefined'}
<div class="flex gap-2 flex-wrap">
{#each predefinedOptions as predefinedOption, i}
<div class="flex gap-1">
<input
id="{predefinedOption}Input"
checked={kind === predefinedOption}
onchange={onPredefinedChange}
type="radio"
name="predefinedKind"
value={predefinedOptions[i]}
/>
<label for="{predefinedOption}Input">{predefinedOption}</label>
</div>
{/each}
</div>
{/if}
</div>
<div class="grow"></div>
<div class="flex flex-col">
<button class="btn" onclick={create}>Create</button>
</div>
</div>
|