From 71ed09a6f70901c9097973a44b24d6a6ced2834f Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 14 Nov 2023 13:20:04 +1100 Subject: Add two-way databinding for lua, and flesh out the lua statusbar --- lua/assets/audio.png | Bin 0 -> 623 bytes lua/assets/battery_20.png | Bin 0 -> 617 bytes lua/assets/battery_40.png | Bin 0 -> 617 bytes lua/assets/battery_60.png | Bin 0 -> 618 bytes lua/assets/battery_80.png | Bin 0 -> 622 bytes lua/assets/battery_empty.png | Bin 0 -> 614 bytes lua/assets/battery_full.bin | Bin 0 -> 292 bytes lua/assets/battery_full.png | Bin 0 -> 618 bytes lua/assets/bt.png | Bin 0 -> 8502 bytes lua/assets/bt_conn.png | Bin 0 -> 654 bytes lua/assets/pause.png | Bin 0 -> 608 bytes lua/assets/play.png | Bin 0 -> 616 bytes lua/backstack.lua | 37 +++++++++++++++++ lua/main.lua | 4 +- lua/main_menu.lua | 19 +++++---- lua/widgets.lua | 95 +++++++++++++++++++++++++++++++++++-------- 16 files changed, 128 insertions(+), 27 deletions(-) create mode 100644 lua/assets/audio.png create mode 100644 lua/assets/battery_20.png create mode 100644 lua/assets/battery_40.png create mode 100644 lua/assets/battery_60.png create mode 100644 lua/assets/battery_80.png create mode 100644 lua/assets/battery_empty.png create mode 100644 lua/assets/battery_full.bin create mode 100644 lua/assets/battery_full.png create mode 100644 lua/assets/bt.png create mode 100644 lua/assets/bt_conn.png create mode 100644 lua/assets/pause.png create mode 100644 lua/assets/play.png create mode 100644 lua/backstack.lua (limited to 'lua') diff --git a/lua/assets/audio.png b/lua/assets/audio.png new file mode 100644 index 00000000..b8ad9071 Binary files /dev/null and b/lua/assets/audio.png differ diff --git a/lua/assets/battery_20.png b/lua/assets/battery_20.png new file mode 100644 index 00000000..9012376f Binary files /dev/null and b/lua/assets/battery_20.png differ diff --git a/lua/assets/battery_40.png b/lua/assets/battery_40.png new file mode 100644 index 00000000..88a0b448 Binary files /dev/null and b/lua/assets/battery_40.png differ diff --git a/lua/assets/battery_60.png b/lua/assets/battery_60.png new file mode 100644 index 00000000..d86c997a Binary files /dev/null and b/lua/assets/battery_60.png differ diff --git a/lua/assets/battery_80.png b/lua/assets/battery_80.png new file mode 100644 index 00000000..344b3703 Binary files /dev/null and b/lua/assets/battery_80.png differ diff --git a/lua/assets/battery_empty.png b/lua/assets/battery_empty.png new file mode 100644 index 00000000..c9176e8c Binary files /dev/null and b/lua/assets/battery_empty.png differ diff --git a/lua/assets/battery_full.bin b/lua/assets/battery_full.bin new file mode 100644 index 00000000..b01ca61f Binary files /dev/null and b/lua/assets/battery_full.bin differ diff --git a/lua/assets/battery_full.png b/lua/assets/battery_full.png new file mode 100644 index 00000000..57122a23 Binary files /dev/null and b/lua/assets/battery_full.png differ diff --git a/lua/assets/bt.png b/lua/assets/bt.png new file mode 100644 index 00000000..180e6b3a Binary files /dev/null and b/lua/assets/bt.png differ diff --git a/lua/assets/bt_conn.png b/lua/assets/bt_conn.png new file mode 100644 index 00000000..7a5f2b27 Binary files /dev/null and b/lua/assets/bt_conn.png differ diff --git a/lua/assets/pause.png b/lua/assets/pause.png new file mode 100644 index 00000000..ec388cd5 Binary files /dev/null and b/lua/assets/pause.png differ diff --git a/lua/assets/play.png b/lua/assets/play.png new file mode 100644 index 00000000..0d0bb34d Binary files /dev/null and b/lua/assets/play.png differ diff --git a/lua/backstack.lua b/lua/backstack.lua new file mode 100644 index 00000000..c54fbac4 --- /dev/null +++ b/lua/backstack.lua @@ -0,0 +1,37 @@ +local lvgl = require("lvgl") + +local backstack = { + root = lvgl.Object(nil, { + w = lvgl.HOR_RES(), + h = lvgl.VER_RES(), + }), + stack = {}, +} + +function backstack:Top() + return self.stack[#self.stack] +end + +function backstack:SetTopParent(parent) + local top = self:Top() + if top and top.root then + top.root:set_parent(parent) + end +end + +function backstack:Push(screen) + self:SetTopParent(nil) + table.insert(self.stack, screen) + self:SetTopParent(self.root) +end + +function backstack:Pop(num) + num = num or 1 + for _ = 1, num do + local removed = table.remove(self.stack) + removed.root:delete() + end + self:SetTopParent(self.root) +end + +return backstack diff --git a/lua/main.lua b/lua/main.lua index 2a80a571..ce9596af 100644 --- a/lua/main.lua +++ b/lua/main.lua @@ -1 +1,3 @@ -require("main_menu"):Create() +local backstack = require("backstack") +local main_menu = require("main_menu"):Create(backstack.root) +backstack:Push(main_menu) diff --git a/lua/main_menu.lua b/lua/main_menu.lua index 924b51cf..f0be33de 100644 --- a/lua/main_menu.lua +++ b/lua/main_menu.lua @@ -5,8 +5,9 @@ local database = require("database") local main_menu = {} -function main_menu:Create() - local root = lvgl.Object(nil, { +function main_menu:Create(parent) + local menu = {} + menu.root = lvgl.Object(parent, { flex = { flex_direction = "column", flex_wrap = "wrap", @@ -17,31 +18,33 @@ function main_menu:Create() w = lvgl.HOR_RES(), h = lvgl.VER_RES(), }) - root:center() + menu.root:center() - widgets.StatusBar(root, {}) + menu.status_bar = widgets.StatusBar(menu.root, {}) - local list = lvgl.List(root, { + menu.list = lvgl.List(menu.root, { w = lvgl.PCT(100), h = lvgl.PCT(100), flex_grow = 1, }) - list:add_btn(nil, "Now Playing"):onClicked(function() + menu.list:add_btn(nil, "Now Playing"):onClicked(function() legacy_ui.open_now_playing(); end) local indexes = database.get_indexes() for id, name in ipairs(indexes) do - local btn = list:add_btn(nil, name) + local btn = menu.list:add_btn(nil, name) btn:onClicked(function() legacy_ui.open_browse(id); end) end - list:add_btn(nil, "Settings"):onClicked(function() + menu.list:add_btn(nil, "Settings"):onClicked(function() legacy_ui.open_settings(); end) + + return menu end return main_menu diff --git a/lua/widgets.lua b/lua/widgets.lua index bcc3ca59..a281620e 100644 --- a/lua/widgets.lua +++ b/lua/widgets.lua @@ -1,37 +1,96 @@ local lvgl = require("lvgl") +local power = require("power") +local bluetooth = require("bluetooth") +local playback = require("playback") local widgets = {} -function widgets.StatusBar(parent) - local container = parent:Object { +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", + flex_direction = "row", + justify_content = "flex-start", + align_items = "center", + align_content = "center", }, w = lvgl.HOR_RES(), - h = 16, + h = 18, } - container:Label { - w = lvgl.SIZE_CONTENT, - h = 12, - text = "<", - } + 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 - container:Label { + status_bar.title = status_bar.root:Label { w = lvgl.PCT(100), h = 16, - text = "cool title", + text = "", flex_grow = 1, } + if opts.title then + status_bar.title.set { text = opts.title } + end - container:Label { - w = lvgl.SIZE_CONTENT, - h = 16, - text = "69%", + 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 -- cgit v1.2.3