dear-imgui-sys 0.13.0

Low-level FFI bindings to Dear ImGui v1.92.8 (docking branch) via cimgui (C API)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
// Stack layout compatibility shim for blueprint-style node editor examples.
//
// Dear ImGui does not ship BeginHorizontal/BeginVertical/Spring as official
// public APIs. imgui-node-editor's blueprints example uses a vendored stack
// layout extension from the imgui-node-editor tree. This file provides a
// repository-owned C ABI shim with compatible behavior, implemented outside
// the Dear ImGui and imgui-node-editor submodules so upstream updates remain
// clean.
//
// References:
// - https://github.com/thedmd/imgui-node-editor
// - https://github.com/ocornut/imgui/discussions/6458
//
// The algorithm follows the MIT-licensed stack layout extension vendored by
// imgui-node-editor while storing all extra state in this shim instead of
// patching ImGuiWindowTempData.

#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui.h>
#include <imgui_internal.h>

#include <algorithm>
#include <cstdint>
#include <memory>
#include <unordered_map>
#include <vector>

namespace
{
enum class StackLayoutItemType
{
    Item,
    Spring
};

struct StackLayoutItem
{
    StackLayoutItemType Type;
    ImRect MeasuredBounds;
    float SpringWeight;
    float SpringSpacing;
    float SpringSize;
    float CurrentAlign;
    float CurrentAlignOffset;
    unsigned int VertexIndexBegin;
    unsigned int VertexIndexEnd;

    explicit StackLayoutItem(StackLayoutItemType type)
        : Type(type)
        , MeasuredBounds(0, 0, 0, 0)
        , SpringWeight(1.0f)
        , SpringSpacing(-1.0f)
        , SpringSize(0.0f)
        , CurrentAlign(0.0f)
        , CurrentAlignOffset(0.0f)
        , VertexIndexBegin(0)
        , VertexIndexEnd(0)
    {
    }
};

struct StackLayout
{
    ImGuiID Id;
    ImGuiLayoutType Type;
    bool Live;
    ImVec2 Size;
    ImVec2 CurrentSize;
    ImVec2 MinimumSize;
    ImVec2 MeasuredSize;
    std::vector<StackLayoutItem> Items;
    int CurrentItemIndex;
    int ParentItemIndex;
    StackLayout* Parent;
    StackLayout* FirstChild;
    StackLayout* NextSibling;
    float Align;
    float Indent;
    ImVec2 StartPos;
    ImVec2 StartCursorMaxPos;

