Skip to content

Commit 8cd3db8

Browse files
authored
Merge pull request #493 from Freeesia/copilot/update-dependency-apps-for-oneocr
OneOcrを利用する際に依存アプリが古かったら更新を促す
2 parents 5de10d5 + af4d30b commit 8cd3db8

15 files changed

Lines changed: 531 additions & 15 deletions

Plugins/WindowTranslator.Plugin.OneOcrPlugin/OneOcr.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Panlingo.LanguageIdentification.FastText;
99
using Windows.Graphics.Imaging;
1010
using WindowTranslator.Collections;
11+
using WindowTranslator.ComponentModel;
1112
using WindowTranslator.Modules;
1213
using WindowTranslator.Plugin.OneOcrPlugin.Properties;
1314
using WinRT;
@@ -17,6 +18,7 @@
1718

1819
namespace WindowTranslator.Plugin.OneOcrPlugin;
1920

21+
[DefaultModule]
2022
public sealed class OneOcr : IOcrModule, IDisposable
2123
{
2224
const string apiKey = "kj)TGtrK>f]b[Piow.gU+nC@s\"\"\"\"\"\"4";

Plugins/WindowTranslator.Plugin.OneOcrPlugin/OneOcrValidator.cs

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
using WindowTranslator.Modules;
1+
using Microsoft.Extensions.Logging;
2+
using WindowTranslator.Modules;
23
using WindowTranslator.Plugin.OneOcrPlugin.Properties;
4+
using Wpf.Ui;
5+
using Wpf.Ui.Controls;
6+
using Wpf.Ui.Extensions;
37

48
namespace WindowTranslator.Plugin.OneOcrPlugin;
59

6-
public class OneOcrValidator : ITargetSettingsValidator
10+
public class OneOcrValidator(IContentDialogService dialogService, ILogger<OneOcrValidator> logger) : ITargetSettingsValidator
711
{
12+
private static readonly ValidateResult Invalid = ValidateResult.Invalid(Resources.OneOcr, Resources.NotFoundModule);
13+
private readonly IContentDialogService dialogService = dialogService;
14+
private readonly ILogger<OneOcrValidator> logger = logger;
15+
private bool versionChecked = false;
16+
817
public async ValueTask<ValidateResult> Validate(TargetSettings settings)
918
{
1019
// 翻訳モジュールで利用しない場合は無条件で有効
@@ -13,16 +22,87 @@ public async ValueTask<ValidateResult> Validate(TargetSettings settings)
1322
return ValidateResult.Valid;
1423
}
1524

25+
// ScreenSketchのバージョンをチェック
26+
if (!this.versionChecked && !await Utility.CheckScreenSketchVersionAsync(this.logger))
27+
{
28+
// バージョンが古い場合、確認ダイアログを表示
29+
var confirmResult = await this.dialogService.ShowSimpleDialogAsync(new()
30+
{
31+
Title = Resources.UpdateDialogTitle,
32+
Content = Resources.UpdateDialogContent,
33+
PrimaryButtonText = Resources.OpenStoreButton,
34+
CloseButtonText = Resources.DoNotUpdateButton
35+
});
36+
37+
if (confirmResult != ContentDialogResult.Primary)
38+
{
39+
return Invalid;
40+
}
41+
42+
// Microsoft Storeを開く
43+
Utility.OpenStoreForUpdate();
44+
45+
// 更新チェック用のキャンセルトークン
46+
var checkCts = new CancellationTokenSource();
47+
var dialogCts = new CancellationTokenSource();
48+
49+
// バックグラウンドでバージョンチェックを継続
50+
var checkTask = Task.Run(async () =>
51+
{
52+
while (!checkCts.Token.IsCancellationRequested)
53+
{
54+
await Task.Delay(TimeSpan.FromSeconds(5), checkCts.Token);
55+
56+
if (await Utility.CheckScreenSketchVersionAsync(this.logger))
57+
{
58+
// 更新完了
59+
dialogCts.Cancel();
60+
return;
61+
}
62+
}
63+
}, checkCts.Token);
64+
65+
try
66+
{
67+
// 更新中ダイアログを表示
68+
await this.dialogService.ShowSimpleDialogAsync(new()
69+
{
70+
Title = Resources.CheckingUpdateTitle,
71+
Content = Resources.CheckingUpdateContent,
72+
CloseButtonText = Resources.AbortButton
73+
}, dialogCts.Token);
74+
75+
// ユーザーが中断した場合
76+
await checkCts.CancelAsync();
77+
return Invalid;
78+
}
79+
catch (OperationCanceledException)
80+
{
81+
// 更新完了によるダイアログのキャンセル
82+
}
83+
84+
try
85+
{
86+
await checkTask;
87+
}
88+
catch (OperationCanceledException)
89+
{
90+
// タスクのキャンセル
91+
}
92+
}
93+
94+
this.versionChecked = true;
95+
1696
if (!Utility.NeedCopyDll())
1797
{
1898
return ValidateResult.Valid;
1999
}
20100

21101
// OneOcrのインストール先を取得
22-
var oneOcrPath = await Utility.FindOneOcrPath().ConfigureAwait(false);
102+
var oneOcrPath = await Utility.FindOneOcrPath();
23103
if (oneOcrPath == null)
24104
{
25-
return ValidateResult.Invalid("OneOcr", Resources.NotFoundModule);
105+
return Invalid;
26106
}
27107

28108
// DLLをコピー
@@ -32,7 +112,7 @@ public async ValueTask<ValidateResult> Validate(TargetSettings settings)
32112
}
33113
catch (Exception ex)
34114
{
35-
return ValidateResult.Invalid("OneOcr", string.Format(Resources.CopyFaild, ex.Message));
115+
return ValidateResult.Invalid(Resources.OneOcr, string.Format(Resources.CopyFaild, ex.Message));
36116
}
37117

38118
return ValidateResult.Valid;

Plugins/WindowTranslator.Plugin.OneOcrPlugin/Properties/Resources.Designer.cs

Lines changed: 85 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Plugins/WindowTranslator.Plugin.OneOcrPlugin/Properties/Resources.de.resx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,37 @@ Bitte versuchen Sie, die Apps "Ausschneiden und skizzieren" oder "Microsoft Foto
157157
<data name="GetLineBoundingBoxFaild" xml:space="preserve">
158158
<value>Fehler beim Abrufen der OCR-Zeilen-Begrenzungsbox. Zeilennummer: {0}, Fehlercode: {1}</value>
159159
</data>
160+
<data name="NotFoundModuleWithVersion" xml:space="preserve">
161+
<value>Abhängige Module nicht gefunden. Nicht verfügbar in dieser Umgebung. {0}</value>
162+
</data>
163+
<data name="UpdateDialogTitle" xml:space="preserve">
164+
<value>Snipping Tool aktualisieren</value>
165+
</data>
166+
<data name="UpdateDialogContent" xml:space="preserve">
167+
<value>Um OneOcr zu verwenden, müssen Sie die App "Snipping Tool" aktualisieren.
168+
169+
Bitte öffnen Sie den Microsoft Store zur Aktualisierung.</value>
170+
</data>
171+
<data name="OpenStoreButton" xml:space="preserve">
172+
<value>Store öffnen</value>
173+
</data>
174+
<data name="DoNotUpdateButton" xml:space="preserve">
175+
<value>Nicht aktualisieren</value>
176+
</data>
177+
<data name="CheckingUpdateTitle" xml:space="preserve">
178+
<value>Suche nach Updates...</value>
179+
</data>
180+
<data name="CheckingUpdateContent" xml:space="preserve">
181+
<value>Suche nach Updates für "Snipping Tool"...
182+
183+
Bitte schließen Sie das Update im Microsoft Store ab.
184+
Der Vorgang wird automatisch fortgesetzt, wenn das Update abgeschlossen ist.
185+
186+
> #### Wenn das Update nicht abgeschlossen wird
187+
> Bitte aktualisieren Sie manuell über "Downloads und Updates" im Microsoft Store und versuchen Sie es erneut.
188+
> [Downloads und Updates öffnen](ms-windows-store://downloadsandupdates)</value>
189+
</data>
190+
<data name="AbortButton" xml:space="preserve">
191+
<value>Abbrechen</value>
192+
</data>
160193
</root>

Plugins/WindowTranslator.Plugin.OneOcrPlugin/Properties/Resources.en.resx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,37 @@ Please try updating "Snipping Tool" or "Microsoft Photos" app.</value>
157157
<data name="GetLineBoundingBoxFaild" xml:space="preserve">
158158
<value>Failed to get OCR line bounding box. Line number: {0}, Error code: {1}</value>
159159
</data>
160+
<data name="NotFoundModuleWithVersion" xml:space="preserve">
161+
<value>Dependent modules not found. Not available in this environment. {0}</value>
162+
</data>
163+
<data name="UpdateDialogTitle" xml:space="preserve">
164+
<value>Update Snipping Tool</value>
165+
</data>
166+
<data name="UpdateDialogContent" xml:space="preserve">
167+
<value>To use OneOcr, you need to update the "Snipping Tool" app.
168+
169+
Please open Microsoft Store to update.</value>
170+
</data>
171+
<data name="OpenStoreButton" xml:space="preserve">
172+
<value>Open Store</value>
173+
</data>
174+
<data name="DoNotUpdateButton" xml:space="preserve">
175+
<value>Don't Update</value>
176+
</data>
177+
<data name="CheckingUpdateTitle" xml:space="preserve">
178+
<value>Checking for Updates...</value>
179+
</data>
180+
<data name="CheckingUpdateContent" xml:space="preserve">
181+
<value>Checking for "Snipping Tool" updates...
182+
183+
Please complete the update in Microsoft Store.
184+
The process will continue automatically when the update is complete.
185+
186+
> #### If the update doesn't complete
187+
> Please manually update from "Downloads and Updates" in Microsoft Store and try again.
188+
> [Open Downloads and Updates](ms-windows-store://downloadsandupdates)</value>
189+
</data>
190+
<data name="AbortButton" xml:space="preserve">
191+
<value>Abort</value>
192+
</data>
160193
</root>

Plugins/WindowTranslator.Plugin.OneOcrPlugin/Properties/Resources.hi.resx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,37 @@
9999
<data name="GetLineBoundingBoxFaild" xml:space="preserve">
100100
<value>OCR लाइन बाउंडिंग बॉक्स प्राप्त करने में विफल। लाइन नंबर: {0}, त्रुटि कोड: {1}</value>
101101
</data>
102+
<data name="NotFoundModuleWithVersion" xml:space="preserve">
103+
<value>निर्भर मॉड्यूल नहीं मिला। इस वातावरण में उपलब्ध नहीं है। {0}</value>
104+
</data>
105+
<data name="UpdateDialogTitle" xml:space="preserve">
106+
<value>स्निपिंग टूल अपडेट करें</value>
107+
</data>
108+
<data name="UpdateDialogContent" xml:space="preserve">
109+
<value>OneOcr का उपयोग करने के लिए, आपको "स्निपिंग टूल" ऐप को अपडेट करना होगा।
110+
111+
कृपया अपडेट करने के लिए Microsoft Store खोलें।</value>
112+
</data>
113+
<data name="OpenStoreButton" xml:space="preserve">
114+
<value>Store खोलें</value>
115+
</data>
116+
<data name="DoNotUpdateButton" xml:space="preserve">
117+
<value>अपडेट न करें</value>
118+
</data>
119+
<data name="CheckingUpdateTitle" xml:space="preserve">
120+
<value>अपडेट जांच रहे हैं...</value>
121+
</data>
122+
<data name="CheckingUpdateContent" xml:space="preserve">
123+
<value>"स्निपिंग टूल" के लिए अपडेट की जांच हो रही है...
124+
125+
कृपया Microsoft Store में अपडेट पूरा करें।
126+
अपडेट पूरा होने पर प्रक्रिया स्वचालित रूप से जारी रहेगी।
127+
128+
> #### यदि अपडेट पूरा नहीं होता है
129+
> कृपया Microsoft Store में "डाउनलोड और अपडेट" से मैन्युअल रूप से अपडेट पूरा करें और पुनः प्रयास करें।
130+
> [डाउनलोड और अपडेट खोलें](ms-windows-store://downloadsandupdates)</value>
131+
</data>
132+
<data name="AbortButton" xml:space="preserve">
133+
<value>रद्द करें</value>
134+
</data>
102135
</root>

0 commit comments

Comments
 (0)