Skip to content

Commit

Permalink
fix cursor autohide race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jan 22, 2023
1 parent 9f589b3 commit 572a42f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Window {
std::mutex renderMutex;
std::condition_variable renderCond;
std::atomic_int waitTimeout = defaultTimeout;
bool ownCursor = false;
double lastRenderAt = 0;
double lastInputAt = 0;
double lastMousePressAt = 0;
Expand Down
6 changes: 5 additions & 1 deletion source/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ bool Window::run(OptionParser& parser) {
dispatch.process();

bool hasInputEvents = !ImGui::GetCurrentContext()->InputEventsQueue.empty();
if (cursorAutoHide > 0 && mpv->playing() && !ImGui::GetIO().WantCaptureMouse) {
if (ownCursor && cursorAutoHide > 0 && mpv->playing() && !ImGui::GetIO().WantCaptureMouse) {
int mode = (glfwGetTime() - lastInputAt) * 1000 > cursorAutoHide ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL;
glfwSetInputMode(window, GLFW_CURSOR, mode);
}
Expand Down Expand Up @@ -192,6 +192,10 @@ void Window::initGLFW(const char* title) {
win->requestRender();
win->dispatch.process();
});
glfwSetCursorEnterCallback(window, [](GLFWwindow* window, int entered) {
auto win = static_cast<Window*>(glfwGetWindowUserPointer(window));
win->ownCursor = entered;
});
glfwSetCursorPosCallback(window, [](GLFWwindow* window, double x, double y) {
auto win = static_cast<Window*>(glfwGetWindowUserPointer(window));
win->lastInputAt = glfwGetTime();
Expand Down

0 comments on commit 572a42f

Please sign in to comment.