Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add color highlight menu #11044

Draft
wants to merge 28 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5e4a437
Add color highlight menu
smasher816 Oct 26, 2023
a6c6b49
LuaCheck'ed
NiLuJe Jan 21, 2024
45cc8b6
Honor the ligthen_factor with colors
NiLuJe Jan 21, 2024
bc3aeab
Random 7AM idea is random
NiLuJe Jan 21, 2024
a022915
More random musings
NiLuJe Jan 21, 2024
9bf0e4e
I can word good now!
NiLuJe Jan 21, 2024
d971a14
This moved to base
NiLuJe Jan 21, 2024
2d9e734
As we effectiuvely only have a single call site for those,
NiLuJe Jan 21, 2024
6e6e9fc
Finally took the opportunity to fix a 10 year old TODO about the
NiLuJe Jan 21, 2024
a158148
Mkay, i'm going to assume the MUL vs. OVER comes from MuPDF...
NiLuJe Jan 21, 2024
0e403a1
Unbreak darkenRect
NiLuJe Jan 22, 2024
bf56868
Drop redudant saveSetting calls
NiLuJe Jan 22, 2024
1d9fd2f
Slightly less awful color highlights in sw nm
NiLuJe Jan 22, 2024
bbb2d9d
Hmm, pretty colors.
NiLuJe Jan 22, 2024
c7a6cf5
Restore colorful bg support in textBoxWidget
NiLuJe Jan 22, 2024
c84de40
Cache the type check
NiLuJe Jan 22, 2024
ed6859d
Add cyan, and make blue actually blue.
NiLuJe Jan 22, 2024
fbde4ed
More logical color order
NiLuJe Jan 22, 2024
8e1a97a
This is always set, no need for a runtime fallback
NiLuJe Jan 22, 2024
32f3fbe
TextBoxWidget: Eh, handle color fgs, too.
NiLuJe Jan 22, 2024
7ba3226
Make Gray our actual legacy grayscale highlights (i.e., it'll follow the
NiLuJe Jan 22, 2024
d314c12
Move the Highlight color menu inside the Highlight Stule dialog
NiLuJe Jan 22, 2024
21d435f
Don't allow changin colors if drawer is invert
NiLuJe Jan 22, 2024
447c92b
Revert "Don't allow changin colors if drawer is invert"
NiLuJe Jan 22, 2024
9d7f70b
Less clunky and more user-friendly warning when you try to modify colors
NiLuJe Jan 22, 2024
e136c07
Might want to drop the bg on actual eInk screens, will have to test
NiLuJe Jan 22, 2024
0c6a453
Don't default to yellow HLs on grayscale screens
NiLuJe Jan 23, 2024
117d18d
Fancy bgcolor in the *other* popup, too
NiLuJe Jan 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
244 changes: 206 additions & 38 deletions frontend/apps/reader/modules/readerhighlight.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local BD = require("ui/bidi")
local BlitBuffer = require("ffi/blitbuffer")
local ButtonDialog = require("ui/widget/buttondialog")
local ConfirmBox = require("ui/widget/confirmbox")
local Device = require("device")
Expand All @@ -18,10 +19,22 @@ local ffiUtil = require("ffi/util")
local time = require("ui/time")
local _ = require("gettext")
local C_ = _.pgettext
local N_ = _.ngettext
local T = ffiUtil.template
local Screen = Device.screen

local ReaderHighlight = InputContainer:extend{}
local ReaderHighlight = InputContainer:extend{
highlight_colors = {
{_("Red"), "red"},
{_("Orange"), "orange"},
{_("Yellow"), "yellow"},
{_("Green"), "green"},
{_("Cyan"), "cyan"},
{_("Blue"), "blue"},
{_("Purple"), "purple"},
{_("Gray"), "gray"},
}
}

local function inside_box(pos, box)
if pos then
Expand Down Expand Up @@ -403,6 +416,55 @@ function ReaderHighlight:addToMainMenu(menu_items)
separator = i == #highlight_style,
})
end
if Screen:isColorScreen() then
table.insert(menu_items.highlight_options.sub_item_table, {
text_func = function()
local saved_color = self.view.highlight.saved_color or "yellow"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Haven't yet looked at the drawing code) but does the presence of "yellow" around here hint that on color devices, highlights will now be yellow by default?
People used to gray will 1) get yellow highlights, or keep getting them gray? and 2) be able to revert to our old same gray?
If yellow, are we fine with force-migrating people to a colorfull world ? Might be fine to advertize the feature - just asking to make us all aware of that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"yellow" is the current color default (when colors are enabled). If you disable color rendering then gray will be used.
I hadn't thought about this being a breaking change, but I suppose it is a change of defaults.
The "Apply default style to all highlights" button could be used to change all old highlights to a new color (including gray), so it is fairly easy to fix (although it is still a step nonetheless)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chosing "Gris" will give the same gray people used to have?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the default gray color to match our legacy default grayscale HL ;).

