|
| 1 | +--!nonstrict |
| 2 | +-- World Clock panel — list of configured timezones with live times. |
| 3 | +-- Add IANA zones (e.g. Europe/Berlin); remove with trash; reorder via drag grip. |
| 4 | + |
| 5 | +local DRAG_TYPE = "world-clock-zone" |
| 6 | + |
| 7 | +local zones = {} |
| 8 | +local draft = "" |
| 9 | +local draftKey = 0 |
| 10 | +local pendingDelete = nil |
| 11 | + |
| 12 | +local function insertionZone(index) |
| 13 | + return ui.dropZone({ |
| 14 | + key = "gap-" .. index, |
| 15 | + accepts = { DRAG_TYPE }, |
| 16 | + value = tostring(index), |
| 17 | + onDrop = "onZoneDropped", |
| 18 | + height = 3, |
| 19 | + radius = 4, |
| 20 | + expandOnDrag = true, |
| 21 | + hitSlop = 24, |
| 22 | + }) |
| 23 | +end |
| 24 | + |
| 25 | +local function zoneRow(id) |
| 26 | + for _, row in ipairs(zones) do |
| 27 | + if row.id == id then |
| 28 | + return row |
| 29 | + end |
| 30 | + end |
| 31 | + return nil |
| 32 | +end |
| 33 | + |
| 34 | +function onZoneDropped(payload, value) |
| 35 | + local insertAt = tonumber(value) |
| 36 | + if type(payload) ~= "string" or payload == "" or insertAt == nil then |
| 37 | + return |
| 38 | + end |
| 39 | + noctalia.state.set("world_clock.cmd", { op = "reorder", zone = payload, index = insertAt }) |
| 40 | +end |
| 41 | + |
| 42 | +local function render() |
| 43 | + local rows = {} |
| 44 | + if #zones == 0 then |
| 45 | + table.insert(rows, ui.label({ |
| 46 | + text = noctalia.tr("ui.empty"), |
| 47 | + color = "on_surface_variant", |
| 48 | + fontSize = 13, |
| 49 | + })) |
| 50 | + else |
| 51 | + for i, row in ipairs(zones) do |
| 52 | + local id = row.id |
| 53 | + local deleting = pendingDelete == id |
| 54 | + local actions |
| 55 | + if deleting then |
| 56 | + actions = ui.row({ gap = 4, align = "center" }, { |
| 57 | + ui.button({ |
| 58 | + glyph = "check", |
| 59 | + variant = "destructive", |
| 60 | + onClick = function() |
| 61 | + noctalia.state.set("world_clock.cmd", { op = "remove", zone = id }) |
| 62 | + pendingDelete = nil |
| 63 | + end, |
| 64 | + }), |
| 65 | + ui.button({ |
| 66 | + glyph = "close", |
| 67 | + variant = "ghost", |
| 68 | + onClick = function() |
| 69 | + pendingDelete = nil |
| 70 | + render() |
| 71 | + end, |
| 72 | + }), |
| 73 | + }) |
| 74 | + else |
| 75 | + actions = ui.button({ |
| 76 | + glyph = "trash", |
| 77 | + variant = "ghost", |
| 78 | + onClick = function() |
| 79 | + pendingDelete = id |
| 80 | + render() |
| 81 | + end, |
| 82 | + }) |
| 83 | + end |
| 84 | + |
| 85 | + table.insert(rows, insertionZone(i)) |
| 86 | + table.insert( |
| 87 | + rows, |
| 88 | + ui.row({ |
| 89 | + key = "zone-" .. id, |
| 90 | + gap = 8, |
| 91 | + align = "center", |
| 92 | + paddingV = 5, |
| 93 | + paddingH = 8, |
| 94 | + fill = "surface_variant/0.35", |
| 95 | + radius = 6, |
| 96 | + }, { |
| 97 | + ui.dragSource({ |
| 98 | + key = "grip-" .. id, |
| 99 | + dragType = DRAG_TYPE, |
| 100 | + payload = id, |
| 101 | + previewAncestor = 1, |
| 102 | + liftFromLayout = true, |
| 103 | + width = 20, |
| 104 | + height = 20, |
| 105 | + align = "center", |
| 106 | + justify = "center", |
| 107 | + tooltip = noctalia.tr("ui.drag_tooltip"), |
| 108 | + }, { |
| 109 | + ui.glyph({ name = "menu-2", size = 14, color = "on_surface_variant" }), |
| 110 | + }), |
| 111 | + ui.column({ flexGrow = 1, gap = 1 }, { |
| 112 | + ui.label({ text = row.label or id, fontWeight = "bold", fontSize = 13 }), |
| 113 | + ui.label({ text = id, color = "on_surface_variant", fontSize = 10 }), |
| 114 | + }), |
| 115 | + ui.column({ align = "end", gap = 1 }, { |
| 116 | + ui.label({ text = row.time or "", fontWeight = "bold", fontSize = 15, color = "primary" }), |
| 117 | + ui.label({ text = row.offset or "", color = "on_surface_variant", fontSize = 10 }), |
| 118 | + }), |
| 119 | + actions, |
| 120 | + }) |
| 121 | + ) |
| 122 | + end |
| 123 | + table.insert(rows, insertionZone(#zones + 1)) |
| 124 | + end |
| 125 | + |
| 126 | + panel.render(ui.column({ flexGrow = 1, gap = 10, padding = 14 }, { |
| 127 | + ui.row({ align = "center", justify = "space_between" }, { |
| 128 | + ui.label({ text = noctalia.tr("title"), fontSize = 16, fontWeight = "bold", flexGrow = 1 }), |
| 129 | + ui.button({ glyph = "close", onClick = function() |
| 130 | + panel.close() |
| 131 | + end }), |
| 132 | + }), |
| 133 | + ui.row({ gap = 8, align = "center" }, { |
| 134 | + ui.input({ |
| 135 | + key = "add-" .. draftKey, |
| 136 | + value = draft, |
| 137 | + placeholder = noctalia.tr("ui.add_placeholder"), |
| 138 | + flexGrow = 1, |
| 139 | + onChange = function(value) |
| 140 | + draft = value |
| 141 | + end, |
| 142 | + onSubmit = "onAdd", |
| 143 | + }), |
| 144 | + ui.button({ |
| 145 | + glyph = "plus", |
| 146 | + variant = "primary", |
| 147 | + onClick = "onAdd", |
| 148 | + }), |
| 149 | + }), |
| 150 | + ui.scroll({ flexGrow = 1, gap = 0 }, rows), |
| 151 | + })) |
| 152 | +end |
| 153 | + |
| 154 | +function onAdd() |
| 155 | + local zone = noctalia.string.trim(draft) |
| 156 | + draft = "" |
| 157 | + draftKey += 1 |
| 158 | + pendingDelete = nil |
| 159 | + if zone ~= "" then |
| 160 | + noctalia.state.set("world_clock.cmd", { op = "add", zone = zone }) |
| 161 | + end |
| 162 | + render() |
| 163 | +end |
| 164 | + |
| 165 | +function onOpen(_context) |
| 166 | + zones = noctalia.state.get("world_clock.zones") or {} |
| 167 | + pendingDelete = nil |
| 168 | + draft = "" |
| 169 | + draftKey += 1 |
| 170 | + panel.setWantsSecondTicks(true) |
| 171 | + render() |
| 172 | +end |
| 173 | + |
| 174 | +noctalia.state.watch("world_clock.zones", function(value) |
| 175 | + if type(value) == "table" then |
| 176 | + zones = value |
| 177 | + else |
| 178 | + zones = {} |
| 179 | + end |
| 180 | + if pendingDelete ~= nil then |
| 181 | + local stillThere = false |
| 182 | + for _, row in ipairs(zones) do |
| 183 | + if row.id == pendingDelete then |
| 184 | + stillThere = true |
| 185 | + break |
| 186 | + end |
| 187 | + end |
| 188 | + if not stillThere then |
| 189 | + pendingDelete = nil |
| 190 | + end |
| 191 | + end |
| 192 | + render() |
| 193 | +end) |
0 commit comments