summaryrefslogtreecommitdiff
path: root/lua/browser.lua
blob: 5c00e8d18b5e779cddf581b318f5b86767af76ec (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
local lvgl = require("lvgl")
local widgets = require("widgets")
local backstack = require("backstack")
local font = require("font")
local queue = require("queue")
local playing = require("playing")
local styles = require("styles")
local playback = require("playback")
local theme = require("theme")
local screen = require("screen")

return screen:new{
    createUi = function(self)
        self.root = lvgl.Object(nil, {
            flex = {
                flex_direction = "column",
                flex_wrap = "wrap",
                justify_content = "flex-start",
                align_items = "flex-start",
                align_content = "flex-start"
            },
            w = lvgl.HOR_RES(),
            h = lvgl.VER_RES()
        })
        self.root:center()

        self.status_bar = widgets.StatusBar(self.root, {
            back_cb = backstack.pop,
            title = self.title
        })

        local header = self.root:Object{
            flex = {
                flex_direction = "column",
                flex_wrap = "wrap",
                justify_content = "flex-start",
                align_items = "flex-start",
                align_content = "flex-start"
            },
            w = lvgl.HOR_RES(),
            h = lvgl.SIZE_CONTENT,
            pad_left = 4,
            pad_right = 4,
            pad_bottom = 2,
            bg_opa = lvgl.OPA(100),
            scrollbar_mode = lvgl.SCROLLBAR_MODE.OFF
        }
        theme.set_style(header, "header")

        if self.breadcrumb then
            header:Label{
                text = self.breadcrumb,
                text_font = font.fusion_10
            }
        end

        local buttons = header:Object({
            flex = {
                flex_direction = "row",
                flex_wrap = "wrap",
                justify_content = "flex-end",
                align_items = "center",
                align_content = "center"
            },
            w = lvgl.PCT(100),
            h = lvgl.SIZE_CONTENT,
            pad_column = 4
        })
        local original_iterator = self.iterator:clone()
        local enqueue = widgets.IconBtn(buttons, "//lua/img/enqueue.png", "Enqueue")
        enqueue:onClicked(function()
            queue.add(original_iterator)
            playback.playing:set(true)
        end)
        -- enqueue:add_flag(lvgl.FLAG.HIDDEN)
        local play = widgets.IconBtn(buttons, "//lua/img/play_small.png", "Play")
        play:onClicked(function()
            queue.clear()
            queue.add(original_iterator)
            playback.playing:set(true)
            backstack.push(playing)
        end)

        self.list = lvgl.List(self.root, {
            w = lvgl.PCT(100),
            h = lvgl.PCT(100),
            flex_grow = 1,
            scrollbar_mode = lvgl.SCROLLBAR_MODE.OFF
        })

        -- local back = self.list:add_btn(nil, "< Back")
        -- back:onClicked(backstack.pop)
        -- back:add_style(styles.list_item)

        self.focused_item = 0
        self.last_item = 0
        self.add_item = function(item)
            if not item then
                return
            end
            self.last_item = self.last_item + 1
            local this_item = self.last_item
            local btn = self.list:add_btn(nil, tostring(item))
            btn:onClicked(function()
                local contents = item:contents()
                if type(contents) == "userdata" then
                    backstack.push(require("browser"):new{
                        title = self.title,
                        iterator = contents,
                        breadcrumb = tostring(item)
                    })
                else
                    queue.clear()
                    queue.add(contents)
                    playback.playing:set(true)
                    backstack.push(playing:new())
                end
            end)
            btn:onevent(lvgl.EVENT.FOCUSED, function()
                self.focused_item = this_item
                if self.last_item - 5 < this_item then
                    self.add_item(self.iterator())
                end
            end)
            btn:add_style(styles.list_item)
            if this_item == 1 then 
              btn:focus()
            end
        end

        for _ = 1, 8 do
            local val = self.iterator()
            if not val then
                break
            end
            self.add_item(val)
        end
    end
}