forked from onatbas/bluetooth.koplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetoothwidget.lua
369 lines (322 loc) · 8.4 KB
/
bluetoothwidget.lua
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
local BD = require("ui/bidi")
local ListView = require("ui/widget/listview")
local bit = require("bit")
local Geom = require("ui/geometry")
local Font = require("ui/font")
local Blitbuffer = require("ffi/blitbuffer")
local Device = require("device")
local InfoMessage = require("ui/widget/infomessage")
local InputContainer = require("ui/widget/container/inputcontainer")
local FrameContainer = require("ui/widget/container/framecontainer")
local CenterContainer = require("ui/widget/container/centercontainer")
local LeftContainer = require("ui/widget/container/leftcontainer")
local RightContainer = require("ui/widget/container/rightcontainer")
local HorizontalGroup = require("ui/widget/horizontalgroup")
local OverlapGroup = require("ui/widget/overlapgroup")
local VerticalGroup = require("ui/widget/verticalgroup")
local HorizontalSpan = require("ui/widget/horizontalspan")
local GestureRange = require("ui/gesturerange")
local TextWidget = require("ui/widget/textwidget")
local ImageWidget = require("ui/widget/imagewidget")
local Size = require("ui/size")
local UIManager = require("ui/uimanager")
local Widget = require("ui/widget/widget")
local _ = require("gettext")
local T = require("ffi/util").template
local Screen = Device.screen
local band = bit.band
local logger = require("logger")
---@class NetworkItem
---@field height number
---@field width number
---@field info table
---@field icon_size any
local NetworkItem = InputContainer:extend({
dimen = nil,
height = Screen:scaleBySize(44),
icon_size = Screen:scaleBySize(32),
width = nil,
info = nil,
background = Blitbuffer.COLOR_WHITE,
})
function NetworkItem:init()
self.dimen = Geom:new({ x = 0, y = 0, w = self.width, h = self.height })
if not self.info.name or self.info.name == "" then
self.info.name = "[hidden]"
end
local bt_icon = "plugins/bluetooth.koplugin/bluetooth.svg"
local status = ""
if self.info.paired then
status = status .. "P"
end
if self.info.trusted then
status = status .. "T"
end
if self.info.connected then
status = status .. "C"
end
local name
if status ~= "" then
name = status .. " | " .. self.info.name
else
name = self.info.name
end
local horizontal_space = HorizontalSpan:new({ width = Size.span.horizontal_default })
self.content_container = OverlapGroup:new({
dimen = self.dimen:copy(),
LeftContainer:new({
dimen = self.dimen:copy(),
HorizontalGroup:new({
horizontal_space,
ImageWidget:new({
file = bt_icon,
width = self.icon_size,
height = self.icon_size,
alpha = true,
is_icon = true,
}),
horizontal_space,
TextWidget:new({
text = name,
face = Font:getFace("cfont"),
}),
}),
}),
})
self.btn_disconnect = nil
self.btn_edit_nw = nil
if self.info.connected then
self.btn_disconnect = FrameContainer:new({
bordersize = 0,
padding = 0,
TextWidget:new({
text = _("disconnect"),
face = Font:getFace("cfont"),
}),
})
table.insert(
self.content_container,
RightContainer:new({
dimen = self.dimen:copy(),
HorizontalGroup:new({
self.btn_disconnect,
horizontal_space,
}),
})
)
self.setting_ui:setConnectedItem(self)
end
self[1] = FrameContainer:new({
padding = 0,
margin = 0,
background = self.background,
bordersize = 0,
width = self.width,
self.content_container,
})
if Device:isTouchDevice() then
self.ges_events.TapSelect = {
GestureRange:new({
ges = "tap",
range = self.dimen,
}),
}
self.ges_events.Hold = {
GestureRange:new({
ges = "hold",
range = self.dimen,
}),
}
end
end
function NetworkItem:refresh()
self:init()
UIManager:setDirty(self.setting_ui, function()
return "ui", self.dimen
end)
end
function NetworkItem:connect()
local connected_item = self.setting_ui:getConnectedItem()
if connected_item then
connected_item:disconnect()
end
self.setting_ui.connect_callback(self.info)
local text = "Connected to " .. self.info.name
self:refresh()
UIManager:show(InfoMessage:new({ text = text, timeout = 3 }))
end
function NetworkItem:disconnect()
local info = InfoMessage:new({ text = _("Disconnecting…") })
UIManager:show(info)
UIManager:forceRePaint()
self.setting_ui.disconnect_callback(self.info)
UIManager:close(info)
self:refresh()
end
function NetworkItem:forget()
local info = InfoMessage:new({ text = _("Forgetting device") })
UIManager:show(info)
UIManager:forceRePaint()
self.setting_ui.forget_callback(self.info)
UIManager:close(info)
self:refresh()
end
function NetworkItem:onTapSelect(arg, ges_ev)
if self.btn_disconnect then
-- noop if touch is not on disconnect button
if ges_ev.pos:intersectWith(self.btn_disconnect.dimen) then
self:disconnect()
end
else
self:connect()
end
return true
end
function NetworkItem:onHold(arg, ges_ev)
self:forget()
return true
end
---@class MinimalPaginator
local MinimalPaginator = Widget:extend({
width = nil,
height = nil,
progress = nil,
})
function MinimalPaginator:getSize()
return Geom:new({ w = self.width, h = self.height })
end
function MinimalPaginator:paintTo(bb, x, y)
self.dimen = self:getSize()
self.dimen.x, self.dimen.y = x, y
-- paint background
bb:paintRoundedRect(x, y, self.dimen.w, self.dimen.h, Blitbuffer.COLOR_LIGHT_GRAY)
-- paint percentage infill
bb:paintRect(x, y, math.ceil(self.dimen.w * self.progress), self.dimen.h, Blitbuffer.COLOR_DARK_GRAY)
end
function MinimalPaginator:setProgress(progress)
self.progress = progress
end
---@class NetworkSetting
---@field width number
---@field height number
---@field device_list any[]
local NetworkSetting = InputContainer:extend({
width = nil,
height = nil,
device_list = nil,
connect_callback = nil,
disconnect_callback = nil,
forget_callback = nil,
})
function NetworkSetting:init()
self.width = self.width or Screen:getWidth() - Screen:scaleBySize(50)
self.width = math.min(self.width, Screen:scaleBySize(600))
local gray_bg = Blitbuffer.COLOR_GRAY_E
local items = {}
for idx, device in ipairs(self.device_list) do
local bg
if band(idx, 1) == 0 then
bg = gray_bg
else
bg = Blitbuffer.COLOR_WHITE
end
table.insert(
items,
NetworkItem:new({
width = self.width,
info = device,
background = bg,
setting_ui = self,
})
)
end
self.status_text = TextWidget:new({
text = "",
face = Font:getFace("ffont"),
})
self.page_text = TextWidget:new({
text = "",
face = Font:getFace("ffont"),
})
self.pagination = MinimalPaginator:new({
width = self.width,
height = Screen:scaleBySize(8),
percentage = 0,
progress = 0,
})
self.height = self.height or math.min(Screen:getHeight() * 3 / 4, Screen:scaleBySize(800))
self.popup = FrameContainer:new({
background = Blitbuffer.COLOR_WHITE,
padding = 0,
bordersize = Size.border.window,
VerticalGroup:new({
align = "left",
self.pagination,
ListView:new({
padding = 0,
items = items,
width = self.width,
height = self.height - self.pagination:getSize().h,
page_update_cb = function(curr_page, total_pages)
self.pagination:setProgress(curr_page / total_pages)
-- self.page_text:setText(curr_page .. "/" .. total_pages)
UIManager:setDirty(self, function()
return "ui", self.popup.dimen
end)
end,
}),
}),
})
self[1] = CenterContainer:new({
dimen = { w = Screen:getWidth(), h = Screen:getHeight() },
self.popup,
})
if Device:isTouchDevice() then
self.ges_events.TapClose = {
GestureRange:new({
ges = "tap",
range = Geom:new({
x = 0,
y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}),
}),
}
end
if not self.connect_callback then
return
end
UIManager:nextTick(function()
local connected_item = self:getConnectedItem()
if connected_item ~= nil then
UIManager:show(InfoMessage:new({
text = T(_("Connected to network %1"), BD.wrap(connected_item.info.name)),
timeout = 3,
}))
self.connect_callback()
end
end)
end
function NetworkSetting:setConnectedItem(item)
self.connected_item = item
end
function NetworkSetting:getConnectedItem()
return self.connected_item
end
function NetworkSetting:onTapClose(arg, ges_ev)
if ges_ev.pos:notIntersectWith(self.popup.dimen) then
UIManager:close(self)
return true
end
end
function NetworkSetting:onCloseWidget()
UIManager:setDirty(nil, "ui", self.popup.dimen)
end
function NetworkSetting:onRefreshList()
self.device_list = self.refresh_list_callback()
UIManager:setDirty(self, function()
return "ui", self.popup.dimen
end)
end
return NetworkSetting