|
| 1 | +<script lang="ts"> |
| 2 | + import { sessionStore } from '$lib/stores/session.svelte'; |
| 3 | + import { gamesStore } from '$lib/stores/games.svelte'; |
| 4 | + import { useGameActions } from '$lib/composables/useGameActions.svelte'; |
| 5 | + import { fly } from 'svelte/transition'; |
| 6 | + import StatusSelector from './StatusSelector.svelte'; |
| 7 | + import ConfirmDialog from './ConfirmDialog.svelte'; |
| 8 | + import { onMount } from 'svelte'; |
| 9 | + import { getPreferredTitle } from '$lib/util'; |
| 10 | +
|
| 11 | + // svelte-ignore non_reactive_update |
| 12 | + let menuRef: HTMLDivElement; |
| 13 | + let showStatusSubmenu = $state(false); |
| 14 | + let isDeleteDialogOpen = $state(false); |
| 15 | +
|
| 16 | + const gameId = $derived(sessionStore.contextMenu.gameId); |
| 17 | + const game = $derived(gameId ? gamesStore.getById(gameId) : undefined); |
| 18 | +
|
| 19 | + const actions = useGameActions(() => game); |
| 20 | +
|
| 21 | + function close() { |
| 22 | + sessionStore.hideContextMenu(); |
| 23 | + showStatusSubmenu = false; |
| 24 | + } |
| 25 | +
|
| 26 | + async function toggleStatus(status: string) { |
| 27 | + if (!gameId) return; |
| 28 | + const currentStatuses = game?.categories || []; |
| 29 | + const newStatuses = currentStatuses.includes(status) |
| 30 | + ? currentStatuses.filter((s) => s !== status) |
| 31 | + : [...currentStatuses, status]; |
| 32 | + await gamesStore.setGameCategories(gameId, newStatuses); |
| 33 | + } |
| 34 | +
|
| 35 | + function handleKeydown(e: KeyboardEvent) { |
| 36 | + if (e.key === 'Escape') close(); |
| 37 | + } |
| 38 | +
|
| 39 | + onMount(() => { |
| 40 | + const handleClickOutside = (e: MouseEvent) => { |
| 41 | + if (menuRef && !menuRef.contains(e.target as Node)) { |
| 42 | + close(); |
| 43 | + } |
| 44 | + }; |
| 45 | + window.addEventListener('mousedown', handleClickOutside); |
| 46 | + return () => window.removeEventListener('mousedown', handleClickOutside); |
| 47 | + }); |
| 48 | +</script> |
| 49 | + |
| 50 | +<svelte:window onkeydown={handleKeydown} onblur={close} /> |
| 51 | + |
| 52 | +<!-- svelte-ignore a11y_no_static_element_interactions --> |
| 53 | +{#if sessionStore.contextMenu.visible && game} |
| 54 | + <div |
| 55 | + bind:this={menuRef} |
| 56 | + class="context-menu" |
| 57 | + role="menu" |
| 58 | + tabindex="-1" |
| 59 | + style="top: {sessionStore.contextMenu.y}px; left: {sessionStore.contextMenu |
| 60 | + .x}px;" |
| 61 | + transition:fly={{ duration: 100, y: 5 }} |
| 62 | + oncontextmenu={(e) => e.preventDefault()} |
| 63 | + > |
| 64 | + <div class="menu-header"> |
| 65 | + <span class="game-title">{getPreferredTitle(game)}</span> |
| 66 | + </div> |
| 67 | + |
| 68 | + <div class="menu-divider"></div> |
| 69 | + |
| 70 | + <button |
| 71 | + class="menu-item" |
| 72 | + onclick={async () => { |
| 73 | + await actions.startGame(); |
| 74 | + close(); |
| 75 | + }} |
| 76 | + > |
| 77 | + <i class="fa-solid fa-play"></i> |
| 78 | + Start Game |
| 79 | + </button> |
| 80 | + |
| 81 | + <button |
| 82 | + class="menu-item" |
| 83 | + onclick={async () => { |
| 84 | + await actions.togglePin(); |
| 85 | + close(); |
| 86 | + }} |
| 87 | + > |
| 88 | + <i |
| 89 | + class={game.is_pinned |
| 90 | + ? 'fa-solid fa-thumbtack-slash' |
| 91 | + : 'fa-solid fa-thumbtack'} |
| 92 | + ></i> |
| 93 | + {game.is_pinned ? 'Unpin' : 'Pin'} |
| 94 | + </button> |
| 95 | + |
| 96 | + <div class="menu-item-with-submenu"> |
| 97 | + <button |
| 98 | + class="menu-item" |
| 99 | + onmouseenter={() => (showStatusSubmenu = true)} |
| 100 | + onclick={() => (showStatusSubmenu = !showStatusSubmenu)} |
| 101 | + > |
| 102 | + <i class="fa-solid fa-tags"></i> |
| 103 | + Status |
| 104 | + <i class="fa-solid fa-chevron-right chevron"></i> |
| 105 | + </button> |
| 106 | + |
| 107 | + {#if showStatusSubmenu} |
| 108 | + <!-- svelte-ignore a11y_no_static_element_interactions --> |
| 109 | + <div |
| 110 | + class="status-submenu" |
| 111 | + role="menu" |
| 112 | + tabindex="-1" |
| 113 | + in:fly={{ x: 5, duration: 150 }} |
| 114 | + onmouseleave={() => (showStatusSubmenu = false)} |
| 115 | + > |
| 116 | + <StatusSelector |
| 117 | + categories={game.categories} |
| 118 | + {toggleStatus} |
| 119 | + clearStatuses={async () => { |
| 120 | + if (gameId) await gamesStore.setGameCategories(gameId, []); |
| 121 | + }} |
| 122 | + /> |
| 123 | + </div> |
| 124 | + {/if} |
| 125 | + </div> |
| 126 | + |
| 127 | + <div class="menu-divider"></div> |
| 128 | + |
| 129 | + <button |
| 130 | + class="menu-item" |
| 131 | + onclick={async () => { |
| 132 | + await actions.editExe(); |
| 133 | + close(); |
| 134 | + }} |
| 135 | + > |
| 136 | + <i class="fa-regular fa-pen-to-square"></i> |
| 137 | + Edit Executable |
| 138 | + </button> |
| 139 | + |
| 140 | + <button |
| 141 | + class="menu-item danger" |
| 142 | + onclick={async () => { |
| 143 | + isDeleteDialogOpen = true; |
| 144 | + close(); // Close the context menu immediately |
| 145 | + }} |
| 146 | + > |
| 147 | + <i class="fa-regular fa-trash-can"></i> |
| 148 | + Delete Game |
| 149 | + </button> |
| 150 | + </div> |
| 151 | +{/if} |
| 152 | + |
| 153 | +{#if game} |
| 154 | + <ConfirmDialog |
| 155 | + bind:isOpen={isDeleteDialogOpen} |
| 156 | + title="Delete Game" |
| 157 | + onConfirm={actions.deleteGame} |
| 158 | + isDanger |
| 159 | + message={`Are you sure you want to delete <i class="danger-highlight">${game.title}</i> ?`} |
| 160 | + /> |
| 161 | +{/if} |
| 162 | + |
| 163 | +<style> |
| 164 | + .context-menu { |
| 165 | + position: fixed; |
| 166 | + z-index: 9999; |
| 167 | + background: var(--main-background); |
| 168 | + border: 1px solid var(--accent); |
| 169 | + border-radius: var(--small-radius); |
| 170 | + box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5); |
| 171 | + padding: 0.5rem; |
| 172 | + min-width: 200px; |
| 173 | + user-select: none; |
| 174 | + } |
| 175 | +
|
| 176 | + .menu-header { |
| 177 | + padding: 0.5rem 0.75rem; |
| 178 | + max-width: 250px; |
| 179 | + } |
| 180 | +
|
| 181 | + .game-title { |
| 182 | + font-size: 0.8rem; |
| 183 | + font-weight: 700; |
| 184 | + color: var(--secondary); |
| 185 | + white-space: nowrap; |
| 186 | + overflow: hidden; |
| 187 | + text-overflow: ellipsis; |
| 188 | + display: block; |
| 189 | + } |
| 190 | +
|
| 191 | + .menu-divider { |
| 192 | + height: 1px; |
| 193 | + background: var(--accent); |
| 194 | + margin: 0.4rem 0; |
| 195 | + } |
| 196 | +
|
| 197 | + .menu-item { |
| 198 | + width: 100%; |
| 199 | + border: 0; |
| 200 | + border-radius: var(--small-radius); |
| 201 | + color: var(--main-text); |
| 202 | + background: transparent; |
| 203 | + padding: 0.6rem 0.75rem; |
| 204 | + font-size: 13px; |
| 205 | + cursor: pointer; |
| 206 | + transition: all 0.2s ease; |
| 207 | + display: flex; |
| 208 | + align-items: center; |
| 209 | + gap: 0.75rem; |
| 210 | + text-align: left; |
| 211 | + } |
| 212 | +
|
| 213 | + .menu-item:hover { |
| 214 | + background: var(--accent); |
| 215 | + } |
| 216 | +
|
| 217 | + .menu-item i { |
| 218 | + font-size: 14px; |
| 219 | + width: 16px; |
| 220 | + text-align: center; |
| 221 | + opacity: 0.8; |
| 222 | + } |
| 223 | +
|
| 224 | + .menu-item.danger { |
| 225 | + color: #f7768e; |
| 226 | + } |
| 227 | +
|
| 228 | + .menu-item.danger:hover { |
| 229 | + background: rgba(247, 118, 142, 0.1); |
| 230 | + } |
| 231 | +
|
| 232 | + .menu-item-with-submenu { |
| 233 | + position: relative; |
| 234 | + } |
| 235 | +
|
| 236 | + .chevron { |
| 237 | + margin-left: auto; |
| 238 | + font-size: 10px !important; |
| 239 | + } |
| 240 | +
|
| 241 | + .status-submenu { |
| 242 | + position: absolute; |
| 243 | + left: 100%; |
| 244 | + top: -5px; |
| 245 | + background: var(--main-background); |
| 246 | + border: 1px solid var(--accent); |
| 247 | + border-radius: var(--small-radius); |
| 248 | + box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5); |
| 249 | + padding: 0.5rem; |
| 250 | + min-width: 180px; |
| 251 | + margin-left: 0.25rem; |
| 252 | + } |
| 253 | +</style> |
0 commit comments