Skip to content

Instantly share code, notes, and snippets.

@Taneb
Created November 25, 2019 09:49
Show Gist options
  • Save Taneb/ca1ed27adee7da28e91ec8b3aa48af08 to your computer and use it in GitHub Desktop.
Save Taneb/ca1ed27adee7da28e91ec8b3aa48af08 to your computer and use it in GitHub Desktop.
Simple image viewer in Haskell
module Main where
import qualified Data.ByteString.Char8 as B -- bad
import qualified Graphics.UI.Gtk as Gtk
import System.Environment
import qualified System.GIO as GIO
updateImage :: Gtk.ImageClass o => o -> Maybe GIO.File -> Maybe GIO.File -> GIO.FileMonitorEvent -> IO ()
updateImage image (Just file) _ GIO.FileMonitorEventChanged = do
pixbuf <- Gtk.pixbufNewFromFile . B.unpack $ GIO.filePath file
Gtk.set (Gtk.toImage image)
[ Gtk.imagePixbuf Gtk.:= pixbuf
]
updateImage _ _ _ _ = pure ()
main :: IO ()
main = do
[ path ] <- getArgs
let file = GIO.fileFromPath $ B.pack path
_ <- Gtk.initGUI
fileMonitor <- GIO.fileMonitor file [] Nothing
window <- Gtk.windowNew
image <- Gtk.imageNew
Gtk.on fileMonitor GIO.fileMonitorChanged (updateImage image)
Gtk.set window
[ Gtk.containerChild Gtk.:= image
]
Gtk.on window Gtk.objectDestroy Gtk.mainQuit
Gtk.widgetShowAll window
Gtk.mainGUI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment