MCPcopy Create free account
hub / github.com/chrxh/alien / PathBox

Function PathBox

external/ImFileDialog/ImFileDialog.cpp:95–223  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

93 return ret;
94 }
95 bool PathBox(const char* label, std::filesystem::path& path, char* pathBuffer, ImVec2 size_arg) {
96 ImGuiWindow* window = ImGui::GetCurrentWindow();
97 if (window->SkipItems)
98 return false;
99
100 bool ret = false;
101 const ImGuiID id = window->GetID(label);
102 int* state = window->StateStorage.GetIntRef(id, 0);
103
104 ImGui::SameLine();
105
106 ImGuiContext& g = *GImGui;
107 const ImGuiStyle& style = g.Style;
108 ImVec2 pos = window->DC.CursorPos;
109 ImVec2 uiPos = ImGui::GetCursorPos();
110 ImVec2 size = ImGui::CalcItemSize(size_arg, 200, GUI_ELEMENT_SIZE);
111 const ImRect bb(pos, pos + size);
112
113 // buttons
114 if (!(*state & 0b001)) {
115 ImGui::PushClipRect(bb.Min, bb.Max, false);
116
117 // background
118 bool hovered = g.IO.MousePos.x >= bb.Min.x && g.IO.MousePos.x <= bb.Max.x &&
119 g.IO.MousePos.y >= bb.Min.y && g.IO.MousePos.y <= bb.Max.y;
120 bool clicked = hovered && ImGui::IsMouseReleased(ImGuiMouseButton_Left);
121 bool anyOtherHC = false; // are any other items hovered or clicked?
122 window->DrawList->AddRectFilled(pos, pos + size, ImGui::ColorConvertFloat4ToU32(ImGui::GetStyle().Colors[(*state & 0b10) ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg]));
123
124 // fetch the buttons (so that we can throw some away if needed)
125 std::vector<std::string> btnList;
126 float totalWidth = 0.0f;
127 for (auto comp : path) {
128 std::string section = comp.string();
129 if (section.size() == 1 && (section[0] == '\\' || section[0] == '/'))
130 continue;
131
132 totalWidth += ImGui::CalcTextSize(section.c_str()).x + style.FramePadding.x * 2.0f + GUI_ELEMENT_SIZE;
133 btnList.push_back(section);
134 }
135 totalWidth -= GUI_ELEMENT_SIZE;
136
137 // UI buttons
138 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, ImGui::GetStyle().ItemSpacing.y));
139 ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 0.0f);
140 bool isFirstElement = true;
141 for (int i = 0; i < btnList.size(); i++) {
142 if (totalWidth > size.x - 30 && i != btnList.size() - 1) { // trim some buttons if there's not enough space
143 float elSize = ImGui::CalcTextSize(btnList[i].c_str()).x + style.FramePadding.x * 2.0f + GUI_ELEMENT_SIZE;
144 totalWidth -= elSize;
145 continue;
146 }
147
148 ImGui::PushID(i);
149 if (!isFirstElement) {
150 ImGui::ArrowButtonEx("##dir_dropdown", ImGuiDir_Right, ImVec2(GUI_ELEMENT_SIZE, GUI_ELEMENT_SIZE));
151 anyOtherHC |= ImGui::IsItemHovered() | ImGui::IsItemClicked();
152 ImGui::SameLine();

Callers 1

m_renderFileDialogMethod · 0.85

Calls 2

ImVec2Class · 0.85
sizeMethod · 0.80

Tested by

no test coverage detected