summaryrefslogtreecommitdiff
path: root/lua/widgets.lua
blob: 9807bc0900a32cccb1e005c6d7c7ef5284bcdd5f (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
99
100
101
102
103
104
105
local lvgl = require("lvgl")
local power = require("power")
local bluetooth = require("bluetooth")
local playback = require("playback")

local widgets = {}

function widgets.StatusBar(parent, opts)
  local status_bar = {}

  status_bar.root = parent:Object {
    flex = {
      flex_direction = "row",
      justify_content = "flex-start",
      align_items = "center",
      align_content = "center",
    },
    w = lvgl.HOR_RES(),
    h = lvgl.SIZE_CONTENT,
    bg_opa = lvgl.OPA(100),
    bg_color = "#fff",
    pad_top = 1,
    pad_bottom = 1,
    pad_column = 1,
    shadow_width = 6,
    shadow_opa = lvgl.OPA(50),
    shaddow_ofs_x = 0,
    scrollbar_mode = lvgl.SCROLLBAR_MODE.OFF,
  }

  if opts.back_cb then
    status_bar.back = status_bar.root:Label {
      w = lvgl.SIZE_CONTENT,
      h = 12,
      text = "<",
    }
    status_bar.back:onClicked(opts.back_cb)
  end

  status_bar.title = status_bar.root:Label {
    w = lvgl.PCT(100),
    h = 16,
    text = "",
    flex_grow = 1,
  }
  if opts.title then
    status_bar.title.set { text = opts.title }
  end

  status_bar.playing = status_bar.root:Image {}
  status_bar.bluetooth = status_bar.root:Image {}
  status_bar.battery = status_bar.root:Image {}

  status_bar.bindings = {
    power.battery_pct:bind(function(percent)
      local src
      if percent >= 95 then
        src = "battery_full.png"
      elseif percent >= 75 then
        src = "battery_80.png"
      elseif percent >= 55 then
        src = "battery_60.png"
      elseif percent >= 35 then
        src = "battery_40.png"
      elseif percent >= 15 then
        src = "battery_20.png"
      else
        src = "battery_empty.png"
      end
      status_bar.battery:set_src("//lua/assets/" .. src)
    end),
    playback.playing:bind(function(playing)
      if playing then
        status_bar.playing:set_src("//lua/assets/play.png")
      else
        status_bar.playing:set_src("//lua/assets/pause.png")
      end
    end),
    playback.track:bind(function(track)
      if track then
        status_bar.playing:clear_flag(lvgl.FLAG.HIDDEN)
      else
        status_bar.playing:add_flag(lvgl.FLAG.HIDDEN)
      end
    end),
    bluetooth.enabled:bind(function(en)
      if en then
        status_bar.bluetooth:clear_flag(lvgl.FLAG.HIDDEN)
      else
        status_bar.bluetooth:add_flag(lvgl.FLAG.HIDDEN)
      end
    end),
    bluetooth.connected:bind(function(connected)
      if connected then
        status_bar.bluetooth:set_src("//lua/assets/bt_conn.png")
      else
        status_bar.bluetooth:set_src("//lua/assets/bt.png")
      end
    end),
  }

  return status_bar
end

return widgets