From c5265450d9b2379d05499d68b5ee29f1a6acf12b Mon Sep 17 00:00:00 2001 From: Zachary Hall Date: Mon, 17 Jul 2023 14:56:02 -0700 Subject: [PATCH] Update open dialog and theme selector The open dialog now fills the window The theme selector now has a background again. --- file_browser.cpp | 6 ++-- file_browser.h | 6 ++-- imgui-filebrowser/imfilebrowser.h | 17 +++++----- main.cpp | 52 +++++++++++++++++-------------- 4 files changed, 46 insertions(+), 35 deletions(-) diff --git a/file_browser.cpp b/file_browser.cpp index adcd2b9..b0ff2cc 100644 --- a/file_browser.cpp +++ b/file_browser.cpp @@ -2,7 +2,7 @@ #include "imfilebrowser.h" -FileBrowser::FileBrowser(bool save) { +FileBrowser::FileBrowser(bool save, ImGuiFileBrowserFlags extra_fallback_flags) { #ifdef PORTALS g_set_application_name("Neko Player"); main_context = g_main_context_default(); @@ -12,7 +12,7 @@ FileBrowser::FileBrowser(bool save) { inner_filter_type = g_variant_type_new("a(us)"); #endif this->save = save; - fallback = ImGui::FileBrowser(save ? ImGuiFileBrowserFlags_CreateNewDir|ImGuiFileBrowserFlags_EnterNewFilename : 0); + fallback = ImGui::FileBrowser((save ? ImGuiFileBrowserFlags_CreateNewDir|ImGuiFileBrowserFlags_EnterNewFilename : 0) + extra_fallback_flags); } void FileBrowser::SetTypeFilters(string name, vector filters) { filter_name = name; @@ -63,9 +63,11 @@ path FileBrowser::GetSelected() { #endif } void FileBrowser::SetWindowSize(int w, int h) { + window_size = ImVec2(w, h); fallback.SetWindowSize(w, h); } void FileBrowser::SetWindowPos(int x, int y) { + window_pos = ImVec2(x, y); fallback.SetWindowPos(x, y); } void FileBrowser::Open() { diff --git a/file_browser.h b/file_browser.h index af293e9..790e5b0 100644 --- a/file_browser.h +++ b/file_browser.h @@ -25,8 +25,10 @@ class FileBrowser { GMainContext *main_context; static void FileBrowserOpenCallback(GObject *src, GAsyncResult *res, gpointer data); static void FileBrowserSaveCallback(GObject *src, GAsyncResult *res, gpointer data); - bool open = false; #endif + optional window_pos; + ImVec2 window_size; + bool open = false; bool save; ImGui::FileBrowser fallback; path pwd; @@ -46,6 +48,6 @@ class FileBrowser { void Open(); void ClearSelected(); bool IsOpened(); - FileBrowser(bool save); + FileBrowser(bool save, ImGuiFileBrowserFlags extra_fallback_flags = 0); ~FileBrowser(); }; \ No newline at end of file diff --git a/imgui-filebrowser/imfilebrowser.h b/imgui-filebrowser/imfilebrowser.h index 5312b50..40d9b91 100644 --- a/imgui-filebrowser/imfilebrowser.h +++ b/imgui-filebrowser/imfilebrowser.h @@ -27,6 +27,8 @@ enum ImGuiFileBrowserFlags_ ImGuiFileBrowserFlags_CloseOnEsc = 1 << 5, // close file browser when pressing 'ESC' ImGuiFileBrowserFlags_CreateNewDir = 1 << 6, // allow user to create new directory ImGuiFileBrowserFlags_MultipleSelection = 1 << 7, // allow user to select multiple files. this will hide ImGuiFileBrowserFlags_EnterNewFilename + ImGuiFileBrowserFlags_NoMove = 1 << 8, // Disable movement of the window + ImGuiFileBrowserFlags_NoResize = 1 << 9, // Disable resizing of the window }; namespace ImGui @@ -332,22 +334,23 @@ inline void ImGui::FileBrowser::Display() { if (posIsSet_) SetNextWindowPos( - ImVec2(static_cast(posX_), static_cast(posY_)), - ImGuiCond_FirstUseEver); + ImVec2(static_cast(posX_), static_cast(posY_))); SetNextWindowSize( - ImVec2(static_cast(width_), static_cast(height_)), - ImGuiCond_FirstUseEver); + ImVec2(static_cast(width_), static_cast(height_))); } if(flags_ & ImGuiFileBrowserFlags_NoModal) { - if(!BeginPopup(openLabel_.c_str())) + if(!BeginPopup(openLabel_.c_str(), + (flags_ & ImGuiFileBrowserFlags_NoMove ? ImGuiWindowFlags_NoMove : 0) | + (flags_ & ImGuiFileBrowserFlags_NoResize ? ImGuiWindowFlags_NoResize : 0))) { return; } } else if(!BeginPopupModal(openLabel_.c_str(), nullptr, - flags_ & ImGuiFileBrowserFlags_NoTitleBar ? - ImGuiWindowFlags_NoTitleBar : 0)) + (flags_ & ImGuiFileBrowserFlags_NoMove ? ImGuiWindowFlags_NoMove : 0) | + (flags_ & ImGuiFileBrowserFlags_NoResize ? ImGuiWindowFlags_NoResize : 0) | + (flags_ & ImGuiFileBrowserFlags_NoTitleBar ? ImGuiWindowFlags_NoTitleBar : 0))) { return; } diff --git a/main.cpp b/main.cpp index 21649e8..3fea10c 100644 --- a/main.cpp +++ b/main.cpp @@ -177,7 +177,7 @@ int main(int, char**) bool show_demo_window = false; ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); - FileBrowser fileDialog(false); + FileBrowser fileDialog(false, ImGuiFileBrowserFlags_NoTitleBar|ImGuiFileBrowserFlags_NoMove|ImGuiFileBrowserFlags_NoResize); fileDialog.SetTitle("Open..."); fileDialog.SetTypeFilters("Audio files", { ".wav", ".ogg", ".mp3", ".qoa", ".flac", ".xm", ".mod"}); std::string userdir = std::getenv( @@ -380,34 +380,38 @@ int main(int, char**) ImGui::SetNextItemWidth(ImGui::GetWindowWidth() - ImGui::GetCursorPosX() - ImGui::GetStyle().WindowPadding.x); ImGui::InputText("##FilterInput", filter, 1024); ImGui::Text("Select a theme..."); - ImVec2 ListBoxSize = ImVec2(ImGui::GetWindowWidth() - (ImGui::GetStyle().WindowPadding.x * 2.0f), -ImGui::GetTextLineHeightWithSpacing() - ImGui::GetStyle().WindowPadding.y); - if (ImGui::BeginTable("##Themes", 2, 0, ListBoxSize)) { - ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthStretch); - ImGui::TableSetupColumn("Remove", ImGuiTableColumnFlags_WidthFixed); - for (auto themePath : Theme::availableThemes) { - if (themePath.stem().string().starts_with(filter)) { - ImGui::TableNextRow(); - ImGui::TableSetColumnIndex(0); - const bool is_selected = themePath == theme->file_path; - if (ImGui::Selectable(themePath.stem().generic_string().c_str(), is_selected, 0)) { - delete theme; - theme = new Theme(themePath); - theme->Apply(accent_color); - break; - } - if (is_selected) { - ImGui::SetItemDefaultFocus(); - } else { - ImGui::TableSetColumnIndex(1); - if (ImGui::SmallButton((string(ICON_FK_WINDOW_CLOSE "##") + themePath.stem().generic_string()).c_str())) { - std::filesystem::remove(themePath); - Theme::updateAvailableThemes(); + ImVec2 ChildSize = ImVec2(ImGui::GetWindowWidth() - (ImGui::GetStyle().WindowPadding.x * 2.0f), -ImGui::GetTextLineHeightWithSpacing() - ImGui::GetStyle().WindowPadding.y); + if (ImGui::BeginChildFrame(ImGui::GetID("##ThemesContainer"), ChildSize)) { + ImVec2 TableSize = ImVec2(ImGui::GetWindowWidth() - (ImGui::GetStyle().WindowPadding.x), -ImGui::GetTextLineHeightWithSpacing() - ImGui::GetStyle().WindowPadding.y); + if (ImGui::BeginTable("##Themes", 2, 0, TableSize)) { + ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthStretch); + ImGui::TableSetupColumn("Remove", ImGuiTableColumnFlags_WidthFixed); + for (auto themePath : Theme::availableThemes) { + if (themePath.stem().string().starts_with(filter)) { + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); + const bool is_selected = themePath == theme->file_path; + if (ImGui::Selectable(themePath.stem().generic_string().c_str(), is_selected, 0)) { + delete theme; + theme = new Theme(themePath); + theme->Apply(accent_color); break; } + if (is_selected) { + ImGui::SetItemDefaultFocus(); + } else { + ImGui::TableSetColumnIndex(1); + if (ImGui::SmallButton((string(ICON_FK_WINDOW_CLOSE "##") + themePath.stem().generic_string()).c_str())) { + std::filesystem::remove(themePath); + Theme::updateAvailableThemes(); + break; + } + } } } + ImGui::EndTable(); } - ImGui::EndTable(); + ImGui::EndChildFrame(); } ImGui::SetNextItemWidth(ImGui::GetWindowWidth() - (ImGui::GetStyle().FramePadding.x * 4)); if (ImGui::SliderFloat("##AccentColor", &accent_color, 0.0, 360.0, "UI hue: %.0f°", ImGuiSliderFlags_NoRoundToFormat)) {