Add dark mode - crossword.koplugin - Unnamed repository; edit this file 'description' to name the repository. (DIR) Log (DIR) Files (DIR) Refs (DIR) Submodules (DIR) README (DIR) LICENSE --- (DIR) commit 90b722901483a0b8d977de2865dafc4fd1aa004b (DIR) parent 06f63fda7640575ff2fafbd3b7cac66d595f1df4 (HTM) Author: Scarlett <social@scarlettmcallister.com> Date: Fri, 23 Dec 2022 22:24:15 -0400 Add dark mode Diffstat: M gameview.lua | 37 ++++++++++++++++++++++++------- M gridsquare.lua | 38 +++++++++++++++++++------------ M gridview.lua | 8 ++++++-- M main.lua | 7 ++++--- M softkeyboard.lua | 22 ++++++++++++++++++---- 5 files changed, 81 insertions(+), 31 deletions(-) --- (DIR) diff --git a/gameview.lua b/gameview.lua @@ -13,6 +13,7 @@ local VerticalGroup = require("ui/widget/verticalgroup") local UIManager = require("ui/uimanager") local logger = require("logger") local _ = require("gettext") +local ffi = require("ffi") local GridView = require("gridview") local SoftKeyboard = require("softkeyboard") @@ -25,6 +26,7 @@ local GameView = InputContainer:new{ active_direction = Solve.DOWN, active_row_num = nil, active_col_num = nil, + dark_mode = nil, } function GameView:init() @@ -56,6 +58,7 @@ function GameView:render() width = Screen:getWidth(), clue_value = self.active_clue, inputbox = self, + dark_mode = self.dark_mode, } -- Calculate grid height. Note that grid_height should not exceed screen width. local screen_h_minus_keyboard_h = Screen:getHeight() - self.keyboard_view.dimen.h @@ -71,6 +74,7 @@ function GameView:render() rows = self.puzzle.size.rows }, grid = self.puzzle:getGrid(), + dark_mode = self.dark_mode, on_tap_callback = function(row_num, col_num) -- On tap, pass the row and col nums to the active puzzle and return -- a clue based on the active direction (i.e.: across or down) @@ -83,16 +87,19 @@ function GameView:render() end, } -- Build the container. + + local gray = ffi.typeof("Color8")(0x22) + self[1] = FrameContainer:new{ width = self.dimen.w, height = self.dimen.h, padding = 0, margin = 0, bordersize = 0, - background = Blitbuffer.COLOR_BLACK, + background = (self.dark_mode and gray or Blitbuffer.COLOR_BLACK), VerticalGroup:new{ align = "center", - background = Blitbuffer.COLOR_GRAY, + background = (self.dark_mode and gray or Blitbuffer.COLOR_GRAY), CenterContainer:new{ dimen = Geom:new{ w = self.dimen.w, @@ -163,7 +170,7 @@ end -- Can reliably accept 1 or -1. Doesn't work well for other numbers. function GameView:movePointer(steps) -- Temp variables for the row and col nums are used here because the values produced by the - -- next if/else block results in two courses of actions. + -- next if/else block results in two courses of actions. local temp_row_num = self.active_row_num local temp_col_num = self.active_col_num @@ -175,10 +182,10 @@ function GameView:movePointer(steps) -- Check to see if advancement landed on a non-active grid square. -- If it did, then move the user to the next solve. if steps >= 1 and (temp_row_num > self.puzzle.size.rows or - temp_col_num > self.puzzle.size.cols or + temp_col_num > self.puzzle.size.cols or not self.puzzle:getClueByPos(temp_row_num, temp_col_num, self.active_direction)) then self:rightChar() - steps = steps - 1 + steps = steps - 1 self:movePointer(steps) elseif steps <= -1 and (temp_row_num < 1 or temp_col_num < 1 or @@ -186,7 +193,7 @@ function GameView:movePointer(steps) self:leftChar() steps = steps + 1 self:movePointer(steps) - else + else self.active_row_num = temp_row_num self.active_col_num = temp_col_num end @@ -278,11 +285,25 @@ function GameView:showGameMenu() text = _("Reveal Puzzle"), enabled = false, callback = function() - + end, }, }, { + { + text = _("Toggle Dark Mode"), + callback = function() + if self.dark_mode then + self.dark_mode = false + else + self.dark_mode = true + end + self:render() + UIManager:close(game_dialog) + end, + }, + }, + { { text = _("Save"), callback = function() @@ -293,7 +314,7 @@ function GameView:showGameMenu() { text = _("Exit"), callback = function() - self.puzzle:save() + self.puzzle:save() UIManager:close(game_dialog) UIManager:close(game_view) end, (DIR) diff --git a/gridsquare.lua b/gridsquare.lua @@ -11,6 +11,7 @@ local Size = require("ui/size") local TextWidget = require("ui/widget/textwidget") local TextBoxWidget = require("ui/widget/textboxwidget") local logger = require("logger") +local ffi = require("ffi") local Guess = require("guess") @@ -24,6 +25,7 @@ local GridSquare = InputContainer:extend{ number_font_size = nil, letter_value = nil, number_value = nil, + dark_mode = nil, } function GridSquare:init() @@ -33,35 +35,41 @@ function GridSquare:init() -- and which square is selected (state 2) local state_bg_color if self.state == "1" then - state_bg_color = Blitbuffer.COLOR_LIGHT_GRAY + state_bg_color = self.dark_mode and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_LIGHT_GRAY elseif self.state == "2" then - state_bg_color = Blitbuffer.COLOR_DARK_GRAY + state_bg_color = self.dark_mode and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_DARK_GRAY end + + local text_fg_color = self.dark_mode and Blitbuffer.COLOR_WHITE or Blitbuffer.COLOR_BLACK + local bg_default_on = self.dark_mode and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_WHITE + local bg_default_off = self.dark_mode and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_BLACK -- Set up the right bg color, letter, etc. local bg_color = self.letter_value ~= "." and - (state_bg_color and state_bg_color or Blitbuffer.COLOR_WHITE) or - Blitbuffer.COLOR_BLACK + (state_bg_color and state_bg_color or bg_default_on) or + bg_default_off -- The status bg is used to indicate whether the letter is correct or incorrect. local status_bg_color, status_height = 0 if self.status == Guess.STATUS.CHECKED_INCORRECT then - status_bg_color = Blitbuffer.COLOR_BLACK + status_bg_color = self.dark_mode and Blitbuffer.COLOR_WHITE or Blitbuffer.COLOR_BLACK status_height = 2 elseif self.letter_value == "." then - -- Set the color to black on non-letter squares - status_bg_color = Blitbuffer.COLOR_BLACK + -- Set the color to black on non-letter squares + local gray = ffi.typeof("Color8")(0x22) + status_bg_color = self.dark_mode and gray or Blitbuffer.COLOR_BLACK + text_fg_color = status_bg_color + bg_color = status_bg_color else - -- Everything else goes white - status_bg_color = Blitbuffer.COLOR_WHITE + -- Everything else goes to default text color + status_bg_color = (self.dark_mode and Blitbuffer.COLOR_WHITE or Blitbuffer.COLOR_BLACK) end self.letter_font_size = TextBoxWidget:getFontSizeToFitHeight(self.height, 1, 0.3) self.number_font_size = self.letter_font_size / 2 - -- Maybe a letter input by the player. self.letter_widget = TextWidget:new{ text = self.letter_value, face = Font:getFace(self.letter_font_face, self.letter_font_size), - fgcolor = Blitbuffer.COLOR_BLACK, + fgcolor = text_fg_color, padding = 0, bold = true, } @@ -69,7 +77,7 @@ function GridSquare:init() self.number_widget = TextWidget:new{ text = self.number_value, face = Font:getFace(self.number_font_face, self.number_font_size), - fgcolor = Blitbuffer.COLOR_BLACK, + fgcolor = text_fg_color, padding = 0, bold = true, } @@ -90,17 +98,19 @@ function GridSquare:init() end }, } + local border_color = self.dark_mode and (self.state == "2" and Blitbuffer.COLOR_WHITE or Blitbuffer.COLOR_DIM_GRAY) or Blitbuffer.COLOR_BLACK -- This is the container for the letter and number. self[1] = FrameContainer:new{ width = self.width - self.margin * 2, height = self.height - self.margin * 2, - color = Blitbuffer.COLOR_WHITE, + color = (self.dark_mode and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_WHITE), background = bg_color, padding = 0, margin = 0, margin_left = margin, --margin = self.margin or 0, - bordersize = 0, + bordersize = self.dark_mode and (self.state and 1 or 0) or 0, + color = border_color, OverlapGroup:new{ dimen = { w = self.width - self.margin, h = self.height - self.margin }, padding = 0, (DIR) diff --git a/gridview.lua b/gridview.lua @@ -9,6 +9,7 @@ local InputContainer = require("ui/widget/container/inputcontainer") local VerticalGroup = require("ui/widget/verticalgroup") local UIManager = require("ui/uimanager") local logger = require("logger") +local ffi = require("ffi") local SoftKeyboard = require("softkeyboard") local GridRow = require("gridrow") @@ -22,7 +23,8 @@ local GridView = InputContainer:new{ cols = nil, rows = nil, }, - on_tap_callback = nil + on_tap_callback = nil, + dark_mode = nil, } function GridView:init() @@ -59,6 +61,7 @@ function GridView:render() table.insert(self.rows_view, row) end -- Build the container. + local gray = ffi.typeof("Color8")(0x22) self[1] = FrameContainer:new{ width = self.inner_dimen.w, height = self.inner_dimen.h, @@ -66,7 +69,7 @@ function GridView:render() padding_bottom = 0, margin = 0, bordersize = 0, - background = Blitbuffer.COLOR_BLACK, + background = (self.dark_mode and gray or Blitbuffer.COLOR_BLACK), VerticalGroup:new{ align = "center", -- Add the rows vertical group. @@ -101,6 +104,7 @@ function GridView:buildRow(squares, row_num) status = square.status, row_num = row_num, -- we pass the row and col so that col_num = col_num, -- the tap callback can propagate values back + dark_mode = self.dark_mode, on_tap_callback = function(row_num, col_num) self.on_tap_callback(row_num, col_num) end (DIR) diff --git a/main.lua b/main.lua @@ -14,6 +14,7 @@ local LuaSettings = require("frontend/luasettings") local logger = require("logger") local json = require("json") local util = require("util") +local ffi = require("ffi") local _ = require("gettext") local Solve = require("solve") @@ -88,7 +89,7 @@ function Crossword:getSubMenuItems() if continue_puzzle_item then table.insert(sub_menu_items, 1, continue_puzzle_item) end if history_menu_items then table.insert(sub_menu_items, 2, history_menu_items[1]) end - + return sub_menu_items end @@ -131,14 +132,14 @@ function Crossword:showLibraryView() onSelectPuzzle = function(item) local puzzle = Puzzle:initializePuzzle(("%s/%s"):format(item.path_to_dir, item.filename)) -- Because the puzzle is added to history, it needs to also be saved. Otherwise, when - -- it's loaded from history there will be no content to restore. + -- it's loaded from history there will be no content to restore. puzzle:save() local history = History:new{} history:init() history:add(puzzle.id, puzzle.title) self:showGameView(puzzle) end} - + library:showDirectoryView(self.puzzle_dir) end --[[-- (DIR) diff --git a/softkeyboard.lua b/softkeyboard.lua @@ -38,6 +38,7 @@ local VirtualKey = InputContainer:new{ keyboard = nil, callback = nil, bg_color = nil, + fg_color = nil, -- This is to inhibit the key's own refresh (useful to avoid conflicts on Layer changing keys) skiptap = nil, skiphold = nil, @@ -92,6 +93,7 @@ function VirtualKey:init() text = self.label, face = self.face, bold = self.bold or false, + fgcolor = self.fg_color } -- Make long labels fit by decreasing font size local max_width = self.width - 2*self.bordersize - 2*Size.padding.small @@ -103,6 +105,7 @@ function VirtualKey:init() text = self.label, face = Font:getFace(self.face.orig_font, new_size), bold = self.bold or false, + fgcolor = self.fg_color } end @@ -354,7 +357,7 @@ function VirtualKeyboard:render(keys_layout, clue_layout) local keyboard_frame = FrameContainer:new{ margin = 0, bordersize = Size.border.default, - background = G_reader_settings:nilOrTrue("keyboard_key_border") and Blitbuffer.COLOR_LIGHT_GRAY or Blitbuffer.COLOR_WHITE, + background = self.dark_mode and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_LIGHT_GRAY, radius = 0, padding = self.padding, allow_mirroring = false, @@ -389,11 +392,15 @@ function VirtualKeyboard:buildClue(clue_value) local h_key_padding = HorizontalSpan:new{width = self.key_padding} local v_key_padding = VerticalSpan:new{width = self.key_padding} + local bg_color = self.dark_mode and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_LIGHT_GRAY + local fg_color = self.dark_mode and Blitbuffer.COLOR_WHITE or Blitbuffer.COLOR_BLACK + local virtual_key_left = VirtualKey:new{ key = "←", key_chars = {"←"}, label = "←", - bg_color = Blitbuffer.COLOR_LIGHT_GRAY, + bg_color = bg_color, + fg_color = fg_color, keyboard = self, width = self.base_key_width, height = self.base_key_height, @@ -402,7 +409,8 @@ function VirtualKeyboard:buildClue(clue_value) key = "→", key_chars = {"→"}, label = "→", - bg_color = Blitbuffer.COLOR_LIGHT_GRAY, + bg_color = bg_color, + fg_color = fg_color, keyboard = self, width = self.base_key_width, height = self.base_key_height, @@ -412,7 +420,8 @@ function VirtualKeyboard:buildClue(clue_value) key = "direction", key_chars = {"direction"}, label = clue_value, - bg_color = Blitbuffer.COLOR_LIGHT_GRAY, + bg_color = bg_color, + fg_color = fg_color, keyboard = self, width = clue_row_width, height = self.base_key_height, @@ -434,6 +443,9 @@ function VirtualKeyboard:buildKeys() local h_key_padding = HorizontalSpan:new{width = self.key_padding} local v_key_padding = VerticalSpan:new{width = self.key_padding} local vertical_group = VerticalGroup:new{ allow_mirroring = false } + + local bg_color = self.dark_mode and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_WHITE + local fg_color = self.dark_mode and Blitbuffer.COLOR_WHITE or Blitbuffer.COLOR_BLACK -- Loop through each key row. for i = 1, #self.KEYS do local horizontal_group = HorizontalGroup:new{ allow_mirroring = false } @@ -464,6 +476,8 @@ function VirtualKeyboard:buildKeys() key_chars = key_chars, icon = self.KEYS[i][j].icon, label = label, + bg_color = bg_color, + fg_color = fg_color, alt_label = alt_label, bold = self.KEYS[i][j].bold, keyboard = self,