- Add floatingMenuStore.ts with global menu state management - Only one floating menu can be open at a time across the entire app - Standardize inactivity timeout to 2 seconds for all menus Updated components: - TrackMenu: integrate with global store, reduce timeout from 5s to 2s - AlbumMenu: integrate with global store, add inactivity timeout - Sidebar: integrate dropdown, context menu, and folder popover - PlaylistManagerView: integrate filter and sort dropdowns - Dropdown.svelte: integrate generic dropdown component The global store ensures that opening any menu (context menu, dropdown, popover) automatically closes all other open menus.
60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import { openMenu as openGlobalMenu, closeMenu as closeGlobalMenu, subscribe as subscribeGlobal, getActiveMenuId } from './floatingMenuStore';
|
|
|
|
type Listener = (menuId: number | null) => void;
|
|
|
|
let activeMenuId: number | null = null;
|
|
const listeners = new Set<Listener>();
|
|
let nextMenuId = 1;
|
|
|
|
// Generate global menu ID for track menus
|
|
function getGlobalMenuId(localId: number): string {
|
|
return `track-menu-${localId}`;
|
|
}
|
|
|
|
export function allocateTrackMenuId(): number {
|
|
// Monotonic id to ensure only one TrackMenu instance matches the active id at a time.
|
|
return nextMenuId++;
|
|
}
|
|
|
|
export function getActiveTrackMenuId(): number | null {
|
|
return activeMenuId;
|
|
}
|
|
|
|
export function setActiveTrackMenuId(menuId: number | null): void {
|
|
if (activeMenuId === menuId) return;
|
|
|
|
// Close previous in global store
|
|
if (activeMenuId !== null) {
|
|
closeGlobalMenu(getGlobalMenuId(activeMenuId));
|
|
}
|
|
|
|
activeMenuId = menuId;
|
|
|
|
// Open new in global store
|
|
if (menuId !== null) {
|
|
openGlobalMenu(getGlobalMenuId(menuId));
|
|
}
|
|
|
|
for (const listener of listeners) listener(activeMenuId);
|
|
}
|
|
|
|
export function subscribeActiveTrackMenuId(listener: Listener): () => void {
|
|
listeners.add(listener);
|
|
listener(activeMenuId);
|
|
return () => {
|
|
listeners.delete(listener);
|
|
};
|
|
}
|
|
|
|
// Subscribe to global store to close track menus when other menus open
|
|
subscribeGlobal(() => {
|
|
const globalActive = getActiveMenuId();
|
|
// If global active menu is not a track menu, close any open track menu
|
|
if (globalActive !== null && !globalActive.startsWith('track-menu-')) {
|
|
if (activeMenuId !== null) {
|
|
activeMenuId = null;
|
|
for (const listener of listeners) listener(null);
|
|
}
|
|
}
|
|
});
|