summaryrefslogtreecommitdiff
path: root/lua/main_menu.lua
blob: 8754df85241e7a326f3f770fa0df3e84bcc37465 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
-- SPDX-FileCopyrightText: 2023 jacqueline <me@jacqueline.id.au>
--
-- SPDX-License-Identifier: GPL-3.0-only

local lvgl = require("lvgl")
local widgets = require("widgets")
local database = require("database")
local sd_card = require("sd_card")
local backstack = require("backstack")
local browser = require("browser")
local playing = require("playing")
local styles = require("styles")
local filesystem = require("filesystem")
local font = require("font")
local theme = require("theme")
local img = require("images")
local playback = require("playback")

return widgets.MenuScreen:new {
  create_ui = function(self)
    widgets.MenuScreen.create_ui(self)

    -- At the top, a card showing details about the current track. Hidden if
    -- there is no track currently playing.
    -- || Cool Song 0:01

    local now_playing = lvgl.Button(self.root, {
      flex = {
        flex_direction = "row",
        flex_wrap = "nowrap",
        justify_content = "flex-start",
        align_items = "center",
        align_content = "flex-start",
      },
      w = lvgl.PCT(100),
      h = lvgl.SIZE_CONTENT,
      margin_all = 2,
      pad_bottom = 2,
      pad_column = 4,
      border_width = 1,
    })
    theme.set_subject(now_playing, "now_playing");

    local play_pause = now_playing:Image { src = img.play_circ }
    local title = now_playing:Label {
      flex_grow = 1,
      h = lvgl.SIZE_CONTENT,
      text = " ",
      long_mode = 1,
    }
    local time_remaining = now_playing:Label {
      text = " ",
      text_font = font.fusion_10,
    }

    now_playing:onClicked(function() backstack.push(playing:new()) end)

    local track_duration = nil

    self.bindings = self.bindings + {
      playback.playing:bind(function(playing)
        if playing then
          play_pause:set_src(img.play_circ)
        else
          play_pause:set_src(img.pause_circ)
        end
      end),
      playback.track:bind(function(track)
        if not track then
          now_playing:add_flag(lvgl.FLAG.HIDDEN)
          return
        else
          now_playing:clear_flag(lvgl.FLAG.HIDDEN)
        end
        title:set { text = track.title }
        if track.duration then
          track_duration = track.duration
        end
      end),
      playback.position:bind(function(pos)
        if not pos then return end
        if not track_duration then return end
        local remaining = track_duration - pos
        if remaining < 0 then remaining = 0 end
        time_remaining:set {
          text = string.format("%d:%02d", remaining // 60, remaining % 60)
        }
      end),
    }

    -- Next, a list showing the user's prefer's music source. This defaults to
    -- a list of all available database indexes, but could also be the contents
    -- of the SD card root.
    local no_indexes_container = self.root:Object {
      w = lvgl.PCT(100),
      flex_grow = 1,
    }
    local no_indexes_label = no_indexes_container:Label {
      w = lvgl.PCT(100),
      h = lvgl.SIZE_CONTENT,
      text_align = 2,
      long_mode = 0,
      margin_all = 4,
      text = "",
    }
    no_indexes_label:center()

    local indexes_list = lvgl.List(self.root, {
      w = lvgl.PCT(100),
      h = lvgl.PCT(100),
      flex_grow = 1,
    })
    local indexes = {}

    for _, idx in ipairs(database.indexes()) do
      local btn = indexes_list:add_btn(nil, tostring(idx))
      btn:onClicked(function()
        backstack.push(browser:new {
          title = tostring(idx),
          iterator = idx:iter(),
          mediatype = idx:type(),
        })
      end)
      btn:add_style(styles.list_item)
      table.insert(indexes, {
        object = btn,
        index = idx,
      })
    end

    local playlist_btn = indexes_list:add_btn(nil, "Playlists")
    playlist_btn:onClicked(function()
      backstack.push(require("playlist_browser"):new {
        title = "Playlists",
        iterator = filesystem.iterator("/Playlists")
      })
    end)
    playlist_btn:add_style(styles.list_item)

    local function show_no_indexes(msg)
      indexes_list:add_flag(lvgl.FLAG.HIDDEN)
      no_indexes_container:clear_flag(lvgl.FLAG.HIDDEN)
      no_indexes_label:set { text = msg }
    end

    local function hide_no_indexes()
      no_indexes_container:add_flag(lvgl.FLAG.HIDDEN)
      indexes_list:clear_flag(lvgl.FLAG.HIDDEN)

      if indexes[1] then
        indexes[1].object:focus()
      end
    end

    local function hide_playlist_listing()
      playlist_btn:add_flag(lvgl.FLAG.HIDDEN)
    end

    local function show_playlist_listing()
      playlist_btn:clear_flag(lvgl.FLAG.HIDDEN)
    end

    local function update_visible_indexes()
      local has_valid_index = false
      for _, idx in ipairs(indexes) do
        local it = idx.index:iter():next()
        if it then
          has_valid_index = true
          idx.object:clear_flag(lvgl.FLAG.HIDDEN)
        else
          idx.object:add_flag(lvgl.FLAG.HIDDEN)
        end
      end
      if has_valid_index then
        hide_no_indexes()

        -- If we have valid indexes, then also check for playlists
        if filesystem.iterator("/Playlists/"):next() == nil then
          hide_playlist_listing()
        else
          show_playlist_listing()
        end
      else
        if require("database").updating:get() then
          show_no_indexes("The database is updating for the first time. Please wait.")
        else
          show_no_indexes("No compatible media was found on your SD Card.")
        end
      end
    end

    self.bindings = self.bindings + {
      database.updating:bind(function()
        update_visible_indexes()
      end),
      sd_card.mounted:bind(function(mounted)
        if not mounted then
          show_no_indexes("SD Card is not inserted or could not be opened.")
        end
      end),
    }

    -- Finally, the bottom bar with icon buttons for other device features.

    local bottom_bar = lvgl.Object(self.root, {
      flex = {
        flex_direction = "row",
        flex_wrap = "nowrap",
        justify_content = "space-evenly",
        align_items = "center",
        align_content = "flex-start",
      },
      w = lvgl.PCT(100),
      h = lvgl.SIZE_CONTENT,
      pad_top = 4,
      pad_bottom = 2,
    })

    -- local queue_btn = bottom_bar:Button {}
    -- queue_btn:Image { src = img.queue }
    -- theme.set_subject(queue_btn, "icon_enabled")

    local usb_btn = bottom_bar:Button {}
    usb_btn:onClicked(function()
      backstack.push(require("settings").MassStorageSettings:new())
    end)
    usb_btn:Image { src = img.usb }
    widgets.Description(usb_btn, "USB Settings")
    theme.set_subject(usb_btn, "menu_icon")

    self.bindings = self.bindings + {
      require("power").plugged_in:bind(function(attached)
        if (attached) then
          usb_btn:clear_flag(lvgl.FLAG.HIDDEN)
        else
          usb_btn:add_flag(lvgl.FLAG.HIDDEN)
        end
      end)
    }

    local files_btn = bottom_bar:Button {}
    files_btn:onClicked(function()
      backstack.push(require("file_browser"):new {
        title = "Files",
        iterator = filesystem.iterator(""),
      })
    end)
    files_btn:Image { src = img.files }
    widgets.Description(files_btn, "File browser")
    theme.set_subject(files_btn, "menu_icon")

    local settings_btn = bottom_bar:Button {}
    settings_btn:onClicked(function()
      backstack.push(require("settings").Root:new())
    end)
    settings_btn:Image { src = img.settings }
    widgets.Description(settings_btn, "Settings")
    theme.set_subject(settings_btn, "menu_icon")
  end,
}