    StackLayout(ImGuiID id, ImGuiLayoutType type)
        : Id(id)
        , Type(type)
        , Live(false)
        , Size(0, 0)
        , CurrentSize(0, 0)
        , MinimumSize(0, 0)
        , MeasuredSize(0, 0)
        , CurrentItemIndex(0)
        , ParentItemIndex(0)
        , Parent(nullptr)
        , FirstChild(nullptr)
        , NextSibling(nullptr)
        , Align(-1.0f)
        , Indent(0.0f)
        , StartPos(0, 0)
        , StartCursorMaxPos(0, 0)
    {
    }
};

struct WindowStackLayoutState
{
    int Frame = -1;
    std::unordered_map<ImGuiID, std::unique_ptr<StackLayout>> Layouts;
    std::vector<StackLayout*> LayoutStack;
    StackLayout* CurrentLayout = nullptr;
    StackLayoutItem* CurrentLayoutItem = nullptr;
};

struct ContextStackLayoutState
{
    std::unordered_map<ImGuiID, WindowStackLayoutState> Windows;
};

std::unordered_map<ImGuiContext*, ContextStackLayoutState> GStackLayoutStates;
thread_local ImGuiContext* GActiveStackLayoutContext = nullptr;
thread_local ImGuiID GActiveStackLayoutWindowId = 0;
thread_local int GActiveStackLayoutFrame = -1;
thread_local WindowStackLayoutState* GActiveStackLayoutState = nullptr;

constexpr float DEFAULT_LAYOUT_ALIGN = 0.5f;

void ClearFastActiveLayoutState(WindowStackLayoutState& state)
{
    if (GActiveStackLayoutState != &state)
        return;

    GActiveStackLayoutContext = nullptr;
    GActiveStackLayoutWindowId = 0;
    GActiveStackLayoutFrame = -1;
    GActiveStackLayoutState = nullptr;
}

void UpdateFastActiveLayoutState(WindowStackLayoutState& state)
{
    if (!state.CurrentLayout)
    {
        ClearFastActiveLayoutState(state);
        return;
    }

    ImGuiContext& g = *GImGui;
    ImGuiWindow* window = ImGui::GetCurrentWindow();
    GActiveStackLayoutContext = &g;
    GActiveStackLayoutWindowId = window->ID;
    GActiveStackLayoutFrame = g.FrameCount;
    GActiveStackLayoutState = &state;
}

WindowStackLayoutState* FindFastActiveLayoutState(ImGuiWindow* window)
{
    if (!GImGui || !window || !GActiveStackLayoutState)
        return nullptr;

    ImGuiContext& g = *GImGui;
    if (GActiveStackLayoutContext != &g
        || GActiveStackLayoutWindowId != window->ID
        || GActiveStackLayoutFrame != g.FrameCount)
        return nullptr;

    if (!GActiveStackLayoutState->CurrentLayout)
        return nullptr;

    return GActiveStackLayoutState;
}

WindowStackLayoutState& GetWindowState()
{
    ImGuiContext& g = *GImGui;
    ImGuiWindow* window = ImGui::GetCurrentWindow();
    auto& context_state = GStackLayoutStates[&g];
    auto& state = context_state.Windows[window->ID];

    if (state.Frame != g.FrameCount)
    {
        for (auto it = state.Layouts.begin(); it != state.Layouts.end();)
        {
            if (!it->second->Live)
                it = state.Layouts.erase(it);
            else
            {
                it->second->Live = false;
                ++it;
            }
        }

        state.LayoutStack.clear();
        state.CurrentLayout = nullptr;
        state.CurrentLayoutItem = nullptr;
        ClearFastActiveLayoutState(state);
        state.Frame = g.FrameCount;
    }

    return state;
}

StackLayout* FindLayout(WindowStackLayoutState& state, ImGuiID id, ImGuiLayoutType type)
{
    IM_ASSERT(type == ImGuiLayoutType_Horizontal || type == ImGuiLayoutType_Vertical);

    auto it = state.Layouts.find(id);
    if (it == state.Layouts.end())
        return nullptr;

    StackLayout* layout = it->second.get();
    if (layout->Type != type)
    {
        layout->Type = type;
        layout->MinimumSize = ImVec2(0.0f, 0.0f);
        layout->Items.clear();
    }

    return layout;
}

StackLayout* CreateNewLayout(WindowStackLayoutState& state, ImGuiID id, ImGuiLayoutType type, ImVec2 size)
{
    IM_ASSERT(type == ImGuiLayoutType_Horizontal || type == ImGuiLayoutType_Vertical);

    auto layout = std::make_unique<StackLayout>(id, type);
    layout->Size = size;
    StackLayout* result = layout.get();
    state.Layouts[id] = std::move(layout);
    return result;
}

void SignedIndent(float indent)
{
    if (indent > 0.0f)
        ImGui::Indent(indent);
    else if (indent < 0.0f)
        ImGui::Unindent(-indent);
}

void PushLayout(WindowStackLayoutState& state, StackLayout* layout)
{
    if (layout)
    {
        layout->Parent = state.CurrentLayout;
        if (layout->Parent != nullptr)
            layout->ParentItemIndex = layout->Parent->CurrentItemIndex;

        if (state.CurrentLayout)
        {
            layout->NextSibling = state.CurrentLayout->FirstChild;
            layout->FirstChild = nullptr;
            state.CurrentLayout->FirstChild = layout;
        }
        else
        {
            layout->NextSibling = nullptr;
            layout->FirstChild = nullptr;
        }
    }

    state.LayoutStack.push_back(layout);
    state.CurrentLayout = layout;
    state.CurrentLayoutItem = nullptr;
    UpdateFastActiveLayoutState(state);
}

void PopLayout(WindowStackLayoutState& state, StackLayout* layout)
{
    IM_ASSERT(!state.LayoutStack.empty());
    IM_ASSERT(state.LayoutStack.back() == layout);

    state.LayoutStack.pop_back();

    if (!state.LayoutStack.empty())
    {
        state.CurrentLayout = state.LayoutStack.back();
        if (state.CurrentLayout
            && state.CurrentLayout->CurrentItemIndex >= 0
            && state.CurrentLayout->CurrentItemIndex < static_cast<int>(state.CurrentLayout->Items.size()))
            state.CurrentLayoutItem = &state.CurrentLayout->Items[state.CurrentLayout->CurrentItemIndex];
        else
            state.CurrentLayoutItem = nullptr;
    }
    else
    {
        state.CurrentLayout = nullptr;
        state.CurrentLayoutItem = nullptr;
    }
    UpdateFastActiveLayoutState(state);
}

StackLayoutItem* GenerateLayoutItem(WindowStackLayoutState& state, StackLayout& layout, StackLayoutItemType type)
{
    IM_ASSERT(layout.CurrentItemIndex <= static_cast<int>(layout.Items.size()));

    if (layout.CurrentItemIndex < static_cast<int>(layout.Items.size()))
    {
        StackLayoutItem& item = layout.Items[layout.CurrentItemIndex];
        if (item.Type != type)
            item = StackLayoutItem(type);
    }
    else
    {
        layout.Items.emplace_back(type);
    }

    state.CurrentLayoutItem = &layout.Items[layout.CurrentItemIndex];
    return state.CurrentLayoutItem;
}

ImVec2 CalculateLayoutSize(StackLayout& layout, bool collapse_springs)
{
    ImVec2 bounds(0.0f, 0.0f);

    if (layout.Type == ImGuiLayoutType_Vertical)
    {
        for (StackLayoutItem& item : layout.Items)
        {
            ImVec2 item_size = item.MeasuredBounds.GetSize();
            if (item.Type == StackLayoutItemType::Item)
            {
                bounds.x = ImMax(bounds.x, item_size.x);
                bounds.y += item_size.y;
            }
            else
            {
                bounds.y += ImFloor(item.SpringSpacing);
                if (!collapse_springs)
                    bounds.y += item.SpringSize;
            }
        }
    }
    else
    {
        for (StackLayoutItem& item : layout.Items)
        {
            ImVec2 item_size = item.MeasuredBounds.GetSize();
            if (item.Type == StackLayoutItemType::Item)
            {
                bounds.x += item_size.x;
                bounds.y = ImMax(bounds.y, item_size.y);
            }
            else
            {
                bounds.x += ImFloor(item.SpringSpacing);
                if (!collapse_springs)
                    bounds.x += item.SpringSize;
            }
        }
    }

    return bounds;
}

float CalculateLayoutItemAlignmentOffset(StackLayout& layout, StackLayoutItem& item)
{
    if (item.CurrentAlign <= 0.0f)
        return 0.0f;

    ImVec2 item_size = item.MeasuredBounds.GetSize();
    float layout_extent = layout.Type == ImGuiLayoutType_Horizontal ? layout.CurrentSize.y : layout.CurrentSize.x;
    float item_extent = layout.Type == ImGuiLayoutType_Horizontal ? item_size.y : item_size.x;

    if (item_extent <= 0.0f)
        return 0.0f;

    return ImFloor(item.CurrentAlign * (layout_extent - item_extent));
}

void TranslateLayoutItem(StackLayoutItem& item, const ImVec2& offset)
{
    if ((offset.x == 0.0f && offset.y == 0.0f) || item.VertexIndexBegin == item.VertexIndexEnd)
        return;

    ImDrawList* draw_list = ImGui::GetWindowDrawList();
    ImDrawVert* begin = draw_list->VtxBuffer.Data + item.VertexIndexBegin;
    ImDrawVert* end = draw_list->VtxBuffer.Data + item.VertexIndexEnd;

    for (ImDrawVert* vtx = begin; vtx < end; ++vtx)
    {
        vtx->pos.x += offset.x;
        vtx->pos.y += offset.y;
    }
}

ImVec2 BalanceLayoutItemAlignment(StackLayout& layout, StackLayoutItem& item)
{
    ImVec2 position_correction(0.0f, 0.0f);
    if (item.CurrentAlign > 0.0f)
    {
        float item_align_offset = CalculateLayoutItemAlignmentOffset(layout, item);
        if (item.CurrentAlignOffset != item_align_offset)
        {
            float offset = item_align_offset - item.CurrentAlignOffset;
            if (layout.Type == ImGuiLayoutType_Horizontal)
                position_correction.y = offset;
            else
                position_correction.x = offset;

            TranslateLayoutItem(item, position_correction);
            item.CurrentAlignOffset = item_align_offset;
        }
    }

    return position_correction;
}

void BalanceLayoutItemsAlignment(StackLayout& layout)
{
    for (StackLayoutItem& item : layout.Items)
        BalanceLayoutItemAlignment(layout, item);
}

void BalanceLayoutSprings(StackLayout& layout)
{
    float total_spring_weight = 0.0f;
    int last_spring_item_index = -1;
    for (int i = 0; i < static_cast<int>(layout.Items.size()); ++i)
    {
        StackLayoutItem& item = layout.Items[i];
        if (item.Type == StackLayoutItemType::Spring)
        {
            total_spring_weight += item.SpringWeight;
            last_spring_item_index = i;
        }
    }

    const bool is_horizontal = layout.Type == ImGuiLayoutType_Horizontal;
    const bool is_auto_sized = ((is_horizontal ? layout.Size.x : layout.Size.y) <= 0.0f) && (layout.Parent == nullptr);
    const float occupied_space = is_horizontal ? layout.MinimumSize.x : layout.MinimumSize.y;
    const float available_space = is_auto_sized ? occupied_space : (is_horizontal ? layout.CurrentSize.x : layout.CurrentSize.y);
    const float free_space = ImMax(available_space - occupied_space, 0.0f);

    float span_start = 0.0f;
    float current_weight = 0.0f;
    for (int i = 0; i < static_cast<int>(layout.Items.size()); ++i)
    {
        StackLayoutItem& item = layout.Items[i];
        if (item.Type != StackLayoutItemType::Spring)
            continue;

        float last_spring_size = item.SpringSize;
        if (free_space > 0.0f && total_spring_weight > 0.0f)
        {
            float next_weight = current_weight + item.SpringWeight;
            float span_end = ImFloor(i == last_spring_item_index ? free_space : (free_space * next_weight / total_spring_weight));
            item.SpringSize = span_end - span_start;
            span_start = span_end;
            current_weight = next_weight;
        }
        else
        {
            item.SpringSize = 0.0f;
        }

        if (last_spring_size != item.SpringSize)
        {
            float difference = item.SpringSize - last_spring_size;
            ImVec2 offset = is_horizontal ? ImVec2(difference, 0.0f) : ImVec2(0.0f, difference);

            item.MeasuredBounds.Max += offset;

            for (int j = i + 1; j < static_cast<int>(layout.Items.size()); ++j)
            {
                StackLayoutItem& translated_item = layout.Items[j];
                TranslateLayoutItem(translated_item, offset);
                translated_item.MeasuredBounds.Min += offset;
                translated_item.MeasuredBounds.Max += offset;
            }
        }
    }
}

bool HasAnyNonZeroSpring(StackLayout& layout)
{
    for (StackLayoutItem& item : layout.Items)
        if (item.Type == StackLayoutItemType::Spring && item.SpringWeight > 0.0f)
            return true;
    return false;
}

void BalanceChildLayouts(StackLayout& layout)
{
    for (StackLayout* child = layout.FirstChild; child != nullptr; child = child->NextSibling)
    {
        if (child->Type == ImGuiLayoutType_Horizontal && child->Size.x <= 0.0f)
            child->CurrentSize.x = layout.CurrentSize.x;
        else if (child->Type == ImGuiLayoutType_Vertical && child->Size.y <= 0.0f)
            child->CurrentSize.y = layout.CurrentSize.y;

        BalanceChildLayouts(*child);

        if (HasAnyNonZeroSpring(*child)
            && child->ParentItemIndex >= 0
            && child->ParentItemIndex < static_cast<int>(layout.Items.size()))
        {
            StackLayoutItem& item = layout.Items[child->ParentItemIndex];
            if (child->Type == ImGuiLayoutType_Horizontal && child->Size.x <= 0.0f)
                item.MeasuredBounds.Max.x = ImMax(item.MeasuredBounds.Max.x, item.MeasuredBounds.Min.x + layout.CurrentSize.x);
            else if (child->Type == ImGuiLayoutType_Vertical && child->Size.y <= 0.0f)
                item.MeasuredBounds.Max.y = ImMax(item.MeasuredBounds.Max.y, item.MeasuredBounds.Min.y + layout.CurrentSize.y);
        }
    }

    BalanceLayoutSprings(layout);
    BalanceLayoutItemsAlignment(layout);
}

void BeginLayoutItem(WindowStackLayoutState& state, StackLayout& layout)
{
    ImGuiContext& g = *GImGui;
    ImGuiWindow* window = ImGui::GetCurrentWindow();
    StackLayoutItem& item = *GenerateLayoutItem(state, layout, StackLayoutItemType::Item);

    item.CurrentAlign = layout.Align;
    if (item.CurrentAlign < 0.0f)
        item.CurrentAlign = DEFAULT_LAYOUT_ALIGN;

    item.CurrentAlignOffset = CalculateLayoutItemAlignmentOffset(layout, item);
    if (item.CurrentAlign > 0.0f)
    {
        if (layout.Type == ImGuiLayoutType_Horizontal)
        {
            window->DC.CursorPos.y += item.CurrentAlignOffset;
        }
        else
        {
            float new_position = window->DC.CursorPos.x + item.CurrentAlignOffset;
            SignedIndent(item.CurrentAlignOffset);
            window->DC.CursorPos.x = new_position;
        }
    }

    item.MeasuredBounds.Min = item.MeasuredBounds.Max = window->DC.CursorPos;
    item.VertexIndexBegin = item.VertexIndexEnd = window->DrawList->_VtxCurrentIdx;
}

void MeasureCurrentLayoutItem(ImGuiWindow* window, const ImVec2& bb_max)
{
    WindowStackLayoutState* state = FindFastActiveLayoutState(window);
    if (!state || !state->CurrentLayoutItem)
        return;

    state->CurrentLayoutItem->MeasuredBounds.Max =
        ImMax(state->CurrentLayoutItem->MeasuredBounds.Max, bb_max);
}

void EndLayoutItem(StackLayout& layout)
{
    ImGuiWindow* window = ImGui::GetCurrentWindow();
    IM_ASSERT(layout.CurrentItemIndex < static_cast<int>(layout.Items.size()));

    StackLayoutItem& item = layout.Items[layout.CurrentItemIndex];
    item.VertexIndexEnd = window->DrawList->_VtxCurrentIdx;

    if (item.CurrentAlign > 0.0f && layout.Type == ImGuiLayoutType_Vertical)
        SignedIndent(-item.CurrentAlignOffset);

    ImVec2 position_correction = BalanceLayoutItemAlignment(layout, item);
    item.MeasuredBounds.Min += position_correction;
    item.MeasuredBounds.Max += position_correction;

    if (layout.Type == ImGuiLayoutType_Horizontal)
        window->DC.CursorPos.y = layout.StartPos.y;
    else
        window->DC.CursorPos.x = layout.StartPos.x;

    layout.CurrentItemIndex++;
}

void AddLayoutSpring(WindowStackLayoutState& state, StackLayout& layout, float weight, float spacing)
{
    ImGuiContext& g = *GImGui;
    ImGuiWindow* window = g.CurrentWindow;
    StackLayoutItem* previous_item = &layout.Items[layout.CurrentItemIndex];

    if (layout.Type == ImGuiLayoutType_Horizontal)
        window->DC.CursorPos.x = previous_item->MeasuredBounds.Max.x;
    else
        window->DC.CursorPos.y = previous_item->MeasuredBounds.Max.y;

    EndLayoutItem(layout);

    StackLayoutItem* spring_item = GenerateLayoutItem(state, layout, StackLayoutItemType::Spring);
    spring_item->MeasuredBounds.Min = spring_item->MeasuredBounds.Max = window->DC.CursorPos;
    spring_item->VertexIndexBegin = spring_item->VertexIndexEnd = window->DrawList->_VtxCurrentIdx;

    if (weight < 0.0f)
        weight = 0.0f;
    spring_item->SpringWeight = weight;

    if (spacing < 0.0f)
        spacing = layout.Type == ImGuiLayoutType_Horizontal ? g.Style.ItemSpacing.x : g.Style.ItemSpacing.y;
    spring_item->SpringSpacing = spacing;

    if (spring_item->SpringSize > 0.0f || spacing > 0.0f)
    {
        ImVec2 spring_size;
        ImVec2 spring_spacing;
        if (layout.Type == ImGuiLayoutType_Horizontal)
        {
            spring_spacing = ImVec2(0.0f, g.Style.ItemSpacing.y);
            spring_size = ImVec2(spacing + spring_item->SpringSize, layout.CurrentSize.y);
        }
        else
        {
            spring_spacing = ImVec2(g.Style.ItemSpacing.x, 0.0f);
            spring_size = ImVec2(layout.CurrentSize.x, spacing + spring_item->SpringSize);
        }

        ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImFloor(spring_spacing));
        ImGui::Dummy(ImFloor(spring_size));
        ImGui::PopStyleVar();
        spring_item->MeasuredBounds.Max = ImMax(spring_item->MeasuredBounds.Max, g.LastItemData.Rect.Max);
        spring_item->VertexIndexEnd = window->DrawList->_VtxCurrentIdx;
    }

