Skip to content

Commit

Permalink
Cleanup InfoMessage:inform() API and its usage
Browse files Browse the repository at this point in the history
This patch changes the second argument of InfoMessage:inform() function
to be either DINFO_DELAY or DINFO_NODELAY with the following
semantics:

DINFO_DELAY - display the message and delay DINFO_DELAY ms before
returning. Save and restore the screen content.

DINFO_NODELAY - display the message and return immediately without
saving/restoring the screen content.

Also, tidy up the call to modBBox() from Shift-X command handler, it
should be unireader:modBBox() and not self:modBBox(unireader) --- there
is no need to pass the reader object to the :modBBox() method.
  • Loading branch information
tigran123 committed Nov 15, 2012
1 parent 81b3da5 commit 6090896
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 145 deletions.
4 changes: 2 additions & 2 deletions commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function Commands:new(obj)
if G_charging_mode == false and G_screen_saver_mode == false then
if G_battery_logging then logBatteryLevel("SLEEP") end
Screen:saveCurrentBB()
InfoMessage:inform("Going into screensaver... ", DINFO_TIMEOUT_FAST, 0, MSG_AUX)
InfoMessage:inform("Going into screensaver... ", DINFO_NODELAY, 0, MSG_AUX)
Screen.kpv_rotation_mode = Screen.cur_rotation_mode
fb:setOrientation(Screen.native_rotation_mode)
util.sleep(1)
Expand Down Expand Up @@ -203,7 +203,7 @@ function Commands:new(obj)
Screen:saveCurrentBB()
Screen.kpv_rotation_mode = Screen.cur_rotation_mode
fb:setOrientation(Screen.native_rotation_mode)
InfoMessage:inform("Going into USB mode... ", DINFO_TIMEOUT_FAST, 0, MSG_AUX)
InfoMessage:inform("Going into USB mode... ", DINFO_NODELAY, 0, MSG_AUX)
util.sleep(1)
os.execute("killall -cont cvm")
end
Expand Down
32 changes: 15 additions & 17 deletions crereader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function CREReader:showJumpHist()
end

