Update Widget Switcher script
This commit is contained in:
@@ -1,40 +1,109 @@
|
|||||||
-- name = "Widgets switcher"
|
-- name = "Переключатель виджетов"
|
||||||
-- description = "Turns screen widgets on and off when buttons are pressed"
|
-- description = "Turns screen widgets on and off when buttons are pressed"
|
||||||
-- type = "widget"
|
-- type = "widget"
|
||||||
-- arguments_help = "Don't change the arguments directly, use the long click menu."
|
|
||||||
-- author = "Andrey Gavrilov"
|
-- author = "Andrey Gavrilov"
|
||||||
-- version = "2.0"
|
-- version = "2.1"
|
||||||
|
|
||||||
--constants--
|
prefs = require "prefs"
|
||||||
|
prefs._name = "widgets"
|
||||||
local widgets = {"weather","weatheronly","clock","alarm","worldclock","monitor","traffic","player","apps","appbox","applist","appfolders","contacts","notify","dialogs","dialer","timer","stopwatch","mail","notes","tasks", "health", "feed","telegram","calendar","calendarw","exchange","finance","bitcoin","control","recorder","calculator","empty"}
|
|
||||||
|
|
||||||
local icons = {"fa:user-clock","fa:sun-cloud","fa:clock","fa:alarm-clock","fa:business-time","fa:network-wired","fa:exchange","fa:play-circle","fa:robot","fa:th","fa:list","fa:folder","fa:address-card","fa:bell","fa:comment-alt-minus","fa:phone-alt","fa:chess-clock","fa:stopwatch","fa:at","fa:sticky-note","fa:calendar-check", "fa:heart-pulse", "fa:rss-square","fa:paper-plane","fa:calendar-alt","fa:calendar-week","fa:euro-sign","fa:chart-line","fa:coins","fa:wifi","fa:microphone-alt","fa:calculator-alt","fa:eraser"}
|
|
||||||
|
|
||||||
local names = {"Clock & weather","Weather","Clock","Alarm","World clock","Monitor","Traffic","Player","Frequent apps","My apps","App list","App folders","Contacts","Notify","Dialogs","Dialer","Timer","Stopwatch","Mail","Notes","Tasks", "Health", "Feed","Telegram","Calendar","Weekly calendar","Exchange","Finance","Bitcoin","Control panel","Recorder","Calculator","Empty widget"}
|
|
||||||
|
|
||||||
--variables--
|
|
||||||
|
|
||||||
local pos = 0
|
local pos = 0
|
||||||
|
local color = aio:colors()
|
||||||
|
local enabled_color = "#1976d2"
|
||||||
|
local disabled_color = aio:colors().button
|
||||||
|
local buttons,colors = {},{}
|
||||||
|
|
||||||
function on_resume()
|
function on_alarm()
|
||||||
if next(settings:get()) == nil then
|
widgets = get_widgets()
|
||||||
set_default_args()
|
if not prefs.widgets then
|
||||||
|
prefs.widgets = widgets.name
|
||||||
end
|
end
|
||||||
ui:set_folding_flag(true)
|
indexes = get_indexes(prefs.widgets, widgets.name)
|
||||||
local buttons,colors = get_buttons()
|
ui:show_buttons(get_buttons())
|
||||||
ui:show_buttons(buttons, colors)
|
|
||||||
end
|
|
||||||
|
|
||||||
function on_click(idx)
|
|
||||||
pos = idx
|
|
||||||
redraw()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_long_click(idx)
|
function on_long_click(idx)
|
||||||
|
system:vibrate(10)
|
||||||
pos = idx
|
pos = idx
|
||||||
local tab = settings:get()
|
if idx > #prefs.widgets then
|
||||||
ui:show_context_menu({{"angle-left",""},{"ban",""},{"angle-right",""},{icons[get_checkbox_idx()[idx]]:gsub("fa:",""),names[get_checkbox_idx()[idx]]}})
|
return
|
||||||
|
end
|
||||||
|
ui:show_context_menu({{"angle-left",""},{"ban",""},{"angle-right",""},{widgets.icon[indexes[idx]],widgets.label[indexes[idx]]}})
|
||||||
|
end
|
||||||
|
|
||||||
|
function on_click(idx)
|
||||||
|
system:vibrate(10)
|
||||||
|
if idx > #prefs.widgets then
|
||||||
|
on_settings()
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local widget = prefs.widgets[idx]
|
||||||
|
if not aio:is_widget_added(widget) then
|
||||||
|
aio:add_widget(widget, get_pos())
|
||||||
|
aio:fold_widget(widget, false)
|
||||||
|
else
|
||||||
|
aio:remove_widget(widget)
|
||||||
|
end
|
||||||
|
on_alarm()
|
||||||
|
end
|
||||||
|
|
||||||
|
function on_dialog_action(data)
|
||||||
|
if data == -1 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local tab = {}
|
||||||
|
for i,v in ipairs(data) do
|
||||||
|
tab[i] = widgets.name[v]
|
||||||
|
end
|
||||||
|
prefs.widgets = tab
|
||||||
|
on_alarm()
|
||||||
|
end
|
||||||
|
|
||||||
|
function on_settings()
|
||||||
|
ui:show_checkbox_dialog("Select widgets", widgets.label, indexes)
|
||||||
|
end
|
||||||
|
|
||||||
|
function get_indexes(tab1,tab2)
|
||||||
|
local tab = {}
|
||||||
|
for i1,v1 in ipairs(tab1) do
|
||||||
|
for i2,v2 in ipairs(tab2) do
|
||||||
|
if v1 == v2 then
|
||||||
|
tab[i1] = i2
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return tab
|
||||||
|
end
|
||||||
|
|
||||||
|
function get_buttons()
|
||||||
|
buttons,colors = {},{}
|
||||||
|
for i,v in ipairs(indexes) do
|
||||||
|
table.insert(buttons, "fa:" .. widgets.icon[v])
|
||||||
|
table.insert(colors, widgets.enabled[v] and enabled_color or disabled_color)
|
||||||
|
end
|
||||||
|
--table.insert(buttons, "fa:gear")
|
||||||
|
--table.insert(colors, color.disabled_icon)
|
||||||
|
return buttons,colors
|
||||||
|
end
|
||||||
|
|
||||||
|
function move(x)
|
||||||
|
local tab = prefs.widgets
|
||||||
|
if (pos*x == -1) or (pos*x == #tab) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local cur = tab[pos]
|
||||||
|
tab[pos] = tab[pos+x]
|
||||||
|
tab[pos+x] = cur
|
||||||
|
prefs.widgets = tab
|
||||||
|
on_alarm()
|
||||||
|
end
|
||||||
|
|
||||||
|
function remove()
|
||||||
|
local tab = prefs.widgets
|
||||||
|
table.remove(tab,pos)
|
||||||
|
prefs.widgets = tab
|
||||||
|
on_alarm()
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_context_menu_click(menu_idx)
|
function on_context_menu_click(menu_idx)
|
||||||
@@ -44,90 +113,42 @@ function on_context_menu_click(menu_idx)
|
|||||||
remove()
|
remove()
|
||||||
elseif menu_idx == 3 then
|
elseif menu_idx == 3 then
|
||||||
move(1)
|
move(1)
|
||||||
elseif menu_idx == 4 then
|
|
||||||
redraw()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_dialog_action(data)
|
function on_widget_action(action, name)
|
||||||
if data == -1 then
|
on_alarm()
|
||||||
return
|
|
||||||
end
|
|
||||||
settings:set(data)
|
|
||||||
on_resume()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_settings()
|
function get_pos()
|
||||||
ui:show_checkbox_dialog("Select widgets", names, get_checkbox_idx())
|
local name = aio:self_name()
|
||||||
|
local tab = aio:active_widgets()
|
||||||
|
for _,v in ipairs(tab) do
|
||||||
|
if v.name == name then
|
||||||
|
return v.position+1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return 4
|
||||||
end
|
end
|
||||||
|
|
||||||
--utilities--
|
function get_widgets()
|
||||||
|
local tab = {}
|
||||||
function redraw()
|
tab.icon = {}
|
||||||
local buttons,colors = get_buttons()
|
tab.name = {}
|
||||||
local checkbox_idx = get_checkbox_idx()
|
tab.label = {}
|
||||||
local widget = widgets[checkbox_idx[pos]]
|
tab.enabled = {}
|
||||||
if aio:is_widget_added(widget) then
|
for i,v in ipairs(aio:available_widgets()) do
|
||||||
aio:remove_widget(widget)
|
if v.type == "builtin" then
|
||||||
colors[pos] = "#909090"
|
table.insert(tab.icon, v.icon)
|
||||||
else
|
table.insert(tab.name, v.name)
|
||||||
aio:add_widget(widget)
|
table.insert(tab.label, v.label)
|
||||||
colors[pos] = "#1976d2"
|
table.insert(tab.enabled, v.enabled)
|
||||||
end
|
end
|
||||||
ui:show_buttons(buttons, colors)
|
|
||||||
end
|
|
||||||
|
|
||||||
function set_default_args()
|
|
||||||
local args = {}
|
|
||||||
for i = 1, #widgets do
|
|
||||||
table.insert(args, i)
|
|
||||||
end
|
|
||||||
settings:set(args)
|
|
||||||
end
|
|
||||||
|
|
||||||
function get_checkbox_idx()
|
|
||||||
local tab = settings:get()
|
|
||||||
for i = 1, #tab do
|
|
||||||
tab[i] = tonumber(tab[i])
|
|
||||||
end
|
end
|
||||||
|
local cal_widget = "my-calendar.lua"
|
||||||
|
table.insert(tab.icon, "calendar-days")
|
||||||
|
table.insert(tab.name, cal_widget)
|
||||||
|
table.insert(tab.label, "Месячный календарь")
|
||||||
|
table.insert(tab.enabled, aio:is_widget_added(cal_widget))
|
||||||
return tab
|
return tab
|
||||||
end
|
end
|
||||||
|
|
||||||
function get_buttons()
|
|
||||||
local buttons,colors = {},{}
|
|
||||||
local checkbox_idx = get_checkbox_idx()
|
|
||||||
for i = 1, #checkbox_idx do
|
|
||||||
table.insert(buttons, icons[checkbox_idx[i]])
|
|
||||||
local widget = widgets[checkbox_idx[i]]
|
|
||||||
if widget ~= nil then
|
|
||||||
if aio:is_widget_added(widget) then
|
|
||||||
table.insert(colors, "#1976d2")
|
|
||||||
else
|
|
||||||
table.insert(colors, "#909090")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return buttons,colors
|
|
||||||
end
|
|
||||||
|
|
||||||
function move(x)
|
|
||||||
local tab = settings:get()
|
|
||||||
if (pos == 1 and x < 0) or (pos == #tab and x > 0) then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local cur = tab[pos]
|
|
||||||
local prev = tab[pos+x]
|
|
||||||
tab[pos+x] = cur
|
|
||||||
tab[pos] = prev
|
|
||||||
settings:set(tab)
|
|
||||||
local buttons,colors = get_buttons()
|
|
||||||
ui:show_buttons(buttons, colors)
|
|
||||||
end
|
|
||||||
|
|
||||||
function remove()
|
|
||||||
local tab = settings:get()
|
|
||||||
table.remove(tab,pos)
|
|
||||||
settings:set(tab)
|
|
||||||
local buttons,colors = get_buttons()
|
|
||||||
ui:show_buttons(buttons, colors)
|
|
||||||
end
|
|
||||||
|
|||||||
Reference in New Issue
Block a user