Module: TurboDesktop::ViewHelpers

Defined in:
lib/turbo_desktop/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#turbo_desktop_app?Boolean

Returns true if the request is from a Turbo Desktop app.

Returns:

  • (Boolean)


4
5
6
# File 'lib/turbo_desktop/view_helpers.rb', line 4

def turbo_desktop_app?
  request.user_agent.to_s.match?(TurboDesktop.configuration.user_agent_pattern)
end

#turbo_desktop_archObject

Returns the architecture: "aarch64", "x86_64", or nil.



20
21
22
23
24
25
26
27
# File 'lib/turbo_desktop/view_helpers.rb', line 20

def turbo_desktop_arch
  return nil unless turbo_desktop_app?

  case request.user_agent.to_s
  when /aarch64/i then "aarch64"
  when /x86_64/i then "x86_64"
  end
end

#turbo_desktop_bridge(component, **options) ⇒ Object

Renders a bridge component data attribute.

<%= tag.button "Export", **turbo_desktop_bridge("menu-item", title: "Export PDF", shortcut: "Cmd+E") %>


32
33
34
35
36
37
38
# File 'lib/turbo_desktop/view_helpers.rb', line 32

def turbo_desktop_bridge(component, **options)
  attrs = { "data-turbo-desktop-bridge" => component }
  options.each do |key, value|
    attrs["data-turbo-desktop-bridge-#{key}"] = value.to_s
  end
  attrs
end

#turbo_desktop_inspector?Boolean

Returns true when the Dev Inspector is enabled in configuration.

Returns:

  • (Boolean)


51
52
53
# File 'lib/turbo_desktop/view_helpers.rb', line 51

def turbo_desktop_inspector?
  TurboDesktop.configuration.inspector_enabled
end

#turbo_desktop_inspector_meta_tagObject

Emits the tag that enables the Dev Inspector in the browser, or nil when the inspector is disabled. Place in your layout ; it is a no-op in production unless you explicitly enable the inspector there.

The tag also carries the same-origin URL of the inspector entry module (served by this engine) so the desktop shell's turbo-desktop.js can import() it instead of guessing a relative path.

<%= turbo_desktop_inspector_meta_tag %>


64
65
66
67
68
69
# File 'lib/turbo_desktop/view_helpers.rb', line 64

def turbo_desktop_inspector_meta_tag
  return nil unless turbo_desktop_inspector?

  tag.meta(name: "turbo-desktop-inspector", content: "enabled",
           data: { inspector_url: turbo_desktop_inspector_url })
end

#turbo_desktop_inspector_urlObject

Same-origin URL of the inspector entry module, under the engine's mount path (configurable via config.inspector_mount_path).



73
74
75
# File 'lib/turbo_desktop/view_helpers.rb', line 73

def turbo_desktop_inspector_url
  "#{TurboDesktop.configuration.inspector_mount_path.chomp("/")}/inspector.js"
end

#turbo_desktop_only(&block) ⇒ Object

Conditionally render content only for desktop apps.



41
42
43
# File 'lib/turbo_desktop/view_helpers.rb', line 41

def turbo_desktop_only(&block)
  capture(&block) if turbo_desktop_app?
end

#turbo_desktop_platformObject

Returns the desktop platform: "macos", "windows", "linux", or nil.



9
10
11
12
13
14
15
16
17
# File 'lib/turbo_desktop/view_helpers.rb', line 9

def turbo_desktop_platform
  return nil unless turbo_desktop_app?

  case request.user_agent.to_s
  when /macOS/i then "macos"
  when /Windows/i then "windows"
  when /Linux/i then "linux"
  end
end

#turbo_web_only(&block) ⇒ Object

Conditionally render content only for web (non-desktop) users.



46
47
48
# File 'lib/turbo_desktop/view_helpers.rb', line 46

def turbo_web_only(&block)
  capture(&block) unless turbo_desktop_app?
end