Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120// Native file dialogs without a windowing toolkit: shell out to what each// platform ships. macOS: osascript. Windows: PowerShell + WinForms. Linux:// zenity, then kdialog. Anything else reports unsupported and the UI falls// back to asking for a path.
export type DialogResult = { path: string } | { cancelled: true } | { unsupported: true };
async function run(cmd: string, args: string[]): Promise<string | undefined> { try { const out = await new Deno.Command(cmd, { args }).output(); if (!out.success) return undefined; const text = new TextDecoder().decode(out.stdout).trim(); return text || undefined; } catch { return undefined; }}
const psQuote = (s: string) => `'${s.replace(/'/g, "''")}'`;
export async function pickOpen(): Promise<DialogResult> { switch (Deno.build.os) { case 'darwin': { // No `of type` filter: it expects UTIs, not extensions, and greys out // every .tile on systems where the UTI isn't registered (dev mode). const path = await run('osascript', [ '-e', 'POSIX path of (choose file with prompt "Open a tile")', ]); return path ? { path } : { cancelled: true }; } case 'windows': { const script = `Add-Type -AssemblyName System.Windows.Forms; ` + `$f = New-Object System.Windows.Forms.OpenFileDialog; ` + `$f.Filter = 'Tiles (*.tile)|*.tile|All files (*.*)|*.*'; ` + `if ($f.ShowDialog() -eq 'OK') { Write-Output $f.FileName }`; const path = await run('powershell', ['-NoProfile', '-Command', script]); return path ? { path } : { cancelled: true }; } case 'linux': { const zenity = await run('zenity', ['--file-selection', '--title=Open a tile', '--file-filter=*.tile']); if (zenity) return { path: zenity }; const kdialog = await run('kdialog', ['--getopenfilename', '.', '*.tile']); if (kdialog) return { path: kdialog }; return { unsupported: true }; } default: return { unsupported: true }; }}
/** Show a file in the platform's file manager, selected when possible. */export async function revealInFolder(path: string): Promise<boolean> { switch (Deno.build.os) { case 'darwin': return (await run('open', ['-R', path])) !== undefined || (await new Deno.Command('open', { args: ['-R', path] }).output()).success; case 'windows': // explorer exits non-zero even on success; fire and report true. await new Deno.Command('explorer', { args: [`/select,${path}`] }).output().catch(() => {}); return true; case 'linux': { const dbus = await new Deno.Command('dbus-send', { args: [ '--session', '--dest=org.freedesktop.FileManager1', '--type=method_call', '/org/freedesktop/FileManager1', 'org.freedesktop.FileManager1.ShowItems', `array:string:file://${path}`, 'string:', ], }).output().catch(() => ({ success: false })); if (dbus.success) return true; const dir = path.replace(/\/[^/]+$/, ''); return (await new Deno.Command('xdg-open', { args: [dir] }).output().catch(() => ({ success: false }))).success; } default: return false; }}
export async function pickSave(defaultName: string): Promise<DialogResult> { const name = defaultName.replace(/[\\/:"<>|?*\n]/g, '-') || 'document.tile'; switch (Deno.build.os) { case 'darwin': { const path = await run('osascript', [ '-e', `POSIX path of (choose file name with prompt "Move tile to" default name "${name.replace(/"/g, '\\"')}")`, ]); return path ? { path } : { cancelled: true }; } case 'windows': { const script = `Add-Type -AssemblyName System.Windows.Forms; ` + `$f = New-Object System.Windows.Forms.SaveFileDialog; ` + `$f.FileName = ${psQuote(name)}; ` + `$f.Filter = 'Tiles (*.tile)|*.tile|All files (*.*)|*.*'; ` + `if ($f.ShowDialog() -eq 'OK') { Write-Output $f.FileName }`; const path = await run('powershell', ['-NoProfile', '-Command', script]); return path ? { path } : { cancelled: true }; } case 'linux': { const zenity = await run('zenity', [ '--file-selection', '--save', '--title=Move tile to', `--filename=${name}`, ]); if (zenity) return { path: zenity }; const kdialog = await run('kdialog', ['--getsavefilename', name]); if (kdialog) return { path: kdialog }; return { unsupported: true }; } default: return { unsupported: true }; }}