--# DeviceMetrics
-- Apple's human interface guidelines put the smallest comfortable tap target
-- at 44 points square, and that is a fixed number: a point is the same
-- physical size on a phone and on a pad. A retracted shelf shows a little
-- more than that of each card, so a card is always tappable without the
-- shelf being open.
--
-- On a pad the whole table is bigger, so a bare 44 points looks like a sliver.
-- The peek is scaled up in proportion to how much bigger the short side of
-- the screen is than a typical phone's.

MINIMUM_TAP_TARGET = 44
PHONE_REFERENCE_SHORT_SIDE = 390
TAP_TARGET_COMFORT = 1.15

local cachedIsPad = nil

-- UIUserInterfaceIdiomPhone is 0 and UIUserInterfaceIdiomPad is 1. The objc
-- bridge is wrapped in pcall because a failure here should cost a nice
-- default, not the whole project.
function deviceIsPad()
  if cachedIsPad ~= nil then return cachedIsPad end
  local answered = false
  if objc and objc.UIDevice then
    local ok, idiom = pcall(function()
      return objc.UIDevice.currentDevice.userInterfaceIdiom
    end)
    if ok and tonumber(idiom) then
      cachedIsPad = (tonumber(idiom) == 1)
      answered = true
    end
  end
  -- if the bridge told us nothing, guess from the screen instead
  if not answered then
    cachedIsPad = math.min(WIDTH, HEIGHT) > 600
  end
  return cachedIsPad
end

function tapTargetSize()
  local target = MINIMUM_TAP_TARGET * TAP_TARGET_COMFORT
  if not deviceIsPad() then return target end
  local scale = math.min(WIDTH, HEIGHT) / PHONE_REFERENCE_SHORT_SIDE
  return target * math.max(1, scale)
end