    layout.CurrentItemIndex++;
    BeginLayoutItem(state, layout);
}

void BeginLayout(ImGuiID id, ImGuiLayoutType type, ImVec2 size, float align)
{
    WindowStackLayoutState& state = GetWindowState();
    ImGuiWindow* window = ImGui::GetCurrentWindow();

    ImGui::PushID(reinterpret_cast<const void*>(static_cast<uintptr_t>(id)));

    StackLayout* layout = FindLayout(state, id, type);
    if (!layout)
        layout = CreateNewLayout(state, id, type, size);

    layout->Live = true;
    PushLayout(state, layout);

    if (layout->Size.x != size.x || layout->Size.y != size.y)
        layout->Size = size;

    layout->Align = align < 0.0f ? -1.0f : ImClamp(align, 0.0f, 1.0f);
    layout->CurrentItemIndex = 0;
    layout->CurrentSize.x = layout->Size.x > 0.0f ? layout->Size.x : layout->MinimumSize.x;
    layout->CurrentSize.y = layout->Size.y > 0.0f ? layout->Size.y : layout->MinimumSize.y;
    layout->StartPos = window->DC.CursorPos;
    layout->StartCursorMaxPos = window->DC.CursorMaxPos;

    if (type == ImGuiLayoutType_Vertical)
    {
        ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, 0.0f));
        ImGui::Dummy(ImVec2(0.0f, 0.0f));
        ImGui::PopStyleVar();

        layout->Indent = layout->StartPos.x - window->DC.CursorPos.x;
        SignedIndent(layout->Indent);
    }

    BeginLayoutItem(state, *layout);
}

