Compare commits

...

2 Commits

Author SHA1 Message Date
8284a9d552 break: ActiveDocument 改为实时获取
优化调试逻辑,增强日志记录,简化代码结构

- 修改 AIProofread.csproj.user 的 <StartAction> 配置,调试时直接启动项目。
- Bridge.cs 中增加异常处理的详细错误信息。
- DocumentInfo.cs 增加日志记录,调整 RunInMainThread 方法逻辑。
- DocumentList.cs 删除过时的 XML 注释。
- ThisAddIn.cs 优化文档管理逻辑,调整 ActiveDocument 属性为只读,改进文档列表处理,删除冗余代码。
2025-05-09 16:59:27 +08:00
a54765e911 优化面板按钮逻辑
- 调整 `TaskPane_VisibleChanged` 方法逻辑,增加异步延迟处理和详细日志记录,修复 `CurrentDocument` 引用问题。
- 优化 `CheckBtnStatus` 方法,提升代码可读性并修正按钮状态逻辑。
- 改进 `TaskPane` 释放逻辑,增加对控件的重置操作。
2025-05-08 16:48:10 +08:00
7 changed files with 90 additions and 54 deletions

Binary file not shown.

View File

@ -7,7 +7,7 @@
<SupportUrlHistory />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<StartAction>Program</StartAction>
<StartAction>Project</StartAction>
<StartProgram>C:\Soft\Kingsoft\WPS Office\12.1.0.20784\office6\wps.exe</StartProgram>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">

View File

@ -452,6 +452,8 @@ namespace AIProofread
catch (Exception ex)
{
Logger.Error("校对文档格式有误或内容异常", ex);
data["code"] = 6;
data["message"] = "文档格式有误或内容异常,请另存文档后再进行校对";
throw ex;
}
//if (loadingDialog != null && !loadingDialog.IsDisposed)

View File

