Implement a different way of adding keybind in lore reader to mark book as read/unread.

Orionik [09-11-22 - 01:06]
Implement a different way of adding keybind in lore reader to mark book as read/unread.
With this new way, we have more control when to add our keybind and we don't add it for unrecognized book. Other addon can then use the same keybind to do something else (TheLibrarium for example)
Filename
Librarian.lua
diff --git a/Librarian.lua b/Librarian.lua
index 407b760..83164c5 100644
--- a/Librarian.lua
+++ b/Librarian.lua
@@ -108,13 +108,6 @@ function Librarian:Initialize(...)
 end

 function Librarian:AddLoreReaderUnreadToggle()
-    local readerKeybinds
-    if LORE_READER.keybindStripDescriptor then
-        readerKeybinds = LORE_READER.keybindStripDescriptor
-    else
-        readerKeybinds = LORE_READER.PCKeybindStripDescriptor
-    end
-
     local function IsSameBook(book, title)
         if book.title then
             return book.title == title
@@ -124,22 +117,27 @@ function Librarian:AddLoreReaderUnreadToggle()
         return bookTitle == title
     end

-    if readerKeybinds then
-        local toggleKeybind =
+    self.loreReaderKeybinds =
+    {
         {
             alignment = KEYBIND_STRIP_ALIGN_RIGHT,
             name = function()
                 local book = self:FindBook(self.lastShownBookId)
-                if not book or (book.unread and IsSameBook(book, LORE_READER.titleText)) then
-                    if self.settings.showUnreadIndicatorInReader then
-                        self.loreReaderUnreadIndicator:SetHidden(false)
+                if not book or not IsSameBook(book, LORE_READER.titleText) then
+                    self.loreReaderUnreadIndicator:SetHidden(true)
+                    return ""
+                else
+                    if book.unread then
+                        if self.settings.showUnreadIndicatorInReader then
+                            self.loreReaderUnreadIndicator:SetHidden(false)
+                        else
+                            self.loreReaderUnreadIndicator:SetHidden(true)
+                        end
+                        return GetString(LIBRARIAN_MARK_READ)
                     else
                         self.loreReaderUnreadIndicator:SetHidden(true)
+                        return GetString(LIBRARIAN_MARK_UNREAD)
                     end
-                    return GetString(LIBRARIAN_MARK_READ)
-                else
-                    self.loreReaderUnreadIndicator:SetHidden(true)
-                    return GetString(LIBRARIAN_MARK_UNREAD)
                 end
             end,
             visible = function()
@@ -151,15 +149,36 @@ function Librarian:AddLoreReaderUnreadToggle()
                 local book = self:FindBook(self.lastShownBookId)
                 if book and IsSameBook(book, LORE_READER.titleText) then
                     self:ToggleReadBook(book)
-                    KEYBIND_STRIP:UpdateKeybindButtonGroup(readerKeybinds)
+                    KEYBIND_STRIP:UpdateKeybindButtonGroup(self.loreReaderKeybinds)
                 end
             end
         }
-        table.insert(readerKeybinds, toggleKeybind)
+    }
+
+    local function OnSceneStateChange(oldState, newState)
+        -- Let's not add the keybind at all if this book is not registered in librarian
+        -- This way, other addon (like Librarium) can override the same keybind for something else
+        local book = self:FindBook(self.lastShownBookId)
+        if not book or not IsSameBook(book, LORE_READER.titleText) then
+            self.loreReaderUnreadIndicator:SetHidden(true)
+            return
+        end

-        self.loreReaderKeybinds = readerKeybinds
+        if newState == SCENE_SHOWING then
+            KEYBIND_STRIP:AddKeybindButtonGroup(self.loreReaderKeybinds)
+        elseif newState == SCENE_HIDDEN then
+            KEYBIND_STRIP:RemoveKeybindButtonGroup(self.loreReaderKeybinds)
+        end
     end

+    -- This list of scene should be the same as the list in esoui/ingame/lorereader/lorereader.lua in its Initialize function
+    LORE_READER_INVENTORY_SCENE:RegisterCallback("StateChange", OnSceneStateChange)
+    LORE_READER_LORE_LIBRARY_SCENE:RegisterCallback("StateChange", OnSceneStateChange)
+    LORE_READER_INTERACTION_SCENE:RegisterCallback("StateChange", OnSceneStateChange)
+    GAMEPAD_LORE_READER_INVENTORY_SCENE:RegisterCallback("StateChange", OnSceneStateChange)
+    GAMEPAD_LORE_READER_LORE_LIBRARY_SCENE:RegisterCallback("StateChange", OnSceneStateChange)
+    GAMEPAD_LORE_READER_INTERACTION_SCENE:RegisterCallback("StateChange", OnSceneStateChange)
+
     self.loreReaderUnreadIndicator = WINDOW_MANAGER:CreateControl("LibrarianLoreReaderUnreadIndicator", ZO_LoreReaderBookContainer, CT_TEXTURE)
     self.loreReaderUnreadIndicator:SetAnchor(TOPLEFT, ZO_LoreReaderBookContainerFirstPage, TOPLEFT, -32, 3)
     self.loreReaderUnreadIndicator:SetAlpha(self.settings.unreadIndicatorTransparency)
@@ -783,9 +802,11 @@ function Librarian.OnShowBook(eventCode, title, body, medium, showTitle, bookId)
     local book = { title = title, body = body, medium = medium, showTitle = showTitle, bookId = bookId }
     LIBRARIAN:AddBook(book, true)
     LIBRARIAN.lastShownBookId = bookId
-    if LIBRARIAN.loreReaderKeybinds then
-        KEYBIND_STRIP:UpdateKeybindButtonGroup(LIBRARIAN.loreReaderKeybinds)
-    end
+
+    -- I don't know why, but if I don't update the keybinds here when opening a book,
+    -- the keybind I add after doesn't work
+    KEYBIND_STRIP:UpdateCurrentKeybindButtonGroups()
+    KEYBIND_STRIP:AddKeybindButtonGroup(LIBRARIAN.loreReaderKeybinds)
 end

 function Librarian.OnMouseEnterRow(control)