mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 16:54:04 +00:00
Lay the pack menu out as IntelliJ's project widget
- Split the menu into the open pack and the other packs - Give each pack a coloured badge with its initials and its folder and version under its name - Drop Disconnect from the menu
This commit is contained in:
1 parent
fe99d5bda5
commit
55141349ef
5 files changed
+105
-31
No files matched your search
@@ -233,7 +233,7 @@ export function MapStudio({ client, world, theme, onOpenPack, onDisconnect }: Ma
|
||||
|
||||
return (
|
||||
<div className="flex h-full flex-col">
|
||||
<StudioToolbar world={world} openingPack={openingPack} loadPacks={loadPacks} onOpenPack={openPack} onHelp={() => setHelpOpen(true)} onSettings={() => setSettingsOpen(true)} onDisconnect={onDisconnect} />
|
||||
<StudioToolbar world={world} openingPack={openingPack} loadPacks={loadPacks} onOpenPack={openPack} onHelp={() => setHelpOpen(true)} onSettings={() => setSettingsOpen(true)} />
|
||||
<SidebarSlot.Provider value={sidebarSlot}>
|
||||
<div className="relative flex min-h-0 flex-1 gap-1.5 pr-1.5">
|
||||
{openingPack !== null && (
|
||||
|
||||
@@ -4,6 +4,7 @@ import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigge
|
||||
import { cn } from '@/lib/utils'
|
||||
import type { StudioPack, WorldInfo } from '../bridge/types'
|
||||
import { Glyph } from './Glyph'
|
||||
import { badgeColour, badgeInitials } from './packBadges'
|
||||
|
||||
interface PackSwitcherProps {
|
||||
world: WorldInfo
|
||||
@@ -11,12 +12,13 @@ interface PackSwitcherProps {
|
||||
opening: StudioPack | null
|
||||
loadPacks(): Promise<StudioPack[]>
|
||||
onOpenPack(pack: StudioPack): void
|
||||
onDisconnect(): void
|
||||
}
|
||||
|
||||
/** The open pack in the window's bar, which lists the server's packs to switch the studio to, as IntelliJ's project widget does. */
|
||||
export function PackSwitcher({ world, opening, loadPacks, onOpenPack, onDisconnect }: PackSwitcherProps): React.JSX.Element {
|
||||
const [packs, setPacks] = useState<StudioPack[] | null>(null)
|
||||
/** The open pack in the window's bar, which lists the server's other packs to switch the studio to, as IntelliJ's project widget does. */
|
||||
export function PackSwitcher({ world, opening, loadPacks, onOpenPack }: PackSwitcherProps): React.JSX.Element {
|
||||
const [packs, setPacks] = useState<StudioPack[]>([])
|
||||
const openPack = opening ?? { key: world.dimension, name: world.dimensionName, version: world.packVersion }
|
||||
const otherPacks = packs.filter((pack) => pack.key !== world.dimension)
|
||||
const refreshPacks = (open: boolean): void => {
|
||||
if (!open) {
|
||||
return
|
||||
@@ -25,42 +27,65 @@ export function PackSwitcher({ world, opening, loadPacks, onOpenPack, onDisconne
|
||||
.then(setPacks)
|
||||
.catch(() => setPacks([]))
|
||||
}
|
||||
const shownName = opening?.name ?? world.dimensionName
|
||||
const shownVersion = opening?.version ?? world.packVersion
|
||||
return (
|
||||
<DropdownMenu onOpenChange={refreshPacks}>
|
||||
<DropdownMenuTrigger asChild disabled={opening !== null}>
|
||||
<Button variant="ghost" size="sm" className="h-[30px] max-w-[50%] gap-2 px-2 font-semibold data-[state=open]:bg-accent">
|
||||
<PackBadge name={shownName} />
|
||||
<span className="truncate">{shownName}</span>
|
||||
{shownVersion !== undefined && <span className="shrink-0 font-normal text-secondary-text tabular-nums">v{shownVersion}</span>}
|
||||
<PackBadge pack={openPack} />
|
||||
<span className="truncate">{openPack.name}</span>
|
||||
{openPack.version !== undefined && <span className="shrink-0 font-normal text-secondary-text tabular-nums">v{openPack.version}</span>}
|
||||
<Glyph glyph={opening === null ? 'chevronDown' : 'refresh'} className={cn(opening !== null && 'animate-spin')} />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="start" className="w-64">
|
||||
{packs === null && <p className="px-2 py-1 text-muted-foreground">Listing packs…</p>}
|
||||
{packs?.length === 0 && <p className="px-2 py-1 text-muted-foreground">No packs to switch to</p>}
|
||||
{packs?.map((pack) => {
|
||||
const current = pack.key === world.dimension
|
||||
return (
|
||||
<DropdownMenuItem key={pack.key} onSelect={() => !current && onOpenPack(pack)} className={cn(current && 'bg-selection')}>
|
||||
<PackBadge name={pack.name} />
|
||||
<span className="min-w-0 flex-1 truncate">{pack.name}</span>
|
||||
<span className="shrink-0 text-[11.5px] text-secondary-text tabular-nums">v{pack.version}</span>
|
||||
</DropdownMenuItem>
|
||||
)
|
||||
})}
|
||||
<div className="-mx-1 my-1 h-px bg-border" />
|
||||
<DropdownMenuItem onSelect={onDisconnect}>Disconnect</DropdownMenuItem>
|
||||
<DropdownMenuContent align="start" className="w-80 max-h-[min(640px,var(--radix-dropdown-menu-content-available-height))]">
|
||||
<SectionLabel>Open Pack</SectionLabel>
|
||||
<DropdownMenuItem className="bg-selection focus:bg-selection">
|
||||
<PackRow pack={openPack} />
|
||||
</DropdownMenuItem>
|
||||
{otherPacks.length > 0 && (
|
||||
<>
|
||||
<div className="-mx-1 my-1 h-px bg-border" />
|
||||
<SectionLabel>Packs</SectionLabel>
|
||||
{otherPacks.map((pack) => (
|
||||
<DropdownMenuItem key={pack.key} onSelect={() => onOpenPack(pack)}>
|
||||
<PackRow pack={pack} />
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
)
|
||||
}
|
||||
|
||||
function PackBadge({ name }: { name: string }): React.JSX.Element {
|
||||
/** A pack as the version may be unknown for: a server that does not send it. */
|
||||
type ShownPack = Omit<StudioPack, 'version'> & { version?: number }
|
||||
|
||||
function PackRow({ pack }: { pack: ShownPack }): React.JSX.Element {
|
||||
return (
|
||||
<span className="flex size-5 shrink-0 items-center justify-center rounded-[5px] bg-primary text-[11px] font-semibold text-primary-foreground uppercase">
|
||||
{name.charAt(0)}
|
||||
<span className="flex min-w-0 flex-1 items-center gap-2.5 py-1">
|
||||
<PackBadge pack={pack} />
|
||||
<span className="flex min-w-0 flex-col">
|
||||
<span className="truncate">{pack.name}</span>
|
||||
<span className="truncate text-[11.5px] text-secondary-text">
|
||||
{pack.version === undefined ? pack.key : `${pack.key} · v${pack.version}`}
|
||||
</span>
|
||||
</span>
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
function SectionLabel({ children }: { children: React.ReactNode }): React.JSX.Element {
|
||||
return <p className="px-2 pt-1.5 pb-1 text-[11.5px] font-semibold text-muted-foreground">{children}</p>
|
||||
}
|
||||
|
||||
function PackBadge({ pack }: { pack: ShownPack }): React.JSX.Element {
|
||||
return (
|
||||
<span
|
||||
className="flex h-5 min-w-5 shrink-0 items-center justify-center rounded-[5px] px-[3px] text-[10.5px] font-semibold text-white"
|
||||
style={{ backgroundColor: badgeColour(pack.key) }}
|
||||
>
|
||||
{badgeInitials(pack.name)}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
@@ -21,15 +21,14 @@ interface StudioToolbarProps {
|
||||
onOpenPack(pack: StudioPack): void
|
||||
onHelp(): void
|
||||
onSettings(): void
|
||||
onDisconnect(): void
|
||||
}
|
||||
|
||||
/** The window's bar: the pack the studio is on, which switches to another, and the studio's own buttons. */
|
||||
export function StudioToolbar({ world, openingPack, loadPacks, onOpenPack, onHelp, onSettings, onDisconnect }: StudioToolbarProps): React.JSX.Element {
|
||||
export function StudioToolbar({ world, openingPack, loadPacks, onOpenPack, onHelp, onSettings }: StudioToolbarProps): React.JSX.Element {
|
||||
return (
|
||||
<header className="flex h-10 shrink-0 items-center gap-1 px-1.5">
|
||||
<IrisLogo className="mx-[5px] size-5" />
|
||||
<PackSwitcher world={world} opening={openingPack} loadPacks={loadPacks} onOpenPack={onOpenPack} onDisconnect={onDisconnect} />
|
||||
<PackSwitcher world={world} opening={openingPack} loadPacks={loadPacks} onOpenPack={onOpenPack} />
|
||||
<div className="flex-1" />
|
||||
<MainToolbarButton action="help" hint="Show every mouse and key shortcut" onClick={onHelp}>
|
||||
<Glyph glyph="help" large />
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { badgeColour, badgeInitials } from './packBadges'
|
||||
|
||||
describe('badgeInitials', () => {
|
||||
it('takes the first letter of a one word name', () => {
|
||||
expect(badgeInitials('Etopia')).toBe('E')
|
||||
})
|
||||
|
||||
it('takes the first letters of the first two words, split at separators, capitals and digits', () => {
|
||||
expect(badgeInitials('voxy-modular')).toBe('VM')
|
||||
expect(badgeInitials('IrisV5')).toBe('IV')
|
||||
expect(badgeInitials('CodexProvider')).toBe('CP')
|
||||
expect(badgeInitials('the overworld pack')).toBe('TO')
|
||||
})
|
||||
|
||||
it('marks a name without letters or digits', () => {
|
||||
expect(badgeInitials('--')).toBe('?')
|
||||
})
|
||||
})
|
||||
|
||||
describe('badgeColour', () => {
|
||||
it('gives a pack the same colour every time', () => {
|
||||
expect(badgeColour('natureplanet')).toBe(badgeColour('natureplanet'))
|
||||
})
|
||||
|
||||
it('spreads packs over the colours', () => {
|
||||
const colours = new Set(['natureplanet', 'overworld', 'cavetest', 'riverworld', 'underworld', 'etopia'].map(badgeColour))
|
||||
expect(colours.size).toBeGreaterThan(2)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,20 @@
|
||||
/** Badge colours as IntelliJ gives its projects: each pack keeps one, picked by its folder. */
|
||||
const BADGE_COLOURS = ['#3f9b5e', '#e07a50', '#1fa0b4', '#c0479c', '#9a55d8', '#5f6ce0', '#4a8fdc', '#7ea64a', '#c89a22'] as const
|
||||
const MOST_INITIALS = 2
|
||||
const WORD_BREAKS = /[^\p{L}\p{N}]+|(?<=\p{Ll})(?=\p{Lu})|(?<=\p{L})(?=\p{N})/u
|
||||
|
||||
/** The first letters of a pack name's words, at most two: "IrisV5" is "IV", "voxy-modular" "VM", "Etopia" "E". */
|
||||
export function badgeInitials(name: string): string {
|
||||
const words = name.split(WORD_BREAKS).filter((word) => word !== '')
|
||||
const initials = words.slice(0, MOST_INITIALS).map((word) => word.charAt(0).toUpperCase()).join('')
|
||||
return initials === '' ? '?' : initials
|
||||
}
|
||||
|
||||
/** The colour of a pack's badge, the same every time for the same folder. */
|
||||
export function badgeColour(packKey: string): string {
|
||||
let hash = 0
|
||||
for (const character of packKey) {
|
||||
hash = (hash * 31 + character.charCodeAt(0)) | 0
|
||||
}
|
||||
return BADGE_COLOURS[Math.abs(hash) % BADGE_COLOURS.length]
|
||||
}
|
||||
Reference in new issue
Block a user