用于通知 GtkPaned 的拆分何时更改的 Gtk3 事件

Gtk3 event for noticing when a GtkPaned's split changes

我正在制作一个小应用程序,我想保存各种可调整大小的小部件的大小和位置,以便下次启动该应用程序时,用户看到的内容与之前相同他们最后退出了。我在弄清楚我应该听什么事件以了解何时调整小部件的大小时遇到​​了一些麻烦。在线查看,似乎 configure-event 是正确的,但在我的测试中,当用户拖动 GtkPaned 的拆分调整小部件大小时,此事件不会触发。我在下面包含了一个最小的例子。

(我知道这是一个 Haskell 程序,但我试图避免任何花哨的功能,以便即使非 Haskell 专家也能阅读它。主要的事情你可能阅读它时无法猜测自己是函数应用程序仅使用 space 完成,因此大多数其他语言中的 f(x, y, z) 是 Haskell 中的 f x y z。 )

import Control.Monad.IO.Class
import Graphics.UI.Gtk

main :: IO ()
main = do
    initGUI
    window <- windowNew
    on window objectDestroy mainQuit

    eventLog <- labelNew (Just "")
    -- For this minimal example, the widgets have no content. But let's
    -- pretend by just asking for a little space which we'll leave blank.
    widgetSetSizeRequest eventLog 200 200

    uiGraph <- labelNew (Just "")
    widgetSetSizeRequest uiGraph 200 200

    dataPane <- vPanedNew
    panedPack1 dataPane eventLog True True
    panedPack2 dataPane uiGraph False True

    -- Create a callback to print the position of the pane's split. We'll
    -- always say that we didn't end up handling the event and that it should
    -- get propagated to wherever it used to go by returning False.
    let report = liftIO $ do
            print =<< panedGetPosition dataPane
            return False

    on window   configureEvent report
    on eventLog configureEvent report
    on uiGraph  configureEvent report
    on dataPane configureEvent report

    containerAdd window dataPane
    widgetShowAll window
    mainGUI

当我 运行 这个程序时,当 window 移动或调整大小时打印窗格拆分的位置,但移动拆分本身时不会打印。

有没有我可以听到的另一个事件会告诉我那是什么时候发生的?我如何知道用户何时自定义视图以便我可以将该信息保存到磁盘?

您可以使用 "notify::{param-name-here}" 信号连接到 GObject 属性 的变化。在您的情况下,它是 position 属性.

python就是这样做的:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

def position(paned, param):
    print(paned.get_position()) # either call object's method
    print(paned.get_property(param.name)) # or obtain property value by name

win = Gtk.Window()
paned = Gtk.Paned()
paned.connect("notify::position", position)
paned.pack1(Gtk.Label.new("abc"))
paned.pack2(Gtk.Label.new("def"))

win.add(paned)
win.show_all()
Gtk.main()

对于Haskell模拟,你可以使用notifyProperty to get a signal for a given attribute. Although there is a panedPosition attribute with the right type, it doesn't work, because it's not implemented as a named attribute under the hood (not sure why). Instead, you have to build the attribute from raw materials yourself, using newAttrFromIntProperty。将各个部分放在一起,您可以添加此行以在每次拆分移动时获得报告:

on dataPane (notifyProperty (newAttrFromIntProperty "position")) report

(不需要问题中现有的 configureEvent 行。)