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("PlotDirectorLocation", GetLocationTip(null)); Refresh("PlotDirectorSyncStatus", GetSyncStatus(null) + "\n" + GetSyncTip(null)); Refresh("PlotDirectorSyncProgress", GetSyncProgress(null)); var enabled = CanOpenBook(null).ToString(); Refresh("PlotDirectorContext", enabled); Refresh("PlotDirectorIntent", 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; } private static string ShortLabel(string text, int limit) => text.Length <= limit ? text : text.Substring(0, limit - (char.IsHighSurrogate(text[limit - 1]) ? 1 : 0)) + "…"; public bool CanOpenBook(Office.IRibbonControl control) => adapter()?.HasRibbonBook == true; public string GetBook(Office.IRibbonControl control) => ShortLabel(GetBookTip(control), 25); public string GetBookTip(Office.IRibbonControl control) => adapter()?.RibbonBook ?? "No linked book"; public string GetLocation(Office.IRibbonControl control) => ShortLabel(GetLocationTip(control), 32); public string GetLocationTip(Office.IRibbonControl control) => adapter()?.RibbonLocation ?? "Location unavailable"; 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 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(); } }