Module: Ask::WebFetch::Backends::FocusRestorer

Defined in:
lib/ask/web_fetch/backends/attached_browser.rb

Overview

macOS dev convenience: the attached Chrome window steals focus when a tab is created, and a developer working in another app wants it back once the tab is closed. Captures the frontmost app before the page is created and re-activates it after close. Deliberately cheap and quiet: one osascript each way, rescued to nil everywhere else (prod is headless Linux — this module is a no-op there, and even on macOS a failure must never fail a fetch).

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.runnerObject



217
218
219
# File 'lib/ask/web_fetch/backends/attached_browser.rb', line 217

def runner
  @runner ||= method(:system)
end

Class Method Details

.capture_frontmostObject



222
223
224
225
226
227
228
229
# File 'lib/ask/web_fetch/backends/attached_browser.rb', line 222

def self.capture_frontmost
  return nil unless RUBY_PLATFORM.include?('darwin')

  out = `osascript -e 'tell application "System Events" to get name of first application process whose frontmost is true' 2>/dev/null`.strip
  out.empty? ? nil : out
rescue StandardError
  nil
end

.restore_frontmost(app) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/ask/web_fetch/backends/attached_browser.rb', line 231

def self.restore_frontmost(app)
  return if app.nil? || app.empty?

  # Never ping-pong: if the user is already looking at Chrome (or
  # headless/CI has no frontmost app at all), there's nothing to
  # give back.
  return if app == 'Google Chrome'

  runner.call("osascript -e 'tell application \"#{app}\" to activate' 2>/dev/null")
  nil
rescue StandardError
  nil
end