304 lines
15 KiB
Vue
304 lines
15 KiB
Vue
<script setup lang="ts">
|
|
// The bar along the top, floating like the parts bar below: menu and where
|
|
// you are on the left, the clock in the middle, the rest on the right. On a
|
|
// narrow bar the names on the way fold into "…" and the buttons used least
|
|
// move into a "⋯" menu, so Main always stays in reach.
|
|
import { CHALLENGES } from '~/lib/logic/challenges'
|
|
import { foldCrumbs } from '~/lib/nav'
|
|
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])
|
|
// The way into the open circuit: Main, then each component entered on the way.
|
|
const path = computed(() =>
|
|
[...ed.nav.value.map((id, i) => ({ id, i })), { id: ed.def.value.id, i: -1 }]
|
|
.filter(p => p.id !== ed.mainId.value)
|
|
.map(p => ({ ...p, name: ed.getDef.value(p.id)?.name ?? '?' })),
|
|
)
|
|
const atMain = computed(() => ed.def.value.id === ed.mainId.value)
|
|
|
|
// How wide the bar is decides what fits in it.
|
|
const dock = ref<HTMLElement>()
|
|
const width = ref(Infinity)
|
|
let sizer: ResizeObserver | undefined
|
|
/** Buttons moved into the "⋯" menu: none; sound, settings and help; also undo, redo and share; also challenges and search. */
|
|
const tucked = computed(() => (width.value > 640 ? 0 : width.value > 520 ? 1 : width.value > 420 ? 2 : 3))
|
|
/** The names on the way here, the last ones in the bar and those before them under "…". */
|
|
const crumbs = computed(() => foldCrumbs(path.value, width.value > 1180 ? 3 : width.value > 900 ? 2 : 1))
|
|
|
|
/** Where a menu opens: under the button that opened it. */
|
|
function below(e: MouseEvent) {
|
|
const r = (e.currentTarget as HTMLElement).getBoundingClientRect()
|
|
return { x: r.left, y: r.bottom + 6 }
|
|
}
|
|
|
|
// The circuit you are in: its name opens what can be done with all of it.
|
|
const circuitMenu = ref<{ x: number; y: number } | null>(null)
|
|
function openCircuitMenu(e: MouseEvent) {
|
|
circuitMenu.value = circuitMenu.value ? null : below(e)
|
|
}
|
|
const circuitEntries = computed<MenuEntry[]>(() => {
|
|
const d = ed.def.value
|
|
const empty = !d.nodes.length
|
|
return [
|
|
{ head: 'Wires' },
|
|
{ label: 'Tidy this circuit', icon: 'lucide:wand-sparkles', disabled: !d.wires.length, run: () => ed.tidyAll() },
|
|
{ label: 'Tidy every circuit', icon: 'lucide:sparkles', run: () => ed.tidyEverything() },
|
|
'sep',
|
|
{ head: 'Memory' },
|
|
{
|
|
label: 'Power off and on',
|
|
icon: 'lucide:power',
|
|
disabled: !ed.circuitStats.value.sequential,
|
|
note: ed.circuitStats.value.sequential ? 'every bit back to 0' : 'no memory here',
|
|
run: () => ed.resetState(),
|
|
},
|
|
'sep',
|
|
{ head: 'As a part' },
|
|
{ label: 'Design its box…', icon: 'lucide:pen-tool', run: () => ed.designFace(d.id) },
|
|
{ label: 'Duplicate', icon: 'lucide:copy', run: () => ed.copyComponent(d.id) },
|
|
{
|
|
label: 'Delete this circuit',
|
|
icon: 'lucide:trash-2',
|
|
danger: true,
|
|
disabled: atMain.value,
|
|
note: atMain.value ? 'the main circuit stays' : undefined,
|
|
run: () => {
|
|
const name = d.name
|
|
ed.removeComponent(d.id)
|
|
ed.notify(`Deleted "${name}" (Ctrl+Z brings it back)`)
|
|
},
|
|
},
|
|
'sep',
|
|
{ head: 'Share' },
|
|
{ label: 'Print or save as PDF', icon: 'lucide:printer', keys: 'Ctrl+P', disabled: empty, run: () => ed.exportSheet('print') },
|
|
{ label: 'Export as SVG', icon: 'lucide:file-image', disabled: empty, run: () => ed.exportSheet('svg') },
|
|
{ label: 'Publish as a puzzle…', icon: 'lucide:puzzle', disabled: !!ed.task.value || empty, run: () => (ed.dialog.value = 'publish') },
|
|
{
|
|
label: ed.ownEntry(d) ? 'Update in the library…' : 'Publish to the library…',
|
|
icon: 'lucide:book-up',
|
|
disabled: !!ed.task.value || empty,
|
|
note: ed.task.value ? 'challenges stay private' : undefined,
|
|
run: () => (ed.dialog.value = 'library-publish'),
|
|
},
|
|
]
|
|
})
|
|
|
|
// Challenges and puzzles open below their button.
|
|
const showChallenges = ed.challengesOpen
|
|
const challengesEl = ref<HTMLElement>()
|
|
const solvedCount = computed(() => CHALLENGES.filter(c => ed.project.value.solved.includes(c.id)).length)
|
|
function outside(e: PointerEvent) {
|
|
if (showChallenges.value && !challengesEl.value?.contains(e.target as Node)) showChallenges.value = false
|
|
}
|
|
/** The drop-down is centred under its button, moved sideways as far as it takes to stay on screen. */
|
|
const popShift = ref(0)
|
|
function placePop() {
|
|
const r = challengesEl.value?.getBoundingClientRect()
|
|
if (!showChallenges.value || !r) return
|
|
const w = Math.min(340, window.innerWidth - 16)
|
|
const left = r.left + r.width / 2 - w / 2
|
|
popShift.value = Math.max(8, Math.min(left, window.innerWidth - 8 - w)) - left
|
|
}
|
|
watch(showChallenges, placePop)
|
|
onMounted(() => {
|
|
window.addEventListener('pointerdown', outside, true)
|
|
window.addEventListener('resize', placePop)
|
|
sizer = new ResizeObserver(([e]) => (width.value = e!.contentRect.width))
|
|
sizer.observe(dock.value!)
|
|
})
|
|
onBeforeUnmount(() => {
|
|
window.removeEventListener('pointerdown', outside, true)
|
|
window.removeEventListener('resize', placePop)
|
|
sizer?.disconnect()
|
|
})
|
|
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 names folded into "…": each leads back to it.
|
|
const foldMenu = ref<{ x: number; y: number } | null>(null)
|
|
const foldEntries = computed<MenuEntry[]>(() => crumbs.value.folded.map(p => ({ label: p.name, icon: 'lucide:box', run: () => ed.goTo(p.i) })))
|
|
|
|
// The buttons that did not fit, in the order they have in the bar.
|
|
const moreMenu = ref<{ x: number; y: number } | null>(null)
|
|
const moreEntries = computed<MenuEntry[]>(() => {
|
|
const list: MenuEntry[] = []
|
|
if (tucked.value > 2) {
|
|
list.push(
|
|
{ label: 'Challenges and puzzles', icon: 'lucide:flag', note: `${solvedCount.value}/${CHALLENGES.length}`, run: () => (showChallenges.value = true) },
|
|
{ label: 'Find anything in the project', icon: 'lucide:search', keys: 'Ctrl+F', run: () => (ed.dialog.value = 'search') },
|
|
)
|
|
}
|
|
if (tucked.value > 1) {
|
|
list.push(
|
|
{ label: 'Undo', icon: 'lucide:undo-2', keys: 'Ctrl+Z', disabled: !ed.canUndo.value, run: () => ed.undo() },
|
|
{ label: 'Redo', icon: 'lucide:redo-2', keys: 'Ctrl+Shift+Z', disabled: !ed.canRedo.value, run: () => ed.redo() },
|
|
)
|
|
}
|
|
list.push(
|
|
{ label: 'Sound', icon: 'lucide:volume-x', checked: ed.sound.value.on, run: () => ed.setSound({ on: !ed.sound.value.on }) },
|
|
{ label: 'Settings', icon: 'lucide:settings', run: () => (ed.dialog.value = 'settings') },
|
|
{ label: 'How to play', icon: 'lucide:circle-help', keys: '?', run: () => (ed.showHelp.value = true) },
|
|
)
|
|
if (tucked.value > 1) list.push('sep', { label: 'Share', icon: linkIcon.value, run: () => (ed.dialog.value = 'share') })
|
|
return list
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<div ref="dock" class="top-dock">
|
|
<header class="top-bar">
|
|
<div class="tb-group min-w-0">
|
|
<button class="bar-btn brand-btn" title="Menu: save, load, settings, main menu (Esc)" @click="ed.pause()">
|
|
<svg viewBox="0 0 32 32" class="size-5 shrink-0" aria-hidden="true">
|
|
<path d="M6 7h10a9 9 0 0 1 0 18H6Z" fill="none" stroke="currentColor" stroke-width="2.5" />
|
|
<circle cx="27" cy="16" r="2.6" fill="none" stroke="currentColor" stroke-width="2.2" />
|
|
</svg>
|
|
<span class="project-name truncate">{{ ed.project.value.name }}</span>
|
|
</button>
|
|
<span class="bar-sep" />
|
|
<nav class="crumbs" aria-label="Where you are">
|
|
<button
|
|
class="bar-btn"
|
|
:class="{ here: atMain, open: atMain && !!circuitMenu }"
|
|
:title="atMain ? 'This circuit: tidy its wires, print, publish, duplicate' : 'Go to Main (components open with a double-click)'"
|
|
@click="atMain ? openCircuitMenu($event) : ed.goMain()"
|
|
>
|
|
<Icon name="lucide:house" class="size-4" />
|
|
<span class="main-label">Main</span>
|
|
<Icon v-if="atMain" name="lucide:chevron-down" class="size-3.5 text-(--muted)" />
|
|
</button>
|
|
<template v-if="crumbs.folded.length">
|
|
<Icon name="lucide:chevron-right" class="crumb-sep size-3.5" />
|
|
<button
|
|
class="bar-btn"
|
|
:class="{ open: !!foldMenu }"
|
|
:title="`${crumbs.folded.length} more on the way here: ${crumbs.folded.map(p => p.name).join(', ')}`"
|
|
@click="foldMenu = foldMenu ? null : below($event)"
|
|
>
|
|
<Icon name="lucide:ellipsis" class="size-4" />
|
|
</button>
|
|
</template>
|
|
<template v-for="p in crumbs.shown" :key="`${p.id}-${p.i}`">
|
|
<Icon name="lucide:chevron-right" class="crumb-sep size-3.5" />
|
|
<button
|
|
class="bar-btn crumb"
|
|
:class="{ here: p.i < 0, open: p.i < 0 && !!circuitMenu }"
|
|
:title="p.i < 0 ? 'This circuit: tidy its wires, design its box, print, publish, duplicate, delete' : `Back to ${p.name}`"
|
|
@click="p.i < 0 ? openCircuitMenu($event) : ed.goTo(p.i)"
|
|
>
|
|
<span class="truncate">{{ p.name }}</span>
|
|
<Icon v-if="p.i < 0" name="lucide:chevron-down" class="size-3.5 shrink-0 text-(--muted)" />
|
|
</button>
|
|
</template>
|
|
</nav>
|
|
<EditorContextMenu v-if="circuitMenu" :x="circuitMenu.x" :y="circuitMenu.y" :entries="circuitEntries" @close="circuitMenu = null" />
|
|
<EditorContextMenu v-if="foldMenu" :x="foldMenu.x" :y="foldMenu.y" :entries="foldEntries" @close="foldMenu = null" />
|
|
</div>
|
|
|
|
<div class="tb-group">
|
|
<div ref="challengesEl" class="relative">
|
|
<button v-if="tucked < 3" class="bar-btn" :class="{ open: showChallenges }" title="Challenges, puzzles and the puzzle of the week" @click="showChallenges = !showChallenges">
|
|
<Icon name="lucide:flag" class="size-4" />
|
|
<span class="bar-text">Challenges</span>
|
|
<span class="bar-count">{{ solvedCount }}/{{ CHALLENGES.length }}</span>
|
|
</button>
|
|
<Transition name="drop">
|
|
<div v-if="showChallenges" class="top-pop" :style="{ marginLeft: `${popShift}px` }">
|
|
<EditorChallengesPanel @close="showChallenges = false" />
|
|
</div>
|
|
</Transition>
|
|
</div>
|
|
<!-- On the narrowest bar Challenges is in "⋯" and the clock buttons are there only for a circuit with a clock. -->
|
|
<template v-if="hasClock || tucked < 3">
|
|
<span v-if="tucked < 3" class="bar-sep" />
|
|
<button
|
|
class="bar-btn"
|
|
:class="{ active: ed.running.value && hasClock }"
|
|
:title="ed.running.value ? 'Pause the clock' : 'Run the clock'"
|
|
:disabled="!hasClock"
|
|
@click="ed.running.value = !ed.running.value"
|
|
>
|
|
<Icon :name="ed.running.value ? 'lucide:pause' : 'lucide:play'" class="size-4" />
|
|
</button>
|
|
<button class="bar-btn" title="One clock cycle" :disabled="!hasClock" @click="ed.running.value = false; ed.step()">
|
|
<Icon name="lucide:step-forward" class="size-4" />
|
|
</button>
|
|
</template>
|
|
<select v-model.number="ed.hz.value" class="tb-select" title="Clock speed" :disabled="!hasClock">
|
|
<option v-for="s in SPEEDS" :key="s.value" :value="s.value">{{ s.label }}</option>
|
|
</select>
|
|
<span v-if="hasClock" class="cycles tabular-nums" title="Clock cycles since this circuit was opened">{{ ed.cycles.value.toLocaleString() }} ticks</span>
|
|
<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" />{{ statusText }}
|
|
</span>
|
|
</div>
|
|
|
|
<div class="tb-group">
|
|
<button v-if="tucked < 3" class="bar-btn" title="Find anything in the project (Ctrl+F)" @click="ed.dialog.value = 'search'"><Icon name="lucide:search" class="size-4" /></button>
|
|
<template v-if="tucked < 2">
|
|
<button class="bar-btn" title="Undo (Ctrl+Z)" :disabled="!ed.canUndo.value" @click="ed.undo()"><Icon name="lucide:undo-2" class="size-4" /></button>
|
|
<button class="bar-btn" title="Redo (Ctrl+Shift+Z)" :disabled="!ed.canRedo.value" @click="ed.redo()"><Icon name="lucide:redo-2" class="size-4" /></button>
|
|
</template>
|
|
<template v-if="!tucked">
|
|
<button class="bar-btn" :title="ed.sound.value.on ? 'Sound on (volume and clock ticks in Settings)' : 'Sound off'" @click="ed.setSound({ on: !ed.sound.value.on })">
|
|
<Icon :name="ed.sound.value.on ? 'lucide:volume-2' : 'lucide:volume-x'" class="size-4" />
|
|
</button>
|
|
<button class="bar-btn" title="Settings: gate symbols, wire crossings, current flow, tidy wiring, sound" @click="ed.dialog.value = 'settings'">
|
|
<Icon name="lucide:settings" class="size-4" />
|
|
</button>
|
|
<button class="bar-btn" title="Help (?)" @click="ed.showHelp.value = true"><Icon name="lucide:circle-help" class="size-4" /></button>
|
|
</template>
|
|
<button v-else class="bar-btn" :class="{ open: !!moreMenu }" title="More: sound, settings, help, …" @click="moreMenu = moreMenu ? null : below($event)">
|
|
<Icon name="lucide:ellipsis" class="size-4" />
|
|
</button>
|
|
<EditorContextMenu v-if="moreMenu" :x="moreMenu.x" :y="moreMenu.y" :entries="moreEntries" @close="moreMenu = null" />
|
|
<span class="bar-sep" />
|
|
<button v-if="tucked < 2" class="bar-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="bar-text">Share</span>
|
|
</button>
|
|
<button
|
|
class="bar-btn account-btn"
|
|
:class="ed.cloudState.value"
|
|
:title="ed.account.value ? `${ed.account.value.email}: ${cloudText}` : 'Sign in to keep your projects on every computer'"
|
|
@click="ed.dialog.value = 'account'"
|
|
>
|
|
<template v-if="ed.account.value">
|
|
<span class="avatar">{{ ed.account.value.email[0]!.toUpperCase() }}</span>
|
|
<span class="sync-dot" />
|
|
</template>
|
|
<template v-else>
|
|
<Icon name="lucide:user" class="size-4" />
|
|
<span class="bar-text">Sign in</span>
|
|
</template>
|
|
</button>
|
|
</div>
|
|
</header>
|
|
</div>
|
|
</template>
|