void EndLayout(ImGuiLayoutType type)
{
    WindowStackLayoutState& state = GetWindowState();
    ImGuiWindow* window = ImGui::GetCurrentWindow();
    IM_ASSERT(state.CurrentLayout != nullptr);
    IM_ASSERT(state.CurrentLayout->Type == type);

    StackLayout* layout = state.CurrentLayout;
    EndLayoutItem(*layout);

    if (layout->CurrentItemIndex < static_cast<int>(layout->Items.size()))
        layout->Items.erase(layout->Items.begin() + layout->CurrentItemIndex, layout->Items.end());

    if (layout->Type == ImGuiLayoutType_Vertical)
        SignedIndent(-layout->Indent);

    PopLayout(state, layout);

    const bool auto_width = layout->Size.x <= 0.0f;
    const bool auto_height = layout->Size.y <= 0.0f;

    ImVec2 new_size = layout->Size;
    if (auto_width)
        new_size.x = layout->CurrentSize.x;
    if (auto_height)
        new_size.y = layout->CurrentSize.y;

    ImVec2 new_minimum_size = CalculateLayoutSize(*layout, true);
    if (new_minimum_size.x != layout->MinimumSize.x || new_minimum_size.y != layout->MinimumSize.y)
    {
        layout->MinimumSize = new_minimum_size;
        if (auto_width)
            new_size.x = new_minimum_size.x;
        if (auto_height)
            new_size.y = new_minimum_size.y;
    }

    if (!auto_width)
        new_size.x = layout->Size.x;
    if (!auto_height)
        new_size.y = layout->Size.y;

    layout->CurrentSize = new_size;

    ImVec2 measured_size = new_size;
    if ((auto_width || auto_height) && layout->Parent)
    {
        if (layout->Type == ImGuiLayoutType_Horizontal && auto_width && layout->Parent->CurrentSize.x > 0.0f)
            layout->CurrentSize.x = layout->Parent->CurrentSize.x;
        else if (layout->Type == ImGuiLayoutType_Vertical && auto_height && layout->Parent->CurrentSize.y > 0.0f)
            layout->CurrentSize.y = layout->Parent->CurrentSize.y;

        BalanceLayoutSprings(*layout);
        measured_size = layout->CurrentSize;
    }

    layout->CurrentSize = new_size;

    ImGui::PopID();

    ImVec2 current_layout_item_max(0.0f, 0.0f);
    if (state.CurrentLayoutItem)
        current_layout_item_max = ImMax(state.CurrentLayoutItem->MeasuredBounds.Max, layout->StartPos + new_size);

    window->DC.CursorPos = layout->StartPos;
    window->DC.CursorMaxPos = layout->StartCursorMaxPos;
    ImGui::ItemSize(new_size);
    ImGui::ItemAdd(ImRect(layout->StartPos, layout->StartPos + measured_size), 0, nullptr, ImGuiItemFlags_NoTabStop);

    if (state.CurrentLayoutItem)
        state.CurrentLayoutItem->MeasuredBounds.Max = current_layout_item_max;

    if (layout->Parent == nullptr)
        BalanceChildLayouts(*layout);
}
}

