Skip to content

Commit

Permalink
Merge branch 'feat/open-close-window' into v0.40.0
Browse files Browse the repository at this point in the history
  • Loading branch information
WingsZeng committed Jun 1, 2024
2 parents 8a76dcf + a43bfa2 commit d84727c
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <unordered_set>
#include "Log.hpp"
#define WLR_USE_UNSTABLE

Expand All @@ -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<PHLWINDOW> g_OpenedWindows = {};

bool g_bMouseWasPressed = false;
bool g_bWorkspaceChanged = false;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<PHLWINDOW>(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<PHLWINDOW>(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; }

Expand All @@ -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
Expand Down Expand Up @@ -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"};
Expand Down

0 comments on commit d84727c

Please sign in to comment.