Skip to content

Commit 7a45dd3

Browse files
AlexAlves87claude
andcommitted
refactor: extract StartupInputConfigurator from App.xaml.cs
Move the mouse-in-pointer startup configuration out of the App constructor into a dedicated StartupInputConfigurator helper. The P/Invoke declarations, the OPENCLAW_DISABLE_MOUSE_IN_POINTER guard, the env-var parsing and the configuration logic all move together; the constructor now calls StartupInputConfigurator.Configure() in the same spot, before InitializeComponent(), preserving startup ordering. Pure extraction, no behavior change. Logging strings, guard order, captured values and caught exceptions are identical to the original. Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent eb06fba commit 7a45dd3

2 files changed

Lines changed: 73 additions & 60 deletions

File tree

‎src/OpenClaw.Tray.WinUI/App.xaml.cs‎

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,10 @@ public IntPtr GetHubWindowHandle()
199199
"OpenClawTray");
200200
private static readonly string CrashLogPath = Path.Combine(DataPath, "crash.log");
201201
private static readonly AppRunMarker s_runMarker = new(Path.Combine(DataPath, "run.marker"));
202-
private const string DisableMouseInPointerEnv = "OPENCLAW_DISABLE_MOUSE_IN_POINTER";
203-
204-
[DllImport("user32.dll", SetLastError = true)]
205-
[return: MarshalAs(UnmanagedType.Bool)]
206-
private static extern bool EnableMouseInPointer([MarshalAs(UnmanagedType.Bool)] bool fEnable);
207-
208-
[DllImport("user32.dll")]
209-
[return: MarshalAs(UnmanagedType.Bool)]
210-
private static extern bool IsMouseInPointerEnabled();
211202

212203
public App()
213204
{
214-
ConfigureMouseInPointerInput();
205+
StartupInputConfigurator.Configure();
215206

216207
// Language override for localization testing (e.g., OPENCLAW_LANGUAGE=zh-CN)
217208
var langOverride = Environment.GetEnvironmentVariable("OPENCLAW_LANGUAGE");
@@ -237,56 +228,6 @@ public App()
237228
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
238229
}
239230

240-
private static void ConfigureMouseInPointerInput()
241-
{
242-
if (IsTruthyEnvVar(Environment.GetEnvironmentVariable(DisableMouseInPointerEnv)))
243-
{
244-
Logger.Warn($"[Input] Mouse-in-pointer startup configuration disabled by {DisableMouseInPointerEnv}=1.");
245-
return;
246-
}
247-
248-
try
249-
{
250-
var before = IsMouseInPointerEnabled();
251-
if (before)
252-
{
253-
Logger.Info("[Input] Mouse-in-pointer was already enabled before WinUI initialization.");
254-
return;
255-
}
256-
257-
var enabled = EnableMouseInPointer(true);
258-
var error = enabled ? 0 : Marshal.GetLastWin32Error();
259-
var after = IsMouseInPointerEnabled();
260-
if (enabled && after)
261-
{
262-
Logger.Info("[Input] Enabled mouse-in-pointer before WinUI initialization for precision touchpad scrolling.");
263-
}
264-
else
265-
{
266-
Logger.Warn($"[Input] EnableMouseInPointer(true) did not enable mouse-in-pointer. Before={before}, After={after}, LastError={error}.");
267-
}
268-
}
269-
catch (DllNotFoundException ex)
270-
{
271-
Logger.Warn($"[Input] EnableMouseInPointer startup configuration failed: {ex.Message}");
272-
}
273-
catch (EntryPointNotFoundException ex)
274-
{
275-
Logger.Warn($"[Input] EnableMouseInPointer startup configuration failed: {ex.Message}");
276-
}
277-
catch (SEHException ex)
278-
{
279-
Logger.Warn($"[Input] EnableMouseInPointer startup configuration failed: {ex.Message}");
280-
}
281-
}
282-
283-
private static bool IsTruthyEnvVar(string? value) =>
284-
value is not null &&
285-
(string.Equals(value, "1", StringComparison.OrdinalIgnoreCase) ||
286-
string.Equals(value, "true", StringComparison.OrdinalIgnoreCase) ||
287-
string.Equals(value, "yes", StringComparison.OrdinalIgnoreCase) ||
288-
string.Equals(value, "on", StringComparison.OrdinalIgnoreCase));
289-
290231
private void OnUnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
291232
{
292233
LogCrash("UnhandledException", e.Exception);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace OpenClawTray.Services;
5+
6+
/// <summary>
7+
/// Configures process-wide mouse-in-pointer input before WinUI initialization
8+
/// so precision touchpad scrolling works. Best-effort; failures are logged and
9+
/// never propagate. Must run before <c>InitializeComponent()</c>.
10+
/// </summary>
11+
internal static class StartupInputConfigurator
12+
{
13+
private const string DisableMouseInPointerEnv = "OPENCLAW_DISABLE_MOUSE_IN_POINTER";
14+
15+
[DllImport("user32.dll", SetLastError = true)]
16+
[return: MarshalAs(UnmanagedType.Bool)]
17+
private static extern bool EnableMouseInPointer([MarshalAs(UnmanagedType.Bool)] bool fEnable);
18+
19+
[DllImport("user32.dll")]
20+
[return: MarshalAs(UnmanagedType.Bool)]
21+
private static extern bool IsMouseInPointerEnabled();
22+
23+
public static void Configure()
24+
{
25+
if (IsTruthyEnvVar(Environment.GetEnvironmentVariable(DisableMouseInPointerEnv)))
26+
{
27+
Logger.Warn($"[Input] Mouse-in-pointer startup configuration disabled by {DisableMouseInPointerEnv}=1.");
28+
return;
29+
}
30+
31+
try
32+
{
33+
var before = IsMouseInPointerEnabled();
34+
if (before)
35+
{
36+
Logger.Info("[Input] Mouse-in-pointer was already enabled before WinUI initialization.");
37+
return;
38+
}
39+
40+
var enabled = EnableMouseInPointer(true);
41+
var error = enabled ? 0 : Marshal.GetLastWin32Error();
42+
var after = IsMouseInPointerEnabled();
43+
if (enabled && after)
44+
{
45+
Logger.Info("[Input] Enabled mouse-in-pointer before WinUI initialization for precision touchpad scrolling.");
46+
}
47+
else
48+
{
49+
Logger.Warn($"[Input] EnableMouseInPointer(true) did not enable mouse-in-pointer. Before={before}, After={after}, LastError={error}.");
50+
}
51+
}
52+
catch (DllNotFoundException ex)
53+
{
54+
Logger.Warn($"[Input] EnableMouseInPointer startup configuration failed: {ex.Message}");
55+
}
56+
catch (EntryPointNotFoundException ex)
57+
{
58+
Logger.Warn($"[Input] EnableMouseInPointer startup configuration failed: {ex.Message}");
59+
}
60+
catch (SEHException ex)
61+
{
62+
Logger.Warn($"[Input] EnableMouseInPointer startup configuration failed: {ex.Message}");
63+
}
64+
}
65+
66+
private static bool IsTruthyEnvVar(string? value) =>
67+
value is not null &&
68+
(string.Equals(value, "1", StringComparison.OrdinalIgnoreCase) ||
69+
string.Equals(value, "true", StringComparison.OrdinalIgnoreCase) ||
70+
string.Equals(value, "yes", StringComparison.OrdinalIgnoreCase) ||
71+
string.Equals(value, "on", StringComparison.OrdinalIgnoreCase));
72+
}

0 commit comments

Comments
 (0)