如何在Haskell gtk2hs中处理Mac OS X中的退出命令(Cmd-Q)

时间:2016-05-22 04:28:32

标签: haskell gtk2hs

我正在https://github.com/gtk2hs/gtk2hs/blob/master/gtk/demo/hello/World.hs尝试示例程序,转载如下:

>>> num.divide(denom, axis='index')
          x         y
A -1.293798 -0.955037
B  0.740514 -0.833974
C  0.530348 -0.195449

如果我在Mac OS X上构建并运行它,Cmd-Q或应用程序菜单中的退出命令不会关闭应用程序。如何捕获此事件并使其关闭应用程序?

更新

我已经为我的项目添加了-- A simple program to demonstrate Gtk2Hs. module Main (Main.main) where import Graphics.UI.Gtk main :: IO () main = do initGUI -- Create a new window window <- windowNew -- Here we connect the "destroy" event to a signal handler. -- This event occurs when we call widgetDestroy on the window -- or if the user closes the window. on window objectDestroy mainQuit -- Sets the border width and tile of the window. Note that border width -- attribute is in 'Container' from which 'Window' is derived. set window [ containerBorderWidth := 10, windowTitle := "Hello World" ] -- Creates a new button with the label "Hello World". button <- buttonNew set button [ buttonLabel := "Hello World" ] -- When the button receives the "clicked" signal, it will call the -- function given as the second argument. on button buttonActivated (putStrLn "Hello World") -- Gtk+ allows several callbacks for the same event. -- This one will cause the window to be destroyed by calling -- widgetDestroy. The callbacks are called in the sequence they were added. on button buttonActivated $ do putStrLn "A \"clicked\"-handler to say \"destroy\"" widgetDestroy window -- Insert the hello-world button into the window. set window [ containerChild := button ] -- The final step is to display this newly created widget. Note that this -- also allocates the right amount of space to the windows and the button. widgetShowAll window -- All Gtk+ applications must have a main loop. Control ends here -- and waits for an event to occur (like a key press or mouse event). -- This function returns if the program should finish. mainGUI 依赖项,在我的源文件中添加了gtk3-mac-integration,并在调用import Graphics.UI.Gtk.OSX后立即添加了以下内容:

initGUI

我肯定错过了一些东西,因为这似乎没有做任何事情(见https://github.com/rcook/gtkapp/commit/8531509d0648ddb657633a33773c09bc5a576014)。

更新号码2

感谢@Jack Henahan和OSXDemo.hs,我现在有了一个有效的解决方案:

app <- applicationNew
on app willTerminate (return ())

1 个答案:

答案 0 :(得分:2)

您需要发送NSApplicationWillTerminate信号。

willTerminate :: ApplicationClass self => Signal self (IO ())
willTerminate = Signal (connect_NONE__NONE "NSApplicationWillTerminate")

config中处理的方式。