Something went wrong. Try again.
Monorepo for Aesthetic.Computer aesthetic.computer
Something went wrong. Try again.
121 kB · 3451 lines
JavaScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452/** * Aesthetic.Computer - Electron Main Process * * Single window type: * - AC Pane (3D flip view with front webview + back terminal) */
const { app, BrowserWindow, ipcMain, globalShortcut, Menu, Tray, dialog, shell, nativeImage, screen, Notification, net, session, systemPreferences, nativeTheme } = require('electron');const path = require('path');const fs = require('fs');const http = require('http');const crypto = require('crypto');const { spawn, execSync } = require('child_process');
// FF1 Bridge - local server for kidlisp.com to communicate with FF1 devicesconst ff1Bridge = require('./ff1-bridge');
// =============================================================================// DEV MODE - Load files from repo instead of app bundle// =============================================================================// To enable: touch ~/.ac-electron-dev// To disable: rm ~/.ac-electron-dev// When enabled, renderer files load from ~/aesthetic-computer/ac-electron/// This lets you iterate on the Electron app without rebuilding!// =============================================================================const DEV_FLAG_PATH = path.join(require('os').homedir(), '.ac-electron-dev');const DEV_REPO_PATH = path.join(require('os').homedir(), 'aesthetic-computer', 'ac-electron');const isDevMode = fs.existsSync(DEV_FLAG_PATH) && fs.existsSync(DEV_REPO_PATH);
if (isDevMode) { console.log('[main] 🔧 DEV MODE ENABLED - loading files from:', DEV_REPO_PATH);}
// Detect PaperWM tiling window manager (GNOME extension)let isPaperWM = false;if (process.platform === 'linux') { try { const extensions = execSync('gnome-extensions list --enabled 2>/dev/null', { encoding: 'utf8', timeout: 3000 }); isPaperWM = extensions.includes('paperwm'); if (isPaperWM) console.log('[main] PaperWM detected - using tiling-friendly window mode'); } catch (e) { // Not GNOME or gnome-extensions not available }}
// =============================================================================// 🎵 Drag-drop audio onto Dock icon / running window// Drag an mp3/wav/flac/ogg/m4a onto the app and the `dj` piece opens it.// Files are served from a localhost streaming http server (Range supported)// so scrubbing works and we don't blow up memory on big files.// =============================================================================const AC_DROP_AUDIO_EXTS = new Set([ '.mp3', '.wav', '.flac', '.ogg', '.m4a', '.aac', '.opus',]);const AC_DROP_MIME = { '.mp3': 'audio/mpeg', '.wav': 'audio/wav', '.flac': 'audio/flac', '.ogg': 'audio/ogg', '.m4a': 'audio/mp4', '.aac': 'audio/aac', '.opus': 'audio/ogg',};const acDropTokens = new Map(); // token -> { path, mime, name }let acDropPendingPayload = null; // queued for the next ready windowlet acDropAudioServer = null;let acDropAudioServerPort = null;
function acDropIsAudio(filePath) { if (!filePath) return false; return AC_DROP_AUDIO_EXTS.has(path.extname(filePath).toLowerCase());}
function acDropMimeFor(filePath) { const ext = path.extname(filePath).toLowerCase(); return AC_DROP_MIME[ext] || 'application/octet-stream';}
function acDropEnsureAudioServer() { if (acDropAudioServer) return Promise.resolve(acDropAudioServerPort); return new Promise((resolve, reject) => { const server = http.createServer((req, res) => { try { const m = req.url.match(/^\/files\/([A-Za-z0-9_-]+)(?:\?.*)?$/); if (!m) { res.writeHead(404); res.end('not found'); return; } const entry = acDropTokens.get(m[1]); if (!entry) { res.writeHead(404); res.end('expired'); return; } let stat; try { stat = fs.statSync(entry.path); } catch (e) { res.writeHead(404); res.end('missing'); return; }
const total = stat.size; const range = req.headers.range; const baseHeaders = { 'Content-Type': entry.mime, 'Accept-Ranges': 'bytes', 'Access-Control-Allow-Origin': '*', 'Cache-Control': 'no-store', };
if (range) { const rm = range.match(/bytes=(\d*)-(\d*)/); if (!rm) { res.writeHead(416); res.end(); return; } let start = rm[1] ? parseInt(rm[1], 10) : 0; let end = rm[2] ? parseInt(rm[2], 10) : total - 1; if (isNaN(start) || isNaN(end) || start > end || end >= total) { res.writeHead(416, { ...baseHeaders, 'Content-Range': `bytes */${total}` }); res.end(); return; } res.writeHead(206, { ...baseHeaders, 'Content-Range': `bytes ${start}-${end}/${total}`, 'Content-Length': end - start + 1, }); if (req.method === 'HEAD') { res.end(); return; } fs.createReadStream(entry.path, { start, end }).pipe(res); } else { res.writeHead(200, { ...baseHeaders, 'Content-Length': total }); if (req.method === 'HEAD') { res.end(); return; } fs.createReadStream(entry.path).pipe(res); } } catch (e) { try { res.writeHead(500); res.end(String(e?.message || e)); } catch {} } }); server.on('error', reject); server.listen(0, '127.0.0.1', () => { acDropAudioServer = server; acDropAudioServerPort = server.address().port; console.log('[ac-drop] loopback audio server on 127.0.0.1:' + acDropAudioServerPort); resolve(acDropAudioServerPort); }); });}
async function acDropRegisterFile(filePath) { if (!acDropIsAudio(filePath)) return null; if (!fs.existsSync(filePath)) { console.warn('[ac-drop] dropped file missing:', filePath); return null; } const port = await acDropEnsureAudioServer(); const token = crypto.randomBytes(8).toString('hex'); const name = path.basename(filePath); const mime = acDropMimeFor(filePath); acDropTokens.set(token, { path: filePath, mime, name }); return { url: `http://127.0.0.1:${port}/files/${token}`, name, mime };}
function acDropBroadcast(payload) { let any = false; for (const entry of windows.values()) { const w = entry?.window; if (w && !w.isDestroyed() && entry.mode === 'ac-pane') { any = true; try { w.webContents.send('ac-dropped-file', payload); } catch {} try { if (w.isMinimized()) w.restore(); w.focus(); } catch {} } } return any;}
async function acDropHandleFile(filePath) { const payload = await acDropRegisterFile(filePath); if (!payload) return; console.log('[ac-drop] dropped file ready:', payload.name, payload.url); const delivered = acDropBroadcast(payload); if (!delivered) { acDropPendingPayload = payload; if (typeof openAcPaneWindow === 'function') { try { const opened = await openAcPaneWindow(); const win = opened?.window; if (win && !win.isDestroyed()) { win.webContents.once('did-finish-load', () => { if (acDropPendingPayload) { try { win.webContents.send('ac-dropped-file', acDropPendingPayload); } catch {} // Leave acDropPendingPayload set — flip-view re-sends on webview // dom-ready, and we want the in-page handler to win. } }); } } catch (e) { console.warn('[ac-drop] failed to open ac pane for dropped file:', e?.message || e); } } }}
// macOS: file drops on the Dock icon arrive via the 'open-file' event.// This handler MUST be registered before 'ready' / 'will-finish-launching'// fires, so we attach it at module load.app.on('open-file', (event, filePath) => { event.preventDefault(); console.log('[ac-drop] open-file (mac):', filePath); if (acDropIsAudio(filePath)) { // If app isn't ready yet, queue once whenReady resolves. if (app.isReady()) acDropHandleFile(filePath); else app.whenReady().then(() => acDropHandleFile(filePath)); }});
// Windows / Linux: route subsequent launches through the primary instance so// drag-onto-icon (and Open With ...) hits second-instance instead of starting// a fresh app.const acDropSingleLock = app.requestSingleInstanceLock();if (!acDropSingleLock) { app.quit();} else { app.on('second-instance', (_event, argv) => { // `--preview <url>` from a relaunch (slab requesting a preview): open a // new frameless preview window in this primary instance and stop — don't // fall through to focusing an unrelated existing window. const previewURL = parsePreviewArg(argv); if (previewURL) { app.whenReady().then(() => openPreviewWindow(previewURL)); return; } for (const arg of argv) { if (acDropIsAudio(arg)) { app.whenReady().then(() => acDropHandleFile(arg)); } } // Surface an existing window when re-launched with a file. for (const entry of windows.values()) { const w = entry?.window; if (w && !w.isDestroyed()) { try { if (w.isMinimized()) w.restore(); w.focus(); } catch {} break; } } });}
// Helper to get file path (repo in dev mode, bundle in production)function getAppPath(relativePath) { if (isDevMode) { return path.join(DEV_REPO_PATH, relativePath); } return path.join(__dirname, relativePath);}
// macOS Tahoe + Chromium fontations workaround (testing with Electron 39 / Chromium M142)// Disable problematic font features that may trigger fontations_ffi crashapp.commandLine.appendSwitch('disable-features', 'FontationsFontBackend,Fontations');if (process.platform === 'darwin') { app.commandLine.appendSwitch('use-angle', 'metal');} else if (process.platform === 'linux') { app.commandLine.appendSwitch('enable-gpu-rasterization'); app.commandLine.appendSwitch('enable-zero-copy'); app.commandLine.appendSwitch('ignore-gpu-blocklist'); app.commandLine.appendSwitch('enable-native-gpu-memory-buffers'); app.commandLine.appendSwitch('enable-accelerated-video-decode'); app.commandLine.appendSwitch('enable-webgl2-compute-context'); app.commandLine.appendSwitch('canvas-oop-rasterization');}// Performance: no throttling, more memory, high-priority audioapp.commandLine.appendSwitch('disable-renderer-backgrounding');app.commandLine.appendSwitch('disable-background-timer-throttling');app.commandLine.appendSwitch('disable-backgrounding-occluded-windows');app.commandLine.appendSwitch('js-flags', '--max-old-space-size=4096');app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required');
// Always expose a local CDP endpoint so external drivers (slab, Claude) can// attach to any window/preview — eval, screenshot, instrument. Idempotent: if// a launch flag already set the port (npm start scripts pass 9222) this is a// no-op. Chromium binds remote debugging to 127.0.0.1 by default → local-only.if (!process.argv.some((a) => a.startsWith('--remote-debugging-port'))) { app.commandLine.appendSwitch('remote-debugging-port', '9222');}
// Preferences storageconst PREFS_PATH = path.join(app.getPath('userData'), 'preferences.json');let preferences = { showTrayTitle: true, trayTitleText: 'Aesthetic.Computer', // Short text next to tray icon launchAtLogin: true, defaultMode: 'ac-pane', // Window float behavior. When true, new AC Pane / Notepat windows // open with alwaysOnTop so they float above other apps (the old // default). When false (new default), they behave like normal windows // and can be ordered underneath. Toggled from the tray menu. alwaysOnTop: false, // When the Notepat Overlay is open, should it pass mouse events // through to the app underneath (visual-only widget) or capture them // (playable floating piano)? Toggled from the tray while the overlay // is open. overlayClickThrough: false,};
function loadPreferences() { try { if (fs.existsSync(PREFS_PATH)) { const data = fs.readFileSync(PREFS_PATH, 'utf8'); preferences = { ...preferences, ...JSON.parse(data) }; } } catch (e) { console.warn('[prefs] Failed to load preferences:', e.message); }}
function savePreferences() { try { fs.writeFileSync(PREFS_PATH, JSON.stringify(preferences, null, 2)); } catch (e) { console.warn('[prefs] Failed to save preferences:', e.message); }}
// Auto-updater (only in production builds)let autoUpdater;let autoUpdaterError = null;let updateDownloaded = false;let updateAvailable = null; // { version, url, releaseNotes }let trayBlinkInterval = null;let trayIconState = 'normal'; // 'normal', 'update', 'blink'let originalTrayIcon = null;let updateTrayIcon = null;let trayRenderer = null; // animatable, live-editable pals+badge tray rendererlet keepsCount = null; // live KidLisp keeps count (tray superscript + menu)const UPDATE_CHECK_INTERVAL = 60 * 60 * 1000; // Check every hourconst SILO_RELEASE_URL = 'https://silo.aesthetic.computer/desktop/latest';
// KidLisp keeps count — surfaced as a top-right superscript on the pals tray// icon (Menuband instrument-number style). The active contract is resolved// from keeps-config (Mongo-backed), then counted via TzKT.const KEEPS_CONFIG_URL = 'https://aesthetic.computer/api/keeps-config?network=mainnet';const KEEPS_COUNT_INTERVAL = 10 * 60 * 1000; // refresh every 10 minutes
try { autoUpdater = require('electron-updater').autoUpdater; // Configure auto-updater for silent background downloads autoUpdater.autoDownload = true; // Download automatically in background autoUpdater.autoInstallOnAppQuit = true; // Install on next launch autoUpdater.on('checking-for-update', () => { console.log('[updater] Checking for updates...'); }); autoUpdater.on('update-available', (info) => { console.log('[updater] Update available:', info.version, '- downloading in background...'); }); autoUpdater.on('update-not-available', () => { console.log('[updater] App is up to date'); }); autoUpdater.on('download-progress', (progress) => { console.log(`[updater] Downloading: ${Math.round(progress.percent)}%`); }); autoUpdater.on('update-downloaded', (info) => { console.log('[updater] Update downloaded:', info.version); updateDownloaded = true; // Show subtle notification - update will install on next launch if (Notification.isSupported()) { const notification = new Notification({ title: 'Update Ready', body: `v${info.version} will install on next launch. Click to restart now.`, icon: path.join(__dirname, 'build', 'icon.png'), silent: true }); notification.on('click', () => { autoUpdater.quitAndInstall(false, true); }); notification.show(); } }); autoUpdater.on('error', (err) => { console.error('[updater] Error:', err.message); }); } catch (e) { autoUpdaterError = e.message; console.log('[updater] Auto-updater not available:', e.message);}
// Start periodic update checksfunction startUpdateChecks() { console.log('[updater] startUpdateChecks called, autoUpdater:', !!autoUpdater, 'isPackaged:', app.isPackaged); if (!autoUpdater) { console.log('[updater] Skipping - autoUpdater not loaded, error:', autoUpdaterError); return; } if (!app.isPackaged) { console.log('[updater] Skipping - not a packaged build'); return; } const checkForUpdates = () => { if (!updateDownloaded) { console.log('[updater] Running update check...'); autoUpdater.checkForUpdates().catch(err => { console.log('[updater] Check failed:', err.message); }); } }; // Initial check after 5 seconds setTimeout(checkForUpdates, 5000); // Then check every hour setInterval(checkForUpdates, UPDATE_CHECK_INTERVAL);}
// Silo release checking (works in dev mode too)function checkSiloForUpdates() { const currentVersion = app.getVersion(); console.log('[silo] Checking for updates, current version:', currentVersion);
const request = net.request(SILO_RELEASE_URL); request.setHeader('User-Agent', 'Aesthetic-Computer-Electron');
let responseData = '';
request.on('response', (response) => { response.on('data', (chunk) => { responseData += chunk.toString(); });
response.on('end', () => { try { const release = JSON.parse(responseData); const latestVersion = release.version;
if (latestVersion && isNewerVersion(latestVersion, currentVersion)) { console.log('[silo] Update available:', latestVersion); const platformKey = process.platform === 'darwin' ? 'mac' : process.platform === 'win32' ? 'win' : 'linux'; updateAvailable = { version: latestVersion, url: release.platforms?.[platformKey]?.url || 'https://aesthetic.computer/desktop', releaseNotes: release.releaseNotes, publishedAt: release.publishedAt }; startTrayBlink(); rebuildTrayMenu(); } else { console.log('[silo] App is up to date'); } } catch (e) { console.warn('[silo] Failed to parse release info:', e.message); } }); });
request.on('error', (err) => { console.warn('[silo] Failed to check for updates:', err.message); });
request.end();}
// Compare semantic versionsfunction isNewerVersion(latest, current) { const parseVersion = (v) => v.split('.').map(n => parseInt(n, 10) || 0); const latestParts = parseVersion(latest); const currentParts = parseVersion(current); for (let i = 0; i < 3; i++) { const l = latestParts[i] || 0; const c = currentParts[i] || 0; if (l > c) return true; if (l < c) return false; } return false;}
// Minimal JSON GET over Electron's net stack (respects system proxy/DNS).function fetchJson(url, { timeoutMs = 8000 } = {}) { return new Promise((resolve, reject) => { const request = net.request(url); request.setHeader('User-Agent', 'Aesthetic-Computer-Electron'); let body = ''; const timer = setTimeout(() => { try { request.abort(); } catch (_) {} reject(new Error('timeout')); }, timeoutMs); request.on('response', (response) => { response.on('data', (chunk) => { body += chunk.toString(); }); response.on('end', () => { clearTimeout(timer); try { resolve(JSON.parse(body)); } catch (e) { reject(e); } }); }); request.on('error', (err) => { clearTimeout(timer); reject(err); }); request.end(); });}
// Resolve the active keeps contract, count its LIVE 1/1 tokens on TzKT, and push// the number onto the tray icon as a top-right superscript. Each keep is a unique// 1/1 KidLisp piece; burned pieces drop to totalSupply 0, so `totalSupply.gt=0`// counts only the active market tokens. Best-effort: any failure just leaves the// previous count (or none) in place.async function updateKeepsCount() { if (!trayRenderer) return; try { const cfg = await fetchJson(KEEPS_CONFIG_URL); const contract = cfg && cfg.contractAddress; if (!contract) return; const network = (cfg.network || 'mainnet').toLowerCase(); const apiBase = network === 'mainnet' ? 'https://api.tzkt.io' : `https://api.${network}.tzkt.io`; const count = await fetchJson(`${apiBase}/v1/tokens/count?contract=${encodeURIComponent(contract)}&totalSupply.gt=0`); const n = Number(count); if (!Number.isFinite(n)) return; console.log('[keeps] kept count:', n, '(', contract, ')'); trayRenderer.setState({ count: n }); if (n !== keepsCount) { // refresh the context-menu label on change keepsCount = n; try { rebuildTrayMenu(); } catch (_) {} } } catch (e) { console.warn('[keeps] count update failed:', e.message); }}
// Start polling the keeps count (initial fetch shortly after boot, then hourly-ish).function startKeepsCountPolling() { setTimeout(updateKeepsCount, 12000); setInterval(updateKeepsCount, KEEPS_COUNT_INTERVAL);}
// Start blinking tray iconfunction startTrayBlink() { if (trayBlinkInterval || !tray) return; console.log('[tray] Starting update blink indicator'); let blinkOn = true; trayBlinkInterval = setInterval(() => { if (!tray) { stopTrayBlink(); return; } if (blinkOn) { // Show update indicator (colored dot or different icon) if (updateTrayIcon) { tray.setImage(updateTrayIcon); } // Also update title to show update available if (process.platform === 'darwin') { tray.setTitle('⬆️ Update'); } } else { // Show normal icon if (originalTrayIcon) { tray.setImage(originalTrayIcon); } if (process.platform === 'darwin') { updateTrayTitle(); } } blinkOn = !blinkOn; }, 1500); // Blink every 1.5 seconds}
// Stop blinkingfunction stopTrayBlink() { if (trayBlinkInterval) { clearInterval(trayBlinkInterval); trayBlinkInterval = null; } if (tray && originalTrayIcon) { // Prefer the live renderer's composited icon (pals + keeps count) over // the plain fallback, so the superscript survives an update-blink cycle. if (trayRenderer) trayRenderer.refresh(); else tray.setImage(originalTrayIcon); updateTrayTitle(); }}
// Create update indicator icon (adds a colored badge)function createUpdateIcon(baseIcon) { if (process.platform === 'darwin') { // On macOS, we can't easily modify template images, so we'll use title instead return baseIcon; } // For Windows/Linux, create a modified icon with a badge try { const size = baseIcon.getSize(); const canvas = nativeImage.createEmpty(); // For now, just return the base icon - could enhance with badge overlay later return baseIcon; } catch (e) { return baseIcon; }}
// Start silo update checks (works in both dev and production)function startSiloUpdateChecks() { // Initial check after 10 seconds setTimeout(checkSiloForUpdates, 10000);
// Then check every hour setInterval(checkSiloForUpdates, UPDATE_CHECK_INTERVAL);}
// Set app name before anything elseapp.setName('Aesthetic.Computer');process.title = 'Aesthetic.Computer';
// Set dock icon on macOS — theme-aware: dark plate in dark mode, light plate// in light mode, re-applied live whenever the system appearance flips.function applyDockIcon() { if (process.platform !== 'darwin' || !app.dock) return; const variant = nativeTheme.shouldUseDarkColors ? 'icon.png' : 'icon-light.png'; const iconPath = path.join(__dirname, 'build', variant); try { const icon = nativeImage.createFromPath(iconPath); if (!icon.isEmpty()) { app.dock.setIcon(icon); console.log(`[dock] icon → ${variant} (${nativeTheme.shouldUseDarkColors ? 'dark' : 'light'} appearance)`); } else { console.warn(`[dock] icon missing or empty: ${iconPath}`); } } catch (e) { console.warn('Could not set dock icon:', e.message); }}applyDockIcon();nativeTheme.on('updated', applyDockIcon);
// On macOS, hide the dock icon when there are no visible windows so the app// acts as a pure menubar daemon. Shown again as soon as any window opens.function syncDockVisibility() { if (process.platform !== 'darwin' || !app.dock) return; const hasVisibleWindow = BrowserWindow.getAllWindows().some( (w) => !w.isDestroyed() && w.isVisible() ); if (hasVisibleWindow) { if (!app.dock.isVisible()) app.dock.show().catch(() => {}); } else { if (app.dock.isVisible()) app.dock.hide(); }}
// Try to load node-pty for real terminal supportlet pty;try { pty = require('node-pty');} catch (e) { console.warn('node-pty not available, terminal features will be limited');}
// Parse command line argsconst args = process.argv.slice(2);const startInDevMode = args.includes('--dev') || args.includes('--development');// NOTE: only read `--piece <name>` when the flag is actually present —// args.indexOf('--piece') is -1 when absent, and args[-1 + 1] = args[0] would// wrongly grab the first arg (e.g. '--dev'), making the piece '--dev', which// 404s on localhost and falls back to prompt-on-production.const pieceFlagIdx = args.indexOf('--piece');const pieceArg = args.find((a) => a.startsWith('--piece=')) || (pieceFlagIdx >= 0 ? args[pieceFlagIdx + 1] : undefined);const initialPiece = pieceArg?.replace('--piece=', '') || 'prompt';
// `--preview <url>` opens a frameless, CDP-attached preview window for any URL// (used by slab to render dynamic/web previews and tile them with terminals).function parsePreviewArg(list) { const eq = list.find((a) => a.startsWith('--preview=')); if (eq) return eq.slice('--preview='.length); const i = list.indexOf('--preview'); return i >= 0 ? list[i + 1] : undefined;}// Parse from the FULL argv, not the sliced `args`: a packaged app's argv is// [exePath, --preview, url] (slice(2) would drop --preview), whereas dev is// [electron, '.', --preview, url]. Searching the whole list handles both.const initialPreviewURL = parsePreviewArg(process.argv);
// URLs - nogap removes the aesthetic gap border for desktop modeconst URLS = { production: `https://aesthetic.computer/${initialPiece}?nogap=true`, development: `http://localhost:8888/${initialPiece}?nogap=true`};
// Track all windows: windowId -> { window, mode, ptyProcess? }const windows = new Map();let windowIdCounter = 0;let focusedWindowId = null;let mainWindowId = null; // Track the "main" window for tray title displaylet currentPiece = 'prompt'; // Current piece/prompt shown in main window
// Find docker binary path (needed for packaged app which may not have PATH set)function getDockerPath() { if (process.platform === 'darwin') { const dockerLocations = [ '/opt/homebrew/bin/docker', // Apple Silicon Homebrew '/usr/local/bin/docker', // Intel Homebrew / Docker Desktop '/Applications/Docker.app/Contents/Resources/bin/docker' ]; for (const loc of dockerLocations) { if (fs.existsSync(loc)) { return loc; } } } return 'docker';}
// Check if Docker is availableasync function checkDocker() { const dockerPath = getDockerPath(); return new Promise((resolve) => { const docker = spawn(dockerPath, ['info'], { stdio: 'pipe' }); docker.on('close', (code) => resolve(code === 0)); docker.on('error', () => resolve(false)); });}
// Check if devcontainer is runningasync function checkDevcontainer() { const dockerPath = getDockerPath(); return new Promise((resolve) => { const docker = spawn(dockerPath, ['ps', '--filter', 'name=aesthetic', '--format', '{{.Names}}'], { stdio: 'pipe' }); let output = ''; docker.stdout.on('data', (data) => output += data.toString()); docker.on('close', () => resolve(output.includes('aesthetic'))); docker.on('error', () => resolve(false)); });}
// Check if container exists (running or stopped)async function checkContainerExists() { const dockerPath = getDockerPath(); return new Promise((resolve) => { const docker = spawn(dockerPath, ['ps', '-a', '--filter', 'name=aesthetic', '--format', '{{.Names}}'], { stdio: 'pipe' }); let output = ''; docker.stdout.on('data', (data) => output += data.toString()); docker.on('close', () => resolve(output.includes('aesthetic'))); docker.on('error', () => resolve(false)); });}
// Start an existing stopped containerasync function startExistingContainer() { const dockerPath = getDockerPath(); return new Promise((resolve, reject) => { console.log('Starting existing container...'); const docker = spawn(dockerPath, ['start', 'aesthetic'], { stdio: 'pipe' }); docker.on('close', (code) => { if (code === 0) { console.log('Container started successfully'); resolve(true); } else { reject(new Error(`docker start exited with code ${code}`)); } }); docker.on('error', (err) => reject(err)); });}
// Start the devcontainerasync function startDevcontainer() { return new Promise((resolve, reject) => { // For packaged app, the workspace is one level up from the app bundle's Resources folder // When running in dev: __dirname is ac-electron, workspace is .. // When packaged: __dirname is inside app.asar, need to find actual workspace let workspaceFolder; if (__dirname.includes('app.asar')) { // Packaged app - assume workspace is at a known location or use env var workspaceFolder = process.env.AC_WORKSPACE || path.join(process.env.HOME, 'Desktop/code/aesthetic-computer'); } else { workspaceFolder = path.resolve(__dirname, '..'); } console.log('Starting devcontainer in:', workspaceFolder); // Find devcontainer CLI let devcontainerPath = 'devcontainer'; if (process.platform === 'darwin') { const locations = [ '/opt/homebrew/bin/devcontainer', '/usr/local/bin/devcontainer', path.join(process.env.HOME, '.npm-global/bin/devcontainer'), path.join(process.env.HOME, 'node_modules/.bin/devcontainer') ]; for (const loc of locations) { if (fs.existsSync(loc)) { devcontainerPath = loc; break; } } } console.log('Using devcontainer CLI:', devcontainerPath); const devcontainer = spawn(devcontainerPath, ['up', '--workspace-folder', workspaceFolder], { stdio: 'pipe', env: { ...process.env, PATH: `${process.env.PATH}:/usr/local/bin:/opt/homebrew/bin` } }); let output = ''; devcontainer.stdout.on('data', (data) => { output += data.toString(); console.log('[devcontainer]', data.toString()); }); devcontainer.stderr.on('data', (data) => { console.error('[devcontainer error]', data.toString()); }); devcontainer.on('close', (code) => { if (code === 0) { resolve(true); } else { reject(new Error(`devcontainer exited with code ${code}`)); } }); devcontainer.on('error', (err) => reject(err)); });}
// Get the focused window's modefunction getFocusedWindowMode() { if (focusedWindowId && windows.has(focusedWindowId)) { const mode = windows.get(focusedWindowId).mode; if (mode === 'production' || mode === 'development') { return mode; } } return (app.isPackaged && !startInDevMode) ? 'production' : 'development';}
// Get the focused windowfunction getFocusedWindow() { if (focusedWindowId && windows.has(focusedWindowId)) { return windows.get(focusedWindowId).window; } return null;}
function createMenu() { const isMac = process.platform === 'darwin'; const template = [ // App menu (macOS only) ...(isMac ? [{ label: 'Aesthetic.Computer', // Explicit name for menu bar submenu: [ { label: 'About Aesthetic.Computer', click: () => { dialog.showMessageBox({ type: 'info', title: 'About Aesthetic.Computer', message: 'Aesthetic.Computer', detail: `Version ${app.getVersion()}`, buttons: ['OK'] }); } }, { type: 'separator' }, { label: 'Check for Updates...', click: () => { if (autoUpdater) { if (updateDownloaded) { dialog.showMessageBox({ type: 'info', title: 'Update Ready', message: 'An update has already been downloaded.', detail: 'It will be installed when you restart the app.', buttons: ['Restart Now', 'Later'], defaultId: 0 }).then(({ response }) => { if (response === 0) { autoUpdater.quitAndInstall(false, true); } }); } else { autoUpdater.checkForUpdates().then(result => { if (!result || !result.updateInfo || result.updateInfo.version === app.getVersion()) { dialog.showMessageBox({ type: 'info', title: 'No Updates', message: 'You are running the latest version.', buttons: ['OK'] }); } }).catch(err => { dialog.showMessageBox({ type: 'error', title: 'Update Check Failed', message: 'Could not check for updates.', detail: err.message, buttons: ['OK'] }); }); } } else { // Show why auto-updater isn't available const reason = autoUpdaterError ? `Failed to load: ${autoUpdaterError}` : app.isPackaged ? 'Unknown error' : 'Only available in packaged builds'; dialog.showMessageBox({ type: 'info', title: 'Updates', message: 'Auto-updates are not available.', detail: reason, buttons: ['OK'] }); } } }, { type: 'separator' }, { label: 'Preferences...', accelerator: 'Cmd+,', click: () => openPreferencesWindow() }, { type: 'separator' }, { role: 'hide' }, { role: 'hideOthers' }, { role: 'unhide' }, { type: 'separator' }, { role: 'quit' } ] }] : []), // File menu { label: 'File', submenu: [ { label: 'New AC Pane', accelerator: 'CmdOrCtrl+N', click: () => openAcPaneWindow() }, { type: 'separator' }, isMac ? { role: 'close' } : { role: 'quit' } ] }, // Edit menu — macOS only delivers Cmd+C/V/X/A to the page when these // role items exist in the app menu; without them paste is dead app-wide. { label: 'Edit', submenu: [ { role: 'undo' }, { role: 'redo' }, { type: 'separator' }, { role: 'cut' }, { role: 'copy' }, { role: 'paste' }, ...(isMac ? [{ role: 'pasteAndMatchStyle' }] : []), { role: 'delete' }, { role: 'selectAll' } ] }, // Go menu (navigation) { label: 'Go', submenu: [ { label: 'Back', accelerator: 'CmdOrCtrl+[', click: () => getFocusedWindow()?.webContents.send('go-back') }, { label: 'Forward', accelerator: 'CmdOrCtrl+]', click: () => getFocusedWindow()?.webContents.send('go-forward') }, { type: 'separator' }, { label: 'Focus Location', accelerator: 'CmdOrCtrl+L', click: () => getFocusedWindow()?.webContents.send('focus-location') }, { type: 'separator' }, { label: 'Prompt', click: () => navigateTo('prompt') }, { label: 'Wand', click: () => navigateTo('wand') }, { label: 'Nopaint', click: () => navigateTo('nopaint') }, { label: 'Whistlegraph', click: () => navigateTo('whistlegraph') } ] }, // View menu { label: 'View', submenu: [ { role: 'reload' }, { role: 'forceReload' }, { type: 'separator' }, { role: 'resetZoom' }, { role: 'zoomIn' }, { role: 'zoomOut' }, { type: 'separator' }, { role: 'togglefullscreen' }, { type: 'separator' }, { label: 'Developer Tools', accelerator: isMac ? 'Cmd+Option+I' : 'Ctrl+Shift+I', click: () => getFocusedWindow()?.webContents.send('toggle-devtools') } ] }, // Window menu { label: 'Window', submenu: [ { role: 'minimize' }, { role: 'zoom' }, ...(isMac ? [ { type: 'separator' }, { role: 'front' } ] : [ { role: 'close' } ]) ] } ];
const menu = Menu.buildFromTemplate(template); Menu.setApplicationMenu(menu);}
function navigateTo(piece) { const mode = getFocusedWindowMode(); const baseUrl = mode === 'production' ? 'https://aesthetic.computer' : 'http://localhost:8888'; getFocusedWindow()?.webContents.send('navigate', `${baseUrl}/${piece}?nogap=true`);}
// ========== Experimental: native notepat (macOS) ==========// ac-native is currently a Linux-only bare-metal runtime (see fedac/native/).// A macOS build doesn't exist yet — when someone adds one and drops the// resulting Mach-O at any of the paths below (or points $AC_NATIVE_BIN at// it), the tray item "Launch Native Notepat" starts working automatically.// Until then, clicking the item shows an installation hint dialog.function resolveNativeBinary() { const repoRoot = path.resolve(__dirname, '..'); const candidates = [ process.env.AC_NATIVE_BIN, path.join(repoRoot, 'fedac', 'native', 'build', 'ac-native-macos'), path.join(repoRoot, 'fedac', 'native', 'build', 'ac-native'), '/usr/local/bin/ac-native', ].filter(Boolean); for (const p of candidates) { try { if (fs.statSync(p).isFile()) return { path: p, candidates }; } catch (_) { /* not found, keep looking */ } } return { path: null, candidates };}
function launchNativeNotepat() { if (process.platform !== 'darwin') { dialog.showMessageBox({ type: 'info', title: 'macOS only', message: 'Native notepat is a macOS-only experimental feature.', }); return; } const { path: binPath, candidates } = resolveNativeBinary(); if (!binPath) { dialog.showMessageBox({ type: 'warning', title: 'ac-native binary not found', message: 'No macOS build of ac-native is installed.', detail: 'ac-native is currently Linux-only bare metal. To enable this menu item, ' + 'build a macOS target from fedac/native/ (no Makefile target exists yet) and ' + 'drop the Mach-O binary at one of these paths, or point $AC_NATIVE_BIN at it:\n\n' + candidates.map((p) => ' • ' + p).join('\n'), }); return; } const repoRoot = path.resolve(__dirname, '..'); const pieceFile = path.join(repoRoot, 'fedac', 'native', 'pieces', 'notepat.mjs'); try { console.log('[native-notepat] spawning:', binPath, pieceFile); const child = spawn(binPath, [pieceFile], { detached: true, stdio: 'ignore', }); child.on('error', (err) => { console.warn('[native-notepat] spawn error:', err); dialog.showMessageBox({ type: 'error', title: 'Native notepat failed to start', message: err.message, }); }); child.unref(); } catch (err) { dialog.showMessageBox({ type: 'error', title: 'Native notepat failed to start', message: err.message, }); }}
// ========== Aesthetic Computer account (shared ~/.ac-token) ==========// The whole AC suite (ac-os, DateWizard, wizards, slab, menuband, …) shares one// login through a single file at ~/.ac-token, written by the `ac-login` CLI// (tezos/ac-login.mjs). This app acts as the account manager from its tray:// it can sign in (CLI runs the OAuth+PKCE browser flow and writes the file) and// sign out (CLI clears it), so every AC tool reads the same session.const os = require('os');const AC_TOKEN_PATH = path.join(os.homedir(), '.ac-token');// Dev: run the CLI straight from the repo (../tezos). Packaged: it's bundled// into Resources via extraResources (see package.json build.extraResources),// because __dirname points inside app.asar where ../tezos doesn't exist —// which is why installed builds showed "AC sign-in unavailable".const acLoginPath = app.isPackaged ? path.join(process.resourcesPath, 'ac-login.mjs') : path.join(__dirname, '..', 'tezos', 'ac-login.mjs');
// Read the shared AC session and derive a sign-in status + identity hint.// Returns { state: 'signed-in' | 'expired' | 'signed-out', who, expiresAt }.// Any failure (missing file, bad JSON, etc.) reads as 'signed-out'.function readAcSession() { try { if (!fs.existsSync(AC_TOKEN_PATH)) { return { state: 'signed-out', who: null, expiresAt: null }; } const raw = JSON.parse(fs.readFileSync(AC_TOKEN_PATH, 'utf8')); // Identity for display: the token file stores a `user` object (the // access_token itself is an encrypted JWE and not client-decodable). // Show the @handle only — never the email/name (PII). const handle = raw?.user?.handle; const who = handle ? (handle.startsWith('@') ? handle : '@' + handle) : null; const expiresAt = typeof raw?.expires_at === 'number' ? raw.expires_at : null; const state = expiresAt != null && expiresAt <= Date.now() ? 'expired' : 'signed-in'; return { state, who, expiresAt }; } catch (_) { return { state: 'signed-out', who: null, expiresAt: null }; }}
// Run the ac-login CLI under Electron's bundled node (no external node needed).// No args = interactive login (CLI opens the browser); ['logout'] clears the// token. When the child exits we refresh the tray status and notify the suite.function runAcLogin(args = []) { if (!fs.existsSync(acLoginPath)) { console.warn('[ac-creds] ac-login CLI not found at', acLoginPath); return; } try { console.log('[ac-creds] running ac-login', args.join(' ') || '(login)'); const child = spawn(process.execPath, [acLoginPath, ...args], { env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' }, stdio: 'ignore', detached: false, }); child.on('error', (err) => { console.warn('[ac-creds] ac-login spawn error:', err?.message || err); }); child.on('exit', (code) => { console.log('[ac-creds] ac-login exited with code', code); rebuildTrayMenu(); broadcastCredentialsChanged(); }); } catch (err) { console.warn('[ac-creds] failed to run ac-login:', err?.message || err); }}
// Signal the rest of the AC suite that the shared token changed.// Suite apps (DateWizard, wizards, slab, menuband) read ~/.ac-token directly;// a future enhancement can post an NSDistributedNotification here for instant// refresh. The real cross-app mechanism is the shared file itself.function broadcastCredentialsChanged() { console.log('[ac-creds] credentials changed; ~/.ac-token updated');}// ========== End Aesthetic Computer account ==========
// ========== System Tray ==========let tray = null;let notepatTray = null;let trayContextMenu = null; // Stored so macOS left-click can pop it explicitly.
// Parse the macOS/Windows accent color into {r,g,b}. Returns null when the// platform exposes no accent color (so the pals logo keeps its native pink).function getAccentRGB() { try { const hex = systemPreferences.getAccentColor?.(); if (!hex || hex.length < 6) return null; return { r: parseInt(hex.slice(0, 2), 16), g: parseInt(hex.slice(2, 4), 16), b: parseInt(hex.slice(4, 6), 16), }; } catch (_) { return null; }}
// Build the pals tray icon from the vector source. The pals SVG is a single// flat-fill path, so — like MenuBand's IconTinter — we recolor it to the macOS// system accent at runtime (here by swapping the fill hex before rasterizing),// then rasterize crisp with sharp. Shrunk a touch below the menubar height and// given a tight, hard drop shadow.async function buildPalsTrayImage() { let sharp; try { sharp = require('sharp'); } catch (e) { console.warn('[tray] sharp unavailable, falling back to raw icon:', e.message); return null; }
const dir = app.isPackaged ? process.resourcesPath : path.join(__dirname, 'build', 'icons'); const svgPath = path.join(dir, 'pals.svg'); if (!fs.existsSync(svgPath)) { console.warn('[tray] pals.svg not found:', svgPath); return null; }
// Always render the pals as PINK line-art (its native look) — ignore the // system accent, and don't flood-fill it into a solid silhouette. const PINK = '#cd5c9b'; console.log('[tray] pals icon — pink line-art');
const svg = fs.readFileSync(svgPath, 'utf8'); // Recolor the single-fill outline vector to pink; a faint same-color stroke // keeps the thin line crisp at small sizes. const pinkSvg = svg.replace( /fill="#[0-9a-fA-F]{3,8}"/g, `fill="${PINK}" stroke="${PINK}" stroke-width="0.5" stroke-linejoin="round"` ); const master = await sharp(Buffer.from(pinkSvg), { density: 1200 }) .trim() .png() .toBuffer();
const SHRINK = 0.94; // logo height relative to the full menubar slot const out = nativeImage.createEmpty(); let added = 0;
for (const scale of [1, 2, 3]) { try { const W = 34 * scale, H = 22 * scale; const lh = Math.max(1, Math.round(22 * SHRINK * scale));
const logo = await sharp(master) .resize({ height: lh, kernel: 'lanczos3' }) .ensureAlpha() .raw() .toBuffer({ resolveWithObject: true }); const lw = logo.info.width; const logoBuf = logo.data; // RGBA
const ox = Math.round((W - lw) / 2); const oy = Math.max(0, Math.round((H - lh) / 2));
const canvas = Buffer.alloc(W * H * 4, 0); // BGRA straight-alpha const put = (x, y, b, g, r, a) => { if (x < 0 || y < 0 || x >= W || y >= H || a <= 0) return; const idx = (y * W + x) * 4; const sa = a / 255; const da = canvas[idx + 3] / 255; const oa = sa + da * (1 - sa); if (oa <= 0) return; canvas[idx] = Math.round((b * sa + canvas[idx] * da * (1 - sa)) / oa); canvas[idx + 1] = Math.round((g * sa + canvas[idx + 1] * da * (1 - sa)) / oa); canvas[idx + 2] = Math.round((r * sa + canvas[idx + 2] * da * (1 - sa)) / oa); canvas[idx + 3] = Math.round(oa * 255); };
// The pink line-art, no drop shadow. for (let y = 0; y < lh; y++) for (let x = 0; x < lw; x++) { const j2 = (y * lw + x) * 4; const a = logoBuf[j2 + 3]; if (a > 0) put(ox + x, oy + y, logoBuf[j2 + 2], logoBuf[j2 + 1], logoBuf[j2], a); }
out.addRepresentation({ scaleFactor: scale, width: W, height: H, buffer: canvas }); added++; } catch (e) { console.warn('[tray] Failed to build pals rep @' + scale + 'x:', e.message); } }
return added > 0 ? out : null;}
async function createSystemTray() { // The pals tray icon: shrunk just below full menubar height, with a soft // drop shadow and a hue tint that follows the macOS accent color. let icon = process.platform === 'darwin' ? await buildPalsTrayImage() : null;
if (!icon || icon.isEmpty()) { // Fallback — load the raw PNG (Windows/Linux, or if processing failed). let iconPath; if (process.platform === 'darwin') { iconPath = app.isPackaged ? path.join(process.resourcesPath, 'palsTray.png') : path.join(__dirname, 'build', 'icons', 'palsTray.png'); } else { iconPath = app.isPackaged ? path.join(process.resourcesPath, 'tray-icon.png') : path.join(__dirname, 'build', 'icons', '16x16.png'); } console.log('[main] Loading tray icon from:', iconPath); icon = nativeImage.createFromPath(iconPath); }
if (!icon || icon.isEmpty()) { console.warn('[main] Tray icon is empty!'); return; }
// Pals is full color — do NOT mark it as a template image (template // images get auto-tinted to monochrome by AppKit).
// Store original icon for blink toggling originalTrayIcon = icon; updateTrayIcon = createUpdateIcon(icon);
tray = new Tray(icon); tray.setToolTip('Aesthetic.Computer'); console.log('[main] System tray created successfully');
// Build and set the context menu rebuildTrayMenu();
// Animatable, live-editable tray renderer (pals + optional badge label). // Default state hides the badge, so this renders the plain pals icon exactly // as before — until ~/.ac-os/tray.json sets {"show":true,"badge":"USB",...}, // which it watches and applies live (edit the file → icon updates instantly). try { const { TrayRenderer } = require('./tray-renderer'); if (trayRenderer) trayRenderer.dispose(); trayRenderer = new TrayRenderer(tray); trayRenderer.startWatching(); } catch (e) { console.warn('[tray] renderer init failed:', e.message); }
if (process.platform === 'darwin') { // macOS: pop the menu manually so a plain left-click reliably opens it // (relying on setContextMenu alone left clicks dead in some sessions). const popMenu = () => { if (trayContextMenu) tray.popUpContextMenu(trayContextMenu); }; tray.on('click', popMenu); tray.on('right-click', popMenu);
// Re-tint the icon live when the user changes their accent color. try { systemPreferences.on('accent-color-changed', () => { buildPalsTrayImage() .then((fresh) => { if (fresh && !fresh.isEmpty()) { originalTrayIcon = fresh; updateTrayIcon = createUpdateIcon(fresh); // The live renderer owns the visible icon (pals + count // superscript) and re-tints itself on accent change; only set // the plain icon directly when no renderer is active, so the // keeps count isn't momentarily wiped. if (tray && trayIconState === 'normal' && !trayRenderer) tray.setImage(fresh); } }) .catch(() => {}); // Re-tint every AC window's chrome to the new accent. const acc = getAccentRGB(); if (acc) { for (const w of BrowserWindow.getAllWindows()) { try { w.webContents.send('ac-accent', acc); } catch (_) {} } } }); } catch (_) {} } else { // Windows/Linux: click toggles the window. tray.on('click', () => { const allWindows = BrowserWindow.getAllWindows(); if (allWindows.length > 0) { const win = allWindows[0]; if (win.isVisible()) { win.hide(); } else { win.show(); } } else { openAcPaneWindow(); } }); }
// Set initial tray title updateTrayTitle();}
// Rebuild the tray context menu (called when update becomes available)function rebuildTrayMenu() { if (!tray) return; const isMac = process.platform === 'darwin'; const menuItems = []; // Update available section (if applicable) if (updateAvailable) { menuItems.push({ label: `🆕 Update Available: v${updateAvailable.version}`, click: () => { shell.openExternal(updateAvailable.url); stopTrayBlink(); } }); menuItems.push({ label: 'Download Update', click: () => { shell.openExternal(updateAvailable.url); stopTrayBlink(); } }); menuItems.push({ label: 'Dismiss', click: () => { updateAvailable = null; stopTrayBlink(); rebuildTrayMenu(); } }); menuItems.push({ type: 'separator' }); } // 🟢 The nom muncher games. menuItems.push({ label: 'numbnom — numbers', click: () => navigateToPiece('numbnom') }); menuItems.push({ label: 'engnom — english', click: () => navigateToPiece('engnom') }); menuItems.push({ label: 'mexinom — español', click: () => navigateToPiece('mexinom') }); menuItems.push({ label: 'dannom — dansk', click: () => navigateToPiece('dannom') }); menuItems.push({ label: 'rusnom — русский', click: () => navigateToPiece('rusnom') }); menuItems.push({ label: 'notenom — music', click: () => navigateToPiece('notenom') });
menuItems.push({ type: 'separator' });
// 🔮 KidLisp Keeps — live kept count (matches the tray superscript) plus a // link out to kidlisp.com. Count comes from the same poller that feeds the // tray icon; shows a neutral label until the first fetch lands. menuItems.push({ label: keepsCount != null ? `🔮 ${keepsCount} keeps kept` : '🔮 KidLisp Keeps', click: () => shell.openExternal('https://kidlisp.com'), });
menuItems.push({ type: 'separator' });
// Prompt (home). menuItems.push({ label: 'prompt', click: () => navigateToPiece('prompt') });
// Aesthetic Computer account section — manages the shared ~/.ac-token that // the rest of the AC suite reads. Sign in writes it (OAuth+PKCE via the // ac-login CLI), sign out clears it. menuItems.push({ type: 'separator' }); if (!fs.existsSync(acLoginPath)) { menuItems.push({ label: 'AC sign-in unavailable', enabled: false }); } else { const session = readAcSession(); if (session.state === 'signed-in') { menuItems.push({ label: `✓ AC: ${session.who || 'signed in'}`, enabled: false }); menuItems.push({ label: 'Sign out', click: () => runAcLogin(['logout']) }); } else if (session.state === 'expired') { menuItems.push({ label: 'AC session expired', enabled: false }); menuItems.push({ label: 'Sign in to Aesthetic Computer', click: () => runAcLogin() }); } else { menuItems.push({ label: 'Sign in to Aesthetic Computer', click: () => runAcLogin() }); } }
menuItems.push({ type: 'separator' });
menuItems.push({ label: 'Quit', accelerator: isMac ? 'Cmd+Q' : 'Alt+F4', click: () => app.quit() }); const contextMenu = Menu.buildFromTemplate(menuItems); trayContextMenu = contextMenu; if (process.platform === 'darwin') { // macOS pops trayContextMenu explicitly from the click handler — binding // it via setContextMenu here would race that and can swallow the click. } else { tray.setContextMenu(contextMenu); }}
function createNotepatTray() { if (notepatTray) return;
let iconPath; if (process.platform === 'darwin') { iconPath = app.isPackaged ? path.join(process.resourcesPath, 'notepatTrayTemplate.png') : path.join(__dirname, 'build', 'icons', 'notepatTrayTemplate.png'); } else { iconPath = app.isPackaged ? path.join(process.resourcesPath, 'notepatTrayTemplate.png') : path.join(__dirname, 'build', 'icons', 'notepatTrayTemplate.png'); }
const icon = nativeImage.createFromPath(iconPath); if (icon.isEmpty()) { console.warn('[notepat-tray] Icon empty at', iconPath); return; } if (process.platform === 'darwin') icon.setTemplateImage(true);
notepatTray = new Tray(icon); notepatTray.setToolTip('Notepat'); if (process.platform === 'darwin') notepatTray.setTitle('notepat');
const open = () => openNotepatWindow(); notepatTray.on('click', open);
const menu = Menu.buildFromTemplate([ { label: 'Open Notepat', click: open }, { label: 'Open Notepat Overlay 🪟', click: () => openNotepatOverlayWindow() }, { type: 'separator' }, { label: 'Launch Native Notepat (experimental)', enabled: process.platform === 'darwin', click: () => launchNativeNotepat(), }, { type: 'separator' }, { label: 'Overlay: Click-Through', type: 'checkbox', checked: !!preferences.overlayClickThrough, click: (item) => { preferences.overlayClickThrough = !!item.checked; savePreferences(); if (notepatOverlayWindow && !notepatOverlayWindow.isDestroyed()) { notepatOverlayWindow.setIgnoreMouseEvents(preferences.overlayClickThrough, { forward: true }); } }, }, { type: 'separator' }, { label: 'Quit', accelerator: process.platform === 'darwin' ? 'Cmd+Q' : 'Alt+F4', click: () => app.quit() }, ]); notepatTray.setContextMenu(menu);}
// ========== End System Tray ==========
// Update the tray title text (shown next to icon in menu bar).// The pals logo speaks for itself — never render piece-name text next to// it. Callers (update flow etc.) can still pass an explicit string for// transient states like "⬆️ Update".function updateTrayTitle(text) { if (!tray) return; if (process.platform === 'darwin') { tray.setTitle(text !== undefined ? text : ''); }}
// Preferences windowlet preferencesWindow = null;
function openPreferencesWindow() { if (preferencesWindow && !preferencesWindow.isDestroyed()) { preferencesWindow.focus(); return; } preferencesWindow = new BrowserWindow({ width: 480, height: 400, resizable: false, minimizable: false, maximizable: false, title: 'Preferences', titleBarStyle: 'hiddenInset', backgroundColor: '#1a1a2e', webPreferences: { nodeIntegration: true, contextIsolation: false } }); preferencesWindow.loadFile(getAppPath('renderer/preferences.html')); preferencesWindow.on('closed', () => { preferencesWindow = null; });}
// Get the focused window or first availablefunction getFocusedWindow() { let win = BrowserWindow.getFocusedWindow(); if (!win) { const allWindows = BrowserWindow.getAllWindows(); win = allWindows.find(w => w.isVisible() && !w.isDestroyed()); } return win;}
// Navigate a window to a specific piecefunction navigateToPiece(piece) { const win = getFocusedWindow(); const mode = getFocusedWindowMode(); const baseUrl = mode === 'production' ? 'https://aesthetic.computer' : 'http://localhost:8888'; const url = `${baseUrl}/${piece}?nogap=true`; if (win) { win.webContents.send('navigate', url); } else { // No window open, create one and navigate openAcPaneWindow().then(result => { if (!result?.window) return; result.window.webContents.once('did-finish-load', () => { result.window.webContents.send('navigate', url); }); }); }}
// Open a new AC Pane windowasync function openAcPaneWindow(options = {}) { return openAcPaneWindowInternal(options);}
// Open a FRAMELESS preview window for an arbitrary URL/path. This is slab's// "dynamic preview" engine: chromeless (no title bar), a normal-level window// so the slab tiler (AXTiler) packs it into the terminal grid, and — like// every ac-electron window — automatically a CDP target on the local// remote-debugging port, so slab/Claude can attach, eval, and screenshot it.// Re-requesting the same URL focuses the existing preview instead of stacking.const previewWindows = new Map(); // url -> BrowserWindowfunction openPreviewWindow(rawUrl) { if (!rawUrl) return null; let url = rawUrl; if (!/^(https?|file):\/\//.test(url)) { url = 'file://' + (url.startsWith('/') ? url : path.resolve(url)); } const existing = previewWindows.get(url); if (existing && !existing.isDestroyed()) { if (existing.isMinimized()) existing.restore(); existing.focus(); return existing; } const win = new BrowserWindow({ width: 480, height: 360, title: url, frame: false, // chromeless transparent: false, backgroundColor: '#000000', alwaysOnTop: false, // a normal window → AXTiler treats it as standard webPreferences: { nodeIntegration: false, contextIsolation: true, sandbox: false, // so preview-preload.js can use ipcRenderer backgroundThrottling: false, preload: getAppPath('renderer/preview-preload.js'), // click-drag to move }, }); win.loadURL(url); const windowId = windowIdCounter++; windows.set(windowId, { window: win, mode: 'preview' }); previewWindows.set(url, win); win.on('focus', () => { focusedWindowId = windowId; }); win.on('closed', () => { windows.delete(windowId); previewWindows.delete(url); }); return win;}
// Frameless preview drag-to-move: the preload streams cursor screen coords; we// move the window by the delta from where the drag began. Keyed per webContents// so multiple previews drag independently.const previewDragOrigin = new Map(); // webContents.id -> { win:[x,y], sx, sy }ipcMain.on('preview-drag-start', (event, { sx, sy }) => { const win = BrowserWindow.fromWebContents(event.sender); if (win) previewDragOrigin.set(event.sender.id, { win: win.getPosition(), sx, sy });});ipcMain.on('preview-drag-move', (event, { sx, sy }) => { const o = previewDragOrigin.get(event.sender.id); const win = BrowserWindow.fromWebContents(event.sender); if (o && win && !win.isDestroyed()) { win.setPosition(o.win[0] + Math.round(sx - o.sx), o.win[1] + Math.round(sy - o.sy)); }});ipcMain.on('preview-drag-end', (event) => { previewDragOrigin.delete(event.sender.id); });
// Open a standalone Notepat window — compact window dedicated to the /notepat piecelet notepatWindow = null;function openNotepatWindow() { if (notepatWindow && !notepatWindow.isDestroyed()) { if (notepatWindow.isMinimized()) notepatWindow.restore(); notepatWindow.show(); notepatWindow.focus(); return notepatWindow; }
const baseUrl = startInDevMode ? 'http://localhost:8888' : 'https://aesthetic.computer';
// Match the AC Pane (prompt) window chrome flags exactly so the two // windows feel like a matched set — frameless, transparent, float-on-top. const notepatOpts = { width: 480, height: 360, minWidth: 320, minHeight: 260, title: 'Notepat', frame: false, transparent: !isPaperWM, hasShadow: isPaperWM, alwaysOnTop: !isPaperWM && preferences.alwaysOnTop, backgroundColor: isPaperWM ? '#000000' : '#00000000', webPreferences: { nodeIntegration: true, contextIsolation: false, webviewTag: true, backgroundThrottling: false, }, }; if (isPaperWM) notepatOpts.type = 'normal'; notepatWindow = new BrowserWindow(notepatOpts);
notepatWindow.loadFile( getAppPath('renderer/notepat-view.html'), { query: { piece: 'notepat', base: baseUrl } }, );
const windowId = windowIdCounter++; windows.set(windowId, { window: notepatWindow, mode: 'notepat' });
notepatWindow.on('closed', () => { windows.delete(windowId); notepatWindow = null; });
return notepatWindow;}
// Open a Notepat Overlay — transparent, chromeless, screen-saver-level// window that floats above every app and follows the user across Spaces.// Like a macOS HUD / widget. Click-through can be toggled from the tray// so the overlay becomes visual-only (mouse events pass to whatever's// behind it). The notepat piece reads ?overlay=true and skips its// background wipe so the desktop shows through between keys.let notepatOverlayWindow = null;function openNotepatOverlayWindow() { if (notepatOverlayWindow && !notepatOverlayWindow.isDestroyed()) { notepatOverlayWindow.show(); notepatOverlayWindow.focus(); return notepatOverlayWindow; }
const baseUrl = startInDevMode ? 'http://localhost:8888' : 'https://aesthetic.computer'; const display = screen.getPrimaryDisplay().workAreaSize; const w = 340, h = 180;
notepatOverlayWindow = new BrowserWindow({ width: w, height: h, x: display.width - w - 24, y: display.height - h - 24, minWidth: 220, minHeight: 130, title: 'Notepat Overlay', frame: false, transparent: true, hasShadow: false, resizable: true, skipTaskbar: true, backgroundColor: '#00000000', type: process.platform === 'darwin' ? 'panel' : undefined, webPreferences: { nodeIntegration: true, contextIsolation: false, webviewTag: true, backgroundThrottling: false, }, });
// screen-saver level sits above fullscreen apps on macOS. Follow the // user across Spaces (and into fullscreen Spaces) so the overlay is // actually always visible. notepatOverlayWindow.setAlwaysOnTop(true, 'screen-saver'); notepatOverlayWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
if (preferences.overlayClickThrough) { notepatOverlayWindow.setIgnoreMouseEvents(true, { forward: true }); }
notepatOverlayWindow.loadFile( getAppPath('renderer/notepat-overlay.html'), { query: { piece: 'notepat', base: baseUrl } }, );
const windowId = windowIdCounter++; windows.set(windowId, { window: notepatOverlayWindow, mode: 'notepat-overlay' });
notepatOverlayWindow.on('closed', () => { windows.delete(windowId); notepatOverlayWindow = null; });
return notepatOverlayWindow;}
// Open KidLisp window (kidlisp.com)function openKidLispWindow() { const { width, height } = screen.getPrimaryDisplay().workAreaSize; const winWidth = Math.min(1200, width * 0.8); const winHeight = Math.min(800, height * 0.8); const win = new BrowserWindow({ width: winWidth, height: winHeight, title: 'KidLisp', backgroundColor: '#0a0a12', webPreferences: { nodeIntegration: false, contextIsolation: true, }, titleBarStyle: 'hiddenInset', trafficLightPosition: { x: 15, y: 12 }, }); win.loadURL('https://kidlisp.com'); // Track it const windowId = windowIdCounter++; windows.set(windowId, { window: win, mode: 'kidlisp' }); win.on('closed', () => { windows.delete(windowId); }); return win;}
// ========== CSS 3D Flip View ==========let ptyProcessFor3D = null;let lastKnownCols = 120;let lastKnownRows = 40;
// Calculate offset position for new windows to avoid overlapfunction getOffsetWindowPosition(sourceWindow, index = 0) { const offset = 40; // Pixels to offset each new window const { width, height } = screen.getPrimaryDisplay().workAreaSize; let baseX, baseY; if (sourceWindow && !sourceWindow.isDestroyed()) { const [srcX, srcY] = sourceWindow.getPosition(); const [srcW, srcH] = sourceWindow.getSize(); // Position to the right of source window baseX = srcX + srcW + 20; baseY = srcY; // If it would go off screen, wrap to upper-left with offset if (baseX + 680 > width) { baseX = 50 + (index * offset); baseY = 50 + (index * offset); } } else { // No source window, use cascading from top-left baseX = 100 + (index * offset); baseY = 100 + (index * offset); } // Apply index offset for multiple windows const x = Math.min(baseX + (index * offset), width - 680); const y = Math.min(baseY + (index * offset), height - 520); return { x: Math.max(0, x), y: Math.max(0, y) };}
async function openAcPaneWindowInternal(options = {}) { const { sourceWindow = null, index = 0 } = options; // Start with a smaller, cuter window - extra height for mode tags at bottom const winWidth = 330; const winHeight = 290; // Calculate position to avoid overlap const { x, y } = getOffsetWindowPosition(sourceWindow, index); // 🪟 Native macOS chrome: the standard OS title bar + traffic lights + native // resize, with a full-bleed webview below it. Set false to fall back to the // custom card chrome. const NATIVE_CHROME = !isPaperWM; const winOpts = { width: winWidth, height: winHeight, x, y, minWidth: 260, minHeight: 220, title: options.piece || 'prompt', frame: NATIVE_CHROME ? true : false, transparent: NATIVE_CHROME ? false : !isPaperWM, hasShadow: NATIVE_CHROME ? true : isPaperWM, alwaysOnTop: !isPaperWM && preferences.alwaysOnTop, backgroundColor: NATIVE_CHROME ? '#111114' : (isPaperWM ? '#000000' : '#00000000'), webPreferences: { nodeIntegration: true, contextIsolation: false, webviewTag: true, backgroundThrottling: false, }, }; // On PaperWM, use 'normal' type so the WM tiles it instead of floating if (isPaperWM) winOpts.type = 'normal'; const win = new BrowserWindow(winOpts); // Keep the native title bar showing the current piece (driven by // 'set-current-piece'), not the flip-view document <title> ("AC Pane"). if (NATIVE_CHROME) win.on('page-title-updated', (e) => e.preventDefault());
// Plumb the launch piece + environment base into the flip-view shell so // `--piece=NAME` (and `--dev`) actually route. The shell reads ?piece= and // ?base= in initWebviewSrc(); without these it always falls back to prompt // on production. Only the pane explicitly given a piece overrides the // default — extra panes opened later still start at the prompt. const baseUrl = startInDevMode ? 'http://localhost:8888' : 'https://aesthetic.computer'; const query = { base: baseUrl }; if (options.piece) query.piece = options.piece; if (isPaperWM) query.wm = 'paper'; if (NATIVE_CHROME) query.chrome = 'native'; // Tint the window chrome to the macOS system accent (kept in sync below via // the 'accent-color-changed' broadcast). Backgrounds still follow light/dark. const _acc = getAccentRGB(); if (_acc) query.accent = [_acc.r, _acc.g, _acc.b].map((c) => c.toString(16).padStart(2, '0')).join(''); win.loadFile(getAppPath('renderer/flip-view.html'), { query });
// 🎮 Grant sticky user activation to the host page so navigator.getGamepads() // works without the user clicking host chrome first. Chromium gates the // Gamepad API behind sticky activation; in a <webview>-host setup the user // typically clicks inside the webview and the host never receives one, // which made the host→guest gamepad bridge silently send empty snapshots. win.webContents.once('did-finish-load', () => { win.webContents.executeJavaScript('1', true).catch(() => {}); });
// Track it const windowId = windowIdCounter++; windows.set(windowId, { window: win, mode: 'ac-pane' }); // Track focus win.on('focus', () => { focusedWindowId = windowId; }); // Register Cmd+F to flip ALL windows, Cmd+B to flip focused window only const flipAllShortcut = process.platform === 'darwin' ? 'CommandOrControl+F' : 'Ctrl+F'; const flipOneShortcut = process.platform === 'darwin' ? 'CommandOrControl+B' : 'Ctrl+B'; globalShortcut.register(flipAllShortcut, () => { for (const entry of windows.values()) { const w = entry.window || entry; if (w && !w.isDestroyed()) { w.webContents.send('toggle-flip'); } } }); globalShortcut.register(flipOneShortcut, () => { if (focusedWindowId && windows.has(focusedWindowId)) { const w = windows.get(focusedWindowId).window; if (w && !w.isDestroyed()) { w.webContents.send('toggle-flip'); } } }); win.on('closed', () => { windows.delete(windowId); if (focusedWindowId === windowId) { focusedWindowId = null; } // Only unregister global shortcuts when the last window closes if (windows.size === 0) { globalShortcut.unregister(flipAllShortcut); globalShortcut.unregister(flipOneShortcut); globalShortcut.unregister('CommandOrControl+Plus'); globalShortcut.unregister('CommandOrControl+='); globalShortcut.unregister('CommandOrControl+-'); globalShortcut.unregister('CommandOrControl+0'); } if (ptyProcessFor3D) { ptyProcessFor3D.kill(); ptyProcessFor3D = null; } }); // PTY for terminal side ipcMain.on('connect-flip-pty', (event) => { if (!pty) { event.sender.send('flip-pty-data', '\r\n\x1b[31mError: node-pty not available\x1b[0m\r\n'); return; } const dockerPath = getDockerPath(); // Start container and emacs daemon if needed ensureContainerForFlip(dockerPath, event.sender); }); ipcMain.on('flip-pty-input', (event, data) => { // Check if we're waiting for a prompt response if (pendingContainerPrompt) { // Collect input until Enter is pressed if (data === '\r' || data === '\n') { handleContainerPromptResponse(pendingInputBuffer || ''); pendingInputBuffer = ''; } else if (data === '\x7f' || data === '\b') { // Backspace if (pendingInputBuffer && pendingInputBuffer.length > 0) { pendingInputBuffer = pendingInputBuffer.slice(0, -1); event.sender.send('flip-pty-data', '\b \b'); } } else if (data.length === 1 && data.charCodeAt(0) >= 32) { // Printable character pendingInputBuffer = (pendingInputBuffer || '') + data; event.sender.send('flip-pty-data', data); } return; } if (ptyProcessFor3D) { ptyProcessFor3D.write(data); } }); // Update terminal dimensions from renderer ipcMain.on('flip-pty-resize', (event, cols, rows) => { lastKnownCols = cols; lastKnownRows = rows; if (ptyProcessFor3D) { try { ptyProcessFor3D.resize(cols, rows); } catch (err) { console.warn('[main] Flip PTY resize failed:', err.message); } } }); // Allow renderer to query current dimensions ipcMain.handle('get-terminal-size', () => ({ cols: lastKnownCols, rows: lastKnownRows })); // Register zoom shortcuts for flip view globalShortcut.register('CommandOrControl+Plus', () => { if (win && !win.isDestroyed()) { win.webContents.send('zoom-in'); } }); globalShortcut.register('CommandOrControl+=', () => { if (win && !win.isDestroyed()) { win.webContents.send('zoom-in'); } }); globalShortcut.register('CommandOrControl+-', () => { if (win && !win.isDestroyed()) { win.webContents.send('zoom-out'); } }); globalShortcut.register('CommandOrControl+0', () => { if (win && !win.isDestroyed()) { win.webContents.send('zoom-reset'); } }); // Custom window resize handler ipcMain.on('resize-window', (event, bounds) => { const senderWindow = BrowserWindow.fromWebContents(event.sender); if (senderWindow) { senderWindow.setBounds({ x: Math.round(bounds.x), y: Math.round(bounds.y), width: Math.round(bounds.width), height: Math.round(bounds.height) }); } }); return { window: win, windowId };}
// State for interactive promptlet pendingContainerPrompt = null;let pendingInputBuffer = '';
async function ensureContainerForFlip(dockerPath, webContents) { const { spawn } = require('child_process'); // First check if Docker is available at all const dockerAvailable = await new Promise((resolve) => { const check = spawn(dockerPath, ['info'], { stdio: 'pipe' }); check.on('close', (code) => resolve(code === 0)); check.on('error', () => resolve(false)); }); if (!dockerAvailable) { webContents.send('flip-pty-data', '\x1b[33mDocker not available. Starting local shell...\x1b[0m\r\n\r\n'); startLocalShellForFlip(webContents); return; } // Check if container exists const containerExists = await new Promise((resolve) => { const check = spawn(dockerPath, ['ps', '-a', '--filter', 'name=aesthetic', '--format', '{{.Names}}'], { stdio: 'pipe' }); let output = ''; check.stdout.on('data', (data) => output += data.toString()); check.on('close', () => resolve(output.includes('aesthetic'))); check.on('error', () => resolve(false)); }); if (!containerExists) { webContents.send('flip-pty-data', '\x1b[33mNo AC container found. Starting local shell...\x1b[0m\r\n'); webContents.send('flip-pty-data', '\x1b[90m(Run "npm run aesthetic" in the repo to start the devcontainer)\x1b[0m\r\n\r\n'); startLocalShellForFlip(webContents); return; } // Ask user if they want to start the devcontainer webContents.send('flip-pty-data', '\x1b[36mStart devcontainer with emacs? [y/n]: \x1b[0m'); // Wait for user input pendingContainerPrompt = { dockerPath, webContents };}
// Handle user response to container promptfunction handleContainerPromptResponse(response) { if (!pendingContainerPrompt) return false; const { dockerPath, webContents } = pendingContainerPrompt; pendingContainerPrompt = null; const answer = response.trim().toLowerCase(); if (answer === 'y' || answer === 'yes') { webContents.send('flip-pty-data', 'y\r\n'); startContainerAndEmacs(dockerPath, webContents); return true; } else { webContents.send('flip-pty-data', answer ? `${answer}\r\n` : 'n\r\n'); webContents.send('flip-pty-data', '\x1b[33mStarting local shell instead...\x1b[0m\r\n\r\n'); startLocalShellForFlip(webContents); return true; }}
// Emacs log file path (inside container, but we'll also keep a local copy)const EMACS_LOG_PATH = '/tmp/ac-emacs-daemon.log';const LOCAL_EMACS_LOG_PATH = path.join(app.getPath('userData'), 'emacs-daemon.log');
// Write to local emacs logfunction writeEmacsLog(content, append = true) { try { if (append) { fs.appendFileSync(LOCAL_EMACS_LOG_PATH, content); } else { fs.writeFileSync(LOCAL_EMACS_LOG_PATH, content); } } catch (e) { console.warn('[main] Failed to write emacs log:', e.message); }}
// Read local emacs logfunction readEmacsLog() { try { if (fs.existsSync(LOCAL_EMACS_LOG_PATH)) { return fs.readFileSync(LOCAL_EMACS_LOG_PATH, 'utf-8'); } } catch (e) { console.warn('[main] Failed to read emacs log:', e.message); } return null;}
async function startContainerAndEmacs(dockerPath, webContents) { const { spawn } = require('child_process'); // Start fresh log for this session const timestamp = new Date().toISOString(); writeEmacsLog(`\n\n========== EMACS SESSION START: ${timestamp} ==========\n`, false); // Restart container to ensure clean state webContents.send('flip-pty-data', '\x1b[33mRestarting container...\x1b[0m\r\n'); writeEmacsLog(`[${timestamp}] Restarting container...\n`); await new Promise((resolve) => { const restart = spawn(dockerPath, ['restart', 'aesthetic'], { stdio: 'pipe' }); restart.on('close', () => resolve()); restart.on('error', () => resolve()); }); // Wait for container to be ready await new Promise(r => setTimeout(r, 2000)); writeEmacsLog(`Container restarted, waiting for ready state...\n`); // Kill any existing emacs daemon first writeEmacsLog(`Killing any existing emacs daemon...\n`); await new Promise((resolve) => { const kill = spawn(dockerPath, ['exec', 'aesthetic', 'pkill', '-9', '-f', 'emacs.*daemon'], { stdio: 'pipe' }); kill.on('close', () => resolve()); kill.on('error', () => resolve()); }); await new Promise(r => setTimeout(r, 500)); // Start emacs daemon with AC config webContents.send('flip-pty-data', '\x1b[33mStarting emacs daemon with AC config...\x1b[0m\r\n'); const configPath = '/home/me/aesthetic-computer/dotfiles/dot_config/emacs.el'; writeEmacsLog(`Starting emacs daemon with config: ${configPath}\n`); writeEmacsLog(`Command: emacs -q --daemon -l ${configPath}\n`); // Start daemon and capture output const emacsStartResult = await new Promise((resolve) => { let output = ''; const emacs = spawn(dockerPath, ['exec', 'aesthetic', 'emacs', '-q', '--daemon', '-l', configPath], { stdio: 'pipe' }); emacs.stdout.on('data', (d) => { output += d.toString(); writeEmacsLog(`[stdout] ${d.toString()}`); }); emacs.stderr.on('data', (d) => { output += d.toString(); writeEmacsLog(`[stderr] ${d.toString()}`); }); emacs.on('close', (code) => resolve({ code, output })); emacs.on('error', (err) => resolve({ code: -1, output: err.message })); }); writeEmacsLog(`\nEmacs daemon exit code: ${emacsStartResult.code}\n`); console.log('[main] Emacs daemon started, code:', emacsStartResult.code); if (emacsStartResult.output) { console.log('[main] Emacs output:', emacsStartResult.output.substring(0, 500)); } // Wait for emacs daemon to be responsive (poll with emacsclient -e t) webContents.send('flip-pty-data', '\x1b[33mWaiting for emacs daemon...\x1b[0m\r\n'); writeEmacsLog(`Waiting for emacs daemon to be responsive...\n`); let emacsReady = false; for (let i = 0; i < 30; i++) { // Max 30 seconds const ready = await new Promise((resolve) => { const check = spawn(dockerPath, ['exec', 'aesthetic', 'emacsclient', '-e', 't'], { stdio: 'pipe' }); check.on('close', (code) => resolve(code === 0)); check.on('error', () => resolve(false)); }); if (ready) { emacsReady = true; break; } await new Promise(r => setTimeout(r, 1000)); webContents.send('flip-pty-data', '.'); writeEmacsLog(`.`); } if (!emacsReady) { writeEmacsLog(`\nFAILED: Emacs daemon did not become responsive after 30 seconds\n`); webContents.send('flip-pty-data', '\r\n\x1b[31mEmacs daemon failed to start.\x1b[0m\r\n'); // Show the log to the user const log = readEmacsLog(); if (log) { webContents.send('flip-pty-data', '\r\n\x1b[33m--- Emacs Startup Log ---\x1b[0m\r\n'); webContents.send('flip-pty-data', log.split('\n').slice(-30).join('\r\n') + '\r\n'); webContents.send('flip-pty-data', '\x1b[33m--- End Log ---\x1b[0m\r\n\r\n'); } webContents.send('flip-pty-data', '\x1b[90mFalling back to fish shell...\x1b[0m\r\n'); startLocalShellForFlip(webContents); return; } writeEmacsLog(`\nEmacs daemon ready!\n`); webContents.send('flip-pty-data', ' ready!\r\n'); // Request current terminal size from renderer before spawning PTY webContents.send('flip-pty-data', '\x1b[33mConnecting to emacs...\x1b[0m\r\n'); writeEmacsLog(`Connecting emacsclient...\n`); // Get the last known size (will be updated by renderer) const { cols, rows } = await new Promise((resolve) => { // Request size update from renderer webContents.send('request-terminal-size'); // Wait a moment for any pending resize to arrive setTimeout(() => { resolve({ cols: lastKnownCols || 120, rows: lastKnownRows || 40 }); }, 100); }); console.log('[main] Starting PTY with size:', cols, 'x', rows); writeEmacsLog(`PTY size: ${cols}x${rows}\n`); writeEmacsLog(`Command: emacsclient -nw -c --eval "(aesthetic-backend 'artery)"\n`); // Connect PTY - run aesthetic-backend to set up tabs ptyProcessFor3D = pty.spawn(dockerPath, [ 'exec', '-it', '-e', 'LANG=en_US.UTF-8', '-e', 'LC_ALL=en_US.UTF-8', '-e', `COLUMNS=${cols}`, '-e', `LINES=${rows}`, 'aesthetic', 'emacsclient', '-nw', '-c', '--eval', "(aesthetic-backend 'artery)" ], { name: 'xterm-256color', cols: cols, rows: rows, cwd: process.env.HOME, env: { ...process.env, TERM: 'xterm-256color', LANG: 'en_US.UTF-8', LC_ALL: 'en_US.UTF-8' } }); let firstDataReceived = false; let sessionStartTime = Date.now(); ptyProcessFor3D.onData((data) => { if (!webContents.isDestroyed()) { webContents.send('flip-pty-data', data); // After first data, send a resize to ensure emacs picks it up if (!firstDataReceived) { firstDataReceived = true; writeEmacsLog(`First data received, emacsclient connected.\n`); // Give emacs a moment to initialize, then force resize setTimeout(() => { if (ptyProcessFor3D) { console.log('[main] Sending post-connect resize:', lastKnownCols, 'x', lastKnownRows); try { ptyProcessFor3D.resize(lastKnownCols, lastKnownRows); } catch (e) { console.warn('[main] Post-connect resize failed:', e.message); } // Send another resize after a longer delay to catch any late initialization setTimeout(() => { if (ptyProcessFor3D) { try { ptyProcessFor3D.resize(lastKnownCols, lastKnownRows); } catch (e) {} } }, 1000); } }, 500); } } }); ptyProcessFor3D.onExit(({ exitCode, signal }) => { const duration = Math.round((Date.now() - sessionStartTime) / 1000); const exitInfo = `Session ended after ${duration}s - exit code: ${exitCode}, signal: ${signal}`; writeEmacsLog(`\n${exitInfo}\n`); console.log('[main]', exitInfo); if (!webContents.isDestroyed()) { webContents.send('flip-pty-data', '\r\n\x1b[33m[Session ended]\x1b[0m\r\n'); // If session was very short (< 5 seconds) or crashed, show the log if (duration < 5 || exitCode !== 0) { const log = readEmacsLog(); if (log) { webContents.send('flip-pty-data', '\r\n\x1b[33m--- Session Log (last 40 lines) ---\x1b[0m\r\n'); const lines = log.split('\n').slice(-40); webContents.send('flip-pty-data', lines.join('\r\n') + '\r\n'); webContents.send('flip-pty-data', '\x1b[33m--- End Log ---\x1b[0m\r\n'); webContents.send('flip-pty-data', `\x1b[90mFull log: ${LOCAL_EMACS_LOG_PATH}\x1b[0m\r\n`); } } } });}
// Start a local shell when docker isn't availablefunction startLocalShellForFlip(webContents) { // Find the best available shell const shellOptions = process.platform === 'darwin' ? ['/opt/homebrew/bin/fish', '/usr/local/bin/fish', '/bin/zsh', '/bin/bash'] : ['/usr/bin/fish', '/bin/bash', '/bin/sh']; let shellPath = '/bin/sh'; for (const s of shellOptions) { if (fs.existsSync(s)) { shellPath = s; break; } } console.log('[main] Starting local shell:', shellPath); ptyProcessFor3D = pty.spawn(shellPath, [], { name: 'xterm-256color', cols: 120, rows: 40, cwd: process.env.HOME, env: { ...process.env, TERM: 'xterm-256color', LANG: 'en_US.UTF-8', LC_ALL: 'en_US.UTF-8' }, env: { ...process.env, TERM: 'xterm-256color' } }); ptyProcessFor3D.onData((data) => { if (!webContents.isDestroyed()) { webContents.send('flip-pty-data', data); } }); ptyProcessFor3D.onExit(() => { if (!webContents.isDestroyed()) { webContents.send('flip-pty-data', '\r\n\x1b[33m[Session ended]\x1b[0m\r\n'); } });}
// Get the repo path (where this Electron app lives, which is inside aesthetic-computer repo)function getRepoPath() { // In development, __dirname is ac-electron/, in packaged app it's inside Resources/app.asar // The repo is the parent of ac-electron const devRepoPath = path.resolve(__dirname, '..'); if (fs.existsSync(path.join(devRepoPath, '.git'))) { return devRepoPath; } // For packaged app, try common locations const homeRepoPath = path.join(process.env.HOME, 'Desktop', 'code', 'aesthetic-computer'); if (fs.existsSync(path.join(homeRepoPath, '.git'))) { return homeRepoPath; } return null;}
// IPC: register a file dropped on a running AC window (path comes from// renderer's HTML5 drop event — Electron's File object exposes .path).ipcMain.handle('ac:register-dropped-file', async (_event, filePath) => { if (!filePath) return { ok: false, reason: 'no-path' }; if (!acDropIsAudio(filePath)) return { ok: false, reason: 'not-audio' }; await acDropHandleFile(filePath); return { ok: true };});
// IPC Handlers for preferencesipcMain.handle('get-preferences', () => preferences);
ipcMain.handle('set-preferences', (event, newPrefs) => { preferences = { ...preferences, ...newPrefs }; savePreferences(); updateTrayTitle(); // Handle launch at login if (process.platform === 'darwin' || process.platform === 'win32') { app.setLoginItemSettings({ openAtLogin: preferences.launchAtLogin, openAsHidden: true }); } return preferences;});
ipcMain.handle('set-tray-title', (event, text) => { updateTrayTitle(text);});
// Update the current piece name (called when webview navigates)ipcMain.handle('set-current-piece', (event, pieceName) => { currentPiece = pieceName || 'prompt'; console.log('[main] Current piece updated:', currentPiece); updateTrayTitle(); // Reflect the current piece in the native window title bar. try { const win = BrowserWindow.fromWebContents(event.sender); if (win && !win.isDestroyed()) win.setTitle(currentPiece); } catch (_) {} return currentPiece;});
// Mark a window as the "main" windowipcMain.handle('set-main-window', (event) => { const win = BrowserWindow.fromWebContents(event.sender); for (const [id, data] of windows) { if (data.window === win) { mainWindowId = id; console.log('[main] Main window set to:', id); return true; } } return false;});
// IPC Handlers for welcome screenipcMain.handle('get-repo-path', () => { const repoPath = getRepoPath(); if (repoPath) { // Return just the base name for cleaner display return { path: repoPath, name: path.basename(repoPath) }; } return null;});
ipcMain.handle('get-git-user', async () => { try { const name = execSync('git config user.name', { encoding: 'utf8', timeout: 3000 }).trim(); const email = execSync('git config user.email', { encoding: 'utf8', timeout: 3000 }).trim(); return { name, email }; } catch (e) { return null; }});
ipcMain.handle('start-flip-devcontainer', async (event) => { const dockerPath = getDockerPath(); startContainerAndEmacs(dockerPath, event.sender); return { success: true };});
ipcMain.handle('start-flip-local-shell', async (event) => { startLocalShellForFlip(event.sender); return { success: true };});
// IPC HandlersipcMain.handle('get-mode', (event) => { // Find which window sent this const win = BrowserWindow.fromWebContents(event.sender); for (const [id, data] of windows) { if (data.window === win) return data.mode; } return 'production';});
ipcMain.handle('get-urls', () => URLS);
// Get app info for desktop.mjs pieceipcMain.handle('get-app-info', () => { return { version: app.getVersion(), electron: process.versions.electron, chrome: process.versions.chrome, platform: process.platform, arch: process.arch, isPackaged: app.isPackaged, updateAvailable: updateAvailable, latestVersion: updateAvailable?.version || null, };});
// CDP (Chrome DevTools Protocol) infoipcMain.handle('get-cdp-info', () => { const args = process.argv.join(' '); const cdpMatch = args.match(/--remote-debugging-port=(\d+)/); const inspectMatch = args.match(/--inspect=(\d+)/); return { enabled: !!cdpMatch, port: cdpMatch ? cdpMatch[1] : null, inspectPort: inspectMatch ? inspectMatch[1] : null };});
ipcMain.handle('check-docker', async () => { console.log('[main] check-docker called'); const result = await checkDocker(); console.log('[main] check-docker result:', result); return result;});
ipcMain.handle('check-container', async () => { console.log('[main] check-container called'); const result = await checkDevcontainer(); console.log('[main] check-container result:', result); return result;});
ipcMain.handle('check-container-exists', async () => { console.log('[main] check-container-exists called'); const result = await checkContainerExists(); console.log('[main] check-container-exists result:', result); return result;});
ipcMain.handle('start-existing-container', async () => { console.log('[main] start-existing-container called'); try { await startExistingContainer(); console.log('[main] start-existing-container success'); return { success: true }; } catch (err) { console.error('[main] start-existing-container error:', err); return { success: false, error: err.message }; }});
ipcMain.handle('start-container', async () => { console.log('[main] start-container called'); try { await startDevcontainer(); console.log('[main] start-container success'); return { success: true }; } catch (err) { console.error('[main] start-container error:', err); return { success: false, error: err.message }; }});
ipcMain.handle('stop-container', async () => { console.log('[main] stop-container called'); try { const dockerPath = getDockerPath(); await new Promise((resolve, reject) => { const docker = spawn(dockerPath, ['stop', 'aesthetic'], { stdio: 'pipe' }); docker.on('close', (code) => { if (code === 0) { console.log('[main] Container stopped successfully'); resolve(true); } else { reject(new Error(`docker stop exited with code ${code}`)); } }); docker.on('error', (err) => reject(err)); }); return { success: true }; } catch (err) { console.error('[main] stop-container error:', err); return { success: false, error: err.message }; }});
ipcMain.handle('stop-container-aggressive', async () => { console.log('[main] stop-container-aggressive called'); try { const dockerPath = getDockerPath(); // Kill all processes in the container first console.log('[main] Killing all processes in container...'); await new Promise((resolve) => { const kill = spawn(dockerPath, ['exec', 'aesthetic', 'pkill', '-9', '-f', '.*'], { stdio: 'pipe' }); kill.on('close', () => resolve()); kill.on('error', () => resolve()); }); // Give it a moment await new Promise(r => setTimeout(r, 500)); // Force stop with timeout console.log('[main] Force stopping container...'); await new Promise((resolve, reject) => { const docker = spawn(dockerPath, ['stop', '-t', '2', 'aesthetic'], { stdio: 'pipe' }); docker.on('close', (code) => { console.log('[main] Container force stopped with code:', code); resolve(true); }); docker.on('error', (err) => { console.error('[main] Error force stopping:', err); resolve(true); // Resolve anyway }); }); // Kill it if still running console.log('[main] Ensuring container is killed...'); await new Promise((resolve) => { const kill = spawn(dockerPath, ['kill', 'aesthetic'], { stdio: 'pipe' }); kill.on('close', () => resolve()); kill.on('error', () => resolve()); }); return { success: true }; } catch (err) { console.error('[main] stop-container-aggressive error:', err); return { success: false, error: err.message }; }});
// =============================================================================// USB Flash Handlers (Linux only)// =============================================================================
ipcMain.handle('usb:list-devices', async () => { console.log('[main] usb:list-devices called'); if (process.platform !== 'linux') { return { success: false, error: 'USB flashing is only supported on Linux' }; }
try { const output = await new Promise((resolve, reject) => { const proc = spawn('lsblk', [ '-J', '-d', '-o', 'NAME,SIZE,MODEL,TRAN,RM,HOTPLUG,TYPE' ], { stdio: 'pipe' }); let stdout = ''; let stderr = ''; proc.stdout.on('data', (d) => stdout += d.toString()); proc.stderr.on('data', (d) => stderr += d.toString()); proc.on('close', (code) => { if (code === 0) resolve(stdout); else reject(new Error(`lsblk exited with code ${code}: ${stderr}`)); }); proc.on('error', (err) => reject(err)); });
const parsed = JSON.parse(output); const devices = (parsed.blockdevices || []).filter(dev => dev.type === 'disk' && (dev.tran === 'usb' || dev.rm === true || dev.rm === '1' || dev.hotplug === true || dev.hotplug === '1') ).map(dev => ({ name: dev.name, path: `/dev/${dev.name}`, size: dev.size, model: (dev.model || 'Unknown USB Device').trim(), }));
console.log('[main] Found USB devices:', devices); return { success: true, devices }; } catch (err) { console.error('[main] usb:list-devices error:', err); return { success: false, error: err.message }; }});
ipcMain.handle('usb:flash-image', async (event, { url, devicePath, filename }) => { console.log('[main] usb:flash-image called:', { url, devicePath, filename }); if (process.platform !== 'linux') { return { success: false, error: 'USB flashing is only supported on Linux' }; }
if (!/^\/dev\/sd[a-z]$/.test(devicePath) && !/^\/dev\/nvme\d+n\d+$/.test(devicePath)) { return { success: false, error: `Invalid device path: ${devicePath}` }; }
const sender = event.sender; const tmpFile = path.join(app.getPath('temp'), filename || 'ac-os.img');
const sendProgress = (stage, percent, message) => { if (!sender.isDestroyed()) { sender.send('usb:flash-progress', { stage, percent, message }); } };
try { // Phase 1: Download image to temp file sendProgress('download', 0, 'Starting download...');
await new Promise((resolve, reject) => { const file = fs.createWriteStream(tmpFile); const request = net.request(url); let totalBytes = 0; let receivedBytes = 0;
request.on('response', (response) => { const cl = response.headers['content-length']; totalBytes = parseInt(Array.isArray(cl) ? cl[0] : cl || '0', 10);
response.on('data', (chunk) => { file.write(chunk); receivedBytes += chunk.length; if (totalBytes > 0) { const pct = Math.round((receivedBytes / totalBytes) * 100); sendProgress('download', pct, `Downloading: ${(receivedBytes / 1e9).toFixed(1)}GB / ${(totalBytes / 1e9).toFixed(1)}GB`); } });
response.on('end', () => file.end(() => resolve())); response.on('error', (err) => { file.destroy(); reject(err); }); });
request.on('error', (err) => { file.destroy(); reject(err); }); request.end(); });
sendProgress('download', 100, 'Download complete');
// Phase 2: Unmount all partitions on the device sendProgress('unmount', 0, `Unmounting ${devicePath}...`);
await new Promise((resolve) => { const umount = spawn('sh', ['-c', `umount ${devicePath}* 2>/dev/null`], { stdio: 'pipe' }); umount.on('close', () => resolve()); umount.on('error', () => resolve()); });
sendProgress('unmount', 100, 'Device unmounted');
// Phase 3: Flash with dd via pkexec (privilege escalation) sendProgress('write', 0, `Writing to ${devicePath}...`);
const imageSize = fs.statSync(tmpFile).size;
await new Promise((resolve, reject) => { const dd = spawn('pkexec', [ 'dd', `if=${tmpFile}`, `of=${devicePath}`, 'bs=4M', 'conv=fsync', 'status=progress' ], { stdio: ['pipe', 'pipe', 'pipe'] });
let lastPercent = 0;
dd.stderr.on('data', (data) => { const text = data.toString(); const match = text.match(/(\d+)\s+bytes/); if (match && imageSize > 0) { const written = parseInt(match[1], 10); const pct = Math.round((written / imageSize) * 100); if (pct > lastPercent) { lastPercent = pct; sendProgress('write', pct, `Writing: ${(written / 1e9).toFixed(1)}GB / ${(imageSize / 1e9).toFixed(1)}GB`); } } });
dd.on('close', (code) => { if (code === 0) resolve(); else reject(new Error(`dd exited with code ${code}`)); }); dd.on('error', (err) => reject(err)); });
sendProgress('write', 100, 'Write complete');
// Phase 4: Sync and eject sendProgress('eject', 0, 'Syncing and ejecting...');
await new Promise((resolve) => { const sync = spawn('sync', [], { stdio: 'pipe' }); sync.on('close', () => resolve()); sync.on('error', () => resolve()); });
await new Promise((resolve) => { const eject = spawn('eject', [devicePath], { stdio: 'pipe' }); eject.on('close', () => resolve()); eject.on('error', () => resolve()); });
sendProgress('eject', 100, 'Device ejected safely');
// Cleanup temp file try { fs.unlinkSync(tmpFile); } catch {}
return { success: true }; } catch (err) { console.error('[main] usb:flash-image error:', err); try { fs.unlinkSync(tmpFile); } catch {} return { success: false, error: err.message }; }});
ipcMain.handle('open-shell', async () => { console.log('[main] open-shell called'); await openAcPaneWindow(); return { success: true };});
// PTY handlers for AC Pane windows (legacy path)ipcMain.handle('connect-pty', async (event) => { console.log('[main] connect-pty called'); if (!pty) { console.error('[main] node-pty not available'); return false; }
const win = BrowserWindow.fromWebContents(event.sender); let windowId = null; let winData = null; for (const [id, data] of windows) { if (data.window === win) { windowId = id; winData = data; break; } }
if (!winData || winData.mode !== 'ac-pane') { console.error('connect-pty called on non-AC Pane window'); return false; }
try { // Spawn docker exec to get into the container with fish shell const dockerPath = getDockerPath(); console.log('[main] Spawning PTY with docker at:', dockerPath); // Ensure PATH includes common locations for packaged app const extraPaths = '/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin'; const fullPath = process.env.PATH ? `${process.env.PATH}:${extraPaths}` : extraPaths; const shellProcess = pty.spawn(dockerPath, ['exec', '-it', 'aesthetic', 'fish'], { name: 'xterm-256color', cols: 120, rows: 30, cwd: process.env.HOME, env: { ...process.env, TERM: 'xterm-256color', // Tell programs not to query for colors (eat/vim/etc respect this) COLORTERM: 'truecolor', // Prevent OSC query responses by indicating we don't support them VTE_VERSION: '', // Empty signals no VTE terminal PATH: fullPath }, });
winData.ptyProcess = shellProcess;
// Forward PTY output to renderer shellProcess.onData((data) => { console.log('[main] PTY data received, length:', data.length, 'preview:', data.substring(0, 50).replace(/\n/g, '\\n')); if (!win.isDestroyed()) { win.webContents.send('pty-data', data); console.log('[main] PTY data sent to renderer'); } else { console.log('[main] Window destroyed, cannot send PTY data'); } });
shellProcess.onExit(({ exitCode }) => { console.log('[main] PTY exited with code:', exitCode); if (!win.isDestroyed()) { win.webContents.send('pty-exit', exitCode); } });
return true; } catch (err) { console.error('[main] PTY spawn failed:', err.message); console.error('[main] This usually means node-pty needs rebuilding for Electron.'); console.error('[main] Run: cd ac-electron && npx electron-rebuild'); return false; }});
ipcMain.on('pty-input', (event, data) => { const win = BrowserWindow.fromWebContents(event.sender); for (const [id, winData] of windows) { if (winData.window === win && winData.ptyProcess) { winData.ptyProcess.write(data); break; } }});
ipcMain.on('pty-resize', (event, cols, rows) => { const win = BrowserWindow.fromWebContents(event.sender); for (const [id, winData] of windows) { if (winData.window === win && winData.ptyProcess) { try { winData.ptyProcess.resize(cols, rows); } catch (err) { // PTY may not be ready yet or already exited console.warn('[main] PTY resize failed:', err.message); } break; } }});
// Global window move handler (for alt+scroll drag in any window)ipcMain.on('move-window', (event, position) => { const senderWindow = BrowserWindow.fromWebContents(event.sender); if (senderWindow) { senderWindow.setPosition(Math.round(position.x), Math.round(position.y)); }});
// Open/close windows from inside the embedded AC prompt (via webview popup interception)ipcMain.handle('ac-open-window', async (event, { url, index = 0, total = 1 } = {}) => { console.log('[main] ac-open-window called with url:', url, 'index:', index, 'total:', total); const sourceWindow = BrowserWindow.fromWebContents(event.sender); const { window: newWindow } = await openAcPaneWindowInternal({ sourceWindow, index }); console.log('[main] openAcPaneWindow returned:', !!newWindow); if (url && newWindow) { newWindow.webContents.once('did-finish-load', () => { if (!newWindow.isDestroyed()) { console.log('[main] Sending navigate to new window:', url); newWindow.webContents.send('navigate', url); } }); } return { success: true };});
ipcMain.handle('ac-close-window', async (event) => { console.log('[main] ac-close-window called'); const senderWindow = BrowserWindow.fromWebContents(event.sender); console.log('[main] sender window found:', !!senderWindow); if (senderWindow && !senderWindow.isDestroyed()) { console.log('[main] Closing window'); senderWindow.close(); } return { success: true };});
// Open external URL in the system's default browseripcMain.handle('open-external-url', async (event, url) => { console.log('[main] Opening external URL:', url); shell.openExternal(url); return { success: true };});
ipcMain.handle('switch-mode', async (event, mode) => { // Always open AC Pane await openAcPaneWindow(); return 'ac-pane';});
ipcMain.handle('spawn-terminal', async (event, command) => { // This would use node-pty in a real implementation return { success: true };});
// Reboot/restart the Electron appipcMain.handle('app-reboot', async () => { console.log('[main] Rebooting app...'); app.relaunch(); app.quit(); return { success: true };});
// ========== IPC Bridge for Artery/Tests ==========// Forward commands from external tools to webview and vice versa
// Navigate to a piece (from shell/artery-tui)ipcMain.on('ac-navigate', (event, piece) => { // Forward to all AC panes for (const [id, winData] of windows) { if (winData.mode === 'ac-pane') { winData.window.webContents.send('ac-navigate', piece); } }});
// Set environment (local/prod)ipcMain.on('ac-set-env', (event, env) => { for (const [id, winData] of windows) { if (winData.mode === 'ac-pane') { winData.window.webContents.send('ac-set-env', env); } }});
// Evaluate JavaScript in webviewipcMain.handle('ac-eval', async (event, code) => { return new Promise((resolve) => { // Find the AC Pane for (const [id, winData] of windows) { if (winData.mode === 'ac-pane') { // Set up one-time listener for result const handler = (event, result) => { ipcMain.removeListener('ac-eval-result', handler); resolve(result); }; ipcMain.on('ac-eval-result', handler); winData.window.webContents.send('ac-eval', code); // Timeout after 10 seconds setTimeout(() => { ipcMain.removeListener('ac-eval-result', handler); resolve({ success: false, error: 'Timeout' }); }, 10000); return; } } resolve({ success: false, error: 'No AC Pane found' }); });});
// Get current stateipcMain.handle('ac-get-state', async (event) => { return new Promise((resolve) => { for (const [id, winData] of windows) { if (winData.mode === 'ac-pane') { const handler = (event, state) => { ipcMain.removeListener('ac-state', handler); resolve(state); }; ipcMain.on('ac-state', handler); winData.window.webContents.send('ac-get-state'); setTimeout(() => { ipcMain.removeListener('ac-state', handler); resolve(null); }, 5000); return; } } resolve(null); });});
// Take screenshotipcMain.handle('ac-screenshot', async (event) => { return new Promise((resolve) => { for (const [id, winData] of windows) { if (winData.mode === 'ac-pane') { const handler = (event, result) => { ipcMain.removeListener('ac-screenshot-result', handler); resolve(result); }; ipcMain.on('ac-screenshot-result', handler); winData.window.webContents.send('ac-screenshot'); setTimeout(() => { ipcMain.removeListener('ac-screenshot-result', handler); resolve({ success: false, error: 'Timeout' }); }, 10000); return; } } resolve({ success: false, error: 'No AC Pane found' }); });});
// Reload webviewipcMain.on('ac-reload', (event) => { for (const [id, winData] of windows) { if (winData.mode === 'ac-pane') { winData.window.webContents.send('ac-reload'); } }});
// ~ command: toggle DevTools docked at bottom, navigating to Console panelipcMain.on('open-devtools', (event) => { const win = BrowserWindow.fromWebContents(event.sender) || BrowserWindow.getFocusedWindow(); if (!win) return; if (win.webContents.isDevToolsOpened()) { win.webContents.closeDevTools(); } else { win.webContents.openDevTools({ mode: 'bottom' }); win.webContents.once('devtools-opened', () => { try { win.webContents.devToolsWebContents?.executeJavaScript( 'DevToolsAPI.showPanel("console")' ).catch(() => {}); } catch (e) { /* ignore if DevTools API unavailable */ } }); }});
// App lifecycleapp.whenReady().then(async () => { loadPreferences();
// Always sync the OS login-item setting with our stored preference on boot. // This way the preference survives updates / re-signing and is always honored. if (process.platform === 'darwin' || process.platform === 'win32') { try { app.setLoginItemSettings({ openAtLogin: preferences.launchAtLogin, openAsHidden: true, }); } catch (e) { console.warn('[main] setLoginItemSettings failed:', e.message); } }
// Detect whether this launch was triggered by macOS at login (or with the // hidden flag). If so, we stay in menubar-daemon mode and don't pop the AC // window — the user will open things explicitly from the tray. let launchedSilently = false; if (process.platform === 'darwin') { try { const lis = app.getLoginItemSettings(); launchedSilently = !!(lis.wasOpenedAtLogin || lis.wasOpenedAsHidden); } catch (e) { /* noop */ } } // Hide the dock immediately so launch-at-login has zero visible footprint // until the user clicks a tray icon. if (launchedSilently) syncDockVisibility();
// Allow clipboard read/write for AC content loaded over https/http or in // webviews. `navigator.clipboard.readText()` in bios.mjs (paste button + // TextInput paste in prompt/chat) is gated behind these permissions in // Electron; without handlers the request silently fails and nothing pastes. const allowClipboard = (permission) => permission === 'clipboard-read' || permission === 'clipboard-sanitized-write' || permission === 'clipboard-write'; const installClipboardPermissions = (sess) => { if (!sess || sess.__acClipboardWired) return; sess.__acClipboardWired = true; sess.setPermissionRequestHandler((_wc, permission, callback) => { if (allowClipboard(permission)) return callback(true); callback(false); }); sess.setPermissionCheckHandler((_wc, permission) => { return allowClipboard(permission); }); }; installClipboardPermissions(session.defaultSession);
createMenu(); createSystemTray(); // Calendar tray removed — DateWizard (Swift) owns the menu-bar calendar now. // createNotepatTray(); // disabled — single pals systray icon only // Start FF1 Bridge server for kidlisp.com integration ff1Bridge.startBridge(); // Start GitHub update checks (works in dev and production) startSiloUpdateChecks();
// Start polling the KidLisp keeps count for the tray superscript. startKeepsCountPolling();
// Check for updates on startup (production builds only) if (autoUpdater && !startInDevMode && app.isPackaged) { setTimeout(() => { console.log('[updater] Checking for updates on startup...'); autoUpdater.checkForUpdates().catch(err => { console.log('[updater] Update check failed:', err.message); }); }, 3000); // Wait 3s after startup } // Watch for reboot request file from devcontainer (electric-snake-bite) const REBOOT_MARKER = path.join(__dirname, '.reboot-requested'); const checkRebootMarker = () => { if (fs.existsSync(REBOOT_MARKER)) { console.log('[main] ⚡🐍 Electric Snake Bite detected! Relaunching...'); fs.unlinkSync(REBOOT_MARKER); // Clean up marker // Relaunch the app then quit current instance app.relaunch(); app.quit(); } }; // Check every 2 seconds for reboot marker setInterval(checkRebootMarker, 2000); setInterval(checkRebootMarker, 2000); // Cold-launch with a file argument (Windows/Linux drop-onto-icon, or // Open With ... from a file manager). macOS routes this through // 'open-file' so we only need argv here. const acDropColdLaunchFile = process.argv.slice(1).find(acDropIsAudio);
// Create initial window(s). // Default is menubar-daemon mode for EVERY launch — no AC window pops on its // own. The user opens pieces explicitly from the tray (or by re-activating the // app, which fires 'activate' below). We only auto-open a window for an // explicit intent: a slab preview host, or a file opened onto the app. if (initialPreviewURL) { openPreviewWindow(initialPreviewURL); // launched as a preview host (slab) } else if (acDropColdLaunchFile) { openAcPaneWindow({ piece: initialPiece }); // opened with a file argument }
if (acDropColdLaunchFile) { acDropHandleFile(acDropColdLaunchFile); }
app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { openAcPaneWindow(); } });
// Keep dock visibility in sync with window state on macOS so the app // fades out of the dock / app-switcher whenever all windows close, and // comes back as soon as a window opens. app.on('browser-window-created', (_, win) => { syncDockVisibility(); win.on('show', syncDockVisibility); win.on('hide', syncDockVisibility); win.on('closed', syncDockVisibility); }); // Allow webview preload scripts (required for webview-preload.js to work) app.on('web-contents-created', (_, contents) => { installClipboardPermissions(contents.session); contents.on('will-attach-webview', (wawevent, webPreferences, params) => { // Verify the preload path is our legitimate webview-preload.js const preloadPath = params.preload; if (preloadPath) { // Use dev path if in dev mode, otherwise use bundled path const expectedPreload = getAppPath('webview-preload.js'); // Allow if it matches our webview-preload.js if (preloadPath.includes('webview-preload.js')) { // Set the absolute path for the preload webPreferences.preload = expectedPreload; console.log('[main] Allowing webview preload:', expectedPreload); } else { // Block unknown preload scripts for security console.warn('[main] Blocking unknown webview preload:', preloadPath); delete webPreferences.preload; } } }); });});
// Handle webview window.open() calls (popups) from guest content// This is required in Electron 12+ as new-window event is deprecatedapp.on('web-contents-created', (event, contents) => { // Only handle webviews if (contents.getType() === 'webview') { contents.on('will-navigate', (navEvent, url) => { if (!url) return; if (url.startsWith('ac://close')) { navEvent.preventDefault(); const allWindows = BrowserWindow.getAllWindows(); for (const win of allWindows) { if (!win.isDestroyed() && win.webContents.id === contents.hostWebContents?.id) { win.close(); break; } } return; } // Handle 'local' / 'prod' commands - switch between dev and production servers try { const navUrl = new URL(url); const pathname = navUrl.pathname.replace(/^\//, ''); if (pathname === 'local' || pathname === 'prod') { navEvent.preventDefault(); const base = pathname === 'local' ? 'http://localhost:8888' : 'https://aesthetic.computer'; const hostWin = BrowserWindow.getAllWindows().find(win => !win.isDestroyed() && win.webContents.id === contents.hostWebContents?.id ); if (hostWin) { console.log(`[main] Switching to ${pathname} server: ${base}`); hostWin.webContents.send('navigate', `${base}/prompt?desktop`); } return; } } catch (e) {} if (url.startsWith('ac://open')) { navEvent.preventDefault(); let targetUrl = ''; try { const urlObj = new URL(url); targetUrl = urlObj.searchParams.get('url') || ''; } catch (err) { console.warn('[main] Failed to parse ac://open URL:', url, err.message); } openAcPaneWindow().then(({ window: newWin }) => { if (newWin && !newWin.isDestroyed()) { newWin.webContents.once('did-finish-load', () => { if (!newWin.isDestroyed() && targetUrl) { newWin.webContents.send('navigate', targetUrl); } }); } }); } });
contents.setWindowOpenHandler(({ url }) => { console.log('[main] Webview window.open:', url); // Handle ac://close request (from prompt.mjs '-' command) if (url.startsWith('ac://close')) { // Find the parent BrowserWindow of this webview const allWindows = BrowserWindow.getAllWindows(); for (const win of allWindows) { if (!win.isDestroyed() && win.webContents.id === contents.hostWebContents?.id) { win.close(); break; } } return { action: 'deny' }; } // Check if this is an external URL (not aesthetic.computer or localhost) // External URLs should open in the system's default browser try { const urlObj = new URL(url); const isExternal = !urlObj.hostname.includes('aesthetic.computer') && !urlObj.hostname.includes('localhost') && !urlObj.hostname.includes('127.0.0.1') && !url.startsWith('ac://'); if (isExternal) { console.log('[main] Opening external URL in system browser:', url); shell.openExternal(url); return { action: 'deny' }; } } catch (e) { console.warn('[main] Failed to parse URL:', url, e.message); } // Handle new window request (from prompt.mjs '+' command) // Open a new AC Pane and navigate to the URL // Find the source window from the webview's host const sourceWindow = BrowserWindow.getAllWindows().find(win => !win.isDestroyed() && win.webContents.id === contents.hostWebContents?.id ); openAcPaneWindowInternal({ sourceWindow, index: 0 }).then(({ window: newWin }) => { if (newWin && !newWin.isDestroyed()) { newWin.webContents.once('did-finish-load', () => { if (!newWin.isDestroyed()) { newWin.webContents.send('navigate', url); } }); } }); return { action: 'deny' }; // We handle it ourselves }); }});
app.on('window-all-closed', () => { // Keep tray icons (AC + Notepat) alive on macOS so the menu bar remains an entry point. // Cmd+Q still quits explicitly. On Windows/Linux, close-all still quits. if (process.platform !== 'darwin') { app.quit(); return; } // Drop out of the dock / app-switcher — pure menubar daemon mode. syncDockVisibility();});
app.on('will-quit', () => { globalShortcut.unregisterAll(); // Stop FF1 Bridge server ff1Bridge.stopBridge(); // Kill all PTY processes for (const [id, winData] of windows) { if (winData.ptyProcess) { winData.ptyProcess.kill(); } } // NOTE: We intentionally do NOT stop the devcontainer when the app quits. // The devcontainer should keep running for the VS Code workspace.});
// Handle certificate errors for localhost in dev modeapp.on('certificate-error', (event, webContents, url, error, certificate, callback) => { if (url.startsWith('https://localhost')) { event.preventDefault(); callback(true); } else { callback(false); }});
// Handle SIGTERM/SIGINT for clean shutdown (when killed via pkill, etc)function cleanup() { console.log('[main] Cleanup triggered'); // Kill all PTY processes for (const [id, winData] of windows) { if (winData.ptyProcess) { winData.ptyProcess.kill(); } } // NOTE: We intentionally do NOT stop the devcontainer. // It should keep running for the VS Code workspace. process.exit(0);}
process.on('SIGTERM', cleanup);process.on('SIGINT', cleanup);