From 60587e08c681e763d3f76d9b5820120435f6084c Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Sun, 1 May 2016 19:39:31 -0700 Subject: [PATCH 1/3] defaults(refactor): remove global hack in filemanagersetdefaults also added screensaver folder setting dialog for kobo --- frontend/apps/filemanager/filemanager.lua | 4 - .../filemanager/filemanagerfilesearcher.lua | 3 - frontend/apps/filemanager/filemanagermenu.lua | 52 +- .../apps/filemanager/filemanagersearch.lua | 6 +- .../filemanager/filemanagersetdefaults.lua | 449 ++++++++---------- .../apps/reader/modules/readerfrontlight.lua | 2 - frontend/apps/reader/modules/readergoto.lua | 4 - frontend/apps/reader/readerui.lua | 2 - frontend/dump.lua | 7 +- frontend/ui/widget/bookstatuswidget.lua | 3 - .../ui/widget/container/inputcontainer.lua | 4 - frontend/ui/widget/dictquicklookup.lua | 3 - frontend/ui/widget/inputdialog.lua | 40 +- frontend/ui/widget/multiinputdialog.lua | 2 +- spec/unit/defaults_spec.lua | 90 ++++ 15 files changed, 389 insertions(+), 282 deletions(-) create mode 100644 spec/unit/defaults_spec.lua diff --git a/frontend/apps/filemanager/filemanager.lua b/frontend/apps/filemanager/filemanager.lua index c670774a0..edd0a913e 100644 --- a/frontend/apps/filemanager/filemanager.lua +++ b/frontend/apps/filemanager/filemanager.lua @@ -157,7 +157,6 @@ function FileManager:init() callback = function() renameFile(file) self:refreshPath() - fileManager.rename_dialog:onClose() UIManager:close(fileManager.rename_dialog) end, }, @@ -165,13 +164,10 @@ function FileManager:init() text = _("Cancel"), enabled = true, callback = function() - fileManager.rename_dialog:onClose() UIManager:close(fileManager.rename_dialog) end, }, }}, - width = Screen:getWidth() * 0.8, - height = Screen:getHeight() * 0.2, } fileManager.rename_dialog:onShowKeyboard() UIManager:show(fileManager.rename_dialog) diff --git a/frontend/apps/filemanager/filemanagerfilesearcher.lua b/frontend/apps/filemanager/filemanagerfilesearcher.lua index 1d6ea1f65..8dc19f518 100644 --- a/frontend/apps/filemanager/filemanagerfilesearcher.lua +++ b/frontend/apps/filemanager/filemanagerfilesearcher.lua @@ -83,7 +83,6 @@ end function FileSearcher:close() if self.search_value then - self.search_dialog:onClose() UIManager:close(self.search_dialog) if string.len(self.search_value) > 0 then self:readDir() -- TODO this probably doesn't need to be repeated once it's been done @@ -131,8 +130,6 @@ function FileSearcher:showSearch() }, }, }, - width = Screen:getWidth() * 0.8, - height = Screen:getHeight() * 0.2, } self.search_dialog:onShowKeyboard() UIManager:show(self.search_dialog) diff --git a/frontend/apps/filemanager/filemanagermenu.lua b/frontend/apps/filemanager/filemanagermenu.lua index 593af668b..f10dd8c0e 100644 --- a/frontend/apps/filemanager/filemanagermenu.lua +++ b/frontend/apps/filemanager/filemanagermenu.lua @@ -2,10 +2,11 @@ local CenterContainer = require("ui/widget/container/centercontainer") local InputContainer = require("ui/widget/container/inputcontainer") local ConfirmBox = require("ui/widget/confirmbox") local UIManager = require("ui/uimanager") -local Device = require("device") local GestureRange = require("ui/gesturerange") +local InputDialog = require("ui/widget/inputdialog") local Geom = require("ui/geometry") -local Screen = require("device").screen +local Device = require("device") +local Screen = Device.screen local DEBUG = require("dbg") local _ = require("gettext") local FileSearcher = require("apps/filemanager/filemanagerfilesearcher") @@ -14,7 +15,7 @@ local SetDefaults = require("apps/filemanager/filemanagersetdefaults") local FileManagerMenu = InputContainer:extend{ tab_item_table = nil, - registered_widgets = {}, + registered_widgets = nil, } function FileManagerMenu:init() @@ -93,8 +94,12 @@ function FileManagerMenu:setUpdateItemTable() }) table.insert(self.tab_item_table.setting, { text = _("Start with last opened file"), - checked_func = function() return G_reader_settings:readSetting("open_last") end, - enabled_func = function() return G_reader_settings:readSetting("lastfile") ~= nil end, + checked_func = function() return + G_reader_settings:readSetting("open_last") + end, + enabled_func = function() return + G_reader_settings:readSetting("lastfile") ~= nil + end, callback = function() local open_last = G_reader_settings:readSetting("open_last") or false G_reader_settings:saveSetting("open_last", not open_last) @@ -112,6 +117,43 @@ function FileManagerMenu:setUpdateItemTable() end -- tools tab + if Device.isKobo() then + table.insert(self.tab_item_table.tools, { + text = _("Screen server folder"), + callback = function() + local ss_folder_path_input + local function save_folder_path() + G_reader_settings:saveSetting( + "screensaver_folder", ss_folder_path_input:getInputText()) + UIManager:close(ss_folder_path_input) + end + local curr_path = G_reader_settings:readSetting("screensaver_folder") + ss_folder_path_input = InputDialog:new{ + title = _("Screen saver folder"), + input = curr_path, + input_hint = "/mnt/onboard/screensaver", + input_type = "text", + buttons = { + { + { + text = _("Cancel"), + callback = function() + UIManager:close(ss_folder_path_input) + end, + }, + { + text = _("Save"), + callback = save_folder_path, + }, + } + }, + enter_callback = save_folder_path, + } + ss_folder_path_input:onShowKeyboard() + UIManager:show(ss_folder_path_input) + end, + }) + end table.insert(self.tab_item_table.tools, { text = _("Advanced settings"), callback = function() diff --git a/frontend/apps/filemanager/filemanagersearch.lua b/frontend/apps/filemanager/filemanagersearch.lua index f4269f07c..af35e2ec9 100644 --- a/frontend/apps/filemanager/filemanagersearch.lua +++ b/frontend/apps/filemanager/filemanagersearch.lua @@ -125,7 +125,7 @@ function Search:getCalibre() self.use_own_metadata_file = false if self.metafile_1 then pcall(lfs.mkdir("temp")) - if io.open(koreaderfile,"r") then + if io.open(koreaderfile, "r") then if lfs.attributes(koreaderfile).modification > lfs.attributes(self.metafile_1).modification then if self.metafile_2 then if lfs.attributes(koreaderfile).modification > lfs.attributes(self.metafile_2).modification then @@ -443,7 +443,9 @@ function Search:find(option) end g.close() if lfs.attributes(koreaderfile).modification < lfs.attributes(self.metafile_1).modification then - lfs.touch(koreaderfile, lfs.attributes(self.metafile_1).modification + 1, lfs.attributes(self.metafile_1).modification + 1) + lfs.touch(koreaderfile, + lfs.attributes(self.metafile_1).modification + 1, + lfs.attributes(self.metafile_1).modification + 1) end if self.metafile_2 then if lfs.attributes(koreaderfile).modification < lfs.attributes(self.metafile_2).modification then diff --git a/frontend/apps/filemanager/filemanagersetdefaults.lua b/frontend/apps/filemanager/filemanagersetdefaults.lua index 74292d4ad..8265de317 100644 --- a/frontend/apps/filemanager/filemanagersetdefaults.lua +++ b/frontend/apps/filemanager/filemanagersetdefaults.lua @@ -10,41 +10,23 @@ local Screen = require("device").screen local Menu = require("ui/widget/menu") local Font = require("ui/font") local util = require("ffi/util") -local DEBUG = require("dbg") local _ = require("gettext") +local dump = require("dump") + + +local defaults_path = DataStorage:getDataDir() .. "/defaults.lua" +local persistent_filename = DataStorage:getDataDir() .. "/defaults.persistent.lua" + local SetDefaults = InputContainer:new{ defaults_name = {}, defaults_value = {}, results = {}, defaults_menu = {}, - already_read = false, + initialized = false, changed = {} } -local function settype(b,t) - if t == "boolean" then - if b == "false" then return false else return true end - else - return b - end -end - -local function getTableValues(t,dtap) - local dummy = "{" - for n,v in pairs(t) do - if dtap:sub(1,4) == "DTAP" or dtap:sub(1,11) == "DDOUBLE_TAP" then - dummy = dummy .. tostring(n) .. " = " .. tostring(v) .. ", " - elseif tonumber(v) then - dummy = dummy .. tostring(v) .. ", " - else - dummy = dummy .. "\"" .. tostring(v) .. "\", " - end - end - dummy = dummy:sub(1,string.len(dummy) - 2) .. "}" - return dummy -end - function SetDefaults:ConfirmEdit() if not SetDefaults.EditConfirmed then UIManager:show(ConfirmBox:new{ @@ -62,21 +44,20 @@ end function SetDefaults:init() self.results = {} - if not self.already_read then - local i = 0 - for n,v in util.orderedPairs(_G) do - if (not string.find(tostring(v), "<")) and (not string.find(tostring(v), ": ")) and string.sub(n,1,1) ~= "_" and string.upper(n) == n and n ~= "LIBRARY_PATH" then - i = i + 1 - self.defaults_name[i] = n - self.defaults_value[i] = v - end - if string.find(tostring(v), "table: ") and string.upper(n) == n and n ~= "ARGV" and n ~= "_G" then - i = i + 1 - self.defaults_name[i] = n - self.defaults_value[i] = getTableValues(v,n) - end + if not self.initialized then + local defaults = {} + local load_defaults = loadfile(defaults_path) + setfenv(load_defaults, defaults) + load_defaults() + + local i = 1 + for n, v in util.orderedPairs(defaults) do + self.defaults_name[i] = n + self.defaults_value[i] = v + i = i + 1 end - self.already_read = true + + self.initialized = true end local menu_container = CenterContainer:new{ @@ -102,176 +83,163 @@ function SetDefaults:init() UIManager:close(menu_container) end - for i=1,#self.defaults_name do + local cancel_button = { + text = _("Cancel"), + enabled = true, + callback = function() + self:close() + UIManager:show(menu_container) + end, + } + + for i=1, #self.defaults_name do self.changed[i] = false - local settings_type = type(_G[self.defaults_name[i]]) - if settings_type == "boolean" then + local setting_name = self.defaults_name[i] + local setting_type = type(_G[setting_name]) + if setting_type == "boolean" then + local editBoolean = function() + self.set_dialog = InputDialog:new{ + title = setting_name, + input = tostring(self.defaults_value[i]), + buttons = { + { + cancel_button, + { + text = "true", + enabled = true, + callback = function() + self.defaults_value[i] = true + _G[setting_name] = true + settings_changed = true + self.changed[i] = true + self.results[i].text = self:build_setting(i) + self:close() + self.defaults_menu:swithItemTable("Defaults", self.results, i) + UIManager:show(menu_container) + end + }, + { + text = "false", + enabled = true, + callback = function() + self.defaults_value[i] = false + _G[setting_name] = false + settings_changed = true + self.changed[i] = true + self.results[i].text = self:build_setting(i) + self.defaults_menu:swithItemTable("Defaults", self.results, i) + self:close() + UIManager:show(menu_container) + end + }, + }, + }, + input_type = setting_type, + width = Screen:getWidth() * 0.95, + } + self.set_dialog:onShowKeyboard() + UIManager:show(self.set_dialog) + end + table.insert(self.results, { text = self:build_setting(i), - callback = function() - self.set_dialog = InputDialog:new{ - title = self.defaults_name[i] .. ":", - input = tostring(self.defaults_value[i]), - buttons = { + callback = editBoolean + }) + elseif setting_type == "table" then + local editTable = function() + local fields = {} + for k, v in util.orderedPairs(_G[setting_name]) do + table.insert(fields, { + text = tostring(k) .. " = " .. tostring(v), + hint = "", + }) + end + self.set_dialog = MultiInputDialog:new{ + title = setting_name, + fields = fields, + buttons = { + { + cancel_button, { - { - text = _("Cancel"), - enabled = true, - callback = function() - self:close() - UIManager:show(menu_container) + text = _("OK"), + enabled = true, + callback = function() + local new_table = {} + for _, field in ipairs(MultiInputDialog:getFields()) do + new_table[field:match("^[^= ]+")] = field:match("[^= ]+$") end - }, - { - text = "true", - enabled = true, - callback = function() - self.defaults_value[i] = true - _G[self.defaults_name[i]] = true - settings_changed = true - self.changed[i] = true - self.results[i].text = self:build_setting(i) - self:close() - self.defaults_menu:swithItemTable("Defaults", self.results, i) - UIManager:show(menu_container) + _G[setting_name] = new_table + + self.defaults_value[i] = _G[setting_name] + settings_changed = true + self.changed[i] = true + + self.results[i].text = self:build_setting(i) + + self:close() + self.defaults_menu:swithItemTable("Defaults", self.results, i) + UIManager:show(menu_container) + end, + }, + }, + }, + input_type = "number", + width = Screen:getWidth() * 0.95, + height = Screen:getHeight() * 0.2, + } + self.set_dialog:onShowKeyboard() + UIManager:show(self.set_dialog) + end + + table.insert(self.results, { + text = self:build_setting(i), + callback = editTable + }) + else + local editNumStr = function() + self.set_dialog = InputDialog:new{ + title = setting_name, + input = tostring(self.defaults_value[i]), + buttons = { + { + cancel_button, + { + text = _("OK"), + enabled = true, + callback = function() + local new_value = self.set_dialog:getInputText() + if setting_type == "boolean" then + if new_value == "true" then + new_value = true + else + new_value = false + end end - }, - { - text = "false", - enabled = true, - callback = function() - self.defaults_value[i] = false - _G[self.defaults_name[i]] = false + if _G[setting_name] ~= new_value then + _G[setting_name] = new_value + self.defaults_value[i] = new_value settings_changed = true self.changed[i] = true self.results[i].text = self:build_setting(i) - self.defaults_menu:swithItemTable("Defaults", self.results, i) - self:close() - UIManager:show(menu_container) end - }, + self:close() + self.defaults_menu:swithItemTable("Defaults", self.results, i) + UIManager:show(menu_container) + end, }, }, - input_type = settings_type, - width = Screen:getWidth() * 0.95, - height = Screen:getHeight() * 0.2, - } - self.set_dialog:onShowKeyboard() - UIManager:show(self.set_dialog) - end - }) - else - if type(_G[self.defaults_name[i]]) == "table" then - table.insert(self.results, { - text = self:build_setting(i), - callback = function() - local fields = {} - for m, n in util.orderedPairs(_G[self.defaults_name[i]]) do - table.insert(fields, { - text = tostring(m) .. " = " .. tostring(n), - hint = "", - }) - end - self.set_dialog = MultiInputDialog:new{ - title = self.defaults_name[i] .. ":", - fields = fields, - buttons = { - { - { - text = _("Cancel"), - enabled = true, - callback = function() - self:close() - UIManager:show(menu_container) - end, - }, - { - text = _("OK"), - enabled = true, - callback = function() - local fields = {} - for _, field in ipairs(MultiInputDialog:getFields()) do - fields[field:match("^[^= ]+")] = field:match("[^= ]+$") - end - _G[self.defaults_name[i]] = fields - self.defaults_value[i] = "{" - DEBUG(_G[self.defaults_name[i]]) - for k,v in util.orderedPairs(_G[self.defaults_name[i]]) do - if tonumber(k) then - self.defaults_value[i] = self.defaults_value[i] .. v .. ", " - else - self.defaults_value[i] = self.defaults_value[i] .. k .. " = " .. v .. ", " - end - end - self.defaults_value[i] = self.defaults_value[i]:sub(1,self.defaults_value[i]:len()-2) .. "}" - - settings_changed = true - self.changed[i] = true - - self.results[i].text = self:build_setting(i) - - self:close() - self.defaults_menu:swithItemTable("Defaults", self.results, i) - UIManager:show(menu_container) - end, - }, - }, - }, - input_type = "number", - width = Screen:getWidth() * 0.95, - height = Screen:getHeight() * 0.2, - } - self.set_dialog:onShowKeyboard() - UIManager:show(self.set_dialog) - end - }) - - else - table.insert(self.results, { - text = self:build_setting(i), - callback = function() - self.set_dialog = InputDialog:new{ - title = self.defaults_name[i] .. ":", - input = tostring(self.defaults_value[i]), - buttons = { - { - { - text = _("Cancel"), - enabled = true, - callback = function() - self:close() - UIManager:show(menu_container) - end, - }, - { - text = _("OK"), - enabled = true, - callback = function() - if type(_G[self.defaults_name[i]]) == "table" then - self.defaults_value[i] = self.set_dialog:getInputText() - elseif _G[self.defaults_name[i]] ~= settype(self.set_dialog:getInputText(),settings_type) then - _G[self.defaults_name[i]] = settype(self.set_dialog:getInputText(),settings_type) - self.defaults_value[i] = _G[self.defaults_name[i]] - end - settings_changed = true - self.changed[i] = true - self.results[i].text = self:build_setting(i) - self:close() - self.defaults_menu:swithItemTable("Defaults", self.results, i) - UIManager:show(menu_container) - end, - }, - }, - }, - input_type = settings_type, - width = Screen:getWidth() * 0.95, - height = Screen:getHeight() * 0.2, - } - self.set_dialog:onShowKeyboard() - UIManager:show(self.set_dialog) - end - }) + }, + input_type = setting_type, + width = Screen:getWidth() * 0.95, + } + self.set_dialog:onShowKeyboard() + UIManager:show(self.set_dialog) end + + table.insert(self.results, { + text = self:build_setting(i), + callback = editNumStr + }) end end self.defaults_menu:swithItemTable("Defaults", self.results) @@ -279,13 +247,12 @@ function SetDefaults:init() end function SetDefaults:close() - self.set_dialog:onClose() UIManager:close(self.set_dialog) end function SetDefaults:ConfirmSave() UIManager:show(ConfirmBox:new{ - text = _("Are you sure you want to save the settings to \"defaults.persistent.lua\"?"), + text = _('Are you sure you want to save the settings to "defaults.persistent.lua"?'), ok_callback = function() self:SaveSettings() end, @@ -293,73 +260,64 @@ function SetDefaults:ConfirmSave() end function SetDefaults:build_setting(j) - local ret = self.defaults_name[j] .. " = " - if type(_G[self.defaults_name[j]]) == "boolean" or type(_G[self.defaults_name[j]]) == "table" then - ret = ret .. tostring(self.defaults_value[j]) + local setting_name = self.defaults_name[j] + local ret = setting_name .. " = " + if type(_G[setting_name]) == "boolean" then + return ret .. tostring(self.defaults_value[j]) + elseif type(_G[setting_name]) == "table" then + return ret .. "{...}" elseif tonumber(self.defaults_value[j]) then - ret = ret .. tostring(tonumber(self.defaults_value[j])) + return ret .. tostring(tonumber(self.defaults_value[j])) else - ret = ret .. "\"" .. tostring(self.defaults_value[j]) .. "\"" + return ret .. "\"" .. tostring(self.defaults_value[j]) .. "\"" end - return ret end function SetDefaults:SaveSettings() - - local function fileread(filename,array) - local file = io.open(filename) - local line = file:read() - local counter = 0 - while line do - counter = counter + 1 - local i = string.find(line,"[-][-]") -- remove comments from file - if (i or 0)>1 then line = string.sub(line,1,i-1) end - array[counter] = line:gsub("^%s*(.-)%s*$", "%1") -- trim - line = file:read() - end - file:close() - end - - local persistent_filename = DataStorage:getDataDir() .. "/defaults.persistent.lua" - local file - if io.open(persistent_filename,"r") == nil then - file = io.open(persistent_filename, "w") - file:write("-- For configuration changes that persists between (nightly) releases\n") + self.results = {} + local persisted_defaults = {} + local file = io.open(persistent_filename, "r") + if file ~= nil then file:close() + local load_defaults = loadfile(persistent_filename) + setfenv(load_defaults, persisted_defaults) + load_defaults() end - local dpl = {} - fileread(persistent_filename, dpl) - local dl = {} - fileread("defaults.lua",dl) - self.results = {} - local done = {} - - for j=1,#SetDefaults.defaults_name do - if not self.changed[j] then done[j] = true end + local checked = {} + for j=1, #self.defaults_name do + if not self.changed[j] then checked[j] = true end end - -- handle case "found in persistent", replace it - for i = 1,#dpl do - for j=1,#SetDefaults.defaults_name do - if not done[j] and string.find(dpl[i],SetDefaults.defaults_name[j] .. " ") == 1 then - dpl[i] = self:build_setting(j) - done[j] = true + -- handle case "found in persistent" and changed, replace it + for k, v in pairs(persisted_defaults) do + for j=1, #self.defaults_name do + if not checked[j] + and k == self.defaults_name[j] then + persisted_defaults[k] = self.defaults_value[j] + checked[j] = true end end end - -- handle case "not in persistent and different in non-persistent", add to persistent - for j=1,#SetDefaults.defaults_name do - if not done[j] then - dpl[#dpl+1] = self:build_setting(j) + -- handle case "not in persistent and different in non-persistent", add to + -- persistent + for j=1, #self.defaults_name do + if not checked[j] then + persisted_defaults[self.defaults_name[j]] = self.defaults_value[j] end end file = io.open(persistent_filename, "w") if file then - for i = 1,#dpl do - file:write(dpl[i] .. "\n") + file:write("-- For configuration changes that persists between updates\n") + for k, v in pairs(persisted_defaults) do + local line = {} + table.insert(line, k) + table.insert(line, " = ") + table.insert(line, dump(v)) + table.insert(line, "\n") + file:write(table.concat(line)) end file:close() UIManager:show(InfoMessage:new{ @@ -368,4 +326,5 @@ function SetDefaults:SaveSettings() end settings_changed = false end + return SetDefaults diff --git a/frontend/apps/reader/modules/readerfrontlight.lua b/frontend/apps/reader/modules/readerfrontlight.lua index d19d1c153..7e82cbf65 100644 --- a/frontend/apps/reader/modules/readerfrontlight.lua +++ b/frontend/apps/reader/modules/readerfrontlight.lua @@ -116,8 +116,6 @@ function ReaderFrontLight:onShowFlDialog() }, }, input_type = "number", - width = Screen:getWidth() * 0.8, - height = Screen:getHeight() * 0.2, } self.fl_dialog:onShowKeyboard() UIManager:show(self.fl_dialog) diff --git a/frontend/apps/reader/modules/readergoto.lua b/frontend/apps/reader/modules/readergoto.lua index ea14d6fbc..9c48567d0 100644 --- a/frontend/apps/reader/modules/readergoto.lua +++ b/frontend/apps/reader/modules/readergoto.lua @@ -1,7 +1,6 @@ local InputContainer = require("ui/widget/container/inputcontainer") local InputDialog = require("ui/widget/inputdialog") local UIManager = require("ui/uimanager") -local Screen = require("device").screen local Event = require("ui/event") local _ = require("gettext") @@ -58,15 +57,12 @@ function ReaderGoto:onShowGotoDialog() }, input_type = "number", enter_callback = function() self:gotoPage() end, - width = Screen:getWidth() * 0.8, - height = Screen:getHeight() * 0.2, } self.goto_dialog:onShowKeyboard() UIManager:show(self.goto_dialog) end function ReaderGoto:close() - self.goto_dialog:onClose() UIManager:close(self.goto_dialog) end diff --git a/frontend/apps/reader/readerui.lua b/frontend/apps/reader/readerui.lua index d98664342..16eb91313 100644 --- a/frontend/apps/reader/readerui.lua +++ b/frontend/apps/reader/readerui.lua @@ -426,8 +426,6 @@ function ReaderUI:unlockDocumentWithPassword(document, try_again) }, }, text_type = "password", - width = Screen:getWidth() * 0.8, - height = Screen:getHeight() * 0.2, } self.password_dialog:onShowKeyboard() UIManager:show(self.password_dialog) diff --git a/frontend/dump.lua b/frontend/dump.lua index 91eff5ebb..4e5f77413 100644 --- a/frontend/dump.lua +++ b/frontend/dump.lua @@ -4,6 +4,7 @@ simple serialization function, won't do uservalues, functions, loops local isUbuntuTouch = os.getenv("UBUNTU_APPLICATION_ISOLATION") ~= nil local insert = table.insert +local indent_prefix = " " local function _serialize(what, outt, indent, max_lv, history) if not max_lv then @@ -19,7 +20,7 @@ local function _serialize(what, outt, indent, max_lv, history) for up, item in ipairs(history) do if item == what then insert(outt, "nil --[[ LOOP:\n") - insert(outt, string.rep("\t", indent - up)) + insert(outt, string.rep(indent_prefix, indent - up)) insert(outt, "^------- ]]") return end @@ -32,7 +33,7 @@ local function _serialize(what, outt, indent, max_lv, history) insert(outt, ",") end insert(outt, "\n") - insert(outt, string.rep("\t", indent+1)) + insert(outt, string.rep(indent_prefix, indent+1)) insert(outt, "[") _serialize(k, outt, indent+1, max_lv, new_history) insert(outt, "] = ") @@ -41,7 +42,7 @@ local function _serialize(what, outt, indent, max_lv, history) end if didrun then insert(outt, "\n") - insert(outt, string.rep("\t", indent)) + insert(outt, string.rep(indent_prefix, indent)) end insert(outt, "}") elseif type(what) == "string" then diff --git a/frontend/ui/widget/bookstatuswidget.lua b/frontend/ui/widget/bookstatuswidget.lua index 9ad75146f..487cf268b 100644 --- a/frontend/ui/widget/bookstatuswidget.lua +++ b/frontend/ui/widget/bookstatuswidget.lua @@ -545,15 +545,12 @@ function BookStatusWidget:onSwitchFocus(inputbox) enter_callback = function() self:closeInputDialog() end, - width = Screen:getWidth() * 0.8, - height = Screen:getHeight() * 0.2, } self.note_dialog:onShowKeyboard() UIManager:show(self.note_dialog) end function BookStatusWidget:closeInputDialog() - self.note_dialog:onClose() UIManager:close(self.note_dialog) end diff --git a/frontend/ui/widget/container/inputcontainer.lua b/frontend/ui/widget/container/inputcontainer.lua index 615fcc39c..83d6167a6 100644 --- a/frontend/ui/widget/container/inputcontainer.lua +++ b/frontend/ui/widget/container/inputcontainer.lua @@ -1,6 +1,5 @@ local WidgetContainer = require("ui/widget/container/widgetcontainer") local UIManager = require("ui/uimanager") -local Screen = require("device").screen local Geom = require("ui/geometry") local Event = require("ui/event") local _ = require("gettext") @@ -128,15 +127,12 @@ function InputContainer:onInput(input) input.callback(self.input_dialog:getInputText()) self:closeInputDialog() end, - width = Screen:getWidth() * 0.8, - height = Screen:getHeight() * 0.2, } self.input_dialog:onShowKeyboard() UIManager:show(self.input_dialog) end function InputContainer:closeInputDialog() - self.input_dialog:onClose() UIManager:close(self.input_dialog) end diff --git a/frontend/ui/widget/dictquicklookup.lua b/frontend/ui/widget/dictquicklookup.lua index cdbb0c5e5..1ef3d0b08 100644 --- a/frontend/ui/widget/dictquicklookup.lua +++ b/frontend/ui/widget/dictquicklookup.lua @@ -424,8 +424,6 @@ function DictQuickLookup:lookupInputWord(hint) self:closeInputDialog() self:inputLookup() end, - width = Screen:getWidth() * 0.8, - height = Screen:getHeight() * 0.2, } self.input_dialog:onShowKeyboard() UIManager:show(self.input_dialog) @@ -440,7 +438,6 @@ function DictQuickLookup:inputLookup() end function DictQuickLookup:closeInputDialog() - self.input_dialog:onClose() UIManager:close(self.input_dialog) end diff --git a/frontend/ui/widget/inputdialog.lua b/frontend/ui/widget/inputdialog.lua index 247818f60..338dd0457 100644 --- a/frontend/ui/widget/inputdialog.lua +++ b/frontend/ui/widget/inputdialog.lua @@ -1,3 +1,40 @@ +--[[-- +Widget for taking user input. + +Example: + + local _ = require("gettext") + local UIManager = require("ui/uimanager") + local sample_input + local saveHandler = function() + print('Got user input:', sample_input:getInputText()) + end + sample_input = InputDialog:new{ + title = _("Dialog title"), + input = "default value", + input_hint = "hint text", + input_type = "text", + buttons = { + { + { + text = _("Cancel"), + callback = function() + UIManager:close(sample_input) + end, + }, + { + text = _("Save"), + callback = saveHandler, + }, + } + }, + enter_callback = saveHandler, + } + sample_input:onShowKeyboard() + UIManager:show(sample_input) + +]] + local InputContainer = require("ui/widget/container/inputcontainer") local FrameContainer = require("ui/widget/container/framecontainer") local CenterContainer = require("ui/widget/container/centercontainer") @@ -38,6 +75,7 @@ local InputDialog = InputContainer:new{ } function InputDialog:init() + self.width = self.width or Screen:getWidth() * 0.8 local title_width = RenderText:sizeUtf8Text(0, self.width, self.title_face, self.title, true).x if title_width > self.width then @@ -78,7 +116,6 @@ function InputDialog:init() show_parent = self, } self.title_bar = LineWidget:new{ - --background = Blitbuffer.gray(0.5), dimen = Geom:new{ w = self.button_table:getSize().w + self.button_padding, h = Screen:scaleBySize(2), @@ -138,6 +175,7 @@ function InputDialog:onShow() end function InputDialog:onCloseWidget() + self:onClose() UIManager:setDirty(nil, function() return "partial", self.dialog_frame.dimen end) diff --git a/frontend/ui/widget/multiinputdialog.lua b/frontend/ui/widget/multiinputdialog.lua index eb479d0c0..f7f7bbf4b 100644 --- a/frontend/ui/widget/multiinputdialog.lua +++ b/frontend/ui/widget/multiinputdialog.lua @@ -40,7 +40,7 @@ function MultiInputDialog:init() scroll = false, parent = self, } - table.insert(VerticalGroupData,CenterContainer:new{ + table.insert(VerticalGroupData, CenterContainer:new{ dimen = Geom:new{ w = self.title_bar:getSize().w, h = input_field[k]:getSize().h, diff --git a/spec/unit/defaults_spec.lua b/spec/unit/defaults_spec.lua new file mode 100644 index 000000000..bda9c28f1 --- /dev/null +++ b/spec/unit/defaults_spec.lua @@ -0,0 +1,90 @@ +describe("defaults module", function() + local Defaults, DataStorage + setup(function() + require("commonrequire") + Defaults = require("apps/filemanager/filemanagersetdefaults") + DataStorage = require("datastorage") + end) + + it("should load all defaults from defaults.lua", function() + Defaults:init() + assert.is_same(#Defaults.defaults_name, 78) + assert.is_same(Defaults.defaults_name[29], 'DHINTCOUNT') + end) + + it("should save changes to defaults.persistent.lua", function() + local persistent_filename = DataStorage:getDataDir() .. "/defaults.persistent.lua" + os.remove(persistent_filename) + + -- not in persistent but checked in defaults + Defaults.changed[14] = true + Defaults.changed[19] = true + Defaults.changed[29] = true + Defaults.changed[64] = true + Defaults.changed[78] = true + Defaults:SaveSettings() + assert.is_same(#Defaults.defaults_name, 78) + assert.is_same(Defaults.defaults_name[29], 'DHINTCOUNT') + assert.is_same(Defaults.defaults_name[78], 'SEARCH_TITLE') + assert.is_same(Defaults.defaults_name[64], 'DTAP_ZONE_MENU') + assert.is_same(Defaults.defaults_name[19], 'DCREREADER_VIEW_MODE') + assert.is_same(Defaults.defaults_name[14], + 'DCREREADER_CONFIG_MARGIN_SIZES_LARGE') + local fd = io.open(persistent_filename, "r") + assert.Equals( +[[-- For configuration changes that persists between updates +SEARCH_TITLE = true +DCREREADER_CONFIG_MARGIN_SIZES_LARGE = { + [1] = 20, + [2] = 20, + [3] = 20, + [4] = 20 +} +DCREREADER_VIEW_MODE = "page" +DHINTCOUNT = 1 +DTAP_ZONE_MENU = { + ["y"] = 0, + ["x"] = 0.125, + ["h"] = 0.25, + ["w"] = 0.75 +} +]], + fd:read("*a")) + fd:close() + + -- in persistent + Defaults:init() + Defaults.changed[29] = true + Defaults.defaults_value[29] = 2 + Defaults.changed[64] = true + Defaults.defaults_value[64] = { + y = 10, + x = 10.125, + h = 20.25, + w = 20.75 + } + Defaults:SaveSettings() + fd = io.open(persistent_filename) + assert.Equals( +[[-- For configuration changes that persists between updates +SEARCH_TITLE = true +DHINTCOUNT = 2 +DTAP_ZONE_MENU = { + ["y"] = 10, + ["x"] = 10.125, + ["h"] = 20.25, + ["w"] = 20.75 +} +DCREREADER_CONFIG_MARGIN_SIZES_LARGE = { + [1] = 20, + [2] = 20, + [3] = 20, + [4] = 20 +} +DCREREADER_VIEW_MODE = "page" +]], + fd:read("*a")) + fd:close() + os.remove(persistent_filename) + end) +end) From 476e35708d7edd27e750868662d529aaa43c45cc Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Wed, 25 May 2016 23:09:49 -0700 Subject: [PATCH 2/3] inputdialog(feat): add is_enter_default attribute to buttons --- frontend/apps/filemanager/filemanagermenu.lua | 2 +- .../filemanager/filemanagersetdefaults.lua | 2 ++ frontend/apps/reader/modules/readergoto.lua | 3 +- frontend/ui/widget/bookstatuswidget.lua | 4 +-- frontend/ui/widget/buttontable.lua | 31 +++++++++++-------- .../ui/widget/container/inputcontainer.lua | 5 +-- frontend/ui/widget/dictquicklookup.lua | 5 +-- frontend/ui/widget/inputdialog.lua | 24 ++++++++++---- 8 files changed, 44 insertions(+), 32 deletions(-) diff --git a/frontend/apps/filemanager/filemanagermenu.lua b/frontend/apps/filemanager/filemanagermenu.lua index f10dd8c0e..cb6416770 100644 --- a/frontend/apps/filemanager/filemanagermenu.lua +++ b/frontend/apps/filemanager/filemanagermenu.lua @@ -143,11 +143,11 @@ function FileManagerMenu:setUpdateItemTable() }, { text = _("Save"), + is_enter_default = true, callback = save_folder_path, }, } }, - enter_callback = save_folder_path, } ss_folder_path_input:onShowKeyboard() UIManager:show(ss_folder_path_input) diff --git a/frontend/apps/filemanager/filemanagersetdefaults.lua b/frontend/apps/filemanager/filemanagersetdefaults.lua index 8265de317..abb205a84 100644 --- a/frontend/apps/filemanager/filemanagersetdefaults.lua +++ b/frontend/apps/filemanager/filemanagersetdefaults.lua @@ -163,6 +163,7 @@ function SetDefaults:init() { text = _("OK"), enabled = true, + is_enter_default = true, callback = function() local new_table = {} for _, field in ipairs(MultiInputDialog:getFields()) do @@ -205,6 +206,7 @@ function SetDefaults:init() cancel_button, { text = _("OK"), + is_enter_default = true, enabled = true, callback = function() local new_value = self.set_dialog:getInputText() diff --git a/frontend/apps/reader/modules/readergoto.lua b/frontend/apps/reader/modules/readergoto.lua index 9c48567d0..5ce52f0de 100644 --- a/frontend/apps/reader/modules/readergoto.lua +++ b/frontend/apps/reader/modules/readergoto.lua @@ -27,6 +27,7 @@ function ReaderGoto:onShowGotoDialog() if self.document.info.has_pages then dialog_title = _("Go to Page") goto_btn = { + is_enter_default = true, text = _("Page"), callback = function() self:gotoPage() end, } @@ -34,6 +35,7 @@ function ReaderGoto:onShowGotoDialog() else dialog_title = _("Go to Location") goto_btn = { + is_enter_default = true, text = _("Location"), callback = function() self:gotoPage() end, } @@ -56,7 +58,6 @@ function ReaderGoto:onShowGotoDialog() }, }, input_type = "number", - enter_callback = function() self:gotoPage() end, } self.goto_dialog:onShowKeyboard() UIManager:show(self.goto_dialog) diff --git a/frontend/ui/widget/bookstatuswidget.lua b/frontend/ui/widget/bookstatuswidget.lua index 487cf268b..21dc5cfbb 100644 --- a/frontend/ui/widget/bookstatuswidget.lua +++ b/frontend/ui/widget/bookstatuswidget.lua @@ -534,6 +534,7 @@ function BookStatusWidget:onSwitchFocus(inputbox) }, { text = _("OK"), + is_enter_default = true, callback = function() self.input_note:setText(self.note_dialog:getInputText()) self:closeInputDialog() @@ -542,9 +543,6 @@ function BookStatusWidget:onSwitchFocus(inputbox) }, }, }, - enter_callback = function() - self:closeInputDialog() - end, } self.note_dialog:onShowKeyboard() UIManager:show(self.note_dialog) diff --git a/frontend/ui/widget/buttontable.lua b/frontend/ui/widget/buttontable.lua index 562edbbe3..b3002dd6d 100644 --- a/frontend/ui/widget/buttontable.lua +++ b/frontend/ui/widget/buttontable.lua @@ -26,21 +26,26 @@ local ButtonTable = FocusManager:new{ } function ButtonTable:init() + self.buttons_layout = {} self.container = VerticalGroup:new{ width = self.width } table.insert(self, self.container) if self.zero_sep then self:addHorizontalSep() end - for i = 1, #self.buttons do + local row_cnt = #self.buttons + for i = 1, row_cnt do + self.buttons_layout[i] = {} local horizontal_group = HorizontalGroup:new{} - local line = self.buttons[i] - local sizer_space = self.sep_width * (#line - 1) + 2 - for j = 1, #line do + local row = self.buttons[i] + local column_cnt = #row + local sizer_space = self.sep_width * (column_cnt - 1) + 2 + for j = 1, column_cnt do + local btn_entry = row[j] local button = Button:new{ - text = line[j].text, - enabled = line[j].enabled, - callback = line[j].callback, - width = (self.width - sizer_space)/#line, + text = btn_entry.text, + enabled = btn_entry.enabled, + callback = btn_entry.callback, + width = (self.width - sizer_space)/column_cnt, bordersize = 0, margin = 0, padding = 0, @@ -56,19 +61,19 @@ function ButtonTable:init() h = button_dim.h, } } - self.buttons[i][j] = button + self.buttons_layout[i][j] = button table.insert(horizontal_group, button) - if j < #line then + if j < column_cnt then table.insert(horizontal_group, vertical_sep) end end -- end for each button table.insert(self.container, horizontal_group) - if i < #self.buttons then + if i < row_cnt then self:addHorizontalSep() end end -- end for each button line - if Device:hasDPad() then - self.layout = self.buttons + if Device:hasDPad() or Device:hasKeyboard() then + self.layout = self.buttons_layout self.layout[1][1]:onFocus() self.key_events.SelectByKeyPress = { {{"Press", "Enter"}} } else diff --git a/frontend/ui/widget/container/inputcontainer.lua b/frontend/ui/widget/container/inputcontainer.lua index 83d6167a6..e688d9603 100644 --- a/frontend/ui/widget/container/inputcontainer.lua +++ b/frontend/ui/widget/container/inputcontainer.lua @@ -116,6 +116,7 @@ function InputContainer:onInput(input) }, { text = _("OK"), + is_enter_default = true, callback = function() input.callback(self.input_dialog:getInputText()) self:closeInputDialog() @@ -123,10 +124,6 @@ function InputContainer:onInput(input) }, }, }, - enter_callback = function() - input.callback(self.input_dialog:getInputText()) - self:closeInputDialog() - end, } self.input_dialog:onShowKeyboard() UIManager:show(self.input_dialog) diff --git a/frontend/ui/widget/dictquicklookup.lua b/frontend/ui/widget/dictquicklookup.lua index 1ef3d0b08..0a66bb0db 100644 --- a/frontend/ui/widget/dictquicklookup.lua +++ b/frontend/ui/widget/dictquicklookup.lua @@ -413,6 +413,7 @@ function DictQuickLookup:lookupInputWord(hint) }, { text = _("Lookup"), + is_enter_default = true, callback = function() self:closeInputDialog() self:inputLookup() @@ -420,10 +421,6 @@ function DictQuickLookup:lookupInputWord(hint) }, } }, - enter_callback = function() - self:closeInputDialog() - self:inputLookup() - end, } self.input_dialog:onShowKeyboard() UIManager:show(self.input_dialog) diff --git a/frontend/ui/widget/inputdialog.lua b/frontend/ui/widget/inputdialog.lua index 338dd0457..014a54133 100644 --- a/frontend/ui/widget/inputdialog.lua +++ b/frontend/ui/widget/inputdialog.lua @@ -6,9 +6,6 @@ Example: local _ = require("gettext") local UIManager = require("ui/uimanager") local sample_input - local saveHandler = function() - print('Got user input:', sample_input:getInputText()) - end sample_input = InputDialog:new{ title = _("Dialog title"), input = "default value", @@ -24,11 +21,15 @@ Example: }, { text = _("Save"), - callback = saveHandler, + -- button with is_enter_default set to true will be + -- triggered after user press the enter key from keyboard + is_enter_default = true, + callback = function() + print('Got user input:', sample_input:getInputText()) + end, }, } }, - enter_callback = saveHandler, } sample_input:onShowKeyboard() UIManager:show(sample_input) @@ -95,6 +96,7 @@ function InputDialog:init() width = self.width, } } + self._input_widget = InputText:new{ text = self.input, hint = self.input_hint, @@ -103,7 +105,17 @@ function InputDialog:init() height = self.text_height or nil, input_type = self.input_type, text_type = self.text_type, - enter_callback = self.enter_callback, + enter_callback = self.enter_callback or function() + for _,btn_row in ipairs(self.buttons) do + for _,btn in ipairs(btn_row) do + require('dbg')('looging for btn', btn) + if btn.is_enter_default then + btn.callback() + return + end + end + end + end, scroll = false, parent = self, } From 9586b36eb6495200dd374f577ada2571bb96d218 Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Thu, 26 May 2016 00:44:42 -0700 Subject: [PATCH 3/3] defaults(fix): purge entry from persistent file --- .../filemanager/filemanagersetdefaults.lua | 15 ++++++- spec/unit/defaults_spec.lua | 39 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/frontend/apps/filemanager/filemanagersetdefaults.lua b/frontend/apps/filemanager/filemanagersetdefaults.lua index abb205a84..e6e98aac2 100644 --- a/frontend/apps/filemanager/filemanagersetdefaults.lua +++ b/frontend/apps/filemanager/filemanagersetdefaults.lua @@ -291,12 +291,23 @@ function SetDefaults:SaveSettings() if not self.changed[j] then checked[j] = true end end - -- handle case "found in persistent" and changed, replace it + -- load default value for defaults + local defaults = {} + local load_defaults = loadfile(defaults_path) + setfenv(load_defaults, defaults) + load_defaults() + -- handle case "found in persistent" and changed, replace/delete it for k, v in pairs(persisted_defaults) do for j=1, #self.defaults_name do if not checked[j] and k == self.defaults_name[j] then - persisted_defaults[k] = self.defaults_value[j] + -- remove from persist if value got reverted back to the + -- default one + if defaults[k] == self.defaults_value[j] then + persisted_defaults[k] = nil + else + persisted_defaults[k] = self.defaults_value[j] + end checked[j] = true end end diff --git a/spec/unit/defaults_spec.lua b/spec/unit/defaults_spec.lua index bda9c28f1..be7cca889 100644 --- a/spec/unit/defaults_spec.lua +++ b/spec/unit/defaults_spec.lua @@ -82,6 +82,45 @@ DCREREADER_CONFIG_MARGIN_SIZES_LARGE = { [4] = 20 } DCREREADER_VIEW_MODE = "page" +]], + fd:read("*a")) + fd:close() + os.remove(persistent_filename) + end) + + it("should delete entry from defaults.persistent.lua if value is reverted back to default", function() + local persistent_filename = DataStorage:getDataDir() .. "/defaults.persistent.lua" + local fd = io.open(persistent_filename, "w") + fd:write( +[[-- For configuration changes that persists between updates +SEARCH_TITLE = true +DCREREADER_CONFIG_MARGIN_SIZES_LARGE = { + [1] = 20, + [2] = 20, + [3] = 20, + [4] = 20 +} +DCREREADER_VIEW_MODE = "page" +DHINTCOUNT = 2 +]]) + fd:close() + + -- in persistent + Defaults:init() + Defaults.changed[29] = true + Defaults.defaults_value[29] = 1 + Defaults:SaveSettings() + fd = io.open(persistent_filename) + assert.Equals( +[[-- For configuration changes that persists between updates +SEARCH_TITLE = true +DCREREADER_VIEW_MODE = "page" +DCREREADER_CONFIG_MARGIN_SIZES_LARGE = { + [1] = 20, + [2] = 20, + [3] = 20, + [4] = 20 +} ]], fd:read("*a")) fd:close()