@ -26,6 +26,7 @@ namespace AIProofread
{
public static readonly string APP_NAME = "AI校对王";
public static readonly string APP_VERSION = "2.2.5";
public static readonly string BuildVersion = "20250509_1656";
public static bool IS_WPS = false;
public static bool UpgradeForcedNotice = false;
public static readonly string APP_BASE_DIR = AppDomain.CurrentDomain.BaseDirectory;
@ -42,7 +43,7 @@ namespace AIProofread
/// <summary>
/// 网页访问地址
/// </summary>
public static string WEB_PATH = AppServer.PROD; //pre-gm-plugin.gachafun.com localhost:5173 gm2-plugin.zverse.group
public static string WEB_PATH = AppServer.DEV; //pre-gm-plugin.gachafun.com localhost:5173 gm2-plugin.zverse.group
public static bool RUN_IN_DEBUG = true;
public static AppEnvironment APP_ENV = AppEnvironment.Prod;
#else

View File

@ -96,6 +96,7 @@ namespace AIProofread.Model
// 初始化
public DocumentInfo(Document doc)
{
Logger.Debug("new DocumentInfo for " + doc.Name);
this.CurrentDocument = doc;
Initialize();
}
@ -109,7 +110,7 @@ namespace AIProofread.Model
{
CreateTaskPane();
}
Logger.Debug("TaskPane.Visible {" + TaskPane == null ? "null" : (TaskPane.Visible ? "true" : "false") + " => true");
Logger.Debug("TaskPane.Visible {" + TaskPane == null ? "null" : (TaskPane.Visible ? "true" : "false") + " => true " + fileName);
TaskPane.Visible = PaneVisible = true;
}
@ -125,10 +126,10 @@ namespace AIProofread.Model
}
public void RunInMainThread(Action action)
public void RunInMainThread(Action action,bool isClose = false)
{
Logger.Debug($"RunInMainThread {action}");
if (null == TaskPane)
Logger.Debug("RunInMainThread for " + fileName);
if (null == TaskPane && !isClose)
{
CreateTaskPane();
}
@ -188,7 +189,7 @@ namespace AIProofread.Model
{
try
{
Logger.Debug("激活 for " + CurrentDocument.FullName + " 没有 visible is " + PaneVisible);
Logger.Debug("激活 for " + CurrentDocument.Name + " 没有 visible is " + PaneVisible);
ShowDocumentStatus("Active");
IsActive = true;
if (Config.IS_WPS && null != TaskPane)
@ -212,9 +213,10 @@ namespace AIProofread.Model
{
PaneVisible = TaskPane.Visible;
TaskPane.Visible = false;
Logger.Debug("取消 for " + CurrentDocument.FullName + " current visible " + PaneVisible);
Logger.Debug("取消 for " + CurrentDocument.Name + " current visible " + PaneVisible);
}
}catch (Exception e)
}
catch (Exception e)
{
Logger.Error("Deactive Error", e);
}
@ -227,7 +229,7 @@ namespace AIProofread.Model
// 判断TaskPane是否已经释放
if (null == TaskPane) return;
if (TaskPane.Control.IsDisposed) return;
ProofreadMainControl control = (ProofreadMainControl)TaskPane.Control;
control.ResetWeb();
@ -304,22 +306,28 @@ namespace AIProofread.Model
//
if (Globals.ThisAddIn.ribbon != null)
{
Globals.ThisAddIn.ribbon.BtnShowPanel.Enabled = !PaneVisible && Proofread;
var visible = TaskPane.Visible;
Globals.ThisAddIn.ribbon.BtnShowPanel.Enabled = !visible && Proofread;
Globals.ThisAddIn.ribbon.SetCommonBtnStatus(!Proofreading);
}
}
private void TaskPane_VisibleChanged(object sender, EventArgs e)
{
if (Config.IS_WPS)
// 异步等待500ms
System.Threading.Tasks.Task.Delay(1000).ContinueWith(t =>
{
if(CurrentDocument == Globals.ThisAddIn.ActiveDocument)
Logger.Debug($"2==> VisibleChanged {CurrentDocument.Name} is {TaskPane.Visible}");
if (Config.IS_WPS)
{
Logger.Debug($"VisibleChanged ${CurrentDocument.Name} is {TaskPane.Visible}");
// 如果已经隐藏 则记录隐藏用于(WPS)多面板的切换的处理
PaneVisible = TaskPane.Visible;
if (CurrentDocument == Globals.ThisAddIn.ActiveDocument.CurrentDocument)
{
// 如果已经隐藏 则记录隐藏用于(WPS)多面板的切换的处理
PaneVisible = TaskPane.Visible;
}
}
}
Globals.ThisAddIn.ActiveDocument?.CheckBtnStatus();
});
CheckBtnStatus();
//Globals.ThisAddIn.ribbon.BtnShowPanel.Enabled = !TaskPane.Visible && Proofread;
}
@ -331,9 +339,8 @@ namespace AIProofread.Model
ComputeUniqueId();
if (TaskPane == null)
{
Logger.Debug("init document Initialize(318) and CreateTaskPane");
Logger.Debug("Initialize(334) and CreateTaskPane for " + CurrentDocument.Name);
CreateTaskPane();
}
}

View File

@ -103,7 +103,6 @@ namespace AIProofread.Model
{
return documentList.IndexOf(item);
}
/// <summary>
/// 设置当前激活的文档
/// </summary>

View File

