确定可见数 Gtk.FlowBox children

Determine number of visible Gtk.FlowBox children

我用的是 Gtk.FlowBoxGtk.FlowBoxChild children。基于用户选择的按钮,我应用了不同的 Gtk.FlowBoxFilterFunc 函数。我想随时确定有多少 children 是可见的(即未过滤)。我已尝试检查所有各种 visibleno_show_all 属性,以及 is_visible()get_visible() 函数。我似乎找不到任何 属性 或方法来确定哪些 children 是未过滤的 - 这可能吗?

我注意到当我使用 GTK Inspector 进行调试时,它似乎检测到变化,因为层次结构树中的文本从黑色变为灰色。这让我觉得这一定是有可能的!

MCVE:

public class Mcve.Application : Gtk.Application {

    public Application () {
        Object (
            application_id: Constants.APP_ID,
            flags: ApplicationFlags.FLAGS_NONE
        );
    }

    protected override void activate () {
        var flow_box = new Gtk.FlowBox () {
            expand = true
        };
        var child1 = new Gtk.FlowBoxChild ();
        child1.child = new Gtk.Label ("Child 1");
        var child2 = new Gtk.FlowBoxChild ();
        child2.child = new Gtk.Label ("Child 2");

        flow_box.add (child1);
        flow_box.add (child2);

        var button = new Gtk.Button.with_label ("Filter");
        button.clicked.connect (() => {
            flow_box.set_filter_func ((child) => {
                var item = child.get_child () as Gtk.Label;
                return item.label == "Child 1";
            });
            foreach (var child in flow_box.get_children ()) {
                var item = child as Gtk.FlowBoxChild;
                int index = item.get_index () + 1;
                debug ("[Child %d] item.visible = %s", index, item.visible.to_string ());
                debug ("[Child %d] item.no_show_all = %s", index, item.no_show_all.to_string ());
                debug ("[Child %d] item.is_visible () = %s", index, item.is_visible ().to_string ());
                debug ("[Child %d] item.get_visible () = %s", index, item.get_visible ().to_string ());
                debug ("[Child %d] item.get_child ().visible = %s", index, item.get_child ().visible.to_string ());
                debug ("[Child %d] item.get_child ().no_show_all = %s", index, item.get_child ().no_show_all.to_string ());
                debug ("[Child %d] item.get_child ().is_visible () = %s", index, item.get_child ().is_visible ().to_string ());
                debug ("[Child %d] item.get_child ().get_visible () = %s", index, item.get_child ().get_visible ().to_string ());
            }
        });

        var grid = new Gtk.Grid ();
        grid.attach (flow_box, 0, 0);
        grid.attach (button, 0, 1);

        var window = new Gtk.Window ();
        window.add (grid);

        this.add_window (window);

        window.present ();
        window.show_all ();
    }

    public static int main (string[] args) {
        var app = new Mcve.Application ();
        return app.run (args);
    }

}

点击筛选时的输出:

[Child 1] item.visible = true
[Child 1] item.no_show_all = false
[Child 1] item.is_visible () = true
[Child 1] item.get_visible () = true
[Child 1] item.get_child ().visible = true
[Child 1] item.get_child ().no_show_all = false
[Child 1] item.get_child ().is_visible () = true
[Child 1] item.get_child ().get_visible () = true
[Child 2] item.visible = true
[Child 2] item.no_show_all = false
[Child 2] item.is_visible () = true
[Child 2] item.get_visible () = true
[Child 2] item.get_child ().visible = true
[Child 2] item.get_child ().no_show_all = false
[Child 2] item.get_child ().is_visible () = true
[Child 2] item.get_child ().get_visible () = true

在 GTK Inspector 中,我可以看到它以某种方式知道第二个 child 不可见:


编辑:看起来 GTK Inspector 响应 mapped 信号:https://github.com/GNOME/gtk/blob/main/gtk/inspector/object-tree.c#L700,并且确实在 FlowBoxChild 上调用 get_mapped () 将 return truefalse 取决于项目是否可见。我不太确定映射小部件意味着什么,所以我有点犹豫要不要相信它。

这可以在 Gtk.FlowBoxChild 小部件上使用 get_mapped() 方法。

就像评论中提到的@BobMorane,GTK documentation 具有以下 map 信号条目:

A widget is mapped when the widget is visible (which is controlled with GtkWidget:visible) and all its parents up to the toplevel widget are also visible. The ::map signal can be used to determine whether a widget will be drawn