Skip to content

Commit

Permalink
add win32 taskbar progress
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Nov 26, 2023
1 parent 257ed8d commit ae93645
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
7 changes: 7 additions & 0 deletions include/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#pragma once
#include "player.h"
#ifdef _WIN32
#include <ole2.h>
#include <shobjidl.h>
#define GLFW_EXPOSE_NATIVE_WIN32
#elif defined(__APPLE__)
#define GLFW_EXPOSE_NATIVE_COCOA
Expand Down Expand Up @@ -69,7 +71,12 @@ class Window : Player {
double lastInputAt = 0;
#ifdef _WIN32
bool borderless = false;
bool oleOk = false;
HWND hwnd;
WNDPROC wndProcOld = nullptr;
ITaskbarList3 *taskbarList = nullptr;

void setupWin32Taskbar();
static LRESULT CALLBACK wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
#endif

Expand Down
32 changes: 25 additions & 7 deletions source/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ Window::Window(Config* config) : Player(config) {
initGLFW();
window = glfwCreateWindow(1280, 720, PLAYER_NAME, nullptr, nullptr);
if (window == nullptr) throw std::runtime_error("Failed to create window!");
installCallbacks(window);
#ifdef _WIN32
hwnd = glfwGetWin32Window(window);
if (SUCCEEDED(OleInitialize(nullptr))) oleOk = true;
#endif

initGui();
installCallbacks(window);
ImGui_ImplGlfw_InitForOpenGL(window, true);
}

Window::~Window() {
ImGui_ImplGlfw_Shutdown();
exitGui();
#ifdef _WIN32
if (taskbarList != nullptr) taskbarList->Release();
if (oleOk) OleUninitialize();
#endif

glfwDestroyWindow(window);
glfwTerminate();
Expand Down Expand Up @@ -83,14 +91,13 @@ bool Window::init(OptionParser& parser) {
#endif

#ifdef _WIN32
HWND hwnd = glfwGetWin32Window(window);
if (oleOk) setupWin32Taskbar();
wndProcOld = (WNDPROC)::GetWindowLongPtr(hwnd, GWLP_WNDPROC);
::SetWindowLongPtr(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(wndProc));
::SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));

mpv->observeProperty<int, MPV_FORMAT_FLAG>("border", [this](int flag) {
borderless = !static_cast<bool>(flag);
HWND hwnd = glfwGetWin32Window(window);
if (borderless) {
DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);
::SetWindowLongA(hwnd, GWL_STYLE, style | WS_CAPTION | WS_MAXIMIZEBOX | WS_THICKFRAME);
Expand Down Expand Up @@ -270,10 +277,7 @@ void Window::translateMod(std::vector<std::string>& keys, int mods) {
}

#ifdef _WIN32
int64_t Window::GetWid() {
HWND hwnd = glfwGetWin32Window(window);
return config->Data.Mpv.UseWid ? static_cast<uint32_t>((intptr_t)hwnd) : 0;
}
int64_t Window::GetWid() { return config->Data.Mpv.UseWid ? static_cast<uint32_t>((intptr_t)hwnd) : 0; }
#endif

GLAddrLoadFunc Window::GetGLAddrFunc() { return (GLAddrLoadFunc)glfwGetProcAddress; }
Expand Down Expand Up @@ -378,6 +382,20 @@ GLFWmonitor* Window::getMonitor(GLFWwindow* target) {
}

#ifdef _WIN32
void Window::setupWin32Taskbar() {
if (FAILED(CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_ALL, IID_ITaskbarList3, (void**)&taskbarList))) return;
if (FAILED(taskbarList->HrInit())) {
taskbarList->Release();
taskbarList = nullptr;
return;
}
mpv->observeEvent(MPV_EVENT_START_FILE, [this](void*) { taskbarList->SetProgressState(hwnd, TBPF_NORMAL); });
mpv->observeEvent(MPV_EVENT_END_FILE, [this](void*) { taskbarList->SetProgressState(hwnd, TBPF_NOPROGRESS); });
mpv->observeProperty<int64_t, MPV_FORMAT_INT64>("percent-pos", [this](int64_t pos) {
if (pos > 0) taskbarList->SetProgressValue(hwnd, pos, 100);
});
}

// borderless window: https://github.com/rossy/borderless-window
LRESULT CALLBACK Window::wndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
auto win = reinterpret_cast<Window*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
Expand Down

0 comments on commit ae93645

Please sign in to comment.