From e9bbf9b353a9ccd5f3c97ff94030c9fe38742e6b Mon Sep 17 00:00:00 2001
From: Jens Nolte <jens@nightmarestudio.de>
Date: Sat, 1 Feb 2020 22:20:43 +0100
Subject: [PATCH] Re-implement invalidation of pango-formatted text

---
 src/QBar/BlockOutput.hs | 25 ++++++++++++++++---------
 src/QBar/Core.hs        |  2 +-
 src/QBar/Theme.hs       |  2 +-
 3 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/src/QBar/BlockOutput.hs b/src/QBar/BlockOutput.hs
index 8230f62..b375673 100644
--- a/src/QBar/BlockOutput.hs
+++ b/src/QBar/BlockOutput.hs
@@ -133,23 +133,30 @@ toImportance' (tMax, tCritical, tError, tWarning, tNormal, tMinimal) val
     valueOtherwise tNormal' Nothing v = 1 - logarithmicMatch v tNormal'
 
 
+invalidateBlock :: BlockOutput -> BlockOutput
+invalidateBlock block@BlockOutput{ _fullText, _shortText } = block {
+  _fullText = normalText . rawText $ _fullText,
+  _shortText = normalText . rawText <$> _shortText,
+  _invalid = True
+}
 
-removePango :: BlockText -> T.Text
-removePango (BlockText b) = foldr ((<>) . removePangoFromSegment) "" b
+
+rawText :: BlockText -> T.Text
+rawText (BlockText b) = foldMap rawTextFromSegment b
   where
-    removePangoFromSegment :: BlockTextSegment -> T.Text
-    removePangoFromSegment BlockTextSegment { active=_active, importance=_importance, text } = text
-    removePangoFromSegment (PangoTextSegment text) =
+    rawTextFromSegment :: BlockTextSegment -> T.Text
+    rawTextFromSegment BlockTextSegment{text} = text
+    rawTextFromSegment (PangoTextSegment text) =
       case parsePango text of
         Left _ -> text
         Right parsed -> removeFormatting parsed
 
 printedLength :: BlockText -> Int64
-printedLength (BlockText b) = foldr ((+) . printedLength') 0 b
+printedLength (BlockText b) = sum . map segmentLength $ b
   where
-    printedLength' :: BlockTextSegment -> Int64
-    printedLength' BlockTextSegment { text, active=_, importance=_ } = T.length text
-    printedLength' (PangoTextSegment _) = 0
+    segmentLength :: BlockTextSegment -> Int64
+    segmentLength BlockTextSegment { text } = T.length text
+    segmentLength (PangoTextSegment pango) = either (const $ T.length pango) (T.length . removeFormatting) $ parsePango pango
 
 mkText :: Bool -> Importance -> T.Text -> BlockText
 mkText active importance text = BlockText [BlockTextSegment { text = pangoFriendly text, active, importance }]
diff --git a/src/QBar/Core.hs b/src/QBar/Core.hs
index acb9654..0957f82 100644
--- a/src/QBar/Core.hs
+++ b/src/QBar/Core.hs
@@ -187,7 +187,7 @@ sharedInterval seconds = do
         updateClickHandler Nothing _ = return ()
         updateClickHandler (Just (block, _)) _ = do
           -- Give user feedback that the block is updating
-          let outdatedBlock = block & invalid.~True
+          let outdatedBlock = invalidateBlock block
           -- The invalidated block output has no event handler
           liftIO $ void $ atomically $ send output . Just $ (outdatedBlock, Nothing)
           -- Notify bar about changed block state to display the feedback
diff --git a/src/QBar/Theme.hs b/src/QBar/Theme.hs
index 7490ed2..ff6f294 100644
--- a/src/QBar/Theme.hs
+++ b/src/QBar/Theme.hs
@@ -71,7 +71,7 @@ rainbowTheme time blocks = reverse $ evalState (mapM rainbowBlock $ reverse bloc
   where
     rainbowBlock :: BlockOutput -> State Integer BlockOutput
     rainbowBlock block = do
-      let text = removePango $ block ^. fullText
+      let text = rawText $ block ^. fullText
       let chars = T.unpack . T.reverse $ text
       coloredChars <- mapM rainbowChar chars
       let rainbowText = T.concat . reverse $ coloredChars
-- 
GitLab