yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
window_browser.cc
Go to the documentation of this file.
2#include "util/i18n/tr.h"
3
4#include <algorithm>
5#include <cctype>
6#include <string>
7#include <utility>
8#include <vector>
9
10#include "absl/strings/str_format.h"
12#include "app/gui/core/icons.h"
16#include "imgui/imgui.h"
17
18namespace yaze {
19namespace editor {
20
21namespace {
22
23std::string LowercaseCopy(std::string value) {
24 std::transform(
25 value.begin(), value.end(), value.begin(),
26 [](unsigned char c) { return static_cast<char>(::tolower(c)); });
27 return value;
28}
29
30bool MatchesWindowSearch(const std::string& query,
31 const WindowDescriptor& window) {
32 if (query.empty()) {
33 return true;
34 }
35
36 const std::string search_str = LowercaseCopy(query);
37 return LowercaseCopy(window.display_name).find(search_str) !=
38 std::string::npos ||
39 LowercaseCopy(window.card_id).find(search_str) != std::string::npos ||
40 LowercaseCopy(window.shortcut_hint).find(search_str) !=
41 std::string::npos;
42}
43
44} // namespace
45
47 : window_manager_(window_manager) {}
48
49void WindowBrowser::Draw(size_t session_id, bool* p_open) {
50 const ImGuiViewport* viewport = ImGui::GetMainViewport();
51 ImVec2 max_window_size(1600.0f, 1000.0f);
52 ImVec2 default_window_size(1080.0f, 700.0f);
53 if (viewport) {
54 max_window_size = ImVec2(std::max(760.0f, viewport->WorkSize.x * 0.95f),
55 std::max(520.0f, viewport->WorkSize.y * 0.95f));
56 default_window_size = ImVec2(
57 std::clamp(viewport->WorkSize.x * 0.72f, 880.0f, max_window_size.x),
58 std::clamp(viewport->WorkSize.y * 0.76f, 600.0f, max_window_size.y));
59 const ImVec2 center(viewport->WorkPos.x + viewport->WorkSize.x * 0.5f,
60 viewport->WorkPos.y + viewport->WorkSize.y * 0.5f);
61 ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
62 }
63
64 ImGui::SetNextWindowSize(default_window_size, ImGuiCond_Appearing);
65 ImGui::SetNextWindowSizeConstraints(ImVec2(780, 520), max_window_size);
66
67 if (ImGui::Begin(
68 absl::StrFormat("%s Window Browser", ICON_MD_DASHBOARD).c_str(),
69 p_open, ImGuiWindowFlags_NoDocking)) {
70 std::vector<std::string> categories =
72 std::sort(categories.begin(), categories.end());
73 if (category_filter_ != "All" &&
74 std::find(categories.begin(), categories.end(), category_filter_) ==
75 categories.end()) {
76 category_filter_ = "All";
77 }
78
79 const auto all_windows = window_manager_.GetWindowsInSession(session_id);
80 auto count_windows = [&](const std::string& category,
81 bool visible_only) -> int {
82 int count = 0;
83 for (const auto& window_id : all_windows) {
84 const auto* window =
85 window_manager_.GetWindowDescriptor(session_id, window_id);
86 if (!window) {
87 continue;
88 }
89 if (category != "All" && window->category != category) {
90 continue;
91 }
92 const bool visible =
93 window->visibility_flag && *window->visibility_flag;
94 if (!visible_only || visible) {
95 ++count;
96 }
97 }
98 return count;
99 };
100 auto set_filtered_windows_visible = [&](const std::string& category,
101 bool visible) {
102 for (const auto& window_id : all_windows) {
103 const auto* window =
104 window_manager_.GetWindowDescriptor(session_id, window_id);
105 if (!window) {
106 continue;
107 }
108 if (category != "All" && window->category != category) {
109 continue;
110 }
111 if (visible) {
112 window_manager_.OpenWindow(session_id, window->card_id);
113 } else {
114 window_manager_.CloseWindow(session_id, window->card_id);
115 }
116 }
117 };
118
119 ImGui::SetNextItemWidth(
120 std::max(280.0f, ImGui::GetContentRegionAvail().x * 0.45f));
121 ImGui::InputTextWithHint(
122 "##Search",
123 absl::StrFormat("%s Search windows...", ICON_MD_SEARCH).c_str(),
125 ImGui::SameLine();
126 if (ImGui::Button(ICON_MD_CLEAR " Clear")) {
127 search_filter_[0] = '\0';
128 }
129 ImGui::SameLine();
130 if (category_filter_ == "All") {
131 if (ImGui::Button(ICON_MD_VISIBILITY " Show All")) {
132 set_filtered_windows_visible("All", true);
133 }
134 ImGui::SameLine();
135 if (ImGui::Button(ICON_MD_VISIBILITY_OFF " Hide All")) {
136 set_filtered_windows_visible("All", false);
137 }
138 } else {
139 if (ImGui::Button(ICON_MD_VISIBILITY " Show Category")) {
140 set_filtered_windows_visible(category_filter_, true);
141 }
142 ImGui::SameLine();
143 if (ImGui::Button(ICON_MD_VISIBILITY_OFF " Hide Category")) {
144 set_filtered_windows_visible(category_filter_, false);
145 }
146 }
147
148 ImGui::Separator();
149
150 const float content_height = ImGui::GetContentRegionAvail().y;
151 const float max_sidebar_width =
152 std::max(220.0f, ImGui::GetContentRegionAvail().x * 0.50f);
153 float category_sidebar_width =
155 max_sidebar_width);
156
157 if (ImGui::BeginChild("##WindowBrowserCategories",
158 ImVec2(category_sidebar_width, content_height),
159 true)) {
160 const int visible_total = count_windows("All", true);
161 const int filtered_total = count_windows("All", false);
162 std::string all_label =
163 absl::StrFormat("%s All Windows (%d/%d)", ICON_MD_DASHBOARD,
164 visible_total, filtered_total);
165 if (ImGui::Selectable(all_label.c_str(), category_filter_ == "All")) {
166 category_filter_ = "All";
167 }
168 ImGui::Separator();
169
170 for (const auto& category : categories) {
171 const int category_total = count_windows(category, false);
172 if (category_total <= 0) {
173 continue;
174 }
175 const int visible_in_category = count_windows(category, true);
176 const std::string icon =
178 std::string label =
179 absl::StrFormat("%s %s (%d/%d)", icon.c_str(), category.c_str(),
180 visible_in_category, category_total);
181 if (ImGui::Selectable(label.c_str(), category_filter_ == category)) {
182 category_filter_ = category;
183 }
184 }
185 }
186 ImGui::EndChild();
187
188 ImGui::SameLine(0.0f, 0.0f);
189
190 const float splitter_width = 6.0f;
191 const ImVec2 splitter_pos = ImGui::GetCursorScreenPos();
192 ImGui::InvisibleButton("##WindowBrowserSplitter",
193 ImVec2(splitter_width, content_height));
194 const bool splitter_hovered = ImGui::IsItemHovered();
195 const bool splitter_active = ImGui::IsItemActive();
196 if (splitter_hovered || splitter_active) {
197 ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeEW);
198 }
199 if (splitter_hovered &&
200 ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
203 category_sidebar_width =
205 max_sidebar_width);
206 }
207 if (splitter_active) {
208 category_sidebar_width =
209 std::clamp(category_sidebar_width + ImGui::GetIO().MouseDelta.x,
210 220.0f, max_sidebar_width);
211 window_manager_.SetWindowBrowserCategoryWidth(category_sidebar_width);
212 ImGui::SetTooltip(tr("Width: %.0f px"), category_sidebar_width);
213 }
214
215 ImVec4 splitter_color = gui::GetOutlineVec4();
216 splitter_color.w =
217 splitter_active ? 0.95f : (splitter_hovered ? 0.72f : 0.35f);
218 ImGui::GetWindowDrawList()->AddLine(
219 ImVec2(splitter_pos.x + splitter_width * 0.5f, splitter_pos.y),
220 ImVec2(splitter_pos.x + splitter_width * 0.5f,
221 splitter_pos.y + content_height),
222 ImGui::GetColorU32(splitter_color), splitter_active ? 2.0f : 1.0f);
223
224 ImGui::SameLine(0.0f, 0.0f);
225
226 if (ImGui::BeginChild("##WindowBrowserTable", ImVec2(0, content_height),
227 false)) {
228 if (ImGui::BeginTable("##WindowTable", 5,
229 ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg |
230 ImGuiTableFlags_Borders |
231 ImGuiTableFlags_Resizable)) {
232 ImGui::TableSetupColumn("Visible", ImGuiTableColumnFlags_WidthFixed,
233 60);
234 ImGui::TableSetupColumn("Pin", ImGuiTableColumnFlags_WidthFixed, 36);
235 ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthStretch);
236 ImGui::TableSetupColumn("Category", ImGuiTableColumnFlags_WidthFixed,
237 130);
238 ImGui::TableSetupColumn("Shortcut", ImGuiTableColumnFlags_WidthFixed,
239 110);
240 ImGui::TableHeadersRow();
241
242 auto windows = (category_filter_ == "All") ? all_windows
243 : std::vector<std::string>{};
244 if (category_filter_ != "All") {
245 auto category_windows = window_manager_.GetWindowsInCategory(
246 session_id, category_filter_);
247 windows.reserve(category_windows.size());
248 for (const auto& window : category_windows) {
249 windows.push_back(window.card_id);
250 }
251 }
252
253 for (const auto& window_id : windows) {
254 const auto* window =
255 window_manager_.GetWindowDescriptor(session_id, window_id);
256 if (!window) {
257 continue;
258 }
259
260 if (!MatchesWindowSearch(search_filter_, *window)) {
261 continue;
262 }
263
264 ImGui::TableNextRow();
265
266 ImGui::TableNextColumn();
267 if (window->visibility_flag) {
268 bool visible = *window->visibility_flag;
269 if (ImGui::Checkbox(
270 absl::StrFormat("##vis_%s", window->card_id).c_str(),
271 &visible)) {
272 window_manager_.ToggleWindow(session_id, window->card_id);
273 }
274 }
275
276 ImGui::TableNextColumn();
277 const bool is_pinned =
278 window_manager_.IsWindowPinned(window->card_id);
279 const ImVec4 pin_color =
281 const float pin_side =
283 ImGui::PushID(
284 absl::StrFormat("browser_pin_%s", window->card_id).c_str());
286 is_pinned ? ICON_MD_PUSH_PIN : ICON_MD_PIN,
287 ImVec2(pin_side, pin_side),
288 is_pinned ? "Unpin window" : "Pin window", is_pinned,
289 pin_color, "window_browser", window->card_id.c_str())) {
290 window_manager_.SetWindowPinned(session_id, window->card_id,
291 !is_pinned);
292 }
293 ImGui::PopID();
294
295 ImGui::TableNextColumn();
296 ImGui::Text("%s %s", window->icon.c_str(),
297 window->display_name.c_str());
298 if (ImGui::IsItemHovered() &&
299 ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
300 window_manager_.OpenWindow(session_id, window->card_id);
301 const std::string window_name =
303 if (!window_name.empty()) {
304 ImGui::SetWindowFocus(window_name.c_str());
305 }
306 }
307
308 ImGui::TableNextColumn();
309 ImGui::TextUnformatted(window->category.c_str());
310
311 ImGui::TableNextColumn();
312 if (window->shortcut_hint.empty()) {
313 ImGui::TextDisabled("-");
314 } else {
315 ImGui::TextDisabled("%s", window->shortcut_hint.c_str());
316 }
317 }
318
319 ImGui::EndTable();
320 }
321 }
322 ImGui::EndChild();
323 }
324 ImGui::End();
325}
326
327} // namespace editor
328} // namespace yaze
WindowBrowser(WorkspaceWindowManager &window_manager)
WorkspaceWindowManager & window_manager_
void Draw(size_t session_id, bool *p_open)
Central registry for all editor cards with session awareness and dependency injection.
std::string GetWorkspaceWindowName(size_t session_id, const std::string &base_window_id) const
Resolve the exact ImGui window name for a panel by base ID.
std::vector< WindowDescriptor > GetWindowsInCategory(size_t session_id, const std::string &category) const
const WindowDescriptor * GetWindowDescriptor(size_t session_id, const std::string &base_window_id) const
std::vector< std::string > GetWindowsInSession(size_t session_id) const
void SetWindowPinned(size_t session_id, const std::string &base_window_id, bool pinned)
bool CloseWindow(size_t session_id, const std::string &base_window_id)
std::vector< std::string > GetAllWindowCategories(size_t session_id) const
static std::string GetCategoryIcon(const std::string &category)
bool OpenWindow(size_t session_id, const std::string &base_window_id)
static constexpr float GetDefaultWindowBrowserCategoryWidth()
bool IsWindowPinned(size_t session_id, const std::string &base_window_id) const
bool ToggleWindow(size_t session_id, const std::string &base_window_id)
void SetWindowBrowserCategoryWidth(float width, bool notify=true)
static float GetStandardWidgetHeight()
#define ICON_MD_SEARCH
Definition icons.h:1673
#define ICON_MD_VISIBILITY
Definition icons.h:2101
#define ICON_MD_VISIBILITY_OFF
Definition icons.h:2102
#define ICON_MD_PIN
Definition icons.h:1470
#define ICON_MD_CLEAR
Definition icons.h:416
#define ICON_MD_DASHBOARD
Definition icons.h:517
#define ICON_MD_PUSH_PIN
Definition icons.h:1529
bool MatchesWindowSearch(const std::string &query, const WindowDescriptor &window)
bool TransparentIconButton(const char *icon, const ImVec2 &size, const char *tooltip, bool is_active, const ImVec4 &active_color, const char *panel_id, const char *anim_id)
Draw a transparent icon button (hover effect only).
ImVec4 GetPrimaryVec4()
ImVec4 GetTextDisabledVec4()
ImVec4 GetOutlineVec4()
Metadata for a dockable editor window (formerly PanelInfo)