Text editor: rotate (#12658)

This commit is contained in:
hius07
2024-10-29 20:49:43 +02:00
committed by GitHub
parent b42b9a8799
commit 97a7ebefb9
9 changed files with 127 additions and 153 deletions

View File

@@ -1,4 +1,5 @@
local BD = require("ui/bidi")
local ButtonDialog = require("ui/widget/buttondialog")
local ConfirmBox = require("ui/widget/confirmbox")
local DataStorage = require("datastorage")
local Dispatcher = require("dispatcher")
@@ -438,7 +439,7 @@ function TextEditor:checkEditFile(file_path, from_history, possibly_new_file)
-- No need to warn if readonly, the user will know it when we open
-- without keyboard and the Save button says "Read only".
local readonly = true
local file = io.open(file_path, 'r+b')
local file = io.open(file_path, "r+b")
if file then
file:close()
readonly = false
@@ -474,18 +475,6 @@ function TextEditor:checkEditFile(file_path, from_history, possibly_new_file)
end
end
function TextEditor:readFileContent(file_path)
local file = io.open(file_path, "rb")
if not file then
-- We checked file existence before, so assume it's
-- because it's a new file
return ""
end
local file_content = file:read("*all")
file:close()
return file_content
end
function TextEditor:saveFileContent(file_path, content)
local ok, err = util.writeToFile(content, file_path)
if ok then
@@ -514,7 +503,6 @@ function TextEditor:editFile(file_path, readonly)
local directory, filename = util.splitFilePathName(file_path) -- luacheck: no unused
local filename_without_suffix, filetype = util.splitFileNameSuffix(filename) -- luacheck: no unused
local is_lua = filetype:lower() == "lua"
local input
local para_direction_rtl = nil -- use UI language direction
if self.force_ltr_para_direction then
para_direction_rtl = false -- force LTR
@@ -524,7 +512,7 @@ function TextEditor:editFile(file_path, readonly)
table.insert(buttons_first_row, {
text = _("Lua check"),
callback = function()
local parse_error = util.checkLuaSyntax(input:getInputText())
local parse_error = util.checkLuaSyntax(self.input:getInputText())
if parse_error then
UIManager:show(InfoMessage:new{
text = T(_("Lua syntax check failed:\n\n%1"), parse_error)
@@ -542,16 +530,16 @@ function TextEditor:editFile(file_path, readonly)
text = _("QR"),
callback = function()
UIManager:show(QRMessage:new{
text = input:getInputText(),
text = self.input:getInputText(),
height = Screen:getHeight(),
width = Screen:getWidth()
})
end,
})
end
input = InputDialog:new{
self.input = InputDialog:new{
title = filename,
input = self:readFileContent(file_path),
input = util.readFromFile(file_path, "rb"),
input_face = Font:getFace(self.font_face, self.font_size),
para_direction_rtl = para_direction_rtl,
auto_para_direction = self.auto_para_direction,
@@ -561,6 +549,9 @@ function TextEditor:editFile(file_path, readonly)
cursor_at_end = false,
readonly = readonly,
add_nav_bar = true,
title_bar_left_icon = "appbar.menu",
title_bar_left_icon_tap_callback = function() self:showMenu() end,
rotation_enabled = true,
keyboard_visible = self.show_keyboard_on_start, -- InputDialog will enforce false if readonly
scroll_by_pan = true,
buttons = {buttons_first_row},
@@ -580,10 +571,13 @@ function TextEditor:editFile(file_path, readonly)
end,
-- File restoring callback
reset_callback = function(content) -- Will add a Reset button
return self:readFileContent(file_path), _("Text reset to last saved content")
return util.readFromFile(file_path, "rb") or "", _("Text reset to last saved content")
end,
-- Close callback
close_callback = function()
if self.input.rotation_mode_backup and self.input.rotation_mode_backup ~= Screen:getRotationMode() then
Screen:setRotationMode(self.input.rotation_mode_backup)
end
self:execWhenDoneFunc()
end,
-- File saving callback
@@ -656,9 +650,9 @@ Do you want to keep this file as empty, or do you prefer to delete it?
end,
}
UIManager:show(input)
UIManager:show(self.input)
if self.show_keyboard_on_start and not readonly then
input:onShowKeyboard()
self.input:onShowKeyboard()
end
-- Note about readonly:
-- We might have liked to still show keyboard even if readonly, just
@@ -688,4 +682,32 @@ function TextEditor:quickEditFile(file_path, done_callback, possible_new_file)
self:checkEditFile(file_path, possible_new_file or false)
end
-- TitleBar left button tap
function TextEditor:showMenu()
local dialog
local buttons = {}
local optionsutil = require("ui/data/optionsutil")
for i, mode in ipairs(optionsutil.rotation_modes) do
buttons[i] = {{
text = optionsutil.rotation_labels[i],
enabled_func = function()
return optionsutil.rotation_modes[i] ~= Screen:getRotationMode()
end,
callback = function()
UIManager:close(dialog)
self.input:onSetRotationMode(optionsutil.rotation_modes[i])
end,
}}
end
dialog = ButtonDialog:new{
shrink_unneeded_width = true,
buttons = buttons,
anchor = function()
return self.input.title_bar.left_button.image.dimen
end,
modal = true,
}
UIManager:show(dialog)
end
return TextEditor