extern "C"
{
void dear_imgui_stack_begin_horizontal_str(const char* str_id, ImVec2 size, float align)
{
    ImGuiWindow* window = ImGui::GetCurrentWindow();
    BeginLayout(window->GetID(str_id), ImGuiLayoutType_Horizontal, size, align);
}

void dear_imgui_stack_begin_horizontal_ptr(const void* ptr_id, ImVec2 size, float align)
{
    ImGuiWindow* window = ImGui::GetCurrentWindow();
    BeginLayout(window->GetID(ptr_id), ImGuiLayoutType_Horizontal, size, align);
}

void dear_imgui_stack_begin_horizontal_int(int id, ImVec2 size, float align)
{
    ImGuiWindow* window = ImGui::GetCurrentWindow();
    BeginLayout(window->GetID(reinterpret_cast<void*>(static_cast<intptr_t>(id))), ImGuiLayoutType_Horizontal, size, align);
}

void dear_imgui_stack_begin_horizontal_id(ImGuiID id, ImVec2 size, float align)
{
    BeginLayout(id, ImGuiLayoutType_Horizontal, size, align);
}

void dear_imgui_stack_end_horizontal()
{
    EndLayout(ImGuiLayoutType_Horizontal);
}

void dear_imgui_stack_begin_vertical_str(const char* str_id, ImVec2 size, float align)
{
    ImGuiWindow* window = ImGui::GetCurrentWindow();
    BeginLayout(window->GetID(str_id), ImGuiLayoutType_Vertical, size, align);
}

void dear_imgui_stack_begin_vertical_ptr(const void* ptr_id, ImVec2 size, float align)
{
    ImGuiWindow* window = ImGui::GetCurrentWindow();
    BeginLayout(window->GetID(ptr_id), ImGuiLayoutType_Vertical, size, align);
}

void dear_imgui_stack_begin_vertical_int(int id, ImVec2 size, float align)
{
    ImGuiWindow* window = ImGui::GetCurrentWindow();
    BeginLayout(window->GetID(reinterpret_cast<void*>(static_cast<intptr_t>(id))), ImGuiLayoutType_Vertical, size, align);
}

void dear_imgui_stack_begin_vertical_id(ImGuiID id, ImVec2 size, float align)
{
    BeginLayout(id, ImGuiLayoutType_Vertical, size, align);
}

void dear_imgui_stack_end_vertical()
{
    EndLayout(ImGuiLayoutType_Vertical);
}

void dear_imgui_stack_spring(float weight, float spacing)
{
    WindowStackLayoutState& state = GetWindowState();
    IM_ASSERT(state.CurrentLayout != nullptr);
    AddLayoutSpring(state, *state.CurrentLayout, weight, spacing);
}

void dear_imgui_stack_item_add(ImGuiWindow* window, ImVec2 bb_max)
{
    MeasureCurrentLayoutItem(window, bb_max);
}

bool dear_imgui_stack_current_layout_type(ImGuiWindow* window, int* layout_type)
{
    WindowStackLayoutState* state = FindFastActiveLayoutState(window);
    if (!state || !state->CurrentLayout)
        return false;

    if (layout_type)
        *layout_type = static_cast<int>(state->CurrentLayout->Type);
    return true;
}

void dear_imgui_stack_suspend_layout()
{
    WindowStackLayoutState& state = GetWindowState();
    PushLayout(state, nullptr);
}

void dear_imgui_stack_resume_layout()
{
    WindowStackLayoutState& state = GetWindowState();
    IM_ASSERT(state.CurrentLayout == nullptr);
    IM_ASSERT(!state.LayoutStack.empty());
    PopLayout(state, nullptr);
}
}