2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/tools/wbuilder.lua

465 lines
15 KiB
Lua
Raw Permalink Normal View History

2014-10-03 08:11:53 +00:00
-- widget test utility
-- usage: ./luajit tools/wtest.lua
2013-07-30 08:21:19 +00:00
require "defaults"
print(package.path)
package.path = "common/?.lua;rocks/share/lua/5.1/?.lua;frontend/?.lua;" .. package.path
package.cpath = "common/?.so;common/?.dll;/usr/lib/lua/?.so;rocks/lib/lua/5.1/?.so;" .. package.cpath
local DataStorage = require("datastorage")
local _ = require("gettext")
-- read settings and check for language override
-- has to be done before requiring other files because
-- they might call gettext on load
G_reader_settings = require("luasettings"):open(
DataStorage:getDataDir().."/settings.reader.lua")
local lang_locale = G_reader_settings:readSetting("language")
if lang_locale then
_.changeLang(lang_locale)
end
2017-08-14 13:59:17 +00:00
local Device = require("device")
local CanvasContext = require("document/canvascontext")
CanvasContext:init(Device)
2013-12-20 16:07:12 +00:00
local UIManager = require("ui/uimanager")
local RenderText = require("ui/rendertext")
local Font = require("ui/font")
local Geom = require("ui/geometry")
local Menu = require("ui/widget/menu")
local Widget = require("ui/widget/widget")
local TextWidget = require("ui/widget/textwidget")
2013-12-20 16:07:12 +00:00
local InfoMessage = require("ui/widget/infomessage")
local InputContainer = require("ui/widget/container/inputcontainer")
local CenterContainer = require("ui/widget/container/centercontainer")
local FrameContainer = require("ui/widget/container/framecontainer")
local AlphaContainer = require("ui/widget/container/alphacontainer")
2013-12-20 16:07:12 +00:00
local ConfirmBox = require("ui/widget/confirmbox")
local TouchMenu = require("ui/widget/touchmenu")
local DocumentRegistry = require("document/documentregistry")
local ReaderUI = require("apps/reader/readerui")
2016-01-31 22:57:54 +00:00
local DEBUG = require("dbg")
local Screen = require("device").screen
local Blitbuffer = require("ffi/blitbuffer")
2016-01-31 22:57:54 +00:00
local InputText = require("ui/widget/inputtext")
DEBUG:turnOn()
2012-04-22 19:29:48 +00:00
2013-03-12 05:08:30 +00:00
-----------------------------------------------------
-- widget that paints the grid on the background
-----------------------------------------------------
TestGrid = Widget:new{}
TestVisible = Widget:new{}
function TestGrid:paintTo(bb)
2014-03-13 13:52:43 +00:00
v_line = math.floor(bb:getWidth() / 50)
h_line = math.floor(bb:getHeight() / 50)
for i=1,h_line do
y_num = i*50
RenderText:renderUtf8Text(bb, 0, y_num+10, Font:getFace("ffont", 12), y_num, true)
bb:paintRect(0, y_num, bb:getWidth(), 1, Blitbuffer.gray(0.7))
2014-03-13 13:52:43 +00:00
end
for i=1,v_line do
x_num = i*50
RenderText:renderUtf8Text(bb, x_num, 10, Font:getFace("ffont", 12), x_num, true)
bb:paintRect(x_num, 0, 1, bb:getHeight(), Blitbuffer.gray(0.7))
2014-03-13 13:52:43 +00:00
end
end
function TestVisible:paintTo(bb)
-- Draw three lines at the borders to assess what the maximum visible coordinates are
v_line = math.floor(bb:getWidth() / 50)
h_line = math.floor(bb:getHeight() / 50)
-- Paint white background for higher contrast
bb:paintRect(0,0,bb:getWidth(),bb:getHeight(), Blitbuffer.COLOR_WHITE)
-- Only render gridtext not lines at a more central postition, so it doesn't interfere with the
for i=1,h_line do
y_num = i*50
RenderText:renderUtf8Text(bb, 40, y_num+10, Font:getFace("ffont", 12), y_num, true)
end
for i=1,v_line do
x_num = i*50
RenderText:renderUtf8Text(bb, x_num, 40, Font:getFace("ffont", 12), x_num, true)
end
2014-08-14 10:09:30 +00:00
-- Handtunable minimal and maximal visible coordinates
local x_min = 0 + 4
local x_max = bb:getWidth() - 4
local y_min = 0 + 3
local y_max = bb:getHeight() - 3 - 12
2014-08-14 10:09:30 +00:00
-- Render extremes on screen
RenderText:renderUtf8Text(bb, 150, 100, Font:getFace("ffont", 22), "x_min = "..x_min, true)
RenderText:renderUtf8Text(bb, 500, 100, Font:getFace("ffont", 22), "x_max = "..x_max, true)
RenderText:renderUtf8Text(bb, 100, 150, Font:getFace("ffont", 22), "y_min = "..y_min, true)
RenderText:renderUtf8Text(bb, 100, 300, Font:getFace("ffont", 22), "y_max = "..y_max, true)
RenderText:renderUtf8Text(bb, 100, 500, Font:getFace("ffont", 26), "Visible screen size : "..(x_max-x_min).."x"..(y_max-y_min), true)
2014-08-14 10:09:30 +00:00
-- Three parallel lines at the top
bb:paintRect(x_min,y_min, x_max, 1 , Blitbuffer.gray(0.7))
bb:paintRect(x_min,y_min + 3, x_max, 1 , Blitbuffer.gray(0.7))
bb:paintRect(x_min,y_min + 6, x_max, 1 , Blitbuffer.gray(0.7))
2014-08-14 10:09:30 +00:00
-- Three parallel lines at the bottom
bb:paintRect(x_min,y_max, x_max, 1 , Blitbuffer.gray(0.7))
bb:paintRect(x_min,y_max - 3, x_max, 1 , Blitbuffer.gray(0.7))
bb:paintRect(x_min,y_max - 6, x_max, 1 , Blitbuffer.gray(0.7))
2014-08-14 10:09:30 +00:00
-- Three parallel lines at the left
bb:paintRect(x_min,y_min, 1, y_max , Blitbuffer.gray(0.7))
bb:paintRect(x_min + 3,y_min, 1, y_max, Blitbuffer.gray(0.7))
bb:paintRect(x_min + 6,y_min, 1, y_max, Blitbuffer.gray(0.7))
2014-08-14 10:09:30 +00:00
-- Three parallel lines at the right
bb:paintRect(x_max,y_min, 1, y_max , Blitbuffer.gray(0.7))
bb:paintRect(x_max - 3,y_min, 1, y_max, Blitbuffer.gray(0.7))
bb:paintRect(x_max - 6,y_min, 1, y_max, Blitbuffer.gray(0.7))
2014-08-14 10:09:30 +00:00
--Two lines spaces 600 pixels
bb:paintRect(100,600, 1, 250 , Blitbuffer.gray(0.7))
bb:paintRect(700,600, 1, 250 , Blitbuffer.gray(0.7))
RenderText:renderUtf8Text(bb, 150, 670, Font:getFace("ffont", 26), "Measure inches per 600 pixels", true)
RenderText:renderUtf8Text(bb, 150, 770, Font:getFace("ffont", 22), "Kobo Aura: 600 pixels/ 2.82 \" = "..(600/2.82).." dpi", true)
end
2013-03-12 05:08:30 +00:00
-----------------------------------------------------
2012-04-22 19:29:48 +00:00
-- we create a widget that paints a background:
2013-03-12 05:08:30 +00:00
-----------------------------------------------------
2012-04-22 19:29:48 +00:00
Background = InputContainer:new{
2014-03-13 13:52:43 +00:00
is_always_active = true, -- receive events when other dialogs are active
key_events = {
OpenDialog = { { "Press" } },
OpenConfirmBox = { { "Del" } },
QuitApplication = { { {"Home","Back"} } }
},
-- contains a gray rectangular desktop
FrameContainer:new{
background = Blitbuffer.COLOR_DARK_GRAY,
2014-03-13 13:52:43 +00:00
bordersize = 0,
dimen = Screen:getSize(),
Widget:new{
dimen = {
w = Screen:getWidth(),
h = Screen:getHeight(),
}
},
}
2012-04-22 19:29:48 +00:00
}
function Background:onOpenDialog()
2014-03-13 13:52:43 +00:00
UIManager:show(InfoMessage:new{
text = "Example message.",
timeout = 10
})
2012-04-22 19:29:48 +00:00
end
function Background:onOpenConfirmBox()
2014-03-13 13:52:43 +00:00
UIManager:show(ConfirmBox:new{
text = "Please confirm delete"
})
2012-04-22 19:29:48 +00:00
end
function Background:onInputError()
2014-03-13 13:52:43 +00:00
UIManager:quit()
2012-04-22 19:29:48 +00:00
end
function Background:onQuitApplication()
2014-03-13 13:52:43 +00:00
UIManager:quit()
2012-04-22 19:29:48 +00:00
end
2013-03-12 05:08:30 +00:00
-----------------------------------------------------
2012-04-22 19:29:48 +00:00
-- example widget: a clock
2013-03-12 05:08:30 +00:00
-----------------------------------------------------
Clock = AlphaContainer:new{
alpha = 0.7,
FrameContainer:new{
background = Blitbuffer.COLOR_WHITE,
bordersize = Screen:scaleBySize(1),
margin = 0,
padding = Screen:scaleBySize(1),
}
2012-04-22 19:29:48 +00:00
}
function Clock:schedFunc()
self[1][1]:free()
self[1][1] = self:getTextWidget()
2014-03-13 13:52:43 +00:00
UIManager:setDirty(self)
-- TODO: wait until next real second shift
UIManager:scheduleIn(1, function() self:schedFunc() end)
2012-04-22 19:29:48 +00:00
end
function Clock:onShow()
self[1][1] = self:getTextWidget()
2014-03-13 13:52:43 +00:00
self:schedFunc()
2012-04-22 19:29:48 +00:00
end
function Clock:getTextWidget()
2014-03-13 13:52:43 +00:00
return CenterContainer:new{
dimen = { w = 300, h = 25 },
TextWidget:new{
text = os.date("%H:%M:%S"),
face = Font:getFace("cfont", 12)
}
}
2012-04-22 19:29:48 +00:00
end
2013-03-12 05:08:30 +00:00
-----------------------------------------------------
-- a confirmbox box widget
-----------------------------------------------------
Quiz = ConfirmBox:new{
2014-03-13 13:52:43 +00:00
text = "Tell me the truth, isn't it COOL?!",
width = Screen:scaleBySize(300),
2014-03-13 13:52:43 +00:00
ok_text = "Yes, of course.",
cancel_text = "No, it's ugly.",
cancel_callback = function()
UIManager:show(InfoMessage:new{
text="You liar!",
})
end,
}
2013-03-12 05:08:30 +00:00
-----------------------------------------------------
-- a menu widget
-----------------------------------------------------
menu_items = {
2014-03-13 13:52:43 +00:00
{text = "item1"},
{text = "item2"},
{text = "This is a very very log item whose length should exceed the width of the menu."},
{text = "item3"},
{text = "item4"},
{text = "item5"},
{text = "item6"},
{text = "item7"},
{text = "item8"},
{text = "item9"},
{text = "item10"},
{text = "item11"},
{text = "item12"},
{text = "item13"},
{text = "item14"},
{text = "item15"},
{text = "item16"},
{text = "item17"},
}
M = Menu:new{
2014-03-13 13:52:43 +00:00
title = "Test Menu",
item_table = menu_items,
width = Screen:scaleBySize(500),
height = Screen:scaleBySize(600),
}
2012-04-22 19:29:48 +00:00
2013-03-12 05:08:30 +00:00
-----------------------------------------------------
-- a reader view widget
-----------------------------------------------------
2012-05-18 22:50:57 +00:00
readerwindow = CenterContainer:new{
2014-03-13 13:52:43 +00:00
dimen = Screen:getSize(),
FrameContainer:new{
background = Blitbuffer.COLOR_BLACK,
2014-03-13 13:52:43 +00:00
}
2012-05-18 22:50:57 +00:00
}
reader = ReaderUI:new{
2014-03-13 13:52:43 +00:00
dialog = readerwindow,
dimen = Geom:new{ w = Screen:getWidth() - 100, h = Screen:getHeight() - 100 },
2016-01-31 22:57:54 +00:00
document = DocumentRegistry:openDocument("spec/front/unit/data/2col.pdf")
--document = DocumentRegistry:openDocument("spec/front/unit/data/djvu3spec.djvu")
2012-05-18 22:50:57 +00:00
}
readerwindow[1][1] = reader
2013-03-12 05:08:30 +00:00
touch_menu = TouchMenu:new{
2014-03-13 13:52:43 +00:00
title = "Document menu",
width = Screen:getWidth(),
tab_item_table = {
{
icon = "appbar.pokeball",
2014-03-13 13:52:43 +00:00
{
text = "item1",
callback = function() end,
2014-03-13 13:52:43 +00:00
},
{
text = "item2",
callback = function() end,
2014-03-13 13:52:43 +00:00
},
{
text = "item3",
callback = function() end,
2014-03-13 13:52:43 +00:00
},
{
text = "item4",
callback = function() end,
2014-03-13 13:52:43 +00:00
},
{
text = "item5",
callback = function() end,
2014-03-13 13:52:43 +00:00
},
{
text = "item6",
callback = function() end,
2014-03-13 13:52:43 +00:00
},
{
text = "item7",
callback = function() end,
2014-03-13 13:52:43 +00:00
},
{
text = "item8",
callback = function() end,
2014-03-13 13:52:43 +00:00
},
{
text = "item9",
callback = function() end,
2014-03-13 13:52:43 +00:00
},
},
{
icon = "appbar.page.corner.bookmark",
2014-03-13 13:52:43 +00:00
{
text = "item10",
callback = function() end,
2014-03-13 13:52:43 +00:00
},
{
text = "item11",
callback = function() end,
2014-03-13 13:52:43 +00:00
},
},
{
icon = "home",
callback = function() DEBUG("hello world!") end
2014-03-13 13:52:43 +00:00
}
},
2013-03-13 02:50:16 +00:00
}
-----------------------------------------------------
-- input box widget
-----------------------------------------------------
2016-01-31 22:57:54 +00:00
local TestInputText = InputText:new{
width = Screen:scaleBySize(400),
2016-01-31 22:57:54 +00:00
enter_callback = function() print("Entered") end,
scroll = false,
input_type = "number",
parent = {
onSwitchFocus = false,
},
2013-07-30 08:21:19 +00:00
}
2013-03-13 02:50:16 +00:00
-----------------------------------------------------
-- key value page
-----------------------------------------------------
function testKeyValuePage()
local KeyValuePage = require("ui/widget/keyvaluepage")
local kvp = KeyValuePage:new{
title = 'Statistics This is a very very log item whose length should exceed the width of the men',
kv_pairs = {
{"1 Current period", "00:00:00"},
{"This is a very very log item whose length should exceed the width of the menu.", "value"},
{"2 Time to read", "00:00:00 00:00:00 00:00:00 00:00:00"},
{"2 Time to read", "00:00:00"},
{"3 Time to read", "00:00:00"},
{"4 Time to read", "00:00:00"},
{"5 Time to read", "00:00:00"},
{"6 Time to read", "00:00:00"},
{"7 Time to read", "00:00:00"},
{"8 Time to read", "00:00:00"},
{"9 Time to read", "00:00:00"},
{"10 Time to read", "00:00:00"},
{"11 Time to read", "00:00:00"},
"----------------------------",
{"12 Time to read", "00:00:00"},
{"13 Time to read", "00:00:00"},
{"14 Time to read", "00:00:00"},
{"15 Time to read", "00:00:00"},
{"16 Time to read", "00:00:00"},
{"17 Time to read", "00:00:00"},
{"18 Time to read", "00:00:00"},
{"19 Time to read", "00:00:00"},
{"20 Time to read", "00:00:00"},
{"21 Time to read", "00:00:00"},
},
}
UIManager:show(kvp)
end
function testBookStatus()
-- doc = DocumentRegistry:openDocument("spec/front/unit/data/juliet.epub")
doc = DocumentRegistry:openDocument("spec/front/unit/data/2col.pdf")
reader = ReaderUI:new{
dialog = readerwindow,
dimen = Geom:new{ w = Screen:getWidth() - 100, h = Screen:getHeight() - 100 },
document = doc
}
local status_page = require("ui/widget/bookstatuswidget"):new {
thumbnail = doc:getCoverPageImage(),
props = doc:getProps(),
document = doc,
ui = reader,
}
UIManager:show(status_page)
end
2016-04-03 04:52:30 +00:00
function testTouchProbe()
local TouchProbe = require("tools/kobo_touch_probe")
2016-04-03 04:52:30 +00:00
UIManager:show(TouchProbe:new{})
end
function testNetworkSetting()
local list = {
{
ssid = "CMU-SECURE",
signal_level = -58,
flags = "[WPA2-PSK-CCMP][ESS]",
signal_quality = 84,
},
{
ssid = "CMU-SECURE 2",
signal_level = -258,
signal_quality = 44,
flags = "[WPA2-PSK-CCMP][ESS]",
password = "okgo",
},
{
ssid = "218",
signal_level = 58,
signal_quality = 100,
flags = "[WEP][ESS]",
},
{
ssid = "318",
signal_level = 100,
signal_quality = 100,
flags = "[WPA2-PSK-CCMP][ESS]",
},
}
for i=1,10 do
table.insert(list, {
ssid = "918-"..tostring(i),
signal_level = -58-i*2,
signal_quality = 84-i*2,
flags = "[WPA2-PSK-CCMP][ESS]",
})
end
local nw = require("ui/widget/networksetting"):new{network_list = list}
UIManager:show(nw)
end
2013-03-12 05:08:30 +00:00
-----------------------------------------------------------------------
-- you may want to uncomment following show calls to see the changes
-----------------------------------------------------------------------
--UIManager:show(Background:new())
--UIManager:show(TestGrid)
UIManager:show(TestVisible)
UIManager:show(Clock:new())
2016-02-16 02:08:04 +00:00
-- UIManager:show(M)
2013-03-13 02:50:16 +00:00
--UIManager:show(Quiz)
--UIManager:show(readerwindow)
2013-07-30 08:21:19 +00:00
--UIManager:show(touch_menu)
2013-07-30 15:08:10 +00:00
--UIManager:show(keyboard)
--UIManager:show(TestInputText)
--TestInputText:onShowKeyboard()
-- testKeyValuePage()
2016-04-03 04:52:30 +00:00
-- testTouchProbe()
-- testBookStatus()
testNetworkSetting()
UIManager:run()