Introducing a new way to review your PRs.

Read the announcement

jamaljsr / polar

One-click Bitcoin Lightning networks for local app development & testing

View on GitHub

19 documents · Updated Oct 19, 2026

docs / electron-desktop-runtime

Electron desktop runtime

The Electron desktop runtime starts Polar as a local desktop application and connects its user interface to local network services. The main process owns application lifetime, windows, menus, the system tray, and IPC (inter-process communication) paths that let the renderer request work or receive events.

Application startup

The main process prepares the desktop environment and then creates one WindowManager. In development it loads the live web app at http://localhost:3000; in a packaged app it loads the built web page from the application bundle. The same runtime then attaches the service bridges and the renderer-facing listeners to the main window.

Startup sequence

The startup sequence moves from process setup to a usable main window and then to the channels that support the running application. This order matters because the MCP bridge and event paths need a renderer window to receive messages.

flowchart TD
    A[Electron main process] --> B[Set logging and runtime environment]
    B --> C[Create main window]
    C --> D[Install application and service IPC listeners]
    D --> E[Start Lightning event forwarding]
    E --> F[Start local MCP bridge]
    F --> G[Running Polar desktop app]

The runtime also merges the user's shell environment into the process environment. This lets local tools such as Docker use the same command path as the user's shell. Development mode adds Electron debugging support and the required developer tools.

References: electron/index.ts• L9-41 electron/windowManager.ts• L23-39 electron/constants.ts• L4-15

Application lifecycle

WindowManager responds to the Electron application lifecycle rather than to a single page load. It creates a new main window when the app becomes active after the window is gone, and it quits when the main window or all windows close.

The renderer can ask the main process to exit after all managed Docker networks stop. Before a normal main-window close, the runtime sends an app-closing message so the renderer can complete its close handling.

References: electron/windowManager.ts• L37-39 electron/windowManager.ts• L75-87 electron/windowManager.ts• L107-132 src/…/network.ts• L804-822

Window management

The runtime presents Polar through a main window and opens focused work areas in separate windows. Window size and position persist across sessions, so the desktop app restores the user's layout instead of treating each launch as a new session.

Main window

The main window hosts the renderer application and is the hub for all renderer-facing communication. It loads the development server or packaged page selected by BASE_URL, hides its native menu bar, and keeps a minimum usable width.

The runtime keeps the main window reference in WindowManager. Other main-process services use that reference to send IPC messages to the renderer, including streamed Lightning events and MCP requests. A completed page load clears the cached Lightning proxy data, including both authenticated and wallet-state clients, so later requests use current node credentials and state.

References: electron/windowManager.ts• L19-21 electron/windowManager.ts• L41-90 electron/constants.ts• L9-15 electron/…/lndProxyServer.ts• L331-336

Secondary windows

Secondary windows show a route inside the same renderer application for focused views such as terminals, Docker logs, or simulation logs. The main process creates them without a visible native menu, shows them after the page is ready, and stores separate state for each route.

The renderer requests these windows through the general application IPC channel. This keeps window creation in the main process while the renderer chooses which application view to display.

References: electron/appIpcListener.ts• L13-47 electron/appIpcListener.ts• L65-73 src/…/index.ts• L57-68

Desktop controls

The main process supplies desktop controls that sit outside the renderer page. The application menu provides standard editing and window actions, while the system tray gives the user a way to hide, restore, or quit Polar without keeping the main window visible.

Application menu

The application menu uses native Electron roles for common edit and window actions. It also provides a help entry that opens the Polar repository in the user's external browser. macOS receives the platform-specific application menu with app visibility and quit actions.

References: electron/appMenu.ts• L3-71 electron/appMenu.ts• L73-116 electron/windowManager.ts• L41-43

System tray

The tray keeps Polar available when the main window is hidden. A tray click restores the window on non-macOS systems, and the tray menu offers minimize, show, and quit actions.

Tray images follow the operating system and current theme. When the system theme changes, the runtime rebuilds the tray menu and updates its images. The tray is destroyed when the application exits.

References: electron/trayManager.ts• L7-29 electron/trayManager.ts• L45-64 electron/trayManager.ts• L81-137 electron/windowManager.ts• L65-68

IPC boundary

IPC is the message boundary between the renderer and the Electron main process. Shared channel names describe the contract, while the main process turns requests into local work and sends back either a result or an error.

Request and response channels

The renderer-facing IPC sender adds a request suffix and a response suffix to each channel. It also adds a temporary reply channel to each request. This lets several requests run at the same time without one response being delivered to the wrong caller.

The main process registers general application listeners and service listeners during startup. Each listener selects the requested operation, awaits its result, and replies on the requested channel. Errors cross the boundary as an object with an err message, which the renderer-side sender turns into a rejected promise.

References: src/…/ipcService.ts• L30-63 electron/appIpcListener.ts• L61-109 electron/windowManager.ts• L23-31 src/…/ipcChannels.ts• L1-50

Event streams

Some node operations produce events after the initial request. The renderer subscribes with a temporary stream channel, and the main process forwards Lightning channel events to the main window through that channel until the renderer unsubscribes.

This one-way path is separate from normal request results. It allows the desktop view to react to node changes without polling for every update.

