logigate/app/components/editor/Toolbar.vue

131 lines
7.0 KiB
Vue

<script setup lang="ts">
// The top of the editor: three small floating groups over the canvas.
// Left, where you are (menu, project, circuit); middle, the clock; right,
// the few things used all the time, and everything else in the ⋯ menu.
import type { MenuEntry } from '~/composables/useMenu'
const ed = useEditor()
const SPEEDS = [
{ value: 0.5, label: '0.5 Hz' },
{ value: 1, label: '1 Hz' },
{ value: 2, label: '2 Hz' },
{ value: 5, label: '5 Hz' },
{ value: 10, label: '10 Hz' },
{ value: 30, label: '30 Hz' },
{ value: 0, label: 'Max' },
]
const hasClock = computed(() => (ed.sim.value?.nl.clocks.length ?? 0) > 0)
const statusText = computed(() => ({ stable: 'Stable', busy: 'Settling', oscillating: 'Oscillating' })[ed.status.value])
const backName = computed(() => {
const id = ed.nav.value.at(-1)
return id ? ed.getDef.value(id)?.name : undefined
})
const cloudText = computed(() => ({ off: '', syncing: 'syncing', synced: 'all saved', offline: 'offline', error: 'sync failed' })[ed.cloudState.value])
const linkIcon = computed(() => {
switch (ed.linkState.value) {
case 'saved':
return 'lucide:cloud-check'
case 'saving':
case 'dirty':
return 'lucide:cloud-upload'
case 'error':
return 'lucide:cloud-alert'
default:
return 'lucide:share-2'
}
})
/** The panel on the right: open on its own while something is selected; this keeps it open. */
const panelOpen = computed(() => ed.sidebars.right)
const more = ref<{ x: number; y: number } | null>(null)
function openMore(e: MouseEvent) {
const r = (e.currentTarget as HTMLElement).getBoundingClientRect()
more.value = more.value ? null : { x: r.right - 240, y: r.bottom + 6 }
}
const moreEntries = computed<MenuEntry[]>(() => [
{ label: 'Your projects', icon: 'lucide:folder', run: () => (ed.dialog.value = 'projects') },
{ label: 'Main menu', icon: 'lucide:house', run: () => ed.showMenu() },
'sep',
{ label: 'Settings', icon: 'lucide:settings', note: 'symbols, wires, sound', run: () => (ed.dialog.value = 'settings') },
{ label: ed.sound.value.on ? 'Sound on' : 'Sound off', icon: ed.sound.value.on ? 'lucide:volume-2' : 'lucide:volume-x', checked: ed.sound.value.on, run: () => ed.setSound({ on: !ed.sound.value.on }) },
{ label: ed.sidebars.left ? 'Hide the parts dock' : 'Show the parts dock', icon: 'lucide:panel-bottom', keys: '[', run: () => ed.toggleSidebar('left') },
{ label: 'Print or save as PDF', icon: 'lucide:printer', keys: 'Ctrl+P', run: () => ed.exportSheet('print') },
'sep',
{ label: 'How to play', icon: 'lucide:circle-help', keys: '?', run: () => (ed.showHelp.value = true) },
{ label: ed.account.value ? `${ed.account.value.email}` : 'Sign in', icon: ed.account.value ? 'lucide:user-check' : 'lucide:user', note: ed.account.value ? cloudText.value : 'keep projects everywhere', run: () => (ed.dialog.value = 'account') },
])
</script>
<template>
<header class="hud">
<div class="hud-group">
<button class="hud-btn brand" title="Main menu (your work is saved)" @click="ed.showMenu()">
<svg viewBox="0 0 32 32" class="size-5" aria-hidden="true">
<path d="M6 7h10a9 9 0 0 1 0 18H6Z" fill="none" stroke="currentColor" stroke-width="2.8" />
<circle cx="27" cy="16" r="2.6" fill="none" stroke="currentColor" stroke-width="2.4" />
</svg>
</button>
<span class="hud-sep" />
<button class="hud-btn hud-project" title="Your projects" @click="ed.dialog.value = 'projects'">
<span class="truncate">{{ ed.project.value.name }}</span>
</button>
<Icon name="lucide:chevron-right" class="size-3.5 shrink-0 text-(--muted)" />
<button v-if="backName" class="hud-btn" :title="`Back to ${backName}`" @click="ed.back()">
<Icon name="lucide:arrow-left" class="size-4" />
</button>
<label class="hud-circuit" title="Circuit you are editing">
<select :value="ed.def.value.id" @change="ed.open(($event.target as HTMLSelectElement).value)">
<option v-for="d in ed.project.value.components" :key="d.id" :value="d.id">{{ d.name }}</option>
</select>
<Icon name="lucide:chevron-down" class="size-3.5 text-(--muted)" />
</label>
</div>
<div class="hud-group hud-sim">
<span class="status" :class="ed.status.value" :title="ed.status.value === 'oscillating' ? 'A loop keeps flipping and never settles (e.g. a NOT feeding itself)' : 'Simulation state'">
<span class="dot" /><span class="hidden lg:inline">{{ statusText }}</span>
</span>
<template v-if="hasClock">
<span class="hud-sep" />
<button class="hud-btn" :class="{ active: ed.running.value }" :title="ed.running.value ? 'Pause the clock' : 'Run the clock'" @click="ed.running.value = !ed.running.value">
<Icon :name="ed.running.value ? 'lucide:pause' : 'lucide:play'" class="size-4" />
</button>
<button class="hud-btn" title="One clock cycle" @click="ed.running.value = false; ed.step()">
<Icon name="lucide:step-forward" class="size-4" />
</button>
<select v-model.number="ed.hz.value" class="hud-select" title="Clock speed">
<option v-for="s in SPEEDS" :key="s.value" :value="s.value">{{ s.label }}</option>
</select>
<span class="cycles tabular-nums" title="Clock cycles since this circuit was opened">{{ ed.cycles.value.toLocaleString() }}</span>
</template>
</div>
<div class="hud-group">
<button class="hud-btn" title="Find anything in the project (Ctrl+F)" @click="ed.dialog.value = 'search'"><Icon name="lucide:search" class="size-4" /></button>
<button class="hud-btn" title="Undo (Ctrl+Z)" :disabled="!ed.canUndo.value" @click="ed.undo()"><Icon name="lucide:undo-2" class="size-4" /></button>
<button class="hud-btn" title="Redo (Ctrl+Shift+Z)" :disabled="!ed.canRedo.value" @click="ed.redo()"><Icon name="lucide:redo-2" class="size-4" /></button>
<span class="hud-sep" />
<button class="hud-btn share-btn" :class="ed.linkState.value" title="Save online, share a link, export or import a file" @click="ed.dialog.value = 'share'">
<Icon :name="linkIcon" class="size-4" />
<span class="hidden md:inline">Share</span>
</button>
<button
class="hud-btn"
:class="{ active: panelOpen }"
:title="panelOpen ? 'Hide the side panel (])' : 'Circuit, pins and tests (])'"
@click="ed.toggleSidebar('right')"
>
<Icon name="lucide:panel-right" class="size-4" />
</button>
<button class="hud-btn account-btn" :class="[ed.cloudState.value, { active: !!more }]" title="More: projects, settings, sound, help, account" @click="openMore">
<template v-if="ed.account.value"><span class="avatar">{{ ed.account.value.email[0]!.toUpperCase() }}</span><span class="sync-dot" /></template>
<Icon v-else name="lucide:ellipsis" class="size-4" />
</button>
</div>
<EditorContextMenu v-if="more" :x="more.x" :y="more.y" :entries="moreEntries" @close="more = null" />
</header>
</template>