Skip to content

Commit 41d8fb9

Browse files
fix: 修正“更换壁纸”路径转译错误问题 #1
1 parent f7d624a commit 41d8fb9

3 files changed

Lines changed: 47 additions & 43 deletions

File tree

Actions/ChangeWallpaperAction.cs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System;
1+
using ClassIsland.Core.Abstractions.Automation;
2+
using ClassIsland.Core.Attributes;
3+
using Microsoft.Extensions.Logging;
4+
using System;
25
using System.Diagnostics;
36
using System.IO;
4-
using System.Text;
57
using System.Threading.Tasks;
6-
using ClassIsland.Core.Abstractions.Automation;
7-
using ClassIsland.Core.Attributes;
8-
using Microsoft.Extensions.Logging;
98
using SystemTools.Settings;
109

1110
namespace SystemTools.Actions;
@@ -22,7 +21,9 @@ public ChangeWallpaperAction(ILogger<ChangeWallpaperAction> logger)
2221

2322
protected override async Task OnInvoke()
2423
{
25-
if (string.IsNullOrWhiteSpace(Settings.ImagePath))
24+
_logger.LogDebug("ChangeWallpaperAction OnInvoke 开始");
25+
26+
if (Settings == null || string.IsNullOrWhiteSpace(Settings.ImagePath))
2627
{
2728
_logger.LogWarning("图片路径为空");
2829
return;
@@ -36,21 +37,13 @@ protected override async Task OnInvoke()
3637

3738
try
3839
{
39-
string escapedPath = Settings.ImagePath.Replace("\\", "\\\\");
40-
string script = $@"
41-
Add-Type @'
42-
using System;
43-
using System.Runtime.InteropServices;
44-
public class W{{[DllImport(""user32.dll"", CharSet=CharSet.Auto)]public static extern int SystemParametersInfo(int a, int b, string c, int d);}}
45-
'@;
46-
[W]::SystemParametersInfo(20, 0, ""{escapedPath}"", 0x01)".Trim();
47-
48-
string encodedScript = Convert.ToBase64String(Encoding.Unicode.GetBytes(script));
40+
var imagePath = Settings.ImagePath;
41+
_logger.LogInformation("正在切换壁纸到: {Path}", imagePath);
4942

5043
var psi = new ProcessStartInfo
5144
{
5245
FileName = "powershell.exe",
53-
Arguments = $"-NoProfile -EncodedCommand {encodedScript}",
46+
Arguments = $"-NoProfile -Command \"Add-Type -TypeDefinition 'using System; using System.Runtime.InteropServices; public class W {{ [DllImport(\\\"user32.dll\\\", CharSet = CharSet.Auto)] public static extern int SystemParametersInfo(int a, int b, string c, int d); }}' -Language CSharp; [W]::SystemParametersInfo(20, 0, '{imagePath.Replace("'", "''")}', 0x01)\"",
5447
CreateNoWindow = true,
5548
UseShellExecute = false,
5649
RedirectStandardOutput = true,
@@ -59,25 +52,27 @@ public class W{{[DllImport(""user32.dll"", CharSet=CharSet.Auto)]public static e
5952
};
6053

6154
using var process = Process.Start(psi);
62-
if (process != null)
55+
if (process == null)
6356
{
64-
string output = await process.StandardOutput.ReadToEndAsync();
65-
string error = await process.StandardError.ReadToEndAsync();
66-
await process.WaitForExitAsync();
57+
throw new Exception("无法启动 PowerShell 进程");
58+
}
6759

68-
if (!string.IsNullOrEmpty(error) && !error.TrimStart().StartsWith("#< CLIXML"))
69-
{
70-
_logger.LogWarning("PowerShell 错误: {Error}", error);
71-
}
60+
string output = await process.StandardOutput.ReadToEndAsync();
61+
string error = await process.StandardError.ReadToEndAsync();
62+
await process.WaitForExitAsync();
7263

73-
if (process.ExitCode == 0)
74-
_logger.LogInformation("壁纸切换成功: {Path}", Settings.ImagePath);
75-
else
76-
_logger.LogWarning("壁纸切换可能失败,退出码: {ExitCode}", process.ExitCode);
64+
if (!string.IsNullOrEmpty(error) && !error.TrimStart().StartsWith("#< CLIXML"))
65+
{
66+
_logger.LogWarning("PowerShell 错误: {Error}", error);
67+
}
68+
69+
if (process.ExitCode == 0)
70+
{
71+
_logger.LogInformation("壁纸切换成功: {Path}", imagePath);
7772
}
7873
else
7974
{
80-
throw new Exception("无法启动 PowerShell 进程");
75+
_logger.LogWarning("壁纸切换可能失败,退出码: {ExitCode}", process.ExitCode);
8176
}
8277
}
8378
catch (Exception ex)
@@ -87,5 +82,6 @@ public class W{{[DllImport(""user32.dll"", CharSet=CharSet.Auto)]public static e
8782
}
8883

8984
await base.OnInvoke();
85+
_logger.LogDebug("ChangeWallpaperAction OnInvoke 完成");
9086
}
9187
}

Controls/WallpaperSettingsControl.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,48 @@ namespace SystemTools.Controls;
1212

1313
public class ChangeWallpaperSettingsControl : ActionSettingsControlBase<ChangeWallpaperSettings>
1414
{
15-
private TextBox _pathBox;
15+
private Avalonia.Controls.TextBox _pathBox;
16+
private Avalonia.Controls.Button _browseButton;
1617

1718
public ChangeWallpaperSettingsControl()
1819
{
19-
var panel = new StackPanel { Spacing = 10, Margin = new(10) };
20+
var panel = new Avalonia.Controls.StackPanel { Spacing = 10, Margin = new(10) };
2021

21-
panel.Children.Add(new TextBlock
22+
panel.Children.Add(new Avalonia.Controls.TextBlock
2223
{
2324
Text = "图片路径:",
2425
FontWeight = Avalonia.Media.FontWeight.Bold
2526
});
2627

27-
_pathBox = new TextBox
28+
_pathBox = new Avalonia.Controls.TextBox
2829
{
29-
Watermark = "输入路径:路径中不得出现中文字符"
30+
Watermark = "请选择壁纸图片文件"
31+
};
32+
_pathBox.TextChanged += (s, e) =>
33+
{
34+
Settings.ImagePath = _pathBox.Text ?? "";
3035
};
3136
panel.Children.Add(_pathBox);
3237

33-
var browseButton = new Button
38+
_browseButton = new Avalonia.Controls.Button
3439
{
3540
Content = "浏览...",
3641
Width = 100,
3742
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Left,
3843
Margin = new(0, 5, 0, 0)
3944
};
40-
browseButton.Click += async (sender, e) => await BrowseButton_Click();
41-
panel.Children.Add(browseButton);
45+
_browseButton.Click += async (sender, e) => await BrowseButton_Click();
46+
panel.Children.Add(_browseButton);
4247

4348
Content = panel;
4449
}
4550

4651
protected override void OnInitialized()
4752
{
4853
base.OnInitialized();
49-
_pathBox[!TextBox.TextProperty] = new Binding(nameof(Settings.ImagePath))
50-
{
51-
Source = Settings
52-
};
54+
_pathBox.Text = Settings.ImagePath;
5355
}
56+
5457
private async Task BrowseButton_Click()
5558
{
5659
try
@@ -82,7 +85,9 @@ private async Task BrowseButton_Click()
8285
var result = await topLevel.StorageProvider.OpenFilePickerAsync(options);
8386
if (result?.Count > 0)
8487
{
85-
Settings.ImagePath = result[0].Path.LocalPath;
88+
var path = result[0].Path.LocalPath;
89+
Settings.ImagePath = path;
90+
_pathBox.Text = path;
8691
}
8792
}
8893
catch (Exception ex)

Settings/MouseInputSettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public class MouseInputSettings
77
{
88
[JsonPropertyName("actions")]
99
public List<MouseAction> Actions { get; set; } = new();
10+
11+
[JsonPropertyName("disableMouseDuringExecution")]
12+
public bool DisableMouseDuringExecution { get; set; } = false;
1013
}
1114

1215
public class MouseAction

0 commit comments

Comments
 (0)