Introducing a new way to review your PRs.

Read the announcement

crmne / spotifast

Spotify, native and fast. One lightweight Rust app for your whole library, local playback, and Spotify Connect on Linux, macOS, and Windows.

View on GitHub

19 documents · Updated Nov 18, 2026

docs / application-runtime

Application runtime and lifecycle

Spotifast is a desktop music client whose process can keep playback and desktop controls alive even when no application window is open. Its runtime turns a launch into one long-lived application state, gives new launches and desktop actions a safe path to that state, and coordinates window creation, hiding, replacement, and final shutdown. See Executable entrypoints and startup diagnostics for launch details and Application state orchestration for the state that the runtime drives.

Runtime model

The runtime has one long-lived App state and a window shell around it. The shell may be recreated while the App continues to own playback, service connections, tray integration, saved state, and remote commands. This separation lets closing a window hide the interface without stopping music, while a later Show action creates a fresh window around the same state (src/app.rs• L955-1014, src/app.rs• L1562-1655).

The process normally runs through these stages:

  1. The executable prepares the launch and decides whether it is a control client, a child visualizer, or the desktop application.
  2. The application claims its per-user instance slot and prepares its runtime folders, settings, and desktop services.
  3. The shell opens the main window or starts headless in the tray.
  4. Each window runs application work before drawing. The same work continues during headless operation.
  5. A close request hides, replaces, or quits the window according to the current intent and settings.
  6. A real quit saves state, flushes pending file writes, and stops the backend.

The flow below shows how the process keeps the application state separate from each window.

flowchart TD
    Launch[Desktop launch] --> Decide{Launch kind}
    Decide -->|Control or link| Running[Running instance]
    Decide -->|Application| Guard[Claim instance slot]
    Guard -->|Second copy| Running
    Guard -->|First copy| Prepare[Prepare folders, settings, and services]
    Prepare --> Shell[Start application shell]
    Shell --> Window[Create main or mini window]
    Shell --> Tray[Run headless in tray]
    Window --> AppState[Long-lived application state]
    Tray --> AppState
    AppState -->|Show| Window
    AppState -->|Hide| Tray
    AppState -->|Switch| Window
    AppState -->|Quit| Shutdown[Save, flush, and stop]

Startup coordination

Startup coordination makes sure that only the real application process creates the desktop services and window. The entrypoint handles update helpers, child visualizer launches, command clients, link validation, instance claiming, directory discovery, settings loading, and shell startup in that order (src/entrypoint.rs• L354-453, src/entrypoint.rs• L482-618).

The launch can carry a Spotify link or a request to start hidden. A link must reach a visible running application so the user can see the target. A hidden launch stays hidden when another instance already exists, which supports login startup without pulling an existing window forward (src/single_instance.rs• L198-245).

Single-instance behavior

Single-instance behavior prevents duplicate Spotify Connect devices, media-control services, and tray items. The first process holds an exclusive per-user slot for its full lifetime. A later process sends a request to that process and exits instead of creating a rival application (src/single_instance.rs• L1-25, src/single_instance.rs• L38-44).

The channel carries both actions and small read snapshots. Actions enter the application's normal command queue. Read requests such as now-playing and device listing use published snapshots, so the channel listener does not read application state directly. The channel also validates link, URI, search, and device arguments before they reach the application (src/single_instance.rs• L46-103, src/single_instance.rs• L250-287).

Portable Windows copies use a separate instance scope. This keeps a portable copy from handing its launches to an installed copy or to another portable copy with different data (src/single_instance.rs• L137-159, src/paths.rs• L111-121).

Window lifecycle

The window is a replaceable view of the long-lived application. The runtime can create the main window, the Winamp-style mini player, or no window at all. Window attachment installs the per-window UI resources and restores the relevant geometry. Window loss releases window-owned visual resources but leaves playback, tray control, and media services active (src/app.rs• L1562-1644, src/app.rs• L1646-1655).

The main window remembers its size, position, and maximized state. The mini player keeps its own position and size policy so it cannot overwrite the main window's geometry. A saved position is checked against the current displays before it is restored (src/entrypoint.rs• L833-927, src/window.rs• L1-4). Platform limits affect optional window features: Wayland cannot set an always-on-top level, and only Windows and X11 can hide the mini player from the taskbar (src/window.rs• L93-130).

Window transitions have three distinct outcomes:

OutcomeRuntime effect
HideClose the window and keep the process in the tray.
ReopenClose one window type and create the other around the same state.
QuitEnd the process after state and file coordination.

