using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Runtime.InteropServices; using Office = Microsoft.Office.Core; namespace PlotDirector.WordAddIn { [ComVisible(true)] public sealed class NativeRibbon : Office.IRibbonExtensibility { private readonly Func adapter; private readonly Dictionary displayed = new Dictionary(); private Office.IRibbonUI ui; public NativeRibbon(Func adapter) { this.adapter = adapter; } public string GetCustomUI(string ribbonID) { using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("PlotDirector.WordAddIn.Ribbon.xml"))) return reader.ReadToEnd(); } public void OnLoad(Office.IRibbonUI ribbonUI) { ui = ribbonUI; displayed.Clear(); } public void Invalidate() { if (ui == null) return; try { Refresh("PlotDirectorStatus", GetStatus(null)); Refresh("PlotDirectorBook", GetBookTip(null)); Refresh("PlotDirectorChapter", GetChapterTip(null)); Refresh("PlotDirectorScene", GetSceneTip(null)); Refresh("PlotDirectorSyncStatus", GetSyncStatus(null) + "\n" + GetSyncTip(null)); Refresh("PlotDirectorSyncProgress", GetSyncProgress(null)); var enabled = CanOpenBook(null).ToString(); Refresh("PlotDirectorContext", enabled); Refresh("PlotDirectorIntent", enabled); Refresh("PlotDirectorWeb", enabled); Refresh("PlotDirectorSynchronise", enabled); Refresh("PlotDirectorRepair", enabled); } catch (COMException) { } } private void Refresh(string id, string value) { if (displayed.TryGetValue(id, out var previous) && previous == value) return; ui.InvalidateControl(id); displayed[id] = value; } public void RestoreTitle(Office.IRibbonControl control, string text) { ui?.InvalidateControl(control.Id); } public bool CanOpenBook(Office.IRibbonControl control) => adapter()?.HasRibbonBook == true; public string GetBookTip(Office.IRibbonControl control) => adapter()?.RibbonBook ?? "No linked book"; public string GetChapterTip(Office.IRibbonControl control) => adapter()?.RibbonChapter ?? "Chapter unverified"; public string GetSceneTip(Office.IRibbonControl control) => adapter()?.RibbonScene ?? "Scene unverified"; public string GetSyncStatus(Office.IRibbonControl control) => adapter()?.ChapterStatusLabel ?? "Not connected"; public string GetSyncProgress(Office.IRibbonControl control) => adapter()?.RibbonSyncProgress ?? " "; public string GetSyncTip(Office.IRibbonControl control) => adapter()?.ChapterStatusTip ?? "Open Companion to connect."; public string GetStatus(Office.IRibbonControl control) => adapter()?.ConnectionState == "connected" ? "Companion connected" : "Companion offline"; public void OpenContext(Office.IRibbonControl control) => adapter()?.OpenWritingWorkspace("tray-context"); public void OpenIntent(Office.IRibbonControl control) => adapter()?.OpenWritingWorkspace("tray-intent"); public void OpenPlotDirector(Office.IRibbonControl control) => adapter()?.OpenWritingWorkspace("tray-workspace"); public void Synchronise(Office.IRibbonControl control) => adapter()?.RequestChapterSynchronisation(false); public void Repair(Office.IRibbonControl control) => adapter()?.RequestChapterSynchronisation(true); public void Troubleshooting(Office.IRibbonControl control) => adapter()?.ShowTroubleshooting(); public void RemoveLegacyMarkers(Office.IRibbonControl control) => adapter()?.RemoveLegacyMarkers(); public void OpenCompanion(Office.IRibbonControl control) => adapter()?.OpenCompanion(); } }