ImGui.NET icon indicating copy to clipboard operation
ImGui.NET copied to clipboard

BeginTabItem's parameter p_open cannot be null reference

Open WilliamQiufeng opened this issue 1 year ago • 0 comments

In ImGui, you can pass a null pointer to p_open so that the tab item is always shown and the selection is managed by imgui itself. However, here

if (ImGui.BeginTabItem("a", ref Unsafe.NullRef<bool>(), ImGuiTabItemFlags.None))
   ImGui.Text("Hi");
   ImGui.EndTabItem();
}

Will simply make it not render, as opposed to passing a ref b where b is a boolean variable.

This comes down to the handling of the reference, since in the method it dereferences it no matter what. The following modified code worked for me:

    public static unsafe bool BeginTabItem(string label, ref bool p_open, ImGuiTabItemFlags flags)
        {
            byte* native_label;
            int label_byteCount = 0;
            if (label != null)
            {
                label_byteCount = Encoding.UTF8.GetByteCount(label);
                if (label_byteCount > Util.StackAllocationSizeLimit)
                {
                    native_label = Util.Allocate(label_byteCount + 1);
                }
                else
                {
                    byte* native_label_stackBytes = stackalloc byte[label_byteCount + 1];
                    native_label = native_label_stackBytes;
                }
                int native_label_offset = Util.GetUtf8(label, native_label, label_byteCount);
                native_label[native_label_offset] = 0;
            }
            else { native_label = null; }

            byte ret;
            if (Unsafe.IsNullRef(ref p_open))
            {
                ret = ImGuiNative.igBeginTabItem(native_label, (byte*)0, flags);
            }
            else
            {
                byte native_p_open_val = p_open ? (byte)1 : (byte)0;
                byte* native_p_open = &native_p_open_val;
                ret = ImGuiNative.igBeginTabItem(native_label, native_p_open, flags);
                p_open = native_p_open_val != 0;
            }
            if (label_byteCount > Util.StackAllocationSizeLimit)
            {
                Util.Free(native_label);
            }
            return ret != 0;
        }

WilliamQiufeng avatar Sep 25 '24 03:09 WilliamQiufeng