if #menu_items == 0 then
InfoMessage:inform("No jump history found ", DINFO_TIMEOUT_SLOW, 1, MSG_WARN)
InfoMessage:inform("No jump history found ", DINFO_DELAY, 1, MSG_WARN)
else
-- if cur points to head, draw entry for current page
if self.jump_history.cur > #self.jump_history then
Expand Down Expand Up @@ -310,7 +310,7 @@ function CREReader:showBookMarks()
.." "..v.notes.." @ "..v.datetime)
end
if #menu_items == 0 then
InfoMessage:inform("No bookmark found ", DINFO_TIMEOUT_SLOW, 1, MSG_WARN)
InfoMessage:inform("No bookmark found ", DINFO_DELAY, 1, MSG_WARN)
else
local bkmk_menu = SelectMenu:new{
menu_title = "Bookmarks",
Expand Down Expand Up @@ -352,7 +352,7 @@ function CREReader:gotoPrevNextTocEntry(direction)
self:fillToc()
end
if #self.toc == 0 then
InfoMessage:inform("No Table of Contents ", DINFO_TIMEOUT_SLOW, 1, MSG_WARN)
InfoMessage:inform("No Table of Contents ", DINFO_DELAY, 1, MSG_WARN)
return
end
-- search for current TOC-entry
Expand Down Expand Up @@ -523,7 +523,7 @@ function CREReader:adjustCreReaderCommands()
change = "Decreasing"
end
self.font_zoom = self.font_zoom + delta
InfoMessage:inform(change.." font size to "..self.font_zoom..". ", DINFO_TIMEOUT_FAST, 1, MSG_AUX)
InfoMessage:inform(change.." font size to "..self.font_zoom..". ", DINFO_NODELAY, 1, MSG_AUX)
Debug("font zoomed to", self.font_zoom)
local prev_xpointer = self.doc:getXPointer()
self.doc:zoomFont(delta)
Expand All @@ -542,7 +542,7 @@ function CREReader:adjustCreReaderCommands()
self.line_space_percent = self.line_space_percent + 10
self.line_space_percent = math.min(self.line_space_percent, 200)
end
InfoMessage:inform("Changing line space to "..self.line_space_percent.."% ", DINFO_TIMEOUT_FAST, 1, MSG_AUX)
InfoMessage:inform("Changing line space to "..self.line_space_percent.."% ", DINFO_NODELAY, 1, MSG_AUX)
Debug("line spacing set to", self.line_space_percent)
local prev_xpointer = self.doc:getXPointer()
self.doc:setDefaultInterlineSpace(self.line_space_percent)
Expand Down Expand Up @@ -596,7 +596,7 @@ function CREReader:adjustCreReaderCommands()
local prev_xpointer = self.doc:getXPointer()
if item_no then
Debug(face_list[item_no])
InfoMessage:inform("Redrawing with "..face_list[item_no].." ", DINFO_TIMEOUT_FAST, 1, MSG_AUX)
InfoMessage:inform("Redrawing with "..face_list[item_no].." ", DINFO_NODELAY, 1, MSG_AUX)
self.doc:setFontFace(face_list[item_no])
self.font_face = face_list[item_no]
end
Expand All @@ -608,15 +608,13 @@ function CREReader:adjustCreReaderCommands()
function(self)
self.default_font = self.font_face
G_reader_settings:saveSetting("cre_font", self.font_face)
InfoMessage:inform("Default document font set ", DINFO_TIMEOUT_SLOW, 1, MSG_WARN,
"Default document font is saved")
InfoMessage:inform("Default document font set ", DINFO_DELAY, 1, MSG_WARN)
end
)
self.commands:add(KEY_F, MOD_ALT, "F",
"toggle font-weight: bold <> normal",
function(self)

InfoMessage:inform("Changing font-weight...", DINFO_TIMEOUT_FAST, 1, MSG_AUX)
InfoMessage:inform("Changing font-weight...", DINFO_NODELAY, 1, MSG_AUX)
local prev_xpointer = self.doc:getXPointer()
self.doc:toggleFontBolder()
self:goto(prev_xpointer, nil, "xpointer")
Expand All @@ -627,9 +625,9 @@ function CREReader:adjustCreReaderCommands()
function(self)
ok = self:addBookmark(self.doc:getXPointer())
if not ok then
InfoMessage:inform("Page already marked ", DINFO_TIMEOUT_SLOW, 1, MSG_WARN)
InfoMessage:inform("Page already marked ", DINFO_DELAY, 1, MSG_WARN)
else
InfoMessage:inform("Page marked ", DINFO_TIMEOUT_SLOW, 1, MSG_WARN)
InfoMessage:inform("Page marked ", DINFO_DELAY, 1, MSG_WARN)
end
end
)
Expand Down Expand Up @@ -661,7 +659,7 @@ function CREReader:adjustCreReaderCommands()
self.jump_history.cur = prev_jump_no
self:goto(self.jump_history[prev_jump_no].page, true, "xpointer")
else
InfoMessage:inform("Already first jump ", DINFO_TIMEOUT_SLOW, 1, MSG_WARN)
InfoMessage:inform("Already first jump ", DINFO_DELAY, 1, MSG_WARN)
end
end
)
Expand All @@ -673,7 +671,7 @@ function CREReader:adjustCreReaderCommands()
self.jump_history.cur = next_jump_no
self:goto(self.jump_history[next_jump_no].page, true, "xpointer")
else
InfoMessage:inform("Already last jump ", DINFO_TIMEOUT_SLOW, 1, MSG_WARN)
InfoMessage:inform("Already last jump ", DINFO_DELAY, 1, MSG_WARN)
end
end
)
Expand All @@ -687,7 +685,7 @@ function CREReader:adjustCreReaderCommands()
end
cre.setGammaIndex(self.gamma_index+delta)
self.gamma_index = cre.getGammaIndex()
InfoMessage:inform("Changing gamma to "..self.gamma_index..". ", DINFO_TIMEOUT_FAST, 1, MSG_AUX)
InfoMessage:inform("Changing gamma to "..self.gamma_index..". ", DINFO_NODELAY, 1, MSG_AUX)
self:redrawCurrentPage()
end
)
Expand Down Expand Up @@ -732,9 +730,9 @@ function CREReader:searchHighLight(search)
if found then
self.pos = pos -- first metch position
self:redrawCurrentPage()
InfoMessage:inform( found.." hits '"..search.."' pos "..pos, DINFO_TIMEOUT_SLOW, 1, MSG_WARN)
InfoMessage:inform( found.." hits '"..search.."' pos "..pos, DINFO_DELAY, 1, MSG_WARN)
else
InfoMessage:inform( "'"..search.."' not found in document ", DINFO_TIMEOUT_SLOW, 1, MSG_WARN)
InfoMessage:inform( "'"..search.."' not found in document ", DINFO_DELAY, 1, MSG_WARN)
end

