From 85a16d0064c1d00ab753198bdf527ccf5fab754b Mon Sep 17 00:00:00 2001 From: poire-z Date: Sat, 29 Aug 2020 18:25:38 +0200 Subject: [PATCH] Open with: add Text Editor plugin --- frontend/apps/filemanager/filemanager.lua | 13 ++++++++++-- frontend/ui/widget/checkbutton.lua | 20 ++++++++++++++++++ frontend/ui/widget/filechooser.lua | 18 +++++++++++++++- frontend/ui/widget/inputdialog.lua | 2 +- frontend/ui/widget/openwithdialog.lua | 25 +++++++++++++++++++++++ frontend/ui/widget/radiobuttontable.lua | 4 ++++ plugins/texteditor.koplugin/main.lua | 1 + 7 files changed, 79 insertions(+), 4 deletions(-) diff --git a/frontend/apps/filemanager/filemanager.lua b/frontend/apps/filemanager/filemanager.lua index 5fb1e454c..3d712c1b1 100644 --- a/frontend/apps/filemanager/filemanager.lua +++ b/frontend/apps/filemanager/filemanager.lua @@ -356,10 +356,19 @@ function FileManager:init() table.insert(buttons, { { text = _("Open with…"), - enabled = DocumentRegistry:getProviders(file) == nil or #(DocumentRegistry:getProviders(file)) > 1, + enabled = DocumentRegistry:getProviders(file) == nil or #(DocumentRegistry:getProviders(file)) > 1 or fileManager.texteditor, callback = function() UIManager:close(self.file_dialog) - self:showSetProviderButtons(file, FileManager.instance, ReaderUI) + local one_time_providers = {} + if fileManager.texteditor then + table.insert(one_time_providers, { + provider_name = _("Text editor"), + callback = function() + fileManager.texteditor:checkEditFile(file) + end, + }) + end + self:showSetProviderButtons(file, FileManager.instance, ReaderUI, one_time_providers) end, }, { diff --git a/frontend/ui/widget/checkbutton.lua b/frontend/ui/widget/checkbutton.lua index 207841f26..f5664a3a3 100644 --- a/frontend/ui/widget/checkbutton.lua +++ b/frontend/ui/widget/checkbutton.lua @@ -14,6 +14,7 @@ Example: ]] +local Blitbuffer = require("ffi/blitbuffer") local CheckMark = require("ui/widget/checkmark") local Device = require("device") local Font = require("ui/font") @@ -50,6 +51,7 @@ function CheckButton:initCheckButton(checked) self.checked = checked self._checkmark = CheckMark:new{ checked = self.checked, + enabled = self.enabled, parent = self.parent or self, show_parent = self.show_parent or self, } @@ -57,6 +59,7 @@ function CheckButton:initCheckButton(checked) text = self.text, face = self.face, max_width = self.max_width, + fgcolor = self.enabled and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_DARK_GRAY, } self._horizontalgroup = HorizontalGroup:new{ self._checkmark, @@ -143,4 +146,21 @@ function CheckButton:unCheck() end) end +function CheckButton:enable() + self.enabled = true + self:initCheckButton(self.checked) + UIManager:setDirty(self.parent, function() + return "ui", self.dimen + end) +end + +function CheckButton:disable() + self.enabled = false + self:initCheckButton(false) + UIManager:setDirty(self.parent, function() + return "ui", self.dimen + -- best to use "ui" instead of "fast" when we make things gray + end) +end + return CheckButton diff --git a/frontend/ui/widget/filechooser.lua b/frontend/ui/widget/filechooser.lua index 24189ade1..be14e3374 100644 --- a/frontend/ui/widget/filechooser.lua +++ b/frontend/ui/widget/filechooser.lua @@ -455,7 +455,7 @@ function FileChooser:getNextFile(curr_file) return next_file end -function FileChooser:showSetProviderButtons(file, filemanager_instance, reader_ui) +function FileChooser:showSetProviderButtons(file, filemanager_instance, reader_ui, one_time_providers) local __, filename_pure = util.splitFilePathName(file) local filename_suffix = util.getFileNameSuffix(file) @@ -486,6 +486,17 @@ function FileChooser:showSetProviderButtons(file, filemanager_instance, reader_u }, }) end + if one_time_providers and #one_time_providers > 0 then + for ___, provider in ipairs(one_time_providers) do + provider.one_time_provider = true + table.insert(radio_buttons, { + { + text = provider.provider_name, + provider = provider, + }, + }) + end + end table.insert(buttons, { { @@ -499,6 +510,11 @@ function FileChooser:showSetProviderButtons(file, filemanager_instance, reader_u is_enter_default = true, callback = function() local provider = self.set_provider_dialog.radio_button_table.checked_button.provider + if provider.one_time_provider then + UIManager:close(self.set_provider_dialog) + provider.callback() + return + end -- always for this file if self.set_provider_dialog._check_file_button.checked then diff --git a/frontend/ui/widget/inputdialog.lua b/frontend/ui/widget/inputdialog.lua index 6c6faeba6..da07d770f 100644 --- a/frontend/ui/widget/inputdialog.lua +++ b/frontend/ui/widget/inputdialog.lua @@ -490,7 +490,7 @@ end function InputDialog:onCloseWidget() self:onClose() - UIManager:setDirty(nil, function() + UIManager:setDirty(nil, self.fullscreen and "full" or function() return "partial", self.dialog_frame.dimen end) end diff --git a/frontend/ui/widget/openwithdialog.lua b/frontend/ui/widget/openwithdialog.lua index afa52de76..441f2f01d 100644 --- a/frontend/ui/widget/openwithdialog.lua +++ b/frontend/ui/widget/openwithdialog.lua @@ -13,6 +13,7 @@ local LeftContainer = require("ui/widget/container/leftcontainer") local LineWidget = require("ui/widget/linewidget") local RadioButtonTable = require("ui/widget/radiobuttontable") local Size = require("ui/size") +local TextBoxWidget = require("ui/widget/textboxwidget") local UIManager = require("ui/uimanager") local VerticalGroup = require("ui/widget/verticalgroup") local VerticalSpan = require("ui/widget/verticalspan") @@ -24,6 +25,21 @@ local OpenWithDialog = InputDialog:extend{} function OpenWithDialog:init() -- init title and buttons in base class InputDialog.init(self) + + -- replace single line title with a multiline one, + -- as the filename might be long + self.title_widget:free() + self.title_widget = FrameContainer:new{ + padding = self.title_padding, + margin = self.title_margin, + bordersize = 0, + TextBoxWidget:new{ + text = self.title, + width = self.width - 2*self.title_padding - 2*self.title_margin, + face = self.title_face, + }, + } + self.face = Font:getFace("cfont", 22) self.radio_button_table = RadioButtonTable:new{ @@ -33,6 +49,15 @@ function OpenWithDialog:init() scroll = false, parent = self, face = self.face, + button_select_callback = function(btn) + if btn.provider.one_time_provider then + self._check_file_button:disable() + self._check_global_button:disable() + else + self._check_file_button:enable() + self._check_global_button:enable() + end + end } self._check_file_button = self._check_file_button or CheckButton:new{ diff --git a/frontend/ui/widget/radiobuttontable.lua b/frontend/ui/widget/radiobuttontable.lua index e70e720b2..f4a17d8ea 100644 --- a/frontend/ui/widget/radiobuttontable.lua +++ b/frontend/ui/widget/radiobuttontable.lua @@ -27,6 +27,7 @@ local RadioButtonTable = FocusManager:new{ face = Font:getFace("cfont", 22), _first_button = nil, checked_button = nil, + button_select_callback = nil, } function RadioButtonTable:init() @@ -72,6 +73,9 @@ function RadioButtonTable:init() } local button_callback = function() self:_checkButton(button) + if self.button_select_callback then + self.button_select_callback(btn_entry) + end end button.callback = button_callback diff --git a/plugins/texteditor.koplugin/main.lua b/plugins/texteditor.koplugin/main.lua index 439cc4bfb..7172aa6b3 100644 --- a/plugins/texteditor.koplugin/main.lua +++ b/plugins/texteditor.koplugin/main.lua @@ -329,6 +329,7 @@ function TextEditor:chooseFile() end function TextEditor:checkEditFile(file_path, from_history, possibly_new_file) + self:loadSettings() local attr = lfs.attributes(file_path) if not possibly_new_file and not attr then UIManager:show(ConfirmBox:new{