[FindMyMouse]Add more shake sensitivity settings (#30829)

This commit is contained in:
Jaime Bernardo
2024-01-16 17:35:54 +00:00
committed by GitHub
parent 6cd8753fe0
commit daafbe5432
7 changed files with 134 additions and 6 deletions

View File

@@ -75,6 +75,11 @@ protected:
static constexpr int FinalAlphaDenominator = 100;
winrt::DispatcherQueueController m_dispatcherQueueController{ nullptr };
// Don't consider movements started past these milliseconds to detect shaking.
int m_shakeIntervalMs = FIND_MY_MOUSE_DEFAULT_SHAKE_INTERVAL_MS;
// By which factor must travelled distance be than the diagonal of the rectangle containing the movements. (value in percent)
int m_shakeFactor = FIND_MY_MOUSE_DEFAULT_SHAKE_FACTOR;
private:
// Save the mouse movement that occurred in any direction.
@@ -87,10 +92,6 @@ private:
// Raw Input may give relative or absolute values. Need to take each case into account.
bool m_seenAnAbsoluteMousePosition = false;
POINT m_lastAbsolutePosition = { 0, 0 };
// Don't consider movements started past these milliseconds to detect shaking.
static constexpr LONG ShakeIntervalMs = 1000;
// By which factor must travelled distance be than the diagonal of the rectangle containing the movements.
static constexpr float ShakeFactor = 4.0f;
static inline byte GetSign(LONG const& num)
{
@@ -398,7 +399,7 @@ void SuperSonar<D>::OnSonarKeyboardInput(RAWINPUT const& input)
template<typename D>
void SuperSonar<D>::DetectShake()
{
ULONGLONG shakeStartTick = GetTickCount64() - ShakeIntervalMs;
ULONGLONG shakeStartTick = GetTickCount64() - m_shakeIntervalMs;
// Prune the story of movements for those movements that started too long ago.
std::erase_if(m_movementHistory, [shakeStartTick](const PointerRecentMovement& movement) { return movement.tick < shakeStartTick; });
@@ -429,7 +430,7 @@ void SuperSonar<D>::DetectShake()
double rectangleHeight = static_cast<double>(maxY) - minY;
double diagonal = sqrt(rectangleWidth * rectangleWidth + rectangleHeight * rectangleHeight);
if (diagonal > 0 && distanceTravelled / diagonal > ShakeFactor)
if (diagonal > 0 && distanceTravelled / diagonal > (m_shakeFactor/100.f))
{
m_movementHistory.clear();
StartSonar();
@@ -767,6 +768,8 @@ public:
m_sonarZoomFactor = settings.spotlightInitialZoom;
m_excludedApps = settings.excludedApps;
m_shakeMinimumDistance = settings.shakeMinimumDistance;
m_shakeIntervalMs = settings.shakeIntervalMs;
m_shakeFactor = settings.shakeFactor;
}
else
{
@@ -794,6 +797,8 @@ public:
m_sonarZoomFactor = localSettings.spotlightInitialZoom;
m_excludedApps = localSettings.excludedApps;
m_shakeMinimumDistance = localSettings.shakeMinimumDistance;
m_shakeIntervalMs = localSettings.shakeIntervalMs;
m_shakeFactor = localSettings.shakeFactor;
UpdateMouseSnooping(); // For the shake mouse activation method
// Apply new settings to runtime composition objects.

View File

@@ -19,6 +19,8 @@ constexpr int FIND_MY_MOUSE_DEFAULT_ANIMATION_DURATION_MS = 500;
constexpr int FIND_MY_MOUSE_DEFAULT_SPOTLIGHT_INITIAL_ZOOM = 9;
constexpr FindMyMouseActivationMethod FIND_MY_MOUSE_DEFAULT_ACTIVATION_METHOD = FindMyMouseActivationMethod::DoubleLeftControlKey;
constexpr int FIND_MY_MOUSE_DEFAULT_SHAKE_MINIMUM_DISTANCE = 1000;
constexpr int FIND_MY_MOUSE_DEFAULT_SHAKE_INTERVAL_MS = 1000;
constexpr int FIND_MY_MOUSE_DEFAULT_SHAKE_FACTOR = 400; // 400 percent
struct FindMyMouseSettings
{
@@ -31,6 +33,8 @@ struct FindMyMouseSettings
int animationDurationMs = FIND_MY_MOUSE_DEFAULT_ANIMATION_DURATION_MS;
int spotlightInitialZoom = FIND_MY_MOUSE_DEFAULT_SPOTLIGHT_INITIAL_ZOOM;
int shakeMinimumDistance = FIND_MY_MOUSE_DEFAULT_SHAKE_MINIMUM_DISTANCE;
int shakeIntervalMs = FIND_MY_MOUSE_DEFAULT_SHAKE_INTERVAL_MS;
int shakeFactor = FIND_MY_MOUSE_DEFAULT_SHAKE_FACTOR;
std::vector<std::wstring> excludedApps;
};

View File

@@ -23,6 +23,8 @@ namespace
const wchar_t JSON_KEY_SPOTLIGHT_INITIAL_ZOOM[] = L"spotlight_initial_zoom";
const wchar_t JSON_KEY_EXCLUDED_APPS[] = L"excluded_apps";
const wchar_t JSON_KEY_SHAKING_MINIMUM_DISTANCE[] = L"shaking_minimum_distance";
const wchar_t JSON_KEY_SHAKING_INTERVAL_MS[] = L"shaking_interval_ms";
const wchar_t JSON_KEY_SHAKING_FACTOR[] = L"shaking_factor";
const wchar_t JSON_KEY_ACTIVATION_SHORTCUT[] = L"activation_shortcut";
}
@@ -396,6 +398,42 @@ void FindMyMouse::parse_settings(PowerToysSettings::PowerToyValues& settings)
{
Logger::warn("Failed to initialize Shaking Minimum Distance from settings. Will use default value");
}
try
{
// Parse Shaking Interval Milliseconds
auto jsonPropertiesObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).GetNamedObject(JSON_KEY_SHAKING_INTERVAL_MS);
int value = static_cast<int>(jsonPropertiesObject.GetNamedNumber(JSON_KEY_VALUE));
if (value >= 0)
{
findMyMouseSettings.shakeIntervalMs = value;
}
else
{
throw std::runtime_error("Invalid Shaking Interval Milliseconds value");
}
}
catch (...)
{
Logger::warn("Failed to initialize Shaking Interval Milliseconds from settings. Will use default value");
}
try
{
// Parse Shaking Factor
auto jsonPropertiesObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).GetNamedObject(JSON_KEY_SHAKING_FACTOR);
int value = static_cast<int>(jsonPropertiesObject.GetNamedNumber(JSON_KEY_VALUE));
if (value >= 0)
{
findMyMouseSettings.shakeFactor = value;
}
else
{
throw std::runtime_error("Invalid Shaking Factor value");
}
}
catch (...)
{
Logger::warn("Failed to initialize Shaking Factor from settings. Will use default value");
}
try
{