Right now chapter titles are completely plain, parsing the tags will allow us to render it properly. Revert chapterTitle addition and add it to seperate branch until its completed Delegating the task of implementing chapter title parsing into a different branch instead of having it incomplete in the main one Merge chtitle into main
72 lines
2.7 KiB
Haskell
72 lines
2.7 KiB
Haskell
{-# LANGUAGE OverloadedStrings, OverloadedLabels #-}
|
|
|
|
module UI (runApp) where
|
|
import qualified GI.Gtk as Gtk
|
|
import qualified GI.Gdk as Gdk
|
|
import qualified GI.Gio as Gio
|
|
import qualified GI.WebKit as WebKit
|
|
import qualified GI.Gtk.Enums as GtkEnums
|
|
|
|
import Control.Monad.Reader (runReaderT)
|
|
import Control.Concurrent.MVar (newMVar, readMVar, swapMVar)
|
|
import Text.HTML.TagSoup (renderTags)
|
|
|
|
import EpubParser (EpubEnv(..), resolveSpine, getChapter, getChapterTitle)
|
|
import Persistence (saveLastRead, loadLastRead)
|
|
|
|
runApp :: EpubEnv -> IO ()
|
|
runApp env = do
|
|
app <- Gtk.applicationNew (Just "com.svitak.reader.v2") []
|
|
|
|
_ <- Gtk.on app #activate $ do
|
|
window <- Gtk.applicationWindowNew app
|
|
Gtk.windowSetDefaultSize window 800 600
|
|
|
|
webView <- WebKit.webViewNew
|
|
Gtk.windowSetChild window (Just webView)
|
|
|
|
spine <- runReaderT resolveSpine env
|
|
let totalChapters = length spine
|
|
|
|
if totalChapters == 0
|
|
then Gtk.windowSetTitle window (Just "Svitak - No Chapters Found")
|
|
else do
|
|
|
|
-- LoadChapter is now inside the 'else' do-block
|
|
let loadChapter index = do
|
|
let maxIdx = totalChapters - 1
|
|
let safeIdx = max 0 (min index maxIdx)
|
|
|
|
writeIORef currentIndex safeIdx
|
|
saveLastRead (bookPath env) safeIdx
|
|
|
|
let filename = spine !! safeIdx
|
|
tags <- runReaderT (getChapter filename) env
|
|
|
|
let chapterTitle = getChapterTitle tags
|
|
Gtk.windowSetTitle window (Just $ "Svitak - " <> chapterTitle)
|
|
|
|
let htmlContent = renderTags tags
|
|
WebKit.webViewLoadHtml webView htmlContent (Just "file:///")
|
|
|
|
maybeSaved <- loadLastRead
|
|
case maybeSaved of
|
|
Just (path, idx) | path == bookPath env -> loadChapter idx
|
|
_ -> loadChapter 0
|
|
|
|
keyCtrl <- Gtk.eventControllerKeyNew
|
|
Gtk.eventControllerSetPropagationPhase keyCtrl GtkEnums.PropagationPhaseCapture
|
|
|
|
_ <- Gtk.on keyCtrl #keyPressed $ \keyval _ _ -> do
|
|
curr <- readIORef currentIndex
|
|
case keyval of
|
|
Gdk.KEY_Right -> loadChapter (curr + 1) >> return True
|
|
Gdk.KEY_Left -> loadChapter (curr - 1) >> return True
|
|
_ -> return False
|
|
|
|
Gtk.widgetAddController window keyCtrl
|
|
|
|
#present window
|
|
|
|
_ <- Gio.applicationRun app Nothing
|
|
return ()
|