@ -57,7 +57,13 @@ namespace AIProofread
/// <summary>
/// 当前文档信息
/// </summary>
public DocumentInfo ActiveDocument { get; set; }
public DocumentInfo ActiveDocument
{
get
{
return documentList.SetActiveDocument(Application.ActiveDocument);
}
}
/// <summary>
/// 智能常识检测对话框 = new FormCommonsenseDetection()
/// </summary>
@ -146,7 +152,7 @@ namespace AIProofread
if (Application.Documents.Count > 0 && Application.ActiveDocument != null)
{
Logger.Debug("ThisAddIn_Startup 开始初始化当前文档");
ActiveDocument = documentList.InitDocument(Application.ActiveDocument);
documentList.InitDocument(Application.ActiveDocument);
// 直接初始化面板
// info.CheckPanel();
@ -197,7 +203,7 @@ namespace AIProofread
public void CheckDocumentClosed(object sender, System.Timers.ElapsedEventArgs e)
{
var existsList = new List<string>();
var existsList = new List<Document>();
try
{
var docList = CurrentWordApplication.Documents;
@ -205,7 +211,7 @@ namespace AIProofread
existsList.Clear();
foreach (Document item in docList)
{
existsList.Add(item.FullName);
existsList.Add(item);
}
// 检测文档是否关闭
@ -219,21 +225,21 @@ namespace AIProofread
continue;
}
// 可能出现另存问题 所以需要更新文件名称
var oldName = item.fileName;
var currentName = item.CurrentDocument.FullName;
if (oldName != currentName)
//var oldName = item.fileName;
//var currentName = item.CurrentDocument.FullName;
//if (oldName != currentName)
//{
// item.fileName = currentName;
//}
if (!existsList.Contains(item.CurrentDocument))
{
item.fileName = currentName;
}
if (!existsList.Contains(currentName))
{
Logger.Debug("检测到文档关闭,已移除:" + currentName);
Logger.Debug("检测到文档关闭,已移除:" + item.fileName);
try
{
item.RunInMainThread(() =>
{
item.Dispose();
});
}, true);
}
catch (Exception ex1)
{
@ -246,7 +252,7 @@ namespace AIProofread
}
catch (Exception ex2)
{
Logger.Error("移除已关闭文档",ex2);
Logger.Error("移除已关闭文档", ex2);
}
}
}
@ -291,8 +297,8 @@ namespace AIProofread
return;
}
if (ActiveDocument == null) return;
// TODO 完成缓存保存
//if (ActiveDocument == null) return;
//// TODO 完成缓存保存
}
@ -370,26 +376,28 @@ namespace AIProofread
private void Application_DocumentChange()
{
// 检测是否存在打开的文档
if (CurrentWordApplication.Documents.Count == 0)
{
return;
}
var document = documentList.InitDocument(CurrentWordApplication.ActiveDocument);
if(document == null) return;
var currentDocument = CurrentWordApplication.ActiveDocument;
Logger.Debug("DocumentChange => " + currentDocument.Name);
CheckDocumentClosed(null, null);
if (formCommonsenseDetection != null)
{
formCommonsenseDetection.SendMessageToWeb("document-change", null);
}
if (InDocumentInList(currentDocument))
{
documentList.InitDocument(currentDocument);
}
//var document = documentList.InitDocument(CurrentWordApplication.ActiveDocument);
//if (document == null) return;
}
public void SetActiveDocument(Document doc)
{
ActiveDocument = documentList.SetActiveDocument(doc);
}
private void Application_WindowDeactivate(Document doc, Window Wn)
{
@ -401,6 +409,24 @@ namespace AIProofread
//}
}
/// <summary>
/// 判断当前文档是否在列表中
/// </summary>
/// <param name="doc"></param>
/// <returns></returns>
private bool InDocumentInList(Document doc)
{
if (doc == null) return false;
foreach (Document item in Application.Documents)
{
if (item == doc)
{
return true;
}
}
return false;
}
/// <summary>
/// 激活文档
/// </summary>
@ -408,17 +434,18 @@ namespace AIProofread
/// <param name="Wn"></param>
private void Application_WindowActivate(Document activeDoc, Window Wn)
{
Logger.Debug("Application_WindowActivate -- " + activeDoc.Name);
if (activeDoc != null)
Logger.Debug("WindowActivate -- " + activeDoc.Name);
if (activeDoc != null && InDocumentInList(activeDoc))
{
Logger.Debug("DocumentChange -- " + activeDoc.Name
+ " 修订模式: is " + activeDoc.TrackRevisions
+ ActiveDocument?.CurrentDocument?.Name + "==》" + activeDoc.Name);
var document = documentList.SetActiveDocument(activeDoc);
// 设置当前文档
ActiveDocument = document;
documentList.SetActiveDocument(activeDoc);
//// 设置当前文档
//ActiveDocument = document;
}
//// 当前文档添加书签集合
//if (!allMarks.ContainsKey(activeDoc))
//{
@ -477,7 +504,7 @@ namespace AIProofread
private void Application_DocumentOpen(Document doc)
{
//LogHelper.Log("DocumentOpen " + doc.Name);
}
}
public int GetMinWidth()
@ -566,20 +593,20 @@ namespace AIProofread
public void GlobalCallback(string callbackId, string result)
{
ActiveDocument?.GlobalCallback(callbackId, result);
documentList.SetActiveDocument(Application.ActiveDocument)?.GlobalCallback(callbackId, result);
}
// 显示面板
public void ShowPanel()
{
Logger.Debug("ShowPanel");
ActiveDocument?.ShowPane();
documentList.SetActiveDocument(Application.ActiveDocument)?.ShowPane();
}
// 隐藏面板
public void HidePanel()
{
ActiveDocument?.HidePane();
documentList.SetActiveDocument(Application.ActiveDocument)?.HidePane();
}
/// <summary>