My 2022 notes are still more or less good in 2023. For GTK4, I am using example Main.hs
:
{-# LANGUAGE LexicalNegation #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
-- From package haskell-gi-base
import Data.GI.Base (AttrOp ((:=)), new, on, set)
-- From package gi-gtk
import qualified GI.Gtk as Gtk
import Control.Monad (void)
main :: IO ()
main = do
app <- new Gtk.Application []
on app #activate (activate app)
void $ #run app Nothing
activate :: Gtk.Application -> IO ()
activate app = do
win <- new Gtk.Window [ #title := "Hi there" ]
button <- new Gtk.Button [ #label := "Click me" ]
on button #clicked (set button [ #sensitive := False,
#label := "Thanks for clicking me" ])
#setChild win (Just button)
#setDefaultSize win 400 -1
#show win
#addWindow app win
My package.yaml
is (I don’t need to constrain the upper bound on vector
):
name: gtkTest
version: 0.1.0.0
dependencies:
- base >= 4.7 && < 5
- gi-gtk >= 4
- haskell-gi-base
- vector
executables:
gtkTest:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
My stack.yaml
is:
# GHC 9.4.6 and gi-gtk-3.0.41
resolver: lts-21.9
extra-deps:
- gi-gdk-4.0.7@sha256:e1915be39e2eebaf445584986dea7b750aef57c00894d1a218a54811da27b381,9025
- gi-gsk-4.0.7@sha256:1c96f0d0fdc39b1dfa3314cb00e57cb39931bd7f1a125fb0890435fcae624952,7393
- gi-gtk-4.0.8@sha256:6011c2d7880c6f9b1a319a593db6b76e5175b3f020fc4b3ca86707ee70c3a1d8,35759
I can check the name of my Stack-supplied MSYS2 directory with:
> stack ls tools --filter msys2
msys2-20230526
and I can check I have set up the environment variables correctly with dir Env:PKG_CONFIG_PATH
and dir Env:XDG_DATA_DIRS
, which on my machine yield:
Name Value
---- -----
PKG_CONFIG_PATH C:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\msys2-20230526\mingw64\lib\pkgconfig
XDG_DATA_DIRS C:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\msys2-20230526\mingw64\share
After all that, stack build
and stack exec gtkTest
behave as expected. stack ghci
however, is disappointing:
❯ stack ghci
Using main module:
1. Package gtkTest, component gtkTest:exe:gtkTest, with main-is file: D:\Users\mike\Code\Haskell\gtkTest2023\app\Main.hs.
Warning: The following GHC options are incompatible with GHCi and have not been passed to it: -threaded.
Configuring GHCi with the following packages: gtkTest.
GHCi, version 9.4.6: https://www.haskell.org/ghc/ :? for help
[1 of 3] Compiling Main ( D:\Users\mike\Code\Haskell\gtkTest2023\app\Main.hs, interpreted )
[2 of 3] Compiling Paths_gtkTest ( D:\Users\mike\Code\Haskell\gtkTest2023\.stack-work\dist\51f21a8f\build\gtkTest\autogen\Paths_gtkTest.hs, interpreted )
Ok, two modules loaded.
Loaded GHCi configuration from C:\Users\mike\AppData\Local\stack\ghci-script\aa682954\ghci-script
ghci> main
Access violation in generated code when executing data at 0x7ff6f31df010
Attempting to reconstruct a stack trace...
Frame Code address
* 0x28361fda80 0x7ff6f31df010
* 0x28361fda88 0x7ff6efa55ae2 C:\sr\snapshots\debcadbf\lib\x86_64-windows-ghc-9.4.6\gi-gtk-4.0.8-6qUVSHE3e3gLkatWPgxKXJ\libHSgi-gtk-4.0.8-6qUVSHE3e3gLkatWPgxKXJ.a(#65:ghc_1998.o)+0x5ad2
(gizmgtkzm4zi0zi8zm6qUVSHE3e3gLkatWPgxKXJ_GIziGtkziObjectsziApplication_toApplication_info+0x55e)