Skip to content

Commit

Permalink
[New PowerToy] OCR PowerToy (#19172)
Browse files Browse the repository at this point in the history
* Init commit

* Fix unintended GUID change of Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests

* Region and click word working

* Code style

* Close even when there is no result from the OCR

* Fix spelling concerns, and make overlay black to match snipping tool

* increase opacity of overlay to match snipping tool

* Code Style and cleanup

* Code style

* Create Logos and hook them into the project file

* Make the PowerOCR VCXProj more like Awake VCXProj

* Rename MainWindow to OCROverlay

* Add WindowUtilities and WindowForms

* Remove fsg to fix spelling error

* launch OCR Overlay on every screen

* Add PowerOCR to Runner Main.cpp

* Add PowerOCR Settings and Properties

* Add PowerOcrViewModel

* Fix wrong setting reference in PowerOcrSettingsVM

* Try to clean up the Cpp project for PowerOCR

* Went to ARM64 was x64 thanks @snickler

* Clean up PowerOCR C++ Proj with file refs

* Rewrite C++ dllmain comparing to awake

* Changes for spelling issues. The rest will stay

* Create PowerOcr Settings Page and add to settings shell

* Fix PowerOcr Settings

* Fix multi-monitor scaling issue

* Add close all overlays when escaping

* Update src/runner/main.cpp to call correct Power OCR dll

Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>

* Update expect.txt

* Add many files from Color Picker for hotkey activation

* Organize project into helper folder

* Use new hotkey activation and keep process alive

* Fix bug where scalebmp wasn't working

* Add The file headers and dispose app.xaml.cs

* Code style changes

* Fix bug where PowerOCR was toggling Awake

* Unsubscribe from keyboard events making they don't fire twice

* Add SndPowerOcrSetting and add to SettingsVM

* Trying to make the runner close PowerOCR when runner closes

* Fix app_name

* Update spellcheck expect

* use mutex on PowerOCR app to keep to single instance

* Rebuild the module interface using ColorPicker as a template.

Process still stays alive.

* Fix project names of the module interface

* Put app startup args back to 0 like color picker

* Runner now finds and enables/disables PowerOCR

* remove unneeded item groups from settings proj, per stefansjfw

* Add PowerOCR Screenshots

* Revert changed project GUID

* Add OOBE content for PowerOCR

* Keep cursor on one screen since the OCR window does not span screens.

* reload settings when activation key is pressed

* New screenshots and OOBE text

* Add PowerOCR as a case in the settings App.xaml.cs OnLaunched

* Settings and OOBE Text Changes

* Using using on bitmaps and change OCR overlay to stay open if no result

* Keyboard activation is handled is true

* Remove unused start PowerOCR OOBE Method

* [PowerOCR]Add some telemetry

* Add some logging

* Don't recreate the OCR overlay Windows more times

* Add to BugReportTool to get event viewer errors

* Fix wrong comment

* Fix another comment

* Add files to installer

* Add to signing

* Don't take Esc away from other apps

* Default to Win Shift R

* Use low level keyboard hook from runner

* Remove esc from local low level keyboard hook

* Fix some nits

* Default to Win Shift T
  • Loading branch information
TheJoeFin committed Aug 25, 2022
1 parent 768603e commit eedea31
Show file tree
Hide file tree
Showing 64 changed files with 3,040 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .github/actions/spell-check/expect.txt
Expand Up @@ -1415,6 +1415,7 @@ Objbase
OBJID
objidl
oblitum
ocr
odbc
odbccp
Oem
Expand Down Expand Up @@ -1474,6 +1475,7 @@ PARENTNOTIFY
PARENTRELATIVEEDITING
PARENTRELATIVEFORADDRESSBAR
PARENTRELATIVEPARSING
PArgb
parray
PARTIALCONFIRMATIONDIALOGTITLE
pathcch
Expand Down Expand Up @@ -1530,6 +1532,7 @@ POPUPWINDOW
posix
powercfg
powerlauncher
POWEROCR
powerpreview
powerrename
POWERRENAMECONTEXTMENU
Expand Down
8 changes: 4 additions & 4 deletions .pipelines/ESRPSigning_core.json
Expand Up @@ -25,14 +25,14 @@
"modules\\AlwaysOnTop\\PowerToys.AlwaysOnTop.exe",
"modules\\AlwaysOnTop\\PowerToys.AlwaysOnTopModuleInterface.dll",

"modules\\ColorPicker\\ColorPicker.dll",
"modules\\ColorPicker\\ColorPickerUI.dll",
"modules\\ColorPicker\\ColorPickerUI.exe",

"modules\\ColorPicker\\PowerToys.ColorPicker.dll",
"modules\\ColorPicker\\PowerToys.ColorPickerUI.dll",
"modules\\ColorPicker\\PowerToys.ColorPickerUI.exe",

"modules\\PowerOCR\\PowerToys.PowerOCRModuleInterface.dll",
"modules\\PowerOCR\\PowerToys.PowerOCR.dll",
"modules\\PowerOCR\\PowerToys.PowerOCR.exe",

"modules\\Awake\\PowerToys.AwakeModuleInterface.dll",
"modules\\Awake\\PowerToys.Awake.exe",
"modules\\Awake\\PowerToys.Awake.dll",
Expand Down
33 changes: 33 additions & 0 deletions PowerToys.sln
Expand Up @@ -415,6 +415,12 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ImageResizerContextMenu", "
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ImageResizerLib", "src\modules\imageresizer\ImageResizerLib\ImageResizerLib.vcxproj", "{18B3DB45-4FFE-4D01-97D6-5223FEEE1853}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PowerOCR", "PowerOCR", "{A50C70A6-2DA0-4027-B90E-B1A40755A8A5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PowerOCR", "src\modules\PowerOCR\PowerOCR\PowerOCR.csproj", "{25C91A4E-BA4E-467A-85CD-8B62545BF674}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PowerOCRModuleInterface", "src\modules\PowerOCR\PowerOCRModuleInterface\PowerOCRModuleInterface.vcxproj", "{6AB6A2D6-F859-4A82-9184-0BD29C9F07D1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.PowerToys.Run.Plugin.History", "src\modules\launcher\Plugins\Microsoft.PowerToys.Run.Plugin.History\Microsoft.PowerToys.Run.Plugin.History.csproj", "{212AD910-8488-4036-BE20-326931B75FB2}"
EndProject
Global
Expand Down Expand Up @@ -1634,6 +1640,30 @@ Global
{18B3DB45-4FFE-4D01-97D6-5223FEEE1853}.Release|x64.Build.0 = Release|x64
{18B3DB45-4FFE-4D01-97D6-5223FEEE1853}.Release|x86.ActiveCfg = Release|x64
{18B3DB45-4FFE-4D01-97D6-5223FEEE1853}.Release|x86.Build.0 = Release|x64
{25C91A4E-BA4E-467A-85CD-8B62545BF674}.Debug|ARM64.ActiveCfg = Debug|ARM64
{25C91A4E-BA4E-467A-85CD-8B62545BF674}.Debug|ARM64.Build.0 = Debug|ARM64
{25C91A4E-BA4E-467A-85CD-8B62545BF674}.Debug|x64.ActiveCfg = Debug|x64
{25C91A4E-BA4E-467A-85CD-8B62545BF674}.Debug|x64.Build.0 = Debug|x64
{25C91A4E-BA4E-467A-85CD-8B62545BF674}.Debug|x86.ActiveCfg = Debug|x64
{25C91A4E-BA4E-467A-85CD-8B62545BF674}.Debug|x86.Build.0 = Debug|x64
{25C91A4E-BA4E-467A-85CD-8B62545BF674}.Release|ARM64.ActiveCfg = Release|ARM64
{25C91A4E-BA4E-467A-85CD-8B62545BF674}.Release|ARM64.Build.0 = Release|ARM64
{25C91A4E-BA4E-467A-85CD-8B62545BF674}.Release|x64.ActiveCfg = Release|x64
{25C91A4E-BA4E-467A-85CD-8B62545BF674}.Release|x64.Build.0 = Release|x64
{25C91A4E-BA4E-467A-85CD-8B62545BF674}.Release|x86.ActiveCfg = Release|x64
{25C91A4E-BA4E-467A-85CD-8B62545BF674}.Release|x86.Build.0 = Release|x64
{6AB6A2D6-F859-4A82-9184-0BD29C9F07D1}.Debug|ARM64.ActiveCfg = Debug|ARM64
{6AB6A2D6-F859-4A82-9184-0BD29C9F07D1}.Debug|ARM64.Build.0 = Debug|ARM64
{6AB6A2D6-F859-4A82-9184-0BD29C9F07D1}.Debug|x64.ActiveCfg = Debug|x64
{6AB6A2D6-F859-4A82-9184-0BD29C9F07D1}.Debug|x64.Build.0 = Debug|x64
{6AB6A2D6-F859-4A82-9184-0BD29C9F07D1}.Debug|x86.ActiveCfg = Debug|x64
{6AB6A2D6-F859-4A82-9184-0BD29C9F07D1}.Debug|x86.Build.0 = Debug|x64
{6AB6A2D6-F859-4A82-9184-0BD29C9F07D1}.Release|ARM64.ActiveCfg = Release|ARM64
{6AB6A2D6-F859-4A82-9184-0BD29C9F07D1}.Release|ARM64.Build.0 = Release|ARM64
{6AB6A2D6-F859-4A82-9184-0BD29C9F07D1}.Release|x64.ActiveCfg = Release|x64
{6AB6A2D6-F859-4A82-9184-0BD29C9F07D1}.Release|x64.Build.0 = Release|x64
{6AB6A2D6-F859-4A82-9184-0BD29C9F07D1}.Release|x86.ActiveCfg = Release|x64
{6AB6A2D6-F859-4A82-9184-0BD29C9F07D1}.Release|x86.Build.0 = Release|x64
{212AD910-8488-4036-BE20-326931B75FB2}.Debug|ARM64.ActiveCfg = Debug|ARM64
{212AD910-8488-4036-BE20-326931B75FB2}.Debug|ARM64.Build.0 = Debug|ARM64
{212AD910-8488-4036-BE20-326931B75FB2}.Debug|x64.ActiveCfg = Debug|x64
Expand Down Expand Up @@ -1784,6 +1814,9 @@ Global
{5A1DB2F0-0715-4B3B-98E6-79BC41540045} = {4AFC9975-2456-4C70-94A4-84073C1CED93}
{93B72A06-C8BD-484F-A6F7-C9F280B150BF} = {6C7F47CC-2151-44A3-A546-41C70025132C}
{18B3DB45-4FFE-4D01-97D6-5223FEEE1853} = {6C7F47CC-2151-44A3-A546-41C70025132C}
{A50C70A6-2DA0-4027-B90E-B1A40755A8A5} = {4574FDD0-F61D-4376-98BF-E5A1262C11EC}
{25C91A4E-BA4E-467A-85CD-8B62545BF674} = {A50C70A6-2DA0-4027-B90E-B1A40755A8A5}
{6AB6A2D6-F859-4A82-9184-0BD29C9F07D1} = {A50C70A6-2DA0-4027-B90E-B1A40755A8A5}
{212AD910-8488-4036-BE20-326931B75FB2} = {4AFC9975-2456-4C70-94A4-84073C1CED93}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
19 changes: 16 additions & 3 deletions installer/PowerToysSetup/Product.wxs
Expand Up @@ -8,6 +8,7 @@
<?define KeyboardManagerProjectName="KeyboardManager"?>
<?define PowerRenameProjectName="PowerRename"?>
<?define ColorPickerProjectName="ColorPicker"?>
<?define PowerOCRProjectName="PowerOCR"?>
<?define VideoConferenceProjectName="VideoConference"?>
<?define AwakeProjectName="Awake"?>
<?define MouseUtilsProjectName="MouseUtils"?>
Expand Down Expand Up @@ -48,11 +49,11 @@

<?define SettingsV2Files=Ijwhost.dll;ColorCode.Core.dll;ColorCode.WinUI.dll;CommunityToolkit.Common.dll;CommunityToolkit.WinUI.dll;CommunityToolkit.WinUI.UI.Controls.Core.dll;CommunityToolkit.WinUI.UI.Controls.DataGrid.dll;CommunityToolkit.WinUI.UI.Controls.Input.dll;CommunityToolkit.WinUI.UI.Controls.Layout.dll;CommunityToolkit.WinUI.UI.Controls.Markdown.dll;CommunityToolkit.WinUI.UI.Controls.Media.dll;CommunityToolkit.WinUI.UI.Controls.Primitives.dll;CommunityToolkit.WinUI.UI.dll;icon.ico;Microsoft.Graphics.Canvas.Interop.dll;Microsoft.InteractiveExperiences.Projection.dll;Microsoft.Windows.ApplicationModel.DynamicDependency.Projection.dll;Microsoft.Windows.ApplicationModel.Resources.Projection.dll;Microsoft.Windows.ApplicationModel.WindowsAppRuntime.Projection.dll;Microsoft.Windows.AppLifecycle.Projection.dll;Microsoft.Windows.SDK.NET.dll;Microsoft.Windows.System.Power.Projection.dll;Microsoft.WindowsAppRuntime.Bootstrap.Net.dll;Microsoft.WinUI.dll;Microsoft.Xaml.Interactions.dll;Microsoft.Xaml.Interactivity.dll;PowerToys.Interop.dll;PowerToys.ManagedCommon.dll;System.Management.dll;PowerToys.ManagedTelemetry.dll;PowerToys.Settings.deps.json;PowerToys.Settings.dll;PowerToys.Settings.exe;PowerToys.Settings.runtimeconfig.json;PowerToys.Settings.UI.Lib.dll;resources.pri;System.IO.Abstractions.dll;System.Text.Json.dll;WinRT.Runtime.dll;Microsoft.Graphics.Canvas.dll;Microsoft.WindowsAppRuntime.Bootstrap.dll;CoreMessagingXP.dll;dcompi.dll;dwmcorei.dll;DwmSceneI.dll;DWriteCore.dll;marshal.dll;Microsoft.DirectManipulation.dll;Microsoft.InputStateManager.dll;Microsoft.Internal.FrameworkUdk.dll;Microsoft.UI.Composition.OSSupport.dll;Microsoft.UI.Input.dll;Microsoft.UI.Windowing.Core.dll;Microsoft.UI.Xaml.Controls.dll;Microsoft.UI.Xaml.Controls.pri;Microsoft.ui.xaml.dll;Microsoft.UI.Xaml.Internal.dll;Microsoft.UI.Xaml.Phone.dll;Microsoft.ui.xaml.resources.19h1.dll;Microsoft.ui.xaml.resources.common.dll;Microsoft.Web.WebView2.Core.dll;Microsoft.Windows.ApplicationModel.Resources.dll;Microsoft.Windows.AppNotifications.Projection.dll;Microsoft.Windows.PushNotifications.Projection.dll;Microsoft.Windows.System.Projection.dll;Microsoft.WindowsAppRuntime.dll;Microsoft.WindowsAppRuntime.Insights.Resource.dll;Microsoft.WindowsAppRuntime.Release.Net.dll;MRM.dll;PushNotificationsLongRunningTask.ProxyStub.dll;WindowsAppRuntime.png;WindowsAppSdk.AppxDeploymentExtensions.Desktop.dll;WinUIEdit.dll;wuceffectsi.dll?>

<?define SettingsV2AssetsModulesFiles=ColorPicker.png;FancyZones.png;AlwaysOnTop.png;Awake.png;ImageResizer.png;KBM.png;MouseUtils.png;PowerLauncher.png;PowerPreview.png;PowerRename.png;PT.png;ShortcutGuide.png;VideoConference.png?>
<?define SettingsV2AssetsModulesFiles=ColorPicker.png;FancyZones.png;AlwaysOnTop.png;Awake.png;ImageResizer.png;KBM.png;MouseUtils.png;PowerLauncher.png;PowerPreview.png;PowerRename.png;PT.png;ShortcutGuide.png;VideoConference.png;PowerOCR.png?>

<?define SettingsV2OOBEAssetsModulesFiles=ColorPicker.gif;AlwaysOnTop.png;Awake.png;FancyZones.gif;FileExplorer.png;ImageResizer.gif;KBM.gif;MouseUtils.gif;PowerRename.gif;Run.gif;OOBEShortcutGuide.png;VideoConferenceMute.png;OOBEPTHero.png?>
<?define SettingsV2OOBEAssetsModulesFiles=ColorPicker.gif;AlwaysOnTop.png;Awake.png;FancyZones.gif;FileExplorer.png;ImageResizer.gif;KBM.gif;MouseUtils.gif;PowerRename.gif;Run.gif;OOBEShortcutGuide.png;VideoConferenceMute.png;OOBEPTHero.png;PowerOCR.gif?>

<?define SettingsV2OOBEAssetsFluentIconsFiles=ColorPicker.png;FancyZones.png;AlwaysOnTop.png;Awake.png;FileExplorerPreview.png;FindMyMouse.png;ImageResizer.png;KeyboardManager.png;MouseHighlighter.png;MouseCrosshairs.png;MouseUtils.png;PowerRename.png;PowerToys.png;PowerToysRun.png;Settings.png;ShortcutGuide.png;VideoConferenceMute.png?>
<?define SettingsV2OOBEAssetsFluentIconsFiles=ColorPicker.png;FancyZones.png;AlwaysOnTop.png;Awake.png;FileExplorerPreview.png;FindMyMouse.png;ImageResizer.png;KeyboardManager.png;MouseHighlighter.png;MouseCrosshairs.png;MouseUtils.png;PowerRename.png;PowerToys.png;PowerToysRun.png;Settings.png;ShortcutGuide.png;VideoConferenceMute.png;PowerOcr.png?>

<?define SettingsV2MicrosoftUIXamlAssetsInstallFiles=NoiseAsset_256x256_PNG.png?>

Expand Down Expand Up @@ -431,6 +432,7 @@
<Directory Id="MonacoPreviewHandlerMonacoSRCFolder" Name="monacoSRC" />
</Directory>
<Directory Id="FancyZonesInstallFolder" Name="$(var.FancyZonesProjectName)" />
<Directory Id="PowerOCRInstallFolder" Name="$(var.PowerOCRProjectName)" />
<Directory Id="AwakeInstallFolder" Name="$(var.AwakeProjectName)">
<Directory Id="AwakeImagesFolder" Name="Images" />
</Directory>
Expand Down Expand Up @@ -894,6 +896,16 @@
</Component>
</DirectoryRef>

<!-- PowerOCR Resources -->
<DirectoryRef Id="PowerOCRInstallFolder" FileSource="$(var.BinDir)modules\$(var.PowerOCRProjectName)">
<!-- !Warning! Make sure to change Component Guid if you update the file list -->
<Component Id="Module_PowerOCR" Guid="5640A7E8-E165-4368-8F08-F8E1E9242BDD" Win64="yes">
<?foreach File in PowerToys.PowerOCR.dll;ControlzEx.dll;Ijwhost.dll;Microsoft.Windows.SDK.NET.dll;Microsoft.Xaml.Behaviors.dll;PowerToys.Common.UI.dll;PowerToys.Interop.dll;PowerToys.ManagedCommon.dll;PowerToys.ManagedTelemetry.dll;PowerToys.PowerOCR.deps.json;PowerToys.PowerOCR.exe;PowerToys.PowerOCR.runtimeconfig.json;PowerToys.PowerOCRModuleInterface.dll;PowerToys.Settings.UI.Lib.dll;System.ComponentModel.Composition.dll;System.IO.Abstractions.dll;System.Management.dll;System.Text.Json.dll;WinRT.Runtime.dll?>
<File Id="PowerOCRFile_$(var.File)" Source="$(var.BinDir)modules\$(var.PowerOCRProjectName)\$(var.File)" />
<?endforeach?>
</Component>
</DirectoryRef>

<!-- Awake -->
<DirectoryRef Id="AwakeInstallFolder" FileSource="$(var.BinDir)modules\$(var.AwakeProjectName)">
<!-- !Warning! Make sure to change Component Guid if you update the file list -->
Expand Down Expand Up @@ -1063,6 +1075,7 @@
<ComponentRef Id="Module_KeyboardManager" />
<ComponentRef Id="Module_KeyboardManager_Editor" />
<ComponentRef Id="Module_KeyboardManager_Engine" />
<ComponentRef Id="Module_PowerOCR" />
<ComponentRef Id="Module_ColorPicker" />
<ComponentRef Id="Module_ColorPicker_Icon"/>
<ComponentRef Id="Module_ColorPicker_Cursor"/>
Expand Down
5 changes: 5 additions & 0 deletions src/common/interop/interop.cpp
Expand Up @@ -195,8 +195,13 @@ public
return gcnew String(CommonSharedConstants::SHOW_COLOR_PICKER_SHARED_EVENT);
}

static String ^ ShowPowerOCRSharedEvent() {
return gcnew String(CommonSharedConstants::SHOW_POWEROCR_SHARED_EVENT);
}

static String ^ AwakeExitEvent() {
return gcnew String(CommonSharedConstants::AWAKE_EXIT_EVENT);
}

};
}
3 changes: 3 additions & 0 deletions src/common/interop/shared_constants.h
Expand Up @@ -38,6 +38,9 @@ namespace CommonSharedConstants
// Path to the event used by AlwaysOnTop
const wchar_t ALWAYS_ON_TOP_PIN_EVENT[] = L"Local\\AlwaysOnTopPinEvent-892e0aa2-cfa8-4cc4-b196-ddeb32314ce8";

// Path to the event used by PowerOCR
const wchar_t SHOW_POWEROCR_SHARED_EVENT[] = L"Local\\PowerOCREvent-dc864e06-e1af-4ecc-9078-f98bee745e3a";

// Max DWORD for key code to disable keys.
const DWORD VK_DISABLED = 0x100;
}
2 changes: 2 additions & 0 deletions src/common/logger/logger_settings.h
Expand Up @@ -23,6 +23,7 @@ struct LogSettings
inline const static std::wstring fancyZonesOldLogPath = L"FancyZonesLogs\\"; // needed to clean up old logs
inline const static std::string shortcutGuideLoggerName = "shortcut-guide";
inline const static std::wstring shortcutGuideLogPath = L"ShortcutGuideLogs\\shortcut-guide-log.txt";
inline const static std::wstring powerOcrLogPath = L"Logs\\power-ocr-log.txt";
inline const static std::string keyboardManagerLoggerName = "keyboard-manager";
inline const static std::wstring keyboardManagerLogPath = L"Logs\\keyboard-manager-log.txt";
inline const static std::string findMyMouseLoggerName = "find-my-mouse";
Expand All @@ -31,6 +32,7 @@ struct LogSettings
inline const static std::string imageResizerLoggerName = "imageresizer";
inline const static std::string powerRenameLoggerName = "powerrename";
inline const static std::string alwaysOnTopLoggerName = "always-on-top";
inline const static std::string powerOcrLoggerName = "power-ocr";
inline const static std::wstring alwaysOnTopLogPath = L"always-on-top-log.txt";
inline const static int retention = 30;
std::wstring logLevel;
Expand Down
10 changes: 10 additions & 0 deletions src/modules/PowerOCR/PowerOCR/App.xaml
@@ -0,0 +1,10 @@
<Application
x:Class="PowerOCR.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PowerOCR"
ShutdownMode="OnExplicitShutdown"
Exit="Application_Exit"
Startup="Application_Startup">
<Application.Resources />
</Application>
88 changes: 88 additions & 0 deletions src/modules/PowerOCR/PowerOCR/App.xaml.cs
@@ -0,0 +1,88 @@
// 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;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using ManagedCommon;
using PowerOCR.Helpers;
using PowerOCR.Keyboard;
using PowerOCR.Settings;

namespace PowerOCR;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application, IDisposable
{
private KeyboardMonitor? keyboardMonitor;
private EventMonitor? eventMonitor;
private Mutex? _instanceMutex;
private int _powerToysRunnerPid;

public void Dispose()
{
GC.SuppressFinalize(this);
keyboardMonitor?.Dispose();
}

private void Application_Startup(object sender, StartupEventArgs e)
{
// allow only one instance of PowerOCR
_instanceMutex = new Mutex(true, @"Local\PowerToys_PowerOCR_InstanceMutex", out bool createdNew);
if (!createdNew)
{
Logger.LogWarning("Another running PowerOCR instance was detected. Exiting PowerOCR");
_instanceMutex = null;
Environment.Exit(0);
return;
}

if (e.Args?.Length > 0)
{
try
{
_ = int.TryParse(e.Args[0], out _powerToysRunnerPid);
Logger.LogInfo($"PowerOCR started from the PowerToys Runner. Runner pid={_powerToysRunnerPid}");

RunnerHelper.WaitForPowerToysRunner(_powerToysRunnerPid, () =>
{
Logger.LogInfo("PowerToys Runner exited. Exiting PowerOCR");
Environment.Exit(0);
});
var userSettings = new UserSettings(new Helpers.ThrottledActionInvoker());
eventMonitor = new EventMonitor();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
else
{
Logger.LogInfo($"PowerOCR started detached from PowerToys Runner.");
_powerToysRunnerPid = -1;
var userSettings = new UserSettings(new Helpers.ThrottledActionInvoker());
keyboardMonitor = new KeyboardMonitor(userSettings);
keyboardMonitor?.Start();
}
}

protected override void OnExit(ExitEventArgs e)
{
if (_instanceMutex != null)
{
_instanceMutex.ReleaseMutex();
}

base.OnExit(e);
}

private void Application_Exit(object sender, ExitEventArgs e)
{
Dispose();
}
}
57 changes: 57 additions & 0 deletions src/modules/PowerOCR/PowerOCR/Helpers/CursorClipper.cs
@@ -0,0 +1,57 @@
// 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;
using System.Windows;

namespace PowerOCR.Helpers;

/// <summary>
/// Functions to constrain the mouse cursor (typically used when dragging)
/// </summary>
public static class CursorClipper
{
/// <summary>
/// Constrain mouse cursor to the area of the specified UI element.
/// </summary>
/// <param name="element">Target UI element.</param>
/// <returns>True on success.</returns>
public static bool ClipCursor(FrameworkElement element)
{
const double dpi96 = 96.0;

var topLeft = element.PointToScreen(new Point(0, 0));

PresentationSource source = PresentationSource.FromVisual(element);
if (source?.CompositionTarget == null)
{
return false;
}

double dpiX = dpi96 * source.CompositionTarget.TransformToDevice.M11;
double dpiY = dpi96 * source.CompositionTarget.TransformToDevice.M22;

var width = (int)((element.ActualWidth + 1) * dpiX / dpi96);
var height = (int)((element.ActualHeight + 1) * dpiY / dpi96);

OSInterop.RECT rect = new OSInterop.RECT
{
Left = (int)topLeft.X,
Top = (int)topLeft.Y,
Right = (int)topLeft.X + width,
Bottom = (int)topLeft.Y + height,
};

return OSInterop.ClipCursor(ref rect);
}

/// <summary>
/// Remove any mouse cursor constraint.
/// </summary>
/// <returns>True on success.</returns>
public static bool UnClipCursor()
{
return OSInterop.ClipCursor(IntPtr.Zero);
}
}
12 changes: 12 additions & 0 deletions src/modules/PowerOCR/PowerOCR/Helpers/IThrottledActionInvoker.cs
@@ -0,0 +1,12 @@
// 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;

namespace PowerOCR.Helpers;

public interface IThrottledActionInvoker
{
void ScheduleAction(Action action, int milliseconds);
}

0 comments on commit eedea31

Please sign in to comment.