self.last_search.search = search
Expand Down
6 changes: 3 additions & 3 deletions defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ DBATTERY_LOGGING = false
-- background colour: 8 = gray, 0 = white, 15 = black
DBACKGROUND_COLOR = 8

-- timeout for info messages in ms
DINFO_TIMEOUT_FAST=1
DINFO_TIMEOUT_SLOW=1500
-- delay for info messages in ms
DINFO_NODELAY=0
DINFO_DELAY=1500

-- toggle defaults
DUNIREADER_SHOW_OVERLAP_ENABLE = true
Expand Down
59 changes: 18 additions & 41 deletions dialog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,59 +91,36 @@ function showInfoMsgWithDelay(text, msec, refresh_mode)
fb:refresh(refresh_mode)
end

--[[ Unified function to inform user about smth. It is generally intended to replace showInfoMsgWithDelay() & InfoMessage:show().
Since the former function is used in Lua-code more often than the latter, I kept here the same sequence of parameters as used in
showInfoMsgWithDelay() + added two not obligatory parameters (see description below).
Thus, this trick allows multiple text replaces thoughout the whole Lua-code: 'showInfoMsgWithDelay(' to 'InfoMessage:inform('
Yet, it would be much better to accompany such replaces by adding the 'message_importance' and, if needed, by
'alternative_voice_message' that is not so restricted in length as 'text'
But replacing InfoMessage:show(...) by InfoMessage:inform(...) MUST be accompanied by adding the 2nd parameter -- either msec=nil or msec=0.
Adding new parameters -- 'message_importance' & 'alternative_voice_message' -- is also appreciated.
--[[
Brief description of the function parameters
-- text : is the text message for visual and (if 'alternative_voice_message' isn't defined) voice notification
-- msec : parameter to define visual notification method
nil: display the message immediately without slowing it down on the emulator
DINFO_TIMEOUT_FAST: display the message and return immediately on Kindle, but after a delay on the emulator
DINFO_TIMEOUT_SLOW: display the message and return after a delay
msec<0: the delay is auto-calculated from the text length
-- message_importance : parameter separating various messages on
-- delay : parameter to define visual notification method
DINFO_NODELAY: display the message and return immediately (screen content not restored)
DINFO_DELAY: display the message and return after a delay (screen content restored)
-- msgtype : parameter separating various messages on
MSG_AUX - not obligatory messages that might be readily avoided
MSG_WARN - warnings (important messages)
MSG_ERROR - errors
MSG_CONFIRM - confirmations
MSG_BUG - bugs
-- alternative_voice_message: not obligatory parameter that allows to send longer messages to TTS-engine
-- tts_text: not obligatory parameter that allows to send longer messages to TTS-engine
if not defined, the default 'text' will be TTS-voiced
]]

function InfoMessage:inform(text, msec, refresh_mode, message_importance, alternative_voice_message)
-- temporary test for 'message_importance'; it might be further removed as soon
-- as every message will be properly marked by 'importance'
if not message_importance then message_importance = 5 end
local popup, voice = InfoMessage:getMethodForEvent(message_importance)
function InfoMessage:inform(text, delay, refresh_mode, msgtype, tts_text)
local popup, voice = InfoMessage:getMethodForEvent(msgtype)
if voice then
alternative_voice_message = alternative_voice_message or text
say(alternative_voice_message)
-- here one may set pause -- it might be useful only if one sound message
-- is directly followed by another, otherwise it's just wasting of time.
--[[ if msec and msec ~=0 then
-- pause = 0.5sec + 40ms/character * string.len() / normalized_voice_speed
util.usleep(500000 + 40*alternative_voice_message:len*10000/self.TTSspeed)
end ]]
end
if not popup then return end -- to avoid drawing popup window
self.ImageFile = self.Images[message_importance] -- select proper image for window
if util.isEmulated()==1 and msec == DINFO_TIMEOUT_FAST then
msec = 300
say(tts_text or text)
end
if not msec or msec == 0 then
if not popup then return end
self.ImageFile = self.Images[msgtype]
if delay == DINFO_NODELAY then
InfoMessage:show(text, refresh_mode)
if util.isEmulated() == 1 then util.usleep(500000) end
elseif delay == DINFO_DELAY then
showInfoMsgWithDelay(text, delay, refresh_mode)
else
if msec < 0 then msec = 500 + string.len(text) * 50 end
showInfoMsgWithDelay(text, msec, refresh_mode)
Debug("InfoMessage:inform(), unrecognized delay=", delay)
end
end

Expand Down Expand Up @@ -180,10 +157,10 @@ function InfoMessage:chooseEventForMethod(event)
event = event or 0
local item_no = 0
local event_list = {
"Messages (e.g. 'Scanning folder...')",
"Messages (e.g. 'Battery logging on')",
"Warnings (e.g. 'Already first jump!')",
"Errors (e.g. 'Zip contains improper content!')",
"Confirmations (e.g. 'Press Y to confirm deleting')",
"Confirmations (e.g. 'Press Y to confirm')",
"Bugs",
}
while item_no ~= event and item_no < #event_list do
Expand Down
20 changes: 10 additions & 10 deletions filechooser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ function FileChooser:addAllCommands()
if InfoMessage.InfoMethod[MSG_CONFIRM] == 0 then -- silent regime
self:deleteFileAtPosition(pos)
else
InfoMessage:inform("Press 'Y' to confirm ", nil, 0, MSG_CONFIRM)
InfoMessage:inform("Press 'Y' to confirm ", DINFO_NODELAY, 0, MSG_CONFIRM)
if ReturnKey() == KEY_Y then self:deleteFileAtPosition(pos) end
end
elseif self.dirs[pos] == ".." then
Expand All @@ -433,7 +433,7 @@ function FileChooser:addAllCommands()
if InfoMessage.InfoMethod[MSG_CONFIRM] == 0 then -- silent regime
self:deleteFolderAtPosition(pos)
else
InfoMessage:inform("Press 'Y' to confirm ", nil, 0, MSG_CONFIRM)
InfoMessage:inform("Press 'Y' to confirm ", DINFO_NODELAY, 0, MSG_CONFIRM)
if ReturnKey() == KEY_Y then self:deleteFolderAtPosition(pos) end
end
end
Expand Down Expand Up @@ -515,11 +515,11 @@ function FileChooser:addAllCommands()
end
)
self.commands:add(KEY_S, nil, "S",
"search among files",
"search files (single space matches all)",
function(self)
local keywords = InputBox:input(0, 0, "Search:")
if keywords then
InfoMessage:inform("Searching... ", DINFO_TIMEOUT_FAST, 1, MSG_AUX)
InfoMessage:inform("Searching... ", DINFO_NODELAY, 1, MSG_AUX)
FileSearcher:init( self.path )
FileSearcher:choose(keywords)
end
Expand All @@ -535,7 +535,7 @@ function FileChooser:addAllCommands()
local fn = self.files[self.perpage*(self.page-1)+self.current - #self.dirs]
os.execute("cp "..InQuotes(DocToHistory(file)).." "
..InQuotes(DocToHistory(self.clipboard.."/"..fn)) )
InfoMessage:inform("File copied to clipboard ", DINFO_TIMEOUT_FAST, 1, MSG_WARN)
InfoMessage:inform("File copied to clipboard ", DINFO_DELAY, 1, MSG_WARN)
end
end
)
Expand All @@ -548,7 +548,7 @@ function FileChooser:addAllCommands()
local fn = self.files[self.perpage*(self.page-1)+self.current - #self.dirs]
os.rename(file, self.clipboard.."/"..fn)
os.rename(DocToHistory(file), DocToHistory(self.clipboard.."/"..fn))
InfoMessage:inform("File moved to clipboard ", DINFO_TIMEOUT_FAST, 0, MSG_WARN)
InfoMessage:inform("File moved to clipboard ", DINFO_DELAY, 0, MSG_WARN)
local pos = self.perpage*(self.page-1)+self.current
table.remove(self.files, pos-#self.dirs)
self.items = self.items - 1
Expand All @@ -562,7 +562,7 @@ function FileChooser:addAllCommands()
function(self)
-- TODO (NuPogodi, 27.09.12): first test whether the clipboard is empty & answer respectively
-- TODO (NuPogodi, 27.09.12): overwrite?
InfoMessage:inform("Moving files from clipboard...", DINFO_TIMEOUT_FAST, 0, MSG_AUX)
InfoMessage:inform("Moving files from clipboard...", DINFO_NODELAY, 0, MSG_AUX)
for f in lfs.dir(self.clipboard) do
if lfs.attributes(self.clipboard.."/"..f, "mode") == "file" then
os.rename(self.clipboard.."/"..f, self.path.."/"..f)
Expand All @@ -577,7 +577,7 @@ function FileChooser:addAllCommands()
"toggle battery level logging",
function(self)
G_battery_logging = not G_battery_logging
InfoMessage:inform("Battery logging "..(G_battery_logging and "ON " or "OFF "), DINFO_TIMEOUT_FAST, 1, MSG_AUX)
InfoMessage:inform("Battery logging "..(G_battery_logging and "on " or "off "), DINFO_DELAY, 1, MSG_AUX)
G_reader_settings:saveSetting("G_battery_logging", G_battery_logging)
end
)
Expand Down Expand Up @@ -679,7 +679,7 @@ function FileChooser:deleteFolderAtPosition(pos)
self.items = #self.dirs + #self.files
self.current, self.page = gotoTargetItem(pos, self.items, pos, self.page, self.perpage)
else
InfoMessage:inform("Directory not empty ", DINFO_TIMEOUT_SLOW, 1, MSG_ERROR)
InfoMessage:inform("Directory not empty ", DINFO_DELAY, 1, MSG_ERROR)
end
end

Expand Down Expand Up @@ -709,6 +709,6 @@ function gotoTargetItem(target_item, all_items, current_item, current_page, perp
end

function warningUnsupportedFunction()
InfoMessage:inform("Unsupported function ", DINFO_TIMEOUT_SLOW, 1, MSG_WARN)
InfoMessage:inform("Unsupported function ", DINFO_DELAY, 1, MSG_WARN)
return nil
end
10 changes: 4 additions & 6 deletions filehistory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ function FileHistory:addAllCommands()
self.keywords = old_keywords
end
if #self.result < 1 then
InfoMessage:inform("No search hits ", DINFO_TIMEOUT_SLOW, 1, MSG_WARN,
"The search has given no results")
InfoMessage:inform("No search hits ", DINFO_DELAY, 1, MSG_WARN)
-- restoring the original data
self.result = old_data
self.items = #self.result
Expand Down Expand Up @@ -238,7 +237,7 @@ function FileHistory:addAllCommands()
self.page = math.floor(item_no / self.perpage) + 1
self.pagedirty = true
else
InfoMessage:inform("File does not exist ", DINFO_TIMEOUT_SLOW, 1, MSG_ERROR)
InfoMessage:inform("File does not exist ", DINFO_DELAY, 1, MSG_ERROR)
end
end
)
Expand All @@ -252,8 +251,7 @@ function FileHistory:addAllCommands()
self:init()
self:setSearchResult(self.keywords)
else
InfoMessage:inform("Press 'Y' to confirm ", nil, 0, MSG_CONFIRM,
"Please, press key Y to delete the book history")
InfoMessage:inform("Press 'Y' to confirm ", DINFO_NODELAY, 0, MSG_CONFIRM)
if ReturnKey() == KEY_Y then
os.remove(DocToHistory(file_to_del))
self:init()
Expand Down Expand Up @@ -287,7 +285,7 @@ function FileHistory:choose(keywords)

-- NuPogodi, 30.09.12: immediate quit (no redraw), if empty
if self:setSearchResult(keywords) < 1 then
InfoMessage:inform("No reading history ", DINFO_TIMEOUT_SLOW, 1, MSG_WARN, "The reading history is empty!")
InfoMessage:inform("No reading history ", DINFO_DELAY, 1, MSG_WARN)
return nil
end

Expand Down
Loading

0 comments on commit 6090896

Please sign in to comment.