Skip to content

Commit

Permalink
Add color highlight menu
Browse files Browse the repository at this point in the history
  • Loading branch information
smasher816 committed Oct 28, 2023
1 parent 39d5495 commit 75c15be
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 42 deletions.
191 changes: 156 additions & 35 deletions frontend/apps/reader/modules/readerhighlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,16 @@ local highlight_style = {
{_("Invert"), "invert"},
}

local default_highlight_colors = {
{_("Red"), "#fe4400"},
{_("Orange"), "#ff8800"},
{_("Yellow"), "#fdff32"},
{_("Green"), "#00ad65"},
{_("Blue"), "#00f2ff"},
{_("Purple"), "#ee00ff"},
{_("Gray"), "#808080"},
}

local note_mark = {
{_("None"), "none"},
{_("Underline"), "underline"},
Expand Down Expand Up @@ -390,6 +400,44 @@ function ReaderHighlight:addToMainMenu(menu_items)
separator = i == #highlight_style,
})
end
table.insert(menu_items.highlight_options.sub_item_table, {
text_func = function()
local saved_color = self.view.highlight.saved_color or "#fdff32"
for __, v in ipairs(self.view.highlight.highlight_colors) do
if v[2] == saved_color then
return T(_("Highlight color: %1"), string.lower(v[1]))
end
end
end,
enabled_func = function()
return Screen:isColorScreen() and self.view.highlight.saved_drawer ~= "invert"
end,
callback = function(touchmenu_instance)
local saved_color = self.view.highlight.saved_color or "#fdff32"
local radio_buttons = {}
for _, v in ipairs(self.view.highlight.highlight_colors) do
table.insert(radio_buttons, {
{
text = v[1],
checked = v[2] == saved_color,
provider = v[2],
},
})
end
UIManager:show(require("ui/widget/radiobuttonwidget"):new{
title_text = _("Highlight color"),
width_factor = 0.5,
keep_shown_on_apply = false,
radio_buttons = radio_buttons,
callback = function(radio)
G_reader_settings:saveSetting("highlight_color", radio.provider)
self.view.highlight.saved_color = radio.provider
UIManager:setDirty(self.dialog, "ui")
if touchmenu_instance then touchmenu_instance:updateItems() end
end,
})
end,
})
table.insert(menu_items.highlight_options.sub_item_table, {
text_func = function()
return T(_("Highlight opacity: %1"), G_reader_settings:readSetting("highlight_lighten_factor", 0.2))
Expand Down Expand Up @@ -916,42 +964,54 @@ function ReaderHighlight:showHighlightNoteOrDialog(page, index, bookmark_note)
end

function ReaderHighlight:onShowHighlightDialog(page, index, is_auto_text)
local buttons = {
local row = {
{
{
text = _("Delete"),
callback = function()
self:deleteHighlight(page, index)
UIManager:close(self.edit_highlight_dialog)
self.edit_highlight_dialog = nil
end,
},
{
text = C_("Highlight", "Style"),
callback = function()
self:editHighlightStyle(page, index)
UIManager:close(self.edit_highlight_dialog)
self.edit_highlight_dialog = nil
end,
},
{
text = is_auto_text and _("Add note") or _("Edit note"),
callback = function()
self:editHighlight(page, index)
UIManager:close(self.edit_highlight_dialog)
self.edit_highlight_dialog = nil
end,
},
{
text = "",
callback = function()
self.selected_text = self.view.highlight.saved[page][index]
self:onShowHighlightMenu(page, index)
UIManager:close(self.edit_highlight_dialog)
self.edit_highlight_dialog = nil
end,
},
}
text = _("Delete"),
callback = function()
self:deleteHighlight(page, index)
UIManager:close(self.edit_highlight_dialog)
self.edit_highlight_dialog = nil
end,
},
{
text = C_("Highlight", "Style"),
callback = function()
self:editHighlightStyle(page, index)
UIManager:close(self.edit_highlight_dialog)
self.edit_highlight_dialog = nil
end,
},
}
if Screen:isColorScreen() and Screen:isColorEnabled() then
table.insert(row, {
text = C_("Highlight", "Color"),
callback = function()
self:editHighlightColor(page, index)
UIManager:close(self.edit_highlight_dialog)
self.edit_highlight_dialog = nil
end,
})
end
table.insert(row, {
text = is_auto_text and _("Add note") or _("Edit note"),
callback = function()
self:editHighlight(page, index)
UIManager:close(self.edit_highlight_dialog)
self.edit_highlight_dialog = nil
end,
})
table.insert(row, {
text = "",
callback = function()
self.selected_text = self.view.highlight.saved[page][index]
self:onShowHighlightMenu(page, index)
UIManager:close(self.edit_highlight_dialog)
self.edit_highlight_dialog = nil
end,
})

local buttons = {
row
}

if self.ui.rolling then
Expand Down Expand Up @@ -1721,6 +1781,7 @@ function ReaderHighlight:saveHighlight(extend_to_sentence)
pboxes = self.selected_text.pboxes,
ext = self.selected_text.ext,
drawer = self.view.highlight.saved_drawer,
color = self.view.highlight.saved_color,
chapter = chapter_name,
}
table.insert(self.view.highlight.saved[page], hl_item)
Expand Down Expand Up @@ -1874,6 +1935,28 @@ function ReaderHighlight:editHighlightStyle(page, i)
self:showHighlightStyleDialog(apply_drawer, item.drawer)
end

function ReaderHighlight:editHighlightColor(page, i)
local item = self.view.highlight.saved[page][i]
local apply_color = function(color)
self:writePdfAnnotation("delete", page, item)
item.color = color
if self.ui.paging then
self:writePdfAnnotation("save", page, item)
local bm_note = self.ui.bookmark:getBookmarkNote(item)
if bm_note then
self:writePdfAnnotation("content", page, item, bm_note)
end
end
UIManager:setDirty(self.dialog, "ui")
self.ui:handleEvent(Event:new("BookmarkUpdated",
self.ui.bookmark:getBookmarkForHighlight({
page = self.ui.paging and page or item.pos0,
datetime = item.datetime,
})))
end
self:showHighlightColorDialog(apply_color, item.color)
end

function ReaderHighlight:showHighlightStyleDialog(caller_callback, item_drawer)
local default_drawer, keep_shown_on_apply
if item_drawer then -- called from editHighlightStyle
Expand Down Expand Up @@ -1904,6 +1987,36 @@ function ReaderHighlight:showHighlightStyleDialog(caller_callback, item_drawer)
})
end

