Desktop Detection - aguspe/turbo_desktop GitHub Wiki

Desktop Detection

Turbo Desktop lets you serve different content depending on whether the user is in the native desktop app or a regular web browser.

Detection Mechanism

The desktop shell adds a custom User-Agent string:

Turbo Desktop/0.1.0 (macOS; aarch64)

The turbo_desktop-rails gem parses this automatically and provides helper methods.

View Helpers

Show content only in the desktop app

<%%= turbo_desktop_only do %>
  <div class="native-toolbar">
    <button data-controller="notification">Native Notification</button>
    <button data-controller="file-picker">Open File</button>
  </div>
<%% end %>

Show content only in web browsers

<%%= turbo_web_only do %>
  <div class="download-banner">
    <p>Get the desktop app for native features!</p>
    <a href="/download">Download</a>
  </div>
<%% end %>

Controller Helpers

class DashboardController < ApplicationController
  def index
    if turbo_desktop_app?
      # Desktop-specific logic
      @show_native_features = true
    end
  end
end

Platform & Architecture

turbo_desktop_platform  # => "macos", "windows", "linux", or nil
turbo_desktop_arch      # => "aarch64", "x86_64", or nil

Use these for platform-specific keyboard shortcuts, UI differences, or feature flags.

Example: Adaptive Layout

The example app uses detection in its layout to show a platform badge:

<nav class="navbar">
  <!-- ... -->
  <%%= turbo_desktop_only do %>
    <span class="badge badge-desktop">
      Desktop (<%%= turbo_desktop_platform %>)
    </span>
  <%% end %>

  <%%= turbo_web_only do %>
    <span class="badge badge-web">Web Browser</span>
  <%% end %>
</nav>

This gives desktop users visual confirmation they're in the native app, while web users see a regular browser badge.

⚠️ **GitHub.com Fallback** ⚠️