diff --git a/app/UI.hs b/app/UI.hs index 17fbd25..c1efe3d 100644 --- a/app/UI.hs +++ b/app/UI.hs @@ -1,6 +1,5 @@ {-# LANGUAGE OverloadedLabels #-} {-# LANGUAGE OverloadedStrings #-} - module UI (runApp) where import Codec.Epub.Data.Metadata (metaTitles, titleText, titleType) @@ -8,7 +7,7 @@ import Control.Concurrent.Async (async) import Control.Concurrent.STM (TVar, atomically, modifyTVar, newTVarIO, readTVar) import Control.Monad (zipWithM) import Control.Monad.Reader (runReaderT) -import Data.GI.Base (AttrOp (..)) +import Data.GI.Base (AttrOp (..), on) import Data.List (find) import qualified Data.Text as T import EpubParser (EpubEnv (..), getChapter, resolveSpine) @@ -84,39 +83,39 @@ initAppServices env stateTVar actionHandler = do pure () ncxScraper :: Scraper T.Text [(FilePath, T.Text)] -ncxScraper = chroots "navPoint" $ do - path <- T.unpack . T.takeWhile (/= '#') <$> attr "src" "content" - title <- T.strip <$> text "navLabel" +ncxScraper = chroots "navPoint" $ + (,) <$> (T.unpack . T.takeWhile (/= '#') <$> attr "src" "content") <*> (T.strip <$> text "navLabel") - pure (path, title) +activate :: EpubEnv -> Gtk.Application -> IO () +activate env app = do + stateTVar <- newTVarIO (initialState env) + window <- Gtk.applicationWindowNew app + webView <- WebKit.webViewNew -runApp :: EpubEnv -> IO () -runApp env = do - app <- Gtk.applicationNew (Just "com.svitak.reader.v3") [] + Gtk.set window [#defaultWidth := 800, #defaultHeight := 600, #child := webView] - _ <- Gtk.on app #activate $ do - stateTVar <- newTVarIO (initialState env) - window <- Gtk.applicationWindowNew app - webView <- WebKit.webViewNew + let dispatch = handleAction stateTVar window webView - Gtk.set window [#defaultWidth := 800, #defaultHeight := 600, #child := webView] + initAppServices env stateTVar dispatch - let dispatch = handleAction stateTVar window webView + keyCtrl <- Gtk.eventControllerKeyNew + Gtk.eventControllerSetPropagationPhase keyCtrl GtkEnums.PropagationPhaseCapture - initAppServices env stateTVar dispatch - - keyCtrl <- Gtk.eventControllerKeyNew - Gtk.eventControllerSetPropagationPhase keyCtrl GtkEnums.PropagationPhaseCapture - - _ <- Gtk.on keyCtrl #keyPressed $ \keyval _ _ -> case keyval of + _ <- on keyCtrl #keyPressed $ \keyval _ _ -> + case keyval of Gdk.KEY_Right -> dispatch NextChapter >> pure True Gdk.KEY_Left -> dispatch PrevChapter >> pure True Gdk.KEY_equal -> dispatch ZoomIn >> pure True Gdk.KEY_minus -> dispatch ZoomOut >> pure True _ -> pure False - Gtk.widgetAddController window keyCtrl - - #present window - _ <- Gio.applicationRun app Nothing + Gtk.widgetAddController window keyCtrl + #present window + pure () + +runApp :: EpubEnv -> IO () +runApp env = do + app <- Gtk.new Gtk.Application [#applicationId := "com.svitak.reader", #flags := [Gio.ApplicationFlagsDefaultFlags]] + _ <- on app #activate (activate env app) + _ <- Gio.applicationRun app Nothing -- we're not working with CLI args here, so Nothing can be passed pure ()