forked from marko/svitak
page tracking
This commit is contained in:
parent
be79979183
commit
8ebfc59d2b
2 changed files with 31 additions and 27 deletions
|
|
@ -113,9 +113,7 @@ getChapter (filename, tocTitle) idx = do
|
|||
let rawText = TE.decodeUtf8 . B.toStrict $ fromEntry e
|
||||
let withImages = embedImages env rawText
|
||||
|
||||
let (_, content) =
|
||||
fromMaybe (tocTitle, "") $
|
||||
scrapeStringLike withImages chapterScraper
|
||||
let (_, content) = fromMaybe (tocTitle, "") $ scrapeStringLike withImages chapterScraper
|
||||
pure $ Chapter tocTitle content idx
|
||||
|
||||
getMimeFromManifest :: DMan.Manifest -> FilePath -> T.Text
|
||||
|
|
|
|||
50
app/UI.hs
50
app/UI.hs
|
|
@ -21,37 +21,42 @@ import qualified GI.Gtk.Enums as GtkEnums
|
|||
import qualified GI.WebKit as WebKit
|
||||
import Persistence (loadLastRead, saveLastRead)
|
||||
import State (initialState, prepareView)
|
||||
import Text.HTML.Scalpel (Scraper, attr, chroots, text)
|
||||
import Types (AppState (..), ChapterView (..), UserAction (..))
|
||||
import Text.HTML.Scalpel (Scraper, text, attr, chroots)
|
||||
|
||||
updateUI :: Gtk.ApplicationWindow -> WebKit.WebView -> ChapterView -> IO ()
|
||||
updateUI window webView view = do
|
||||
Gtk.set window [#title := viewTitle view]
|
||||
updateUI :: Gtk.ApplicationWindow -> WebKit.WebView -> ChapterView -> Int -> Int -> IO ()
|
||||
updateUI window webView view currentIdx totalCount = do
|
||||
let ttext = T.pack (show (currentIdx + 1)) <> "/ " <> T.pack (show totalCount) <> ": " <> viewTitle view
|
||||
Gtk.set window [#title := ttext]
|
||||
WebKit.webViewLoadHtml webView (viewHtml view) Nothing
|
||||
|
||||
handleAction :: TVar AppState -> Gtk.ApplicationWindow -> WebKit.WebView -> UserAction -> IO ()
|
||||
handleAction stateTVar window webView action = do
|
||||
st <- atomically $ readTVar stateTVar
|
||||
let totalChapters = length (cAllChapters st)
|
||||
|
||||
case action of
|
||||
ZoomIn -> updateZoom stateTVar webView (zoomlvl st + 0.1)
|
||||
ZoomOut -> updateZoom stateTVar webView (max 0.1 (zoomlvl st - 0.1))
|
||||
_ -> do
|
||||
let nextIdx = case action of
|
||||
NextChapter -> cIdx st + 1
|
||||
PrevChapter -> cIdx st - 1
|
||||
LoadSpecific i -> i
|
||||
_ -> cIdx st
|
||||
if null (cAllChapters st)
|
||||
then pure ()
|
||||
else do
|
||||
let nextIdx = case action of
|
||||
NextChapter -> cIdx st + 1
|
||||
PrevChapter -> cIdx st - 1
|
||||
LoadSpecific i -> i
|
||||
_ -> cIdx st
|
||||
|
||||
let safeIdx = max 0 (min (totalChapters -1 ) nextIdx)
|
||||
let safeIdx = max 0 (min (totalChapters - 1) nextIdx)
|
||||
|
||||
case prepareView st safeIdx of
|
||||
Nothing -> pure ()
|
||||
Just view -> do
|
||||
updateUI window webView view
|
||||
atomically $ modifyTVar stateTVar $ \s -> s {cIdx = viewIdx view}
|
||||
_ <- async $ saveLastRead (bookPath (cEnv st)) (viewIdx view)
|
||||
pure ()
|
||||
case prepareView st safeIdx of
|
||||
Nothing -> pure ()
|
||||
Just view -> do
|
||||
updateUI window webView view safeIdx totalChapters
|
||||
atomically $ modifyTVar stateTVar $ \s -> s {cIdx = viewIdx view}
|
||||
_ <- async $ saveLastRead (bookPath (cEnv st)) (viewIdx view)
|
||||
pure ()
|
||||
|
||||
updateZoom :: TVar AppState -> WebKit.WebView -> Double -> IO ()
|
||||
updateZoom stateTVar webView level = do
|
||||
|
|
@ -69,8 +74,7 @@ initAppServices env stateTVar actionHandler = do
|
|||
Just t -> titleText t
|
||||
Nothing -> if null titles then "Unknown" else titleText (head titles)
|
||||
|
||||
atomically $ modifyTVar stateTVar $ \s ->
|
||||
s {cSpine = ispine, cAllChapters = allChapters, bTitle = T.pack bookTitle}
|
||||
atomically $ modifyTVar stateTVar $ \s -> s {cSpine = ispine, cAllChapters = allChapters, bTitle = T.pack bookTitle}
|
||||
|
||||
_ <- GLib.idleAdd GLibConst.PRIORITY_DEFAULT $ do
|
||||
maybeSaved <- loadLastRead
|
||||
|
|
@ -96,12 +100,16 @@ runApp env = do
|
|||
stateTVar <- newTVarIO (initialState env)
|
||||
window <- Gtk.applicationWindowNew app
|
||||
webView <- WebKit.webViewNew
|
||||
|
||||
Gtk.set window [#defaultWidth := 800, #defaultHeight := 600, #child := webView]
|
||||
|
||||
let dispatch = handleAction stateTVar window webView
|
||||
|
||||
initAppServices env stateTVar dispatch
|
||||
|
||||
keyCtrl <- Gtk.eventControllerKeyNew
|
||||
Gtk.eventControllerSetPropagationPhase keyCtrl GtkEnums.PropagationPhaseCapture
|
||||
|
||||
_ <- Gtk.on keyCtrl #keyPressed $ \keyval _ _ -> case keyval of
|
||||
Gdk.KEY_Right -> dispatch NextChapter >> pure True
|
||||
Gdk.KEY_Left -> dispatch PrevChapter >> pure True
|
||||
|
|
@ -110,9 +118,7 @@ runApp env = do
|
|||
_ -> pure False
|
||||
|
||||
Gtk.widgetAddController window keyCtrl
|
||||
|
||||
#present window
|
||||
|
||||
initAppServices env stateTVar dispatch
|
||||
|
||||
_ <- Gio.applicationRun app Nothing
|
||||
pure ()
|
||||
|
|
|
|||
Loading…
Reference in a new issue