The shell selects the outcome from application intents. A show request during headless operation creates a window. A close request hides only when a working tray item exists and background playback is enabled (src/app.rs• L955-1014, src/app.rs• L1657-1691, src/app.rs• L14500-14546).

Headless runtime

Headless operation keeps the application useful without a visible window. The runtime continues to process remote commands, backend events, media commands, power and network changes, tray actions, playback polling, and file state while it waits for a Show or Quit request (src/app.rs• L977-1010, src/app.rs• L14500-14535).

A start-hidden launch waits briefly for the desktop tray host to expose the tray item. If no tray host appears before the deadline, the runtime opens the window because there would otherwise be no reliable way to return to the application (src/app.rs• L1666-1691). The shell also rejects a hidden start when no tray or background-play setting can keep the process reachable (src/app.rs• L999-1010).

Runtime configuration

Runtime configuration combines command-line choices, saved settings, session state, and the selected data profile. Settings control whether closing the window keeps playback alive, whether the tray is used, the title-bar mode, Dock visibility on macOS, and mini-player behavior (src/settings.rs• L261-399). The entrypoint applies launch-only overrides before it creates the App, so a session-specific device name or hidden start does not need to rewrite the saved configuration (src/entrypoint.rs• L482-529).

The application uses conventional per-user configuration, state, and cache folders. A portable Windows copy uses spotifast-data beside the executable and keeps its own window record and instance scope. Settings and session data remain separate from disposable caches, while logs and panic records support diagnosis (src/paths.rs• L1-25, src/paths.rs• L73-137, src/paths.rs• L154-183).

Shutdown coordination

Shutdown is a coordinated boundary rather than a window event alone. The application first records settings and the restorable session. It then waits for queued file writes for a bounded time before stopping the backend. This protects the next launch from losing recent playback, window, and navigation state (src/app.rs• L14997-15089).

Window-level cleanup also saves state when an eframe window exits. Final backend shutdown belongs to the application shell's real quit path, so hiding or switching windows does not stop services that must outlive a window (src/entrypoint.rs• L1539-1591, src/app.rs• L955-1014).

Public runtime surface

The crate exposes its internal runtime modules so diagnostics and tests can reach them. This surface includes the application, path, settings, single-instance, and window services that support the lifecycle described above. Platform-specific services remain available only on their target systems, and demo-only services remain limited to tests or demo builds (src/lib.rs• L1-70).

References: src/lib.rs• L1-70

Change checklist

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

  • Keep the instance-slot scope aligned with the data-profile rules, especially for portable Windows copies.
  • Keep launch outcomes aligned across the CLI, single-instance request names, application intents, and shell close results.
  • Keep main-window and mini-player persistence separate so switching windows cannot replace the other window's geometry.
  • Keep hidden-start behavior aligned with tray availability and the background-play setting, or the process can become unreachable.
  • Keep shutdown ordering aligned with file-writer flushing and backend teardown so state is not lost or services stop too early.
  • Keep public module visibility and platform gates aligned when runtime modules move, so diagnostics and tests retain the intended access.

Important failure modes

TriggerConsequenceGuard
A second launch starts the full application instead of using the instance slot.Duplicate media services, tray items, or Connect devices can compete.Hold the exclusive slot for the process lifetime and surface requests to the first process.
A hidden start has no visible tray item.The process can run with no way for the user to show its window.Wait for the tray host, then set a show request when the wait expires.
A mini-player close writes main-window geometry.The next main window opens at the wrong size or position.Use separate persistence for each window kind.
A saved window position points to a disconnected monitor.The window can open outside the visible desktop.Recover off-screen geometry before normal window use.
Shutdown skips the file flush.Recent settings or session state may not reach disk.Save state, flush pending writes, then stop the backend.
A runtime module loses its intended export or platform gate.Diagnostics, tests, or target-specific builds can fail to reach the service they need.Keep src/lib.rs exports and conditional visibility aligned with the supported build targets.

Key files

FileWhy to read it
src/entrypoint.rsShows the launch sequence, shell setup, window creation, and final eframe boundary.
src/app.rsDefines the long-lived application state and the hide, show, switch, headless, and quit contracts.
src/single_instance.rsExplains the instance slot, request channel, and remote-control snapshots.
src/settings.rsDefines the saved settings that change runtime and window behavior.
src/paths.rsDefines installed and portable data profiles and their persistence boundaries.
src/window.rsDocuments display recovery and platform limits for window behavior.
src/lib.rsShows the public runtime module surface and its platform and build gates.
src/spotifast.rsShows the small executable entrypoint that delegates to startup coordination.