5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

stackでhaskell-gi

Last updated at Posted at 2021-05-22

「haskell-gi」のHelloWorldをstackで実行してみた。

環境

OS: Gentoo Base System release 2.7
GUI: GNOME
stack: Version 2.5.1
LTS: lts-17.12

haskell-gi

https://github.com/haskell-gi/haskell-gi
上記サイトには説明として「GObject Introspection対応のライブラリのHaskellバインディングを生成します。」と書かれています。

stackでHelloWorld

公式サイトのexamplesにあるhello-world.hsを実行してみます。
https://github.com/haskell-gi/haskell-gi/blob/master/examples/introductory/src/hello-world.hs

haskell-world.hsのコード

{-# LANGUAGE OverloadedStrings, OverloadedLabels #-}

{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}

{- Hello World example of GTK+ documentation. For information please refer to README -}

module Main where

-- import qualified Data.Text as T
import Data.Text ()

import qualified GI.Gio as Gio
import qualified GI.Gtk as Gtk
import Data.GI.Base

printHello :: IO ()
printHello = putStrLn "Hello, World!"

activateApp :: Gtk.Application -> IO ()
activateApp app = do
  w <- new Gtk.ApplicationWindow [ #application := app
                                 , #title := "Haskell Gi - Examples - Hello World"
                                 , #defaultHeight := 200
                                 , #defaultWidth := 200
                                 ]

  bbox <- new Gtk.ButtonBox [ #orientation := Gtk.OrientationHorizontal ]
  #add w bbox
  
  btn <- new Gtk.Button [ #label := "Hello World!"]
  on btn #clicked printHello
  on btn #clicked $ Gtk.widgetDestroy w
  #add bbox btn
  
  #showAll w
  return ()

main :: IO ()
main = do
  app <- new Gtk.Application [ #applicationId := "haskell-gi.examples.hello-world"
                             , #flags := [ Gio.ApplicationFlagsFlagsNone ]
                             ]
  on app #activate $ activateApp app
  Gio.applicationRun app Nothing
  return ()

上記コードをstackで実行してみたいと思います。

stack new my-project
cd my-project

stack.yamlファイルにextra-depsを下記の通りに追記します。

extra-deps:
- gtk2hs-buildtools-0.13.8.0
- glib-0.13.8.1
- cairo-0.13.8.1
- pango-0.13.8.1
- gi-gtk-3.0.37
- gi-gdk-3.0.24
- gi-gdkpixbuf-2.0.26
- gi-gmodule-2.0.1
- gi-atk-2.0.23
- gi-cairo-1.0.25
- gi-gio-2.0.28
- gi-gobject-2.0.26
- gi-harfbuzz-0.0.4
- gi-pango-1.0.24
- haskell-gi-0.25.0
- haskell-gi-base-0.25.0
- gi-glib-2.0.25
- reactive-banana-1.2.1.0

packege.yamlファイルのdependenciesを下記の通りに設定します。

dependencies:
- base >= 4.7 && < 5
- gi-gtk
- haskell-gi-base
- text
- gi-gio

src/Main.hsにhello-world.hsコードをコピペします。

{-# LANGUAGE OverloadedStrings, OverloadedLabels #-}

{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}

{- Hello World example of GTK+ documentation. For information please refer to README -}

module Main where

-- import qualified Data.Text as T
import Data.Text ()

import qualified GI.Gio as Gio
import qualified GI.Gtk as Gtk
import Data.GI.Base

printHello :: IO ()
printHello = putStrLn "Hello, World!"

activateApp :: Gtk.Application -> IO ()
activateApp app = do
  w <- new Gtk.ApplicationWindow [ #application := app
                                 , #title := "Haskell Gi - Examples - Hello World"
                                 , #defaultHeight := 200
                                 , #defaultWidth := 200
                                 ]

  bbox <- new Gtk.ButtonBox [ #orientation := Gtk.OrientationHorizontal ]
  #add w bbox

  btn <- new Gtk.Button [ #label := "Hello World!"]
  on btn #clicked printHello
  on btn #clicked $ Gtk.widgetDestroy w
  #add bbox btn

  #showAll w
  return ()

main :: IO ()
main = do
  app <- new Gtk.Application [ #applicationId := "haskell-gi.examples.hello-world"
                             , #flags := [ Gio.ApplicationFlagsFlagsNone ]
                             ]
  on app #activate $ activateApp app
  Gio.applicationRun app Nothing
  return ()

コンパイルして実行します。

stack build
stack exec my-project-exe

下記の通りに実行されます。
20210522.png

全てのソースコード

以上です。

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?