Skip to content

Commit aacd7a9

Browse files
committed
翻訳エラー時にダイアログ出せない問題修正
ついでにエラー表示をまとめる
1 parent 8cc7175 commit aacd7a9

6 files changed

Lines changed: 69 additions & 45 deletions

File tree

WindowTranslator.Abstractions/AppUserException.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,16 @@
33
/// <summary>
44
/// ユーザの操作ミスなど、アプリケーションの使用方法に起因する例外を表します。
55
/// </summary>
6-
/// <param name="message"></param>
7-
public class AppUserException(string? message) : Exception(message);
6+
/// <param name="message">エラーメッセージ</param>
7+
/// <param name="innerException">内部例外</param>
8+
public class AppUserException(string? message, Exception? innerException) : Exception(message, innerException)
9+
{
10+
/// <summary>
11+
/// コンストラクタ
12+
/// </summary>
13+
/// <param name="message">エラーメッセージ</param>
14+
public AppUserException(string? message)
15+
: this(message, null)
16+
{
17+
}
18+
}

WindowTranslator/Extensions/KamishibaiExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,17 @@ public static Task<bool> CloseAsync(this IWindow window)
2222
window.Close();
2323
return taskSource.Task;
2424
}
25+
26+
public static async ValueTask OpenErrorDialogAsync(this IPresentationService presentationService, string err, Exception ex, string target, string imagePath, object? owner = null)
27+
{
28+
if (ex is AppUserException || (ex is AggregateException { InnerExceptions: var exs } && exs.OfType<AppUserException>().Any()))
29+
{
30+
presentationService.ShowMessage(ex.Message, target, icon: MessageBoxImage.Exclamation, owner: owner);
31+
}
32+
else
33+
{
34+
await presentationService.OpenErrorReportDialogAsync(err, ex, target, imagePath, owner, new() { WindowStartupLocation = WindowStartupLocation.CenterOwner });
35+
}
36+
}
37+
2538
}

WindowTranslator/Modules/Main/MainViewModelBase.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using WindowTranslator.Modules.Capture;
1515
using WindowTranslator.Properties;
1616
using WindowTranslator.Stores;
17+
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
1718

1819
namespace WindowTranslator.Modules.Main;
1920

@@ -197,7 +198,7 @@ private async Task CreateTextOverlayAsync()
197198
this.capture.StopCapture();
198199
var path = Path.Combine(PathUtility.UserDir, $"ocr_error", $"{DateTime.UtcNow:yyyyMMdd'T'HHmmss'Z'}.png");
199200
await sbmp.TrySaveImage(path);
200-
await this.presentationService.OpenErrorReportDialogAsync(Resources.FaildOcr, e, this.name, path);
201+
await this.presentationService.OpenErrorDialogAsync(Resources.FaildOcr, e, this.name, path);
201202
StrongReferenceMessenger.Default.Send<CloseMessage>(new(this));
202203
return;
203204
}
@@ -295,9 +296,12 @@ private async Task TranslateAsync(IEnumerable<TextRect> texts)
295296
}
296297
catch (Exception e) when (e is not OperationCanceledException)
297298
{
299+
this.logger.LogError(e, "翻訳中にエラーが発生");
298300
this.timer.DisposeAsync().Forget();
299301
this.capture.StopCapture();
300-
await this.presentationService.OpenErrorReportDialogAsync(Resources.FaildTranslate, e, this.name, string.Empty);
302+
// 翻訳失敗してエラーで閉じる場合はキューをクリア
303+
Interlocked.Exchange(ref this.lastRequested, null);
304+
await Application.Current.Dispatcher.BeginInvoke(async () => await this.presentationService.OpenErrorDialogAsync(Resources.FaildOverlay, e, this.name, string.Empty));
301305
StrongReferenceMessenger.Default.Send<CloseMessage>(new(this));
302306
}
303307
finally

