SuperMcBrent

app_home

Aug 17th, 2025 (edited)
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | Gaming | 0 0
  1. local buttons = {}
  2.  
  3. local BTN_W, BTN_H = 13, 5
  4. local PADDING_X, PADDING_Y = 2, 4
  5. local GAP_X, GAP_Y = 3, 2
  6.  
  7. local function mainView(ctx)
  8.     return {
  9.         init = function()
  10.             buttons = {}
  11.             local W, H = ctx.os.size()
  12.             local x, y = PADDING_X, PADDING_Y
  13.  
  14.             for _, id in ipairs(ctx.os.list()) do
  15.                 if id ~= "home" then
  16.                     local app = ctx.os.get(id)
  17.                     local label = app.name or id
  18.                     local btnId = "home_btn_" .. id
  19.  
  20.                     ctx.libs().button.create({
  21.                         name = btnId,
  22.                         app = "home",
  23.                         view = "page",
  24.                         x = x,
  25.                         y = y,
  26.                         w = BTN_W,
  27.                         h = BTN_H,
  28.                         colorOn = colors.cyan,
  29.                         textOn = label,
  30.                         textX = x + math.floor(BTN_W / 2) - math.floor(#label / 2),
  31.                         textY = y + 2,
  32.                     })
  33.  
  34.                     table.insert(buttons, { id = id, btnId = btnId })
  35.  
  36.                     -- grid layout: fill row, then wrap
  37.                     x = x + BTN_W + GAP_X
  38.                     if x + BTN_W > W then
  39.                         x = PADDING_X
  40.                         y = y + BTN_H + GAP_Y
  41.                     end
  42.                 end
  43.             end
  44.         end,
  45.  
  46.         draw = function(mon)
  47.             for _, b in ipairs(buttons) do
  48.                 ctx.libs().button.draw(b.btnId, mon)
  49.             end
  50.         end,
  51.  
  52.         touch = function(x, y)
  53.             for _, b in ipairs(buttons) do
  54.                 if ctx.libs().button.isWithinBoundingBox(x, y, b.btnId) then
  55.                     ctx.os.navigate(b.id)
  56.                     return
  57.                 end
  58.             end
  59.         end
  60.     }
  61. end
  62.  
  63. local views = {
  64.     root = mainView
  65. }
  66.  
  67. return {
  68.     id = "home",
  69.     name = "Home",
  70.     protocol = "home",
  71.     receive = function(ctx, sender, message) end,
  72.     views = views,
  73.     create = function(ctx)
  74.         for k, v in pairs(views) do
  75.             local view = v(ctx)
  76.             if view and view.init then
  77.                 view.init()
  78.             end
  79.         end
  80.     end,
  81.     resume = function(ctx) end,
  82.     suspend = function(ctx) end,
  83.     destroy = function(ctx) end,
  84.     update = function(ctx, dt) end,
  85.     draw = function(ctx, mon, viewId)
  86.         local v = views[viewId]
  87.         if v and v(ctx).draw then
  88.             v(ctx).draw(mon)
  89.         end
  90.     end,
  91.     touch = function(ctx, x, y, viewId)
  92.         local v = views[viewId]
  93.         if v and v(ctx).touch then
  94.             v(ctx).touch(x, y)
  95.         end
  96.     end
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment