aboutsummaryrefslogtreecommitdiff
path: root/sourcehut-builds/bar.luau
blob: 862287d5b1dd9c2815731dfce18653e14fe12f29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
--!nonstrict
-- Sourcehut Builds - Bar widget that toggles the build status panel and shows the status of recent tasks.
--
-- You can configure the glyphs shown for the different statuses, as well as
-- when there are no recent builds.

local K = require("./constants.luau")

--------------------------------------------------------------------------------
-- Constants
--------------------------------------------------------------------------------
local GLYPHS = {
  default = "glyph",
  ok = "ok_glyph",
  failed = "failed_glyph",
  inProgress = "in_progress_glyph",
}
local STYLE = {
  color = {
    closed = "on_surface",
    open = "primary"
  }
}

--------------------------------------------------------------------------------
-- State
--------------------------------------------------------------------------------
local isPanelOpen: boolean = false
local buildState: SourcehutBuildsState = require("./empty_state.luau")

--------------------------------------------------------------------------------
-- Internal Functions
--------------------------------------------------------------------------------

------------------------------------------------------------------ Logic Helpers

local function hasRecentBuilds(): boolean
  return buildState.successful > 0 or buildState.failed > 0 or buildState.inProgress > 0
end

----------------------------------------------------------------------------- UI

-- Renders the bar
local function render()
  barWidget.setGlyphColor(isPanelOpen and STYLE.color.open or STYLE.color.closed)

  barWidget.setTooltip(noctalia.tr("bar.tooltip"))

  local container = barWidget.isVertical() and ui.column or ui.row
  barWidget.render(container({ gap = 6, align = "center" }, {
    not hasRecentBuilds() and ui.glyph({ name = noctalia.getConfig(GLYPHS.default), size = 14, color = isPanelOpen and STYLE.color.open or STYLE.color.closed }) or nil,
    buildState.successful > 0 and ui.glyph({ name = noctalia.getConfig(GLYPHS.ok), size = 14, color = isPanelOpen and STYLE.color.open or STYLE.color.closed }) or nil,
    buildState.failed > 0 and ui.glyph({ name = noctalia.getConfig(GLYPHS.failed), size = 14, color = isPanelOpen and STYLE.color.open or STYLE.color.closed }) or nil,
    buildState.inProgress > 0 and ui.glyph({ name = noctalia.getConfig(GLYPHS.inProgress), size = 14, color = isPanelOpen and STYLE.color.open or STYLE.color.closed }) or nil,
  }))
end


---------------------------------------------------------------- Event Listeners

local function onPanelStateChanged(value: boolean)
  isPanelOpen = value
  render()
end

local function onBuildsStateChanged(value: SourcehutBuildsState)
  buildState = value
  render()
end

---------------------------------------------------------------------- Lifecycle

local function run()
  noctalia.setUpdateInterval(K.bar.update_interval)
end

--------------------------------------------------------------------------------
-- Listeners
--------------------------------------------------------------------------------

noctalia.state.watch(K.events.panel_state_updated, onPanelStateChanged)
noctalia.state.watch(K.events.builds_state_updated, onBuildsStateChanged)

--------------------------------------------------------------------------------
-- Public API
--------------------------------------------------------------------------------
function update()
  render()
end

function onClick()
	noctalia.togglePanel(K.panel.id)
end

function onConfigChanged()
end

run()