owickstrom / gi-gtk-declarative

Declarative GTK+ programming in Haskell
https://owickstrom.github.io/gi-gtk-declarative/
288 stars 35 forks source link

Window is not destroyed when event loop stops, only when program exits #105

Open miguel-negrao opened 3 years ago

miguel-negrao commented 3 years ago

In the code below, after clicking the close button of the window, the window will not close, staying open while the program is still running. Only after pressing enter on the command prompt, unblocking the getLine function, does the window close when the process exits. For my use case I need the window to close when the "run" function returns.

{-# LANGUAGE OverloadedLabels  #-}
{-# LANGUAGE OverloadedLists   #-}
{-# LANGUAGE OverloadedStrings #-}

module Main where

import           Data.Function                  ( (&) )
import           Data.Text                      ( Text )
import           Pipes
import qualified Pipes.Extras                  as Pipes
import           Control.Monad                  ( void )

import           GI.Gtk                         ( Label(..)
                                                , Window(..)
                                                )
import           GI.Gtk.Declarative
import           GI.Gtk.Declarative.App.Simple

type State = ()

data Event = Closed

view' :: State -> AppView Window Event
view' _ =
  bin
      Window
      [ #title := "Hello"
      , on #deleteEvent (const (True, Closed))
      , #widthRequest := 400
      , #heightRequest := 300
      ]
    $ widget Label [#label := "Nothing here yet."]

update' :: State -> Event -> Transition State Event
update' _ Closed      = Exit

main' :: IO ()
main' = void $ run App { view         = view'
                      , update       = update'
                      , inputs       = []
                      , initialState = ()
                      }

main = do
    main'
    getLine

In the following gi-gtk program that does not happen:

{-# 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
import Control.Concurrent

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 ()

main = do
    main'
    getLine