References: src/…/ipcService.ts• L65-92 src/…/lndProxyClient.ts• L92-115 electron/…/lndProxyServer.ts• L17-25 electron/…/lndProxyServer.ts• L179-193

Renderer-facing proxies

The main process protects the renderer from direct access to local node RPC (remote procedure call) services. Renderer proxy clients send typed application requests over IPC, and the matching main-process proxy connects to the selected local node and returns data in a renderer-safe form.

Backend proxy services

The runtime starts proxy listeners for LND, Tapd, and Litd. The LND boundary also supports a wallet-unlocker path for state checks before wallet authentication is available. These listeners map shared channel names to node operations, reuse connections for a node during the session, and clear those connections when the app asks to clear its cache or when the main page reloads.

The proxies also normalize service responses before they cross IPC. They add expected default values and convert binary values to text where needed. LND wallet-state failures evict its cached clients, which lets later requests rebuild a connection after stale credentials or node state cause an error. LND event subscriptions remain active as a stream, while ordinary operations return one response.

For the node-level service boundary and its operation groups, see Backend proxy services.

References: electron/windowManager.ts• L25-31 electron/appIpcListener.ts• L51-73 electron/…/lndProxyServer.ts• L223-253 electron/…/lndProxyServer.ts• L263-336 electron/…/tapdProxyServer.ts• L167-234 electron/…/litdProxyServer.ts• L67-125

General application proxy

The general application listener handles work that belongs to the desktop host rather than to a node service. It opens secondary windows, clears service caches, forwards HTTP requests, and handles zip and unzip work for the renderer.

The application store uses the app IPC prefix for these calls. This gives renderer code one async request pattern for host actions while keeping file, network, and window work in the main process.

References: electron/appIpcListener.ts• L13-47 electron/appIpcListener.ts• L51-73 electron/httpProxy.ts• L18-40 src/…/index.ts• L57-68 src/…/ipcChannels.ts• L2-7

Desktop MCP bridge

The Electron runtime starts a local MCP (Model Context Protocol) HTTP bridge after the main window exists. The bridge gives local MCP clients access to tool discovery and tool execution, then passes each request through IPC to the renderer's MCP model.

The bridge binds to localhost and uses a configurable local port. It exposes health, tool discovery, and tool execution routes. The renderer returns tool definitions or executes the named tool through the application store, so MCP clients use the same active application state as the desktop UI.

For the HTTP contract, tool flow, limits, and renderer-side MCP behavior, see Desktop MCP integration.

References: electron/windowManager.ts• L32-35 electron/mcpBridge.ts• L57-113 electron/mcpBridge.ts• L118-215 src/…/ipc.ts• L50-79 electron/constants.ts• L6-7

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the development and packaged BASE_URL choices aligned with the build output and the way Electron is launched.
  • Keep every renderer proxy client, shared IPC channel, and main-process listener aligned when an operation is added or renamed.
  • Preserve temporary reply channels for concurrent requests and stream subscriptions. A fixed response channel can mix results from separate calls.
  • Update window-close and tray behavior together when changing quit, hide, or restore rules. These paths control whether the app exits or stays available in the tray.
  • Start or move the MCP bridge only with a valid main window. Its renderer handoff depends on that window and its active store.
  • Clear both authenticated and wallet-state LND proxy caches when node identity, credentials, or reload behavior changes.

Important failure modes

TriggerConsequenceGuard
Packaged page path or development server is unavailableThe main window opens without the Polar rendererKeep BASE_URL and the build or dev-server contract aligned
Renderer and main process use different IPC channel namesRequests receive no handler or no replyChange src/…/ipcChannels.ts, the renderer client, and the main listener as one contract
A renderer request has no usable reply channelThe promise remains pending or concurrent calls can receive the wrong resultUse the generated request and response channels from the IPC helpers
Main window closes before a renderer-facing service repliesThe result or event cannot reach the rendererStop or retry work when the window is unavailable
MCP bridge port is already in useMCP clients cannot connect to this app's bridgeSet POLAR_MCP_PORT to a free localhost port
A node service is stopped or its credentials are staleThe matching proxy request returns an errorStart the selected network and clear proxy caches after credential or node changes

Key files

FileWhy to read it
electron/index.tsShows the main-process entry point and runtime setup
electron/windowManager.tsDefines startup order, window lifetime, tray creation, and bridge startup
electron/appIpcListener.tsDefines host actions exposed to the renderer
electron/appMenu.tsDefines native application menu behavior
electron/trayManager.tsDefines tray visibility, quit behavior, and theme-aware controls
src/…/ipcService.tsDefines the renderer request, response, and event-stream patterns
src/…/ipcChannels.tsDefines the channel contract shared by both processes
electron/…/lndProxyServer.tsIllustrates the main-process Lightning service boundary, wallet-state access, cache recovery, and event forwarding
electron/…/tapdProxyServer.tsIllustrates the Taproot Asset service boundary
electron/…/litdProxyServer.tsIllustrates the Litd service boundary
electron/mcpBridge.tsDefines the local HTTP-to-renderer MCP bridge
src/…/ipc.tsDefines the renderer side of MCP discovery and execution