I have this little project where I try to use gi-gtk to draw on a window.
https://github.com/bigos/cairo-example/blob/b9480dc63d6fff3bc195d35c7422f193fc8ae7d4/src/Main.hs
I have figured out how to make gi-gtk work with Cairo.
https://github.com/haskell-gi/haskell-gi/wiki/Using-Cairo-with-haskell-gi-generated-bindings
But I have a problem with obtaining cairo context. In the following code I have two events. The first event works fine. But I can't figure out how to get the cairo context needed for drawing on canves when I listen to key presses on window object.
win <- Gtk.windowNew WindowTypeToplevel
canvas <- Gtk.drawingAreaNew
Gtk.containerAdd win canvas
_ <- Gtk.onWidgetDraw canvas $ \context ->
renderWithContext context (updateCanvas canvas) >> pure True
_ <- Gtk.onWidgetKeyPressEvent win $ \x -> do
vvv <- Gdk.getEventKeyKeyval x
-- How do I draw on canvas here?
(putStrLn ("You have pressed key code " ++ (show vvv))) >> pure True
onWidgetDraw, and you use Gtk.widgetQueueDraw on the widget to trigger the drawing. This makes sense because the UI always has to be able to redraw the window, so whatever you would draw inonWidgetKeyPressEvent, if you could, would get lost when the user resizes the window or so. - Joachim Breitner