Skip to content

Commit

Permalink
Add unit convert with highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
zwim committed Aug 27, 2023
1 parent 9894afe commit 6df5e1f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
@@ -1,3 +1,6 @@
Version: 1.2.0
add convert unit to reader highlight button dialog

Version: 1.1.6
adapt to new TitleBar widget

Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -13,6 +13,8 @@ The file `VERSION` is used for update-informations. If you delete it, you don't

You find the calculator in `More tools/calculator` additionally you can set a gesture (in the device submenu) to call it.

If you select a text starting with a number in reader and long press on it (>4 seconds), then you can select `Convert unit`, which directly brings you to unit conversion. (see: https://github.com/koreader/koreader/issues/10834)

Enter a calculation and press ``. You can see your inputs in lines starting with `ixxx:` and the results in lines with `oxxx:`.

If you change something in an old line, this line will be used for the next calculation.
Expand Down
4 changes: 2 additions & 2 deletions VERSION
@@ -1,2 +1,2 @@
Version: 1.1.6
Date: 20220129
Version: 1.2.0
Date: 20230827
5 changes: 2 additions & 3 deletions calculatorconvertdialog.lua
Expand Up @@ -127,7 +127,7 @@ local temperature_table = {

local CalculatorConvertDialog = InputContainer:new{
is_always_active = true,
title = _("Convert"),
title = title or _("Convert"),
modal = true,
width = math.floor(Screen:getWidth() * 0.8),
face = Font:getFace("cfont", 22),
Expand Down Expand Up @@ -275,11 +275,10 @@ function CalculatorConvertDialog:init()
end

self[1] = ButtonDialogTitle:new{
title = _("♺ Convert"),
title = self.title or _("♺ Convert"),
title_align = "center",
buttons = highlight_buttons,
}

end

function CalculatorConvertDialog:onShow()
Expand Down
48 changes: 45 additions & 3 deletions main.lua
Expand Up @@ -61,7 +61,7 @@ local Calculator = WidgetContainer:new{
},
significant_places = 5, -- decimal places
lower_bound = 4, -- switch to scientific if <=10^lower_bound
upper_bound = 6 -- switch to scientific if >=10^upper_bound
upper_bound = 6, -- switch to scientific if >=10^upper_bound
}

function Calculator:init()
Expand All @@ -74,6 +74,23 @@ function Calculator:init()
end
self:onDispatcherRegisterActions()
self.ui.menu:registerToMainMenu(self)

-- Add button to readerhighlight dialog
if self.ui.highlight then
self.ui.highlight:addToHighlightDialog("13_convert", function(this)
return {
text = _("Convert Unit"),
show_in_highlight_dialog_func = function()
return this.selected_text.text:find("^%p*%d+") ~= nil
end,
callback = function()
self:convertUnit(this.selected_text.text)
this:onClose()
end,
}
end)
end

end

function Calculator:addKeyboard()
Expand Down Expand Up @@ -256,6 +273,33 @@ function Calculator:expandTabs(str, num)
return str:gsub("\t",(" "):rep(num))
end

function Calculator:convertUnit(text_containing_unit)
self:onCalculatorStart()

-- delete multiline --
if text_containing_unit:find("\n") then
text_containing_unit = text_containing_unit:sub(1, text_containing_unit:find("\n") - 1)
end
-- get only first number (incl. decimal)
local number_pattern = "%d+[.,]*%d*"
local text_without_unit = text_containing_unit
if text_containing_unit:find(number_pattern) then
text_without_unit = text_containing_unit:sub(text_containing_unit:find(number_pattern))
end

self.history = self.history .. text_without_unit
self.input_dialog:setInputText(self.history)
self.input_dialog._input_widget:goToEndOfLine()

self:calculate(self.history)

self.convert_dialog = CalculatorConvertDialog:new{
parent = self,
title = "♺ Convert: " .. text_containing_unit,
}
UIManager:show(self.convert_dialog)
end

function Calculator:onCalculatorStart()
self.angle_mode = G_reader_settings:readSetting("calculator_angle_mode") or self.angle_mode
self.number_format = G_reader_settings:readSetting("calculator_number_format") or self.number_format
Expand Down Expand Up @@ -296,11 +340,9 @@ function Calculator:onCalculatorStart()
local expand = -1 -- expand tabs with x spaces
self.input_dialog = self:generateInputDialog(self:expandTabs(self.status_line, 1), hint)
local old_height = self.input_dialog.title_bar:getHeight()
print("xxx", old_height, expand)
repeat
expand = expand + 1
self.input_dialog = self:generateInputDialog(self:expandTabs(self.status_line, expand), hint)
print("xxx", old_height, expand )
until (expand > 50 or self.input_dialog.title_bar:getHeight() ~= old_height )

self.input_dialog = self:generateInputDialog(self:expandTabs(self.status_line, expand - 1), hint)
Expand Down

0 comments on commit 6df5e1f

Please sign in to comment.