WindowTranslator/Modules/Main/MainWindowModule.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,38 @@ private async Task OpenTargetWindowCoreAsync(IntPtr targetWindowHandle, string n
2121
{
2222
using var l = await this.asyncLock.EnterAsync();
2323
var scope = provider.CreateScope();
24-
var options = scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<UserSettings>>();
25-
var presentationService = scope.ServiceProvider.GetRequiredService<IPresentationService>();
26-
var processInfo = scope.ServiceProvider.GetRequiredService<IProcessInfoStoreInternal>();
27-
processInfo.SetTargetProcess(targetWindowHandle, name);
28-
29-
if (!options.Value.Targets.ContainsKey(name) && !await presentationService.OpenAllSettingsDialogAsync(name))
24+
try
3025
{
31-
return;
32-
}
3326

34-
var window = options.Value.Common.ViewMode switch
35-
{
36-
ViewMode.Capture => await presentationService.OpenCaptureMainWindowAsync(),
37-
ViewMode.Overlay => await presentationService.OpenOverlayMainWindowAsync(),
38-
_ => throw new NotSupportedException(),
39-
};
40-
var info = new WindowInfo(name, targetWindowHandle, window);
41-
window.Closed += (_, _) =>
27+
var options = scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<UserSettings>>();
28+
var presentationService = scope.ServiceProvider.GetRequiredService<IPresentationService>();
29+
var processInfo = scope.ServiceProvider.GetRequiredService<IProcessInfoStoreInternal>();
30+
processInfo.SetTargetProcess(targetWindowHandle, name);
31+
32+
if (!options.Value.Targets.ContainsKey(name) && !await presentationService.OpenAllSettingsDialogAsync(name))
33+
{
34+
return;
35+
}
36+
37+
var window = options.Value.Common.ViewMode switch
38+
{
39+
ViewMode.Capture => await presentationService.OpenCaptureMainWindowAsync(),
40+
ViewMode.Overlay => await presentationService.OpenOverlayMainWindowAsync(),
41+
_ => throw new NotSupportedException(),
42+
};
43+
var info = new WindowInfo(name, targetWindowHandle, window);
44+
window.Closed += (_, _) =>
45+
{
46+
scope.Dispose();
47+
this.OpenedWindows.Remove(info);
48+
};
49+
this.OpenedWindows.Add(info);
50+
}
51+
catch (Exception)
4252
{
4353
scope.Dispose();
44-
this.OpenedWindows.Remove(info);
45-
};
46-
this.OpenedWindows.Add(info);
54+
throw;
55+
}
4756
}
4857

4958
public void Dispose()

WindowTranslator/Modules/Settings/AllSettingsViewModel.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,7 @@ public async Task SaveAsync(object window)
321321
}
322322
catch (Exception ex)
323323
{
324-
if (ex is AppUserException || (ex is AggregateException { InnerExceptions: var exs } && exs.OfType<AppUserException>().Any()))
325-
{
326-
this.presentationService.ShowMessage(ex.Message, icon: Kamishibai.MessageBoxImage.Exclamation, owner: window);
327-
}
328-
else
329-
{
330-
await this.presentationService.OpenErrorReportDialogAsync(Resources.FaildApplySettings, ex, this.target, string.Empty, owner: window);
331-
}
324+
await this.presentationService.OpenErrorDialogAsync(Resources.FaildApplySettings, ex, this.target, string.Empty, owner: window);
332325
}
333326
}
334327
else

WindowTranslator/Modules/Startup/StartupViewModel.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
using CommunityToolkit.Mvvm.ComponentModel;
2-
using CommunityToolkit.Mvvm.Input;
3-
using Composition.WindowsRuntimeHelpers;
4-
using Kamishibai;
5-
using Microsoft.Extensions.DependencyInjection;
6-
using System.Collections.ObjectModel;
1+
using System.Collections.ObjectModel;
72
using System.Collections.Specialized;
83
using System.Diagnostics;
94
using System.Windows;
105
using System.Windows.Input;
116
using System.Windows.Interop;
7+
using CommunityToolkit.Mvvm.ComponentModel;
8+
using CommunityToolkit.Mvvm.Input;
9+
using Composition.WindowsRuntimeHelpers;
10+
using Kamishibai;
11+
using Microsoft.Extensions.DependencyInjection;
1212
using Windows.Graphics.Capture;
1313
using Windows.Win32.Foundation;
14+
using WindowTranslator.Extensions;
1415
using WindowTranslator.Modules.Main;
1516
using WindowTranslator.Properties;
1617
using static Windows.Win32.PInvoke;
@@ -117,14 +118,7 @@ public async Task RunAsync()
117118
}
118119
catch (Exception ex)
119120
{
120-
if (ex is AppUserException || (ex is AggregateException { InnerExceptions: var exs } && exs.OfType<AppUserException>().Any()))
121-
{
122-
this.presentationService.ShowMessage(ex.Message, icon: Kamishibai.MessageBoxImage.Exclamation, owner: window);
123-
}
124-
else
125-
{
126-
await this.presentationService.OpenErrorReportDialogAsync(Resources.FaildOverlay, ex, p.Name, string.Empty, owner: window);
127-
}
121+
await this.presentationService.OpenErrorDialogAsync(Resources.FaildOverlay, ex, p.Name, string.Empty, window);
128122
}
129123
if (!beforeVisible)
130124
{

0 commit comments

Comments
 (0)