-
-
Notifications
You must be signed in to change notification settings - Fork 666
Expand file tree
/
Copy pathAnimationEffects.cpp
More file actions
158 lines (124 loc) · 5.36 KB
/
Copy pathAnimationEffects.cpp
File metadata and controls
158 lines (124 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "pch.h"
#include "AnimationEffects.h"
#if __has_include("AnimationEffects.g.cpp")
#include "AnimationEffects.g.cpp"
#endif
namespace winrt::Telegram::Native::implementation
{
PFN_SystemParametersInfoW AnimationEffects::s_SystemParametersInfoW = nullptr;
PNF_add_AnimationsEnabledChanged AnimationEffects::s_add_AnimationsEnabledChanged = nullptr;
PNF_remove_AnimationsEnabledChanged AnimationEffects::s_remove_AnimationsEnabledChanged = nullptr;
UISettings AnimationEffects::s_settings;
std::mutex AnimationEffects::s_mutex;
std::atomic<bool> AnimationEffects::s_initialized{ false };
std::atomic<AnimationEffectsState> AnimationEffects::s_state{ AnimationEffectsState::Auto };
std::map<EventRegistration, TypedEventHandler<UISettings, UISettingsAnimationsEnabledChangedEventArgs>> AnimationEffects::s_events;
bool AnimationEffects::Supported()
{
if (auto settings6 = s_settings.try_as<IUISettings6>())
{
return true;
}
return false;
}
bool AnimationEffects::Enabled()
{
if (s_SystemParametersInfoW)
{
BOOL enabled = FALSE;
if (s_SystemParametersInfoW(SPI_GETCLIENTAREAANIMATION, 0, &enabled, 0))
{
return enabled;
}
return false;
}
return s_settings.AnimationsEnabled();
}
void AnimationEffects::Initialize()
{
if (s_initialized.load())
{
return;
}
std::lock_guard<std::mutex> lock(s_mutex);
if (auto settings6 = s_settings.try_as<IUISettings6>())
{
auto vtbl = *reinterpret_cast<void***>(winrt::get_abi(settings6));
s_add_AnimationsEnabledChanged = reinterpret_cast<PNF_add_AnimationsEnabledChanged>(vtbl[6]);
s_remove_AnimationsEnabledChanged = reinterpret_cast<PNF_remove_AnimationsEnabledChanged>(vtbl[7]);
HMODULE user32 = GetModuleHandle(L"User32.dll");
if (!user32) user32 = LoadLibrary(L"User32.dll");
s_SystemParametersInfoW = reinterpret_cast<PFN_SystemParametersInfoW>(GetProcAddress(user32, "SystemParametersInfoW"));
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(reinterpret_cast<PVOID*>(&s_SystemParametersInfoW), SystemParametersInfoWHook);
DetourAttach(reinterpret_cast<PVOID*>(&s_add_AnimationsEnabledChanged), add_AnimationsEnabledChanged);
DetourAttach(reinterpret_cast<PVOID*>(&s_remove_AnimationsEnabledChanged), remove_AnimationsEnabledChanged);
DetourTransactionCommit();
}
s_initialized = true;
}
AnimationEffectsState AnimationEffects::State()
{
return s_state.load();
}
void AnimationEffects::State(AnimationEffectsState state)
{
s_state = state;
std::lock_guard<std::mutex> lock(s_mutex);
for (const auto& event : s_events)
{
UISettings settings{ nullptr };
winrt::copy_from_abi(settings, event.first.event_source);
event.second(settings, nullptr);
}
}
DWORD WINAPI AnimationEffects::SystemParametersInfoWHook(_In_ UINT uiAction, _In_ UINT uiParam, _Pre_maybenull_ _Post_valid_ PVOID pvParam, _In_ UINT fWinIni)
{
if (uiAction == SPI_GETCLIENTAREAANIMATION && pvParam != nullptr)
{
// We want the following behavior:
// If animation effects are disabled system-wide, we always disable them in the app
// However, if they are enabled, we let the user choose.
// If state is not Auto
// -> Query system value
// -> If system value FALSE => FALSE
// -> Otherwise, value provided by user
// -> Otherwise return system value
auto state = s_state.load();
if (state != AnimationEffectsState::Auto)
{
BOOL enabled = FALSE;
if (s_SystemParametersInfoW(SPI_GETCLIENTAREAANIMATION, 0, &enabled, 0))
{
if (!enabled)
{
*(BOOL*)pvParam = FALSE;
return TRUE;
}
}
*(BOOL*)pvParam = (BOOL)state;
return TRUE;
}
}
return s_SystemParametersInfoW(uiAction, uiParam, pvParam, fWinIni);
}
int32_t __stdcall AnimationEffects::add_AnimationsEnabledChanged(void* pThis, void* handler, winrt::event_token* token)
{
std::lock_guard<std::mutex> lock(s_mutex);
TypedEventHandler<UISettings, UISettingsAnimationsEnabledChangedEventArgs> typedHandler{ nullptr };
winrt::attach_abi(typedHandler, handler);
auto result = s_add_AnimationsEnabledChanged(pThis, handler, token);
EventRegistration registration{ pThis, *reinterpret_cast<winrt::event_token const*>(token) };
s_events[registration] = typedHandler;
return result;
}
int32_t __stdcall AnimationEffects::remove_AnimationsEnabledChanged(void* pThis, winrt::event_token token)
{
std::lock_guard<std::mutex> lock(s_mutex);
auto result = s_remove_AnimationsEnabledChanged(pThis, token);
EventRegistration registration{ pThis, token };
s_events.erase(registration);
return result;
}
}