for __, v in ipairs(self.highlight_colors) do
if v[2] == saved_color then
return T(_("Highlight color: %1"), string.lower(v[1]))
end
end
return T(_("Highlight color: %1"), saved_color)
end,
enabled_func = function()
return Screen:isColorEnabled() and self.view.highlight.saved_drawer ~= "invert"
end,
callback = function(touchmenu_instance)
local default_color = G_reader_settings:readSetting("highlight_color", "yellow")
local saved_color = self.view.highlight.saved_color or "yellow"
local radio_buttons = {}
for _, v in ipairs(self.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,
default_provider = default_color,
callback = function(radio)
self.view.highlight.saved_color = radio.provider
UIManager:setDirty(self.dialog, "ui")
if touchmenu_instance then touchmenu_instance:updateItems() end
end,
})
end,
hold_callback = function(touchmenu_instance)
G_reader_settings:saveSetting("highlight_color", self.view.highlight.saved_color)
UIManager:show(Notification:new{
text = T(_("Default highlight color changed to '%1'."), self.view.highlight.saved_color),
smasher816 marked this conversation as resolved.
Show resolved Hide resolved
})
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 @@ -475,6 +537,40 @@ function ReaderHighlight:addToMainMenu(menu_items)
})
end,
})
if Screen:isColorScreen() then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, we cannot update the drawer on b/w devices? It might be useful.

Copy link
Member

@NiLuJe NiLuJe Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the issues is that the menu entry to choose the color is also hidden, so we'd need to pop quite a few of those checks to allow that, and that includes @poire-z's concerns about the fancy highlight on-press menu ;).

FWIW, I imagine that, even on grayscale devices, one might want to be able to choose the color, for interoperability purposes (esp. on PDF).

But, that's a fairly gnarly can of worms to be opening ;).

table.insert(menu_items.highlight_options.sub_item_table, {
text = _("Apply default style to all highlights"),
enabled_func = function()
return Screen:isColorEnabled()
end,
callback = function(touchmenu_instance)
UIManager:show(ConfirmBox:new{
text = _("Are you sure you want to edit all highlights."),
smasher816 marked this conversation as resolved.
Show resolved Hide resolved
icon = "texture-box",
ok_callback = function()
local count = 0
for _, items in pairs(self.view.highlight.saved) do
if items then
count = count + #items
for i = 1, #items do
local item = items[i]
item.drawer = self.view.highlight.saved_drawer
item.color = self.view.highlight.saved_color
end
end
end
if count > 0 then
UIManager:setDirty(self.dialog, "ui")
UIManager:show(Notification:new{
text = T(N_("Applied default style to 1 highlight",
"Applied default style to %1 highlights", count), count),
})
end
end
})
end,
})
end
if self.document.info.has_pages then
menu_items.panel_zoom_options = {
text = _("Panel zoom (manga/comic)"),
Expand Down Expand Up @@ -977,42 +1073,57 @@ 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() then
table.insert(row, {
text = C_("Highlight", "Color"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it too busy now with a 5 buttons row? Screenshot ?
(No alternative idea, and I don't really want a 3rd row, so let's go with that even if busy.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it looks ok. It doesn't change the button size much. Honestly the Add note button could maybe get renamed to "Note" or something to be a single word for consistency.

PXL_20231101_002015057

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(row of 5 + row of 4 looks a bit ugly. Fortunately, people with non-color devices will continue to get quality UI :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a vague idea of changing the word "Color" to a highlighter pen icon, but that would probably look weirder.

enabled_func = function()
return Screen:isColorEnabled()
end,
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 @@ -1723,7 +1834,6 @@ function ReaderHighlight:onCycleHighlightStyle()
next_style_num = 1
end
self.view.highlight.saved_drawer = highlight_style[next_style_num][2]
self.ui.doc_settings:saveSetting("highlight_drawer", self.view.highlight.saved_drawer)
UIManager:show(Notification:new{
text = T(_("Default highlight style changed to '%1'."), highlight_style[next_style_num][1]),
})
Expand Down Expand Up @@ -1852,6 +1962,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 @@ -2005,6 +2116,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)
smasher816 marked this conversation as resolved.
Show resolved Hide resolved
item.color = color
if self.ui.paging then
self:writePdfAnnotation("save", page, item)
smasher816 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -2035,6 +2168,37 @@ 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", "yellow")
keep_shown_on_apply = true
end
local radio_buttons = {}
for _, v in ipairs(self.highlight_colors) do
table.insert(radio_buttons, {
{
text = v[1],
checked = item_color == v[2],
bgcolor = BlitBuffer.colorFromName(v[1]),
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 @@ -2160,7 +2324,8 @@ function ReaderHighlight:getSavedExtendedHighlightPage(hl_or_bm, page, index)
end
local item = {}
item.datetime = highlight.datetime
item.drawer = highlight.drawer
item.drawer = highlight.drawer or self.highlight.saved_drawer
item.color = highlight.color or self.highlight.saved_color
item.pos0 = highlight.ext[page].pos0
item.pos0.zoom = highlight.pos0.zoom
item.pos0.rotation = highlight.pos0.rotation
Expand All @@ -2175,6 +2340,8 @@ end
function ReaderHighlight:onReadSettings(config)
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 G_reader_settings: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 Down Expand Up @@ -2204,6 +2371,7 @@ end

function ReaderHighlight:onSaveSettings()
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