[FindMyMouse]Do not activate on game mode (#13990)

* [FindMyMouse] Do not activate on game mode

* Add settings scaffolding

* fix spellchecker

* Address PR comments

* Adress UI feedback
This commit is contained in:
Jaime Bernardo 2021-10-25 19:39:48 +01:00 committed by GitHub
parent db90802e6e
commit af8366f0fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 248 additions and 12 deletions

View file

@ -1713,6 +1713,7 @@ QUERYOPEN
QUEUESYNC
Quickime
QUICKLAYOUTSWITCH
QUNS
qwertyuiopasdfghjklzxcvbnm
qword
qwrtyuiopsghjklzxvnm

View file

@ -267,6 +267,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "utils", "utils", "{B39DC643
src\common\utils\EventLocker.h = src\common\utils\EventLocker.h
src\common\utils\EventWaiter.h = src\common\utils\EventWaiter.h
src\common\utils\exec.h = src\common\utils\exec.h
src\common\utils\game_mode.h = src\common\utils\game_mode.h
src\common\utils\HDropIterator.h = src\common\utils\HDropIterator.h
src\common\utils\HttpClient.h = src\common\utils\HttpClient.h
src\common\utils\json.h = src\common\utils\json.h

View file

@ -0,0 +1,12 @@
#pragma once
#include <shellapi.h>
inline bool detect_game_mode()
{
QUERY_USER_NOTIFICATION_STATE notification_state;
if (SHQueryUserNotificationState(&notification_state) != S_OK)
{
return false;
}
return (notification_state == QUNS_RUNNING_D3D_FULL_SCREEN);
}

View file

@ -3,6 +3,7 @@
#include "pch.h"
#include "FindMyMouse.h"
#include "trace.h"
#include "common/utils/game_mode.h"
#ifdef COMPOSITION
namespace winrt
@ -18,6 +19,8 @@ namespace ABI
}
#endif
bool m_doNotActivateOnGameMode = true;
#pragma region Super_Sonar_Base_Code
template<typename D>
@ -250,6 +253,12 @@ void SuperSonar<D>::OnSonarInput(WPARAM flags, HRAWINPUT hInput)
template<typename D>
void SuperSonar<D>::OnSonarKeyboardInput(RAWINPUT const& input)
{
// Don't activate if game mode is on.
if (m_doNotActivateOnGameMode && detect_game_mode())
{
return;
}
if (input.data.keyboard.VKey != VK_CONTROL)
{
StopSonar();
@ -777,6 +786,11 @@ bool FindMyMouseIsEnabled()
return (m_sonar != nullptr);
}
void FindMyMouseSetDoNotActivateOnGameMode(bool doNotActivate)
{
m_doNotActivateOnGameMode = doNotActivate;
}
// Based on SuperSonar's original wWinMain.
int FindMyMouseMain(HINSTANCE hinst)
{

View file

@ -3,4 +3,4 @@
int FindMyMouseMain(HINSTANCE hinst);
void FindMyMouseDisable();
bool FindMyMouseIsEnabled();
void FindMyMouseSetDoNotActivateOnGameMode(bool doNotActivate);

View file

@ -6,6 +6,14 @@
#include <thread>
#include <common/utils/logger_helper.h>
namespace
{
const wchar_t JSON_KEY_PROPERTIES[] = L"properties";
const wchar_t JSON_KEY_VALUE[] = L"value";
const wchar_t JSON_KEY_DO_NOT_ACTIVATE_ON_GAME_MODE[] = L"do_not_activate_on_game_mode";
}
extern "C" IMAGE_DOS_HEADER __ImageBase;
HMODULE m_hModule;
@ -43,6 +51,9 @@ private:
// Load initial settings from the persisted values.
void init_settings();
// Helper function to extract the settings
void parse_settings(PowerToysSettings::PowerToyValues& settings);
public:
// Constructor
FindMyMouse()
@ -96,6 +107,8 @@ public:
PowerToysSettings::PowerToyValues values =
PowerToysSettings::PowerToyValues::from_json_string(config, get_key());
parse_settings(values);
values.save_to_settings_file();
}
catch (std::exception&)
@ -135,6 +148,7 @@ void FindMyMouse::init_settings()
// Load and parse the settings file for this PowerToy.
PowerToysSettings::PowerToyValues settings =
PowerToysSettings::PowerToyValues::load_from_settings_file(FindMyMouse::get_key());
parse_settings(settings);
}
catch (std::exception&)
{
@ -142,6 +156,30 @@ void FindMyMouse::init_settings()
}
}
void FindMyMouse::parse_settings(PowerToysSettings::PowerToyValues& settings)
{
FindMyMouseSetDoNotActivateOnGameMode(true);
auto settingsObject = settings.get_raw_json();
if (settingsObject.GetView().Size())
{
try
{
auto jsonPropertiesObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES).GetNamedObject(JSON_KEY_DO_NOT_ACTIVATE_ON_GAME_MODE);
FindMyMouseSetDoNotActivateOnGameMode((bool)jsonPropertiesObject.GetNamedBoolean(JSON_KEY_VALUE));
}
catch (...)
{
Logger::warn("Failed to get 'do not activate on game mode' setting");
}
}
else
{
Logger::info("Find My Mouse settings are empty");
}
}
extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create()
{
return new FindMyMouse();

View file

@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Text.Json.Serialization;
namespace Microsoft.PowerToys.Settings.UI.Library
{
public class FindMyMouseProperties
{
[JsonPropertyName("do_not_activate_on_game_mode")]
public BoolProperty DoNotActivateOnGameMode { get; set; }
public FindMyMouseProperties()
{
DoNotActivateOnGameMode = new BoolProperty(true);
}
}
}

View file

@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Text.Json.Serialization;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
namespace Microsoft.PowerToys.Settings.UI.Library
{
public class FindMyMouseSettings : BasePTModuleSettings, ISettingsConfig
{
public const string ModuleName = "Find My Mouse";
[JsonPropertyName("properties")]
public FindMyMouseProperties Properties { get; set; }
public FindMyMouseSettings()
{
Name = ModuleName;
Properties = new FindMyMouseProperties();
Version = "1.0";
}
public string GetModuleName()
{
return Name;
}
// This can be utilized in the future if the settings.json file is to be modified/deleted.
public bool UpgradeSettingsConfiguration()
{
return false;
}
}
}

View file

@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Microsoft.PowerToys.Settings.UI.Library
{
public class FindMyMouseSettingsIPCMessage
{
[JsonPropertyName("powertoys")]
public SndFindMyMouseSettings Powertoys { get; set; }
public FindMyMouseSettingsIPCMessage()
{
}
public FindMyMouseSettingsIPCMessage(SndFindMyMouseSettings settings)
{
this.Powertoys = settings;
}
public string ToJsonString()
{
return JsonSerializer.Serialize(this);
}
}
}

View file

@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Microsoft.PowerToys.Settings.UI.Library
{
public class SndFindMyMouseSettings
{
[JsonPropertyName("FindMyMouse")]
public FindMyMouseSettings FindMyMouse { get; set; }
public SndFindMyMouseSettings()
{
}
public SndFindMyMouseSettings(FindMyMouseSettings settings)
{
FindMyMouse = settings;
}
public string ToJsonString()
{
return JsonSerializer.Serialize(this);
}
}
}

View file

@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.CompilerServices;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
@ -10,10 +11,16 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
{
public class MouseUtilsViewModel : Observable
{
private ISettingsUtils SettingsUtils { get; set; }
private GeneralSettings GeneralSettingsConfig { get; set; }
public MouseUtilsViewModel(ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc)
private FindMyMouseSettings FindMyMouseSettingsConfig { get; set; }
public MouseUtilsViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<FindMyMouseSettings> findMyMouseSettingsRepository, Func<string, int> ipcMSGCallBackFunc)
{
SettingsUtils = settingsUtils;
// To obtain the general settings configurations of PowerToys Settings.
if (settingsRepository == null)
{
@ -24,7 +31,17 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
_isFindMyMouseEnabled = GeneralSettingsConfig.Enabled.FindMyMouse;
// set the callback functions value to hangle outgoing IPC message.
// To obtain the find my mouse settings, if the file exists.
// If not, to create a file with the default settings and to return the default configurations.
if (findMyMouseSettingsRepository == null)
{
throw new ArgumentNullException(nameof(findMyMouseSettingsRepository));
}
FindMyMouseSettingsConfig = findMyMouseSettingsRepository.SettingsConfig;
_findMyMouseDoNotActivateOnGameMode = FindMyMouseSettingsConfig.Properties.DoNotActivateOnGameMode.Value;
// set the callback functions value to handle outgoing IPC message.
SendConfigMSG = ipcMSGCallBackFunc;
}
@ -43,14 +60,42 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig);
SendConfigMSG(outgoing.ToString());
// TODO: Implement when this module has properties.
// NotifyPropertyChanged();
NotifyFindMyMousePropertyChanged();
}
}
}
public bool FindMyMouseDoNotActivateOnGameMode
{
get
{
return _findMyMouseDoNotActivateOnGameMode;
}
set
{
if (_findMyMouseDoNotActivateOnGameMode != value)
{
_findMyMouseDoNotActivateOnGameMode = value;
FindMyMouseSettingsConfig.Properties.DoNotActivateOnGameMode.Value = value;
NotifyFindMyMousePropertyChanged();
}
}
}
public void NotifyFindMyMousePropertyChanged([CallerMemberName] string propertyName = null)
{
OnPropertyChanged(propertyName);
SndFindMyMouseSettings outsettings = new SndFindMyMouseSettings(FindMyMouseSettingsConfig);
SndModuleSettings<SndFindMyMouseSettings> ipcMessage = new SndModuleSettings<SndFindMyMouseSettings>(outsettings);
SendConfigMSG(ipcMessage.ToJsonString());
SettingsUtils.SaveSettings(FindMyMouseSettingsConfig.ToJsonString(), FindMyMouseSettings.ModuleName);
}
private Func<string, int> SendConfigMSG { get; }
private bool _isFindMyMouseEnabled;
private bool _findMyMouseDoNotActivateOnGameMode;
}
}

View file

@ -1702,4 +1702,8 @@ From there, simply click on a Markdown file, PDF file or SVG icon in the File Ex
<value>Press the Left Control key twice to focus the mouse pointer.</value>
<comment>"Left Control" is a keyboard key.</comment>
</data>
<data name="MouseUtils_Prevent_Activation_On_Game_Mode.Content" xml:space="preserve">
<value>Do not activate when Game Mode is on</value>
<comment>"Game mode" is the Windows feature to prevent notification when playing a game.</comment>
</data>
</root>

View file

@ -14,12 +14,21 @@
<controls:SettingsPageControl.ModuleContent>
<StackPanel Orientation="Vertical">
<controls:Setting x:Uid="MouseUtils_Enable_FindMyMouse" Icon="&#xF272;">
<controls:Setting.ActionContent>
<ToggleSwitch IsOn="{x:Bind ViewModel.IsFindMyMouseEnabled, Mode=TwoWay}" HorizontalAlignment="Right"/>
</controls:Setting.ActionContent>
</controls:Setting>
<controls:SettingExpander IsExpanded="True">
<controls:SettingExpander.Header>
<controls:Setting x:Uid="MouseUtils_Enable_FindMyMouse" Icon="&#xF272;">
<controls:Setting.ActionContent>
<ToggleSwitch IsOn="{x:Bind ViewModel.IsFindMyMouseEnabled, Mode=TwoWay}" HorizontalAlignment="Right"/>
</controls:Setting.ActionContent>
</controls:Setting>
</controls:SettingExpander.Header>
<controls:SettingExpander.Content>
<CheckBox x:Uid="MouseUtils_Prevent_Activation_On_Game_Mode"
IsChecked="{x:Bind Mode=TwoWay, Path=ViewModel.FindMyMouseDoNotActivateOnGameMode}"
Margin="{StaticResource ExpanderSettingMargin}"
IsEnabled="{x:Bind ViewModel.IsFindMyMouseEnabled, Mode=OneWay}" />
</controls:SettingExpander.Content>
</controls:SettingExpander>
</StackPanel>
</controls:SettingsPageControl.ModuleContent>
<controls:SettingsPageControl.PrimaryLinks>

View file

@ -15,7 +15,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
public MouseUtilsPage()
{
var settingsUtils = new SettingsUtils();
ViewModel = new MouseUtilsViewModel(SettingsRepository<GeneralSettings>.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage);
ViewModel = new MouseUtilsViewModel(settingsUtils, SettingsRepository<GeneralSettings>.GetInstance(settingsUtils), SettingsRepository<FindMyMouseSettings>.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage);
DataContext = ViewModel;
InitializeComponent();
}