Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: timer #268

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions MisakaTranslator-WPF/TranslateWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public partial class TranslateWindow
public volatile bool IsOCRingFlag; //线程锁:判断是否正在OCR线程中,保证同时只有一组在跑OCR
public bool IsNotPausedFlag; //是否处在暂停状态(专用于OCR),为真可以翻译

private static Timer ocrTimer; // ocr timing-task
private volatile bool ocrTimerPause; // ocr timing-task pause flag

private bool _isShowSource; //是否显示原文
private bool _isLocked;

Expand Down Expand Up @@ -270,6 +273,22 @@ public static ITranslator TranslatorAuto(string translator)
}
}

/// <summary>
/// 定时事件
/// </summary>
private void Hook_OnTimingActivity()
{
ocrTimerPause = false;
ocrTimer = new Timer(registerTimingOCR, null, 0, Common.UsingOCRDelay);
}
private void registerTimingOCR(object obj)
{
if (!ocrTimerPause)
{
TranslateEventOcr(isTimer: true);
}
}

/// <summary>
/// 键盘点击事件
/// </summary>
Expand All @@ -296,7 +315,7 @@ private void Hook_OnMouseActivity(object sender, POINT e)
/// OCR事件
/// </summary>
/// <param name="isRenew">是否是重新获取翻译</param>
private async void TranslateEventOcr(bool isRenew = false)
private async void TranslateEventOcr(bool isRenew = false, bool isTimer = false)
{
if (!IsNotPausedFlag && IsOCRingFlag)
return;
Expand All @@ -307,7 +326,7 @@ private async void TranslateEventOcr(bool isRenew = false)
for (int i = 0; i < 3; i++)
{
// 重新OCR不需要等待
if (!isRenew)
if (!isRenew || isTimer)
await Task.Delay(Common.UsingOCRDelay);

srcText = await Common.ocr.OCRProcessAsync();
Expand Down Expand Up @@ -714,6 +733,7 @@ private void Pause_Item_Click(object sender, RoutedEventArgs e)
{
PauseButton.SetValue(FontAwesome.WPF.Awesome.ContentProperty, FontAwesomeIcon.Play);
}
ocrTimerPause = !ocrTimerPause;
Common.textHooker.Pause = !Common.textHooker.Pause;
}
else
Expand Down Expand Up @@ -751,6 +771,11 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs
Common.appSettings.TF_SizeW = Convert.ToString((int)this.ActualWidth);
Common.appSettings.TF_SizeH = Convert.ToString((int)this.ActualHeight);

if (ocrTimer != null)
{
ocrTimerPause = true;
ocrTimer.Dispose();
}
if (hook != null)
{
hook.Stop();
Expand Down