From 63e1e544624c31feb4684c208e31230472ae598b Mon Sep 17 00:00:00 2001
From: Gabriel Tofvesson <gabriel@tofvesson.se>
Date: Sat, 12 Oct 2024 00:37:02 +0200
Subject: [PATCH] Center item detail title

---
 itemcontroller.lua | 42 +++++++++++++++++++++++++++++++-----------
 1 file changed, 31 insertions(+), 11 deletions(-)

diff --git a/itemcontroller.lua b/itemcontroller.lua
index e5b002f..9e492ba 100644
--- a/itemcontroller.lua
+++ b/itemcontroller.lua
@@ -241,6 +241,22 @@ end
 
 local function NOOP() end
 
+local function getCenterPad(contentWidth, widthBudget)
+  if widthBudget < contentWidth then
+    return 0, 0
+  end
+
+  local pad = (widthBudget - contentWidth) / 2
+  return math.floor(pad), math.ceil(pad)
+end
+
+local function fitText(text, widthBudget)
+  if #text <= widthBudget then
+    return text
+  end
+  return text:sub(1, #widthBudget)
+end
+
 local PAGES = {
   MAIN = function(state)
     local pageState = state:currentPageState({})
@@ -291,23 +307,27 @@ local PAGES = {
   end,
 
   GROUP_DETAIL = function(state, newPage)
+    local paddingSide = 1
+    local paddingTop = 1
+    
     local group = state:getExtra()
     if group == nil then
       state:setPage("MAIN")
       return NOOP
     end
 
-    local title = Text:new{
-      text = group:getSimpleName(),
-      bgColor = colors.gray
-    }
+    local itemName = fitText(group:getSimpleName(), state.width - (paddingSide * 2))
+    local itemLeftPad, itemRightPad = getCenterPad(#itemName, state.width - (paddingSide * 2))
 
     local paddedTitle = Padding:new{
       top = 1,
-      left = 1,
-      right = 1,
+      left = itemLeftPad,
+      right = itemRightPad,
       bottom = 1,
-      element = title,
+      element = Text:new{
+        text = itemName,
+        bgColor = colors.gray
+      },
       bgColor = colors.gray
     }
 
@@ -344,10 +364,10 @@ local PAGES = {
     local stuffContainer = Container:new{
       [Children:getId()] = {
         Padding:new{
-          top = 1,
-          left = 1,
-          right = 1,
-          bottom = 1,
+          top = paddingTop,
+          left = paddingSide,
+          right = paddingSide,
+          bottom = 0,
           element = infoElements,
         }
       },