From 1b56c7ffae039dad2c2d5c876a2ea9881e651211 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 14 Jan 2023 14:21:16 +0100 Subject: [PATCH] ui: Show task progress in task bar --- lib/libimhex/include/hex/api/event.hpp | 1 + lib/libimhex/include/hex/api/imhex_api.hpp | 14 +++++++ lib/libimhex/source/api/imhex_api.cpp | 4 ++ main/source/main.cpp | 5 --- main/source/window/win_window.cpp | 41 ++++++++++++++++++++- plugins/builtin/source/content/ui_items.cpp | 8 +++- 6 files changed, 65 insertions(+), 8 deletions(-) diff --git a/lib/libimhex/include/hex/api/event.hpp b/lib/libimhex/include/hex/api/event.hpp index 9f1eaffcc..b1a0dc3d0 100644 --- a/lib/libimhex/include/hex/api/event.hpp +++ b/lib/libimhex/include/hex/api/event.hpp @@ -121,6 +121,7 @@ namespace hex { EVENT_DEF(EventFrameBegin); EVENT_DEF(EventFrameEnd); EVENT_DEF(EventWindowInitialized); + EVENT_DEF(EventSetTaskBarIconState, u32, u32, u32); EVENT_DEF(RequestOpenWindow, std::string); EVENT_DEF(RequestSelectionChange, Region); diff --git a/lib/libimhex/include/hex/api/imhex_api.hpp b/lib/libimhex/include/hex/api/imhex_api.hpp index ba4ec5a3d..59b91716a 100644 --- a/lib/libimhex/include/hex/api/imhex_api.hpp +++ b/lib/libimhex/include/hex/api/imhex_api.hpp @@ -191,6 +191,20 @@ namespace hex { char **envp; }; + enum class TaskProgressState { + Reset, + Progress, + Flash + }; + + enum class TaskProgressType { + Normal, + Warning, + Error + }; + + void setTaskBarProgress(TaskProgressState state, TaskProgressType type, u32 progress); + const ProgramArguments &getProgramArguments(); std::optional getProgramArgument(int index); diff --git a/lib/libimhex/source/api/imhex_api.cpp b/lib/libimhex/source/api/imhex_api.cpp index faf18ac9f..3bfed1cc3 100644 --- a/lib/libimhex/source/api/imhex_api.cpp +++ b/lib/libimhex/source/api/imhex_api.cpp @@ -409,6 +409,10 @@ namespace hex { } + void setTaskBarProgress(TaskProgressState state, TaskProgressType type, u32 progress) { + EventManager::post(u32(state), u32(type), progress); + } + const ProgramArguments &getProgramArguments() { return impl::s_programArguments; } diff --git a/main/source/main.cpp b/main/source/main.cpp index b497847d7..42f1c2ad7 100644 --- a/main/source/main.cpp +++ b/main/source/main.cpp @@ -2,7 +2,6 @@ #include #include -#include #include "window.hpp" @@ -15,10 +14,6 @@ int main(int argc, char **argv, char **envp) { using namespace hex; ImHexApi::System::impl::setProgramArguments(argc, argv, envp); -#if defined(OS_WINDOWS) - ImHexApi::System::impl::setBorderlessWindowMode(true); -#endif - bool shouldRestart = false; do { diff --git a/main/source/window/win_window.cpp b/main/source/window/win_window.cpp index 6e3d01157..1e3e67449 100644 --- a/main/source/window/win_window.cpp +++ b/main/source/window/win_window.cpp @@ -11,8 +11,6 @@ #include #include - #include - #include #define GLFW_EXPOSE_NATIVE_WIN32 #include @@ -22,6 +20,8 @@ #include #include #include + #include + #include #include @@ -32,6 +32,7 @@ namespace hex { static LONG_PTR g_oldWndProc; static float g_titleBarHeight; static ImGuiMouseCursor g_mouseCursorIcon; + static Microsoft::WRL::ComPtr g_taskbarList; static LRESULT commonWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { @@ -188,6 +189,7 @@ namespace hex { void Window::initNative() { + ImHexApi::System::impl::setBorderlessWindowMode(true); // Add plugin library folders to dll search path for (const auto &path : hex::fs::getDefaultPaths(fs::ImHexPath::Libraries)) { @@ -299,6 +301,41 @@ namespace hex { return EXCEPTION_CONTINUE_SEARCH; }); } + + if (SUCCEEDED(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED))) { + CoCreateInstance(CLSID_TaskbarList, nullptr, CLSCTX_INPROC_SERVER, IID_ITaskbarList4, &g_taskbarList); + } + + + EventManager::subscribe([hwnd](u32 state, u32 type, u32 progress){ + using enum ImHexApi::System::TaskProgressState; + switch (ImHexApi::System::TaskProgressState(state)) { + case Reset: + g_taskbarList->SetProgressState(hwnd, TBPF_NOPROGRESS); + g_taskbarList->SetProgressValue(hwnd, 0, 0); + break; + case Flash: + FlashWindow(hwnd, true); + break; + case Progress: + g_taskbarList->SetProgressState(hwnd, TBPF_INDETERMINATE); + g_taskbarList->SetProgressValue(hwnd, progress, 100); + break; + } + + using enum ImHexApi::System::TaskProgressType; + switch (ImHexApi::System::TaskProgressType(type)) { + case Normal: + g_taskbarList->SetProgressState(hwnd, TBPF_NORMAL); + break; + case Warning: + g_taskbarList->SetProgressState(hwnd, TBPF_PAUSED); + break; + case Error: + g_taskbarList->SetProgressState(hwnd, TBPF_ERROR); + break; + } + }); } void Window::beginNativeWindowFrame() { diff --git a/plugins/builtin/source/content/ui_items.cpp b/plugins/builtin/source/content/ui_items.cpp index 59a5a61ad..fde0c2eaa 100644 --- a/plugins/builtin/source/content/ui_items.cpp +++ b/plugins/builtin/source/content/ui_items.cpp @@ -230,11 +230,15 @@ namespace hex::plugin::builtin { auto &tasks = TaskManager::getRunningTasks(); auto frontTask = tasks.front(); + auto progress = float(frontTask->getValue()) / frontTask->getMaxValue(); + + ImHexApi::System::setTaskBarProgress(ImHexApi::System::TaskProgressState::Progress, ImHexApi::System::TaskProgressType::Normal, progress * 100); + auto widgetStart = ImGui::GetCursorPos(); ImGui::TextSpinner(hex::format("({})", taskCount).c_str()); ImGui::SameLine(); - ImGui::SmallProgressBar(frontTask->getMaxValue() == 0 ? 1 : (float(frontTask->getValue()) / frontTask->getMaxValue()), (ImGui::GetCurrentWindow()->MenuBarHeight() - 10_scaled) / 2.0); + ImGui::SmallProgressBar(frontTask->getMaxValue() == 0 ? 1 : progress, (ImGui::GetCurrentWindow()->MenuBarHeight() - 10_scaled) / 2.0); ImGui::SameLine(); auto widgetEnd = ImGui::GetCursorPos(); @@ -273,6 +277,8 @@ namespace hex::plugin::builtin { if (ImGui::ToolBarButton(ICON_VS_DEBUG_STOP, ImGui::GetStyleColorVec4(ImGuiCol_Text))) frontTask->interrupt(); ImGui::PopStyleVar(); + } else { + ImHexApi::System::setTaskBarProgress(ImHexApi::System::TaskProgressState::Reset, ImHexApi::System::TaskProgressType::Normal, 0); } }); }