Skip to content

Commit 365a743

Browse files
committed
Make code editor child window collapsible
Toggles drawing of the whole child as interfering with the user-resizable window makes ImGui forget the previously set height when expanding it again.
1 parent 0131b4e commit 365a743

2 files changed

Lines changed: 39 additions & 12 deletions

File tree

src/gui/preset_editor/CodeEditorWindow.cpp

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "CodeEditorWindow.h"
22

33
#include "CodeContextInformation.h"
4+
#include "IconsFontAwesome7.h"
45

56
#include "imgui.h"
67

@@ -15,25 +16,42 @@ void CodeEditorWindow::Draw()
1516

1617
ImGui::PushID("CodeEditorWindow");
1718

18-
if (ImGui::BeginChild("CodeEditorChild", ImVec2(0, 500), ImGuiChildFlags_Borders | ImGuiChildFlags_ResizeY, ImGuiWindowFlags_None))
19+
if (ImGui::Button(_collapsed ? ICON_FA_CARET_RIGHT "##CollapseButton" : ICON_FA_CARET_DOWN "##CollapseButton"))
1920
{
20-
if (ImGui::BeginTabBar("OpenCodeTabs", ImGuiTabBarFlags_AutoSelectNewTabs))
21+
_collapsed = !_collapsed;
22+
}
23+
24+
ImGui::SameLine();
25+
26+
if (!_collapsed)
27+
{
28+
ImGui::SetNextWindowSize(ImVec2(0, 500), ImGuiCond_FirstUseEver);
29+
ImGui::SetNextWindowBgAlpha(_collapsed ? 0.0f : 0.5f);
30+
if (ImGui::BeginChild("CodeEditorChild", ImVec2(0, 500), ImGuiChildFlags_ResizeY, ImGuiWindowFlags_None))
2131
{
22-
for (auto it = begin(_codeEditorTabs); it != end(_codeEditorTabs);)
32+
if (ImGui::BeginTabBar("OpenCodeTabs", ImGuiTabBarFlags_AutoSelectNewTabs))
2333
{
24-
if (it->Draw())
34+
for (auto it = begin(_codeEditorTabs); it != end(_codeEditorTabs);)
2535
{
26-
++it;
27-
continue;
28-
}
36+
if (it->Draw())
37+
{
38+
++it;
39+
continue;
40+
}
2941

30-
it = _codeEditorTabs.erase(it);
42+
it = _codeEditorTabs.erase(it);
43+
}
3144
}
3245

3346
ImGui::EndTabBar();
3447
}
48+
ImGui::EndChild();
49+
}
50+
else
51+
{
52+
ImGui::SameLine();
53+
ImGui::TextDisabled("%s", ("(" + std::to_string(_codeEditorTabs.size()) + " Tabs Open)").c_str());
3554
}
36-
ImGui::EndChild();
3755

3856
if (_codeEditorTabs.empty())
3957
{
@@ -45,6 +63,9 @@ void CodeEditorWindow::Draw()
4563

4664
void CodeEditorWindow::OpenCodeInTab(ExpressionCodeTypes type, std::string& code, int index)
4765
{
66+
// Fold out the editor if the user opens a new tab or focuses an existing one.
67+
_collapsed = false;
68+
4869
std::string newTabTitle = CodeContextInformation::GetContextName(type, index);
4970
for (auto& tab : _codeEditorTabs)
5071
{
@@ -55,8 +76,12 @@ void CodeEditorWindow::OpenCodeInTab(ExpressionCodeTypes type, std::string& code
5576
}
5677
}
5778

58-
_codeEditorTabs.emplace_back(type, code, index);
79+
CodeEditorTab newCodeEditorTab(type, code, index);
80+
newCodeEditorTab.SetSelected();
81+
_codeEditorTabs.push_back(std::move(newCodeEditorTab));
82+
5983
_visible = true;
6084
}
6185

86+
6287
} // namespace Editor

src/gui/preset_editor/CodeEditorWindow.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ class CodeEditorWindow
2020
void OpenCodeInTab(ExpressionCodeTypes type, std::string& code, int index);
2121

2222
private:
23-
bool _visible{false};
24-
std::list<CodeEditorTab> _codeEditorTabs;
23+
bool _visible{false}; //!< Determines if the code editor window is visible.
24+
bool _collapsed{false}; //!< If true, the window is displayed collapsed, e.g. the tab contents aren't rendered.
25+
26+
std::list<CodeEditorTab> _codeEditorTabs; //!< Currently opened editor tabs.
2527
};
2628

2729
} // namespace Editor

0 commit comments

Comments
 (0)