function ReaderHighlight:showHighlightColorDialog(caller_callback, item_color)
local default_color, keep_shown_on_apply
if item_color then -- called from editHighlightColor
default_color = self.view.highlight.saved_color or
G_reader_settings:readSetting("highlight_color", "#fdff32")
keep_shown_on_apply = true
end
local radio_buttons = {}
for _, v in ipairs(self.view.highlight.highlight_colors) do
table.insert(radio_buttons, {
{
text = v[1],
checked = item_color == v[2],
provider = v[2],
},
})
end
local RadioButtonWidget = require("ui/widget/radiobuttonwidget")
UIManager:show(RadioButtonWidget:new{
title_text = _("Highlight color"),
width_factor = 0.5,
keep_shown_on_apply = keep_shown_on_apply,
radio_buttons = radio_buttons,
default_provider = default_color,
callback = function(radio)
caller_callback(radio.provider)
end,
})
end

function ReaderHighlight:startSelection()
self.highlight_page, self.highlight_idx = self:saveHighlight()
self.select_mode = true
Expand Down Expand Up @@ -2030,6 +2143,7 @@ function ReaderHighlight:getSavedExtendedHighlightPage(hl_or_bm, page, index)
local item = {}
item.datetime = highlight.datetime
item.drawer = highlight.drawer
item.color = highlight.color
item.pos0 = highlight.ext[page].pos0
item.pos0.zoom = highlight.pos0.zoom
item.pos0.rotation = highlight.pos0.rotation
Expand All @@ -2042,8 +2156,13 @@ function ReaderHighlight:getSavedExtendedHighlightPage(hl_or_bm, page, index)
end

function ReaderHighlight:onReadSettings(config)
G_reader_settings:initializeExtSettings("highlight_colors", default_highlight_colors)
self.view.highlight.highlight_colors = G_reader_settings:readSetting("highlight_colors")
or default_highlight_colors
self.view.highlight.saved_drawer = config:readSetting("highlight_drawer")
or G_reader_settings:readSetting("highlight_drawing_style") or self.view.highlight.saved_drawer
self.view.highlight.saved_color = config:readSetting("highlight_color")
or self.view.highlight.saved_color
self.view.highlight.disabled = G_reader_settings:has("default_highlight_action")
and G_reader_settings:readSetting("default_highlight_action") == "nothing"

Expand All @@ -2070,7 +2189,9 @@ function ReaderHighlight:onUpdateHoldPanRate()
end

function ReaderHighlight:onSaveSettings()
G_reader_settings:saveSetting("highlight_colors", self.view.highlight.highlight_colors)
self.ui.doc_settings:saveSetting("highlight_drawer", self.view.highlight.saved_drawer)
self.ui.doc_settings:saveSetting("highlight_color", self.view.highlight.saved_color)
self.ui.doc_settings:saveSetting("panel_zoom_enabled", self.panel_zoom_enabled)
end

Expand Down
24 changes: 17 additions & 7 deletions frontend/apps/reader/modules/readerview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ function ReaderView:init()
bbox = nil,
}
self.highlight = {
highlight_colors = {},
lighten_factor = G_reader_settings:readSetting("highlight_lighten_factor", 0.2),
note_mark = G_reader_settings:readSetting("highlight_note_marker"),
temp_drawer = "invert",
temp_color = "#fdff32",
temp = {},
saved_drawer = "lighten",
saved_color = "#fdff32",
saved = {},
indicator = nil, -- geom: non-touch highlight position indicator: {x = 50, y=50}
}
Expand Down Expand Up @@ -504,7 +507,7 @@ function ReaderView:drawTempHighlight(bb, x, y)
for i = 1, #boxes do
local rect = self:pageToScreenTransform(page, boxes[i])
if rect then
self:drawHighlightRect(bb, x, y, rect, self.highlight.temp_drawer)
self:drawHighlightRect(bb, x, y, rect, self.highlight.temp_drawer, self.highlight.temp_color)
end
end
end
Expand Down Expand Up @@ -551,12 +554,13 @@ function ReaderView:drawPageSavedHighlight(bb, x, y)
local boxes = self.document:getPageBoxesFromPositions(page, item.pos0, item.pos1)
if boxes then
local drawer = item.drawer or self.highlight.saved_drawer
local color = Blitbuffer.colorFromString(item.color or self.highlight.saved_color)
local draw_note_mark = self.highlight.note_mark and
self.ui.bookmark:getBookmarkNote({datetime = item.datetime})
for _, box in ipairs(boxes) do
local rect = self:pageToScreenTransform(page, box)
if rect then
self:drawHighlightRect(bb, x, y, rect, drawer, draw_note_mark)
self:drawHighlightRect(bb, x, y, rect, drawer, color, draw_note_mark)
if draw_note_mark and self.highlight.note_mark == "sidemark" then
draw_note_mark = false -- side mark in the first line only
end
Expand Down Expand Up @@ -599,11 +603,12 @@ function ReaderView:drawXPointerSavedHighlight(bb, x, y)
local boxes = self.document:getScreenBoxesFromPositions(pos0, pos1, true) -- get_segments=true
if boxes then
local drawer = item.drawer or self.highlight.saved_drawer
local color = Blitbuffer.colorFromString(item.color or self.highlight.saved_color)
local draw_note_mark = self.highlight.note_mark and
self.ui.bookmark:getBookmarkNote({datetime = item.datetime})
for _, box in ipairs(boxes) do
if box.h ~= 0 then
self:drawHighlightRect(bb, x, y, box, drawer, draw_note_mark)
self:drawHighlightRect(bb, x, y, box, drawer, color, draw_note_mark)
if draw_note_mark and self.highlight.note_mark == "sidemark" then
draw_note_mark = false -- side mark in the first line only
end
Expand All @@ -616,18 +621,23 @@ function ReaderView:drawXPointerSavedHighlight(bb, x, y)
end -- end for all saved highlight
end

function ReaderView:drawHighlightRect(bb, _x, _y, rect, drawer, draw_note_mark)
function ReaderView:drawHighlightRect(bb, _x, _y, rect, drawer, color, draw_note_mark)
local x, y, w, h = rect.x, rect.y, rect.w, rect.h
color = color or Blitbuffer.COLOR_GRAY
if drawer == "lighten" then
bb:lightenRect(x, y, w, h, self.highlight.lighten_factor)
local alpha = 0xFF*(self.highlight.lighten_factor or 0.5)
if not Screen:isColorEnabled() then
color = Blitbuffer.Color8A(0, alpha)
end
bb:lightenRect(x, y, w, h, color)
elseif drawer == "underscore" then
bb:paintRect(x, y + h - 1, w, Size.line.medium, Blitbuffer.COLOR_GRAY)
bb:paintRect(x, y + h - 1, w, Size.line.medium, color)
elseif drawer == "strikeout" then
local line_y = y + math.floor(h / 2) + 1
if self.ui.paging then
line_y = line_y + 2
end
bb:paintRect(x, line_y, w, Size.line.medium, Blitbuffer.COLOR_BLACK)
bb:paintRect(x, line_y, w, Size.line.medium, color)
elseif drawer == "invert" then
bb:invertRect(x, y, w, h)
end
Expand Down

0 comments on commit 75c15be

Please sign in to comment.