From 0b71568d9775c84aa880e5ae9208919281d99d1a Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 6 Dec 2023 11:05:02 +0100 Subject: [PATCH] impr: Better UI handling when window is very small --- main/gui/include/window.hpp | 5 +- main/gui/source/window/window.cpp | 100 +++++++----------- .../content/views/view_command_palette.hpp | 4 +- plugins/builtin/romfs/layouts/minimal.hexlyt | 2 +- 4 files changed, 40 insertions(+), 71 deletions(-) diff --git a/main/gui/include/window.hpp b/main/gui/include/window.hpp index a3cf5fb54..0639ff0fd 100644 --- a/main/gui/include/window.hpp +++ b/main/gui/include/window.hpp @@ -34,10 +34,7 @@ namespace hex { void setupNativeWindow(); void beginNativeWindowFrame(); void endNativeWindowFrame(); - void drawTitleBar(); - - void drawTitleBarBorderless() const; - void drawTitleBarBorder(); + void drawTitleBar() const; void frameBegin(); void frame(); diff --git a/main/gui/source/window/window.cpp b/main/gui/source/window/window.cpp index 761d0382c..454056d61 100644 --- a/main/gui/source/window/window.cpp +++ b/main/gui/source/window/window.cpp @@ -250,7 +250,7 @@ namespace hex { } } - void Window::drawTitleBarBorderless() const { + void Window::drawTitleBar() const { auto titleBarHeight = ImGui::GetCurrentWindow()->MenuBarHeight(); auto buttonSize = ImVec2(titleBarHeight * 1.5F, titleBarHeight - 1); @@ -259,49 +259,55 @@ namespace hex { ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImGui::GetColorU32(ImGuiCol_ScrollbarGrabActive)); ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetColorU32(ImGuiCol_ScrollbarGrabHovered)); + const auto windowSize = ImHexApi::System::getMainWindowSize(); + const auto searchBoxSize = ImVec2(std::sqrt(windowSize.x) * 14_scaled, titleBarHeight - 3_scaled); + const auto searchBoxPos = ImVec2((windowSize / 2 - searchBoxSize / 2).x, 3_scaled); + // Custom titlebar buttons implementation for borderless window mode auto &titleBarButtons = ContentRegistry::Interface::impl::getTitleBarButtons(); // Draw custom title bar buttons if (!titleBarButtons.empty()) { ImGui::SetCursorPosX(ImGui::GetWindowWidth() - buttonSize.x * float(4 + titleBarButtons.size())); - for (const auto &[icon, tooltip, callback]: titleBarButtons) { - if (ImGuiExt::TitleBarButton(icon.c_str(), buttonSize)) { - callback(); + + if (ImGui::GetCursorPosX() > (searchBoxPos.x + searchBoxSize.x)) { + for (const auto &[icon, tooltip, callback]: titleBarButtons) { + if (ImGuiExt::TitleBarButton(icon.c_str(), buttonSize)) { + callback(); + } + ImGuiExt::InfoTooltip(Lang(tooltip)); } - ImGuiExt::InfoTooltip(Lang(tooltip)); } } - // Draw minimize, restore and maximize buttons - ImGui::SetCursorPosX(ImGui::GetWindowWidth() - buttonSize.x * 3); - if (ImGuiExt::TitleBarButton(ICON_VS_CHROME_MINIMIZE, buttonSize)) - glfwIconifyWindow(this->m_window); - if (glfwGetWindowAttrib(this->m_window, GLFW_MAXIMIZED)) { - if (ImGuiExt::TitleBarButton(ICON_VS_CHROME_RESTORE, buttonSize)) - glfwRestoreWindow(this->m_window); - } else { - if (ImGuiExt::TitleBarButton(ICON_VS_CHROME_MAXIMIZE, buttonSize)) - glfwMaximizeWindow(this->m_window); + if (ImHexApi::System::isBorderlessWindowModeEnabled()) { + // Draw minimize, restore and maximize buttons + ImGui::SetCursorPosX(ImGui::GetWindowWidth() - buttonSize.x * 3); + if (ImGuiExt::TitleBarButton(ICON_VS_CHROME_MINIMIZE, buttonSize)) + glfwIconifyWindow(this->m_window); + if (glfwGetWindowAttrib(this->m_window, GLFW_MAXIMIZED)) { + if (ImGuiExt::TitleBarButton(ICON_VS_CHROME_RESTORE, buttonSize)) + glfwRestoreWindow(this->m_window); + } else { + if (ImGuiExt::TitleBarButton(ICON_VS_CHROME_MAXIMIZE, buttonSize)) + glfwMaximizeWindow(this->m_window); + } + + ImGui::PushStyleColor(ImGuiCol_ButtonActive, 0xFF7A70F1); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, 0xFF2311E8); + + // Draw close button + if (ImGuiExt::TitleBarButton(ICON_VS_CHROME_CLOSE, buttonSize)) { + ImHexApi::System::closeImHex(); + } + + ImGui::PopStyleColor(2); } - - ImGui::PushStyleColor(ImGuiCol_ButtonActive, 0xFF7A70F1); - ImGui::PushStyleColor(ImGuiCol_ButtonHovered, 0xFF2311E8); - - // Draw close button - if (ImGuiExt::TitleBarButton(ICON_VS_CHROME_CLOSE, buttonSize)) { - ImHexApi::System::closeImHex(); - } - - ImGui::PopStyleColor(5); + ImGui::PopStyleColor(3); ImGui::PopStyleVar(); { - const auto windowSize = ImHexApi::System::getMainWindowSize(); - const auto searchBoxSize = ImVec2(std::sqrt(windowSize.x) * 14_scaled, titleBarHeight - 3_scaled); - const auto searchBoxPos = ImVec2((windowSize / 2 - searchBoxSize / 2).x, 3_scaled); - const auto buttonColor = [](float alpha) { return ImU32(ImColor(ImGui::GetStyleColorVec4(ImGuiCol_DockingEmptyBg) * ImVec4(1, 1, 1, alpha))); }; @@ -323,40 +329,6 @@ namespace hex { } } - void Window::drawTitleBarBorder() { - auto titleBarHeight = ImGui::GetCurrentWindow()->MenuBarHeight(); - auto buttonSize = ImVec2(titleBarHeight * 1.5F, titleBarHeight - 1); - - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); - ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetColorU32(ImGuiCol_MenuBarBg)); - ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImGui::GetColorU32(ImGuiCol_ScrollbarGrabActive)); - ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetColorU32(ImGuiCol_ScrollbarGrabHovered)); - - auto &titleBarButtons = ContentRegistry::Interface::impl::getTitleBarButtons(); - - // Draw custom title bar buttons - if (!titleBarButtons.empty()) { - ImGui::SetCursorPosX(ImGui::GetWindowWidth() - buttonSize.x * (titleBarButtons.size() + 0.5F)); - for (const auto &[icon, tooltip, callback]: titleBarButtons) { - if (ImGuiExt::TitleBarButton(icon.c_str(), buttonSize)) { - callback(); - } - ImGuiExt::InfoTooltip(Lang(tooltip)); - } - } - - ImGui::PopStyleColor(3); - ImGui::PopStyleVar(); - } - - void Window::drawTitleBar() { - if (ImHexApi::System::isBorderlessWindowModeEnabled()) { - drawTitleBarBorderless(); - } else { - drawTitleBarBorder(); - } - } - static bool isAnyViewOpen() { const auto &views = ContentRegistry::Views::impl::getEntries(); return std::any_of(views.begin(), views.end(), @@ -1127,7 +1099,7 @@ namespace hex { } }); - glfwSetWindowSizeLimits(this->m_window, 720_scaled, 480_scaled, GLFW_DONT_CARE, GLFW_DONT_CARE); + glfwSetWindowSizeLimits(this->m_window, 480_scaled, 360_scaled, GLFW_DONT_CARE, GLFW_DONT_CARE); glfwShowWindow(this->m_window); } diff --git a/plugins/builtin/include/content/views/view_command_palette.hpp b/plugins/builtin/include/content/views/view_command_palette.hpp index b77eec92e..69a293368 100644 --- a/plugins/builtin/include/content/views/view_command_palette.hpp +++ b/plugins/builtin/include/content/views/view_command_palette.hpp @@ -20,8 +20,8 @@ namespace hex::plugin::builtin { [[nodiscard]] bool shouldProcess() const override { return true; } [[nodiscard]] bool hasViewMenuItemEntry() const override { return false; } - [[nodiscard]] ImVec2 getMinSize() const override { return scaled({ 600, 150 }); } - [[nodiscard]] ImVec2 getMaxSize() const override { return scaled({ 600, 150 }); } + [[nodiscard]] ImVec2 getMinSize() const override { return ImVec2(std::min(ImHexApi::System::getMainWindowSize().x, 600_scaled), 150_scaled); } + [[nodiscard]] ImVec2 getMaxSize() const override { return this->getMinSize(); } private: enum class MatchType diff --git a/plugins/builtin/romfs/layouts/minimal.hexlyt b/plugins/builtin/romfs/layouts/minimal.hexlyt index 255c3329b..969a05945 100644 --- a/plugins/builtin/romfs/layouts/minimal.hexlyt +++ b/plugins/builtin/romfs/layouts/minimal.hexlyt @@ -189,4 +189,4 @@ hex.builtin.tools.ieee754=0 hex.builtin.tools.invariant_multiplication=0 hex.builtin.tools.tcp_client_server=0 hex.builtin.tools.euclidean_algorithm=0 -MainWindowSize=720,1000 +MainWindowSize=600,800