diff --git a/src/main.cpp b/src/main.cpp index 58a85c0..231e826 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include #include "Log.hpp" #define WLR_USE_UNSTABLE @@ -14,6 +15,11 @@ #include "Shrink.hpp" PHLWINDOW g_pPreviouslyFocusedWindow = nullptr; +PHLWINDOW g_pPreviouslyClosedWindow = nullptr; +// HACK: This is a hack to keep track of all opened windows, +// since the openWindow event is called after the activeWindow. +std::unordered_set g_OpenedWindows = {}; + bool g_bMouseWasPressed = false; bool g_bWorkspaceChanged = false; @@ -106,6 +112,11 @@ static void onActiveWindowChange(void *self, std::any data) { PHANDLE, "plugin:hyprfocus:mouse_enabled") ->getDataStaticPtr(); + static auto *const PANIMATECOUSEDBYCLOSE = + (Hyprlang::INT *const *)HyprlandAPI::getConfigValue( + PHANDLE, "plugin:hyprfocus:animate_coused_by_close") + ->getDataStaticPtr(); + if (!**PHYPRFOCUSENABLED) { hyprfocus_log(LOG, "HyprFocus is disabled"); return; @@ -121,6 +132,21 @@ static void onActiveWindowChange(void *self, std::any data) { return; } + if (g_OpenedWindows.find(PWINDOW) == g_OpenedWindows.end()) { + hyprfocus_log(LOG, "Window is newly opened"); + g_OpenedWindows.insert(PWINDOW); + g_pPreviouslyFocusedWindow = PWINDOW; + return; + } + + if (!**PANIMATECOUSEDBYCLOSE && + g_pPreviouslyFocusedWindow == g_pPreviouslyClosedWindow) { + hyprfocus_log(LOG, "Previously focused window was closed, not animating"); + g_pPreviouslyFocusedWindow = PWINDOW; + g_pPreviouslyClosedWindow = nullptr; + return; + } + if (!**PMOUSEENABLED && g_bMouseWasPressed) { hyprfocus_log(LOG, "Mouse was pressed, not animating"); g_pPreviouslyFocusedWindow = PWINDOW; @@ -165,6 +191,29 @@ static void onMouseButton(void *self, std::any data) { } } +// static void onWindowOpen(void *self, std::any data) { +// try { +// auto const PWINDOW = std::any_cast(data); +// +// } catch (std::bad_any_cast &e) { +// hyprfocus_log(ERR, "Cast Error: {}", e.what()); +// } catch (std::exception &e) { +// hyprfocus_log(ERR, "Error: {}", e.what()); +// } +// } + +static void onWindowClose(void *self, std::any data) { + try { + auto const PWINDOW = std::any_cast(data); + g_pPreviouslyClosedWindow = PWINDOW; + g_OpenedWindows.erase(PWINDOW); + } catch (std::bad_any_cast &e) { + hyprfocus_log(ERR, "Cast Error: {}", e.what()); + } catch (std::exception &e) { + hyprfocus_log(ERR, "Error: {}", e.what()); + } +} + // Do NOT change this function. APICALL EXPORT std::string PLUGIN_API_VERSION() { return HYPRLAND_API_VERSION; } @@ -181,6 +230,8 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { Hyprlang::INT{1}); HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprfocus:focus_animation", Hyprlang::STRING("none")); + HyprlandAPI::addConfigValue( + PHANDLE, "plugin:hyprfocus:animate_coused_by_close", Hyprlang::INT{0}); HyprlandAPI::addDispatcher(PHANDLE, "animatefocused", &flashCurrentWindow); #ifdef FLASH @@ -215,6 +266,20 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { }); hyprfocus_log(LOG, "Registered mouse button callback"); + // static auto P3 = HyprlandAPI::registerCallbackDynamic( + // PHANDLE, "openWindow", + // [&](void *self, SCallbackInfo &info, std::any data) { + // onWindowOpen(self, data); + // }); + // hyprfocus_log(LOG, "Registered open window callback"); + + static auto P4 = HyprlandAPI::registerCallbackDynamic( + PHANDLE, "closeWindow", + [&](void *self, SCallbackInfo &info, std::any data) { + onWindowClose(self, data); + }); + hyprfocus_log(LOG, "Registered close window callback"); + HyprlandAPI::reloadConfig(); return {"hyprfocus", "animate windows on focus", "Vortex", "2.0"};