// When updating page content, this mechanism prevents overlapped I/O; // otherwise OneNote may throw a COMException: // hrLastModifiedDateDidNotMatch (HRESULT 0x80042010) // REF: https://msdn.microsoft.com/en-us/library/office/ff966472(v=office.14).aspx async void FireAndForgetAsync(TemplateMethod template, IPageSettingsModel settings) { Action action = () => { try { SynchronizationContext.Current?.OperationStarted(); var context = PageContext.CurrentWindow(Application); XDocument page; if (context.IsMyJournalNotebook(out page)) { var sw = Stopwatch.StartNew(); template.Invoke(context, page, settings); PageTemplate.ScrollToTopOfPage(); Tracer.WriteInfoLine("{0} elapsed time: {1} ms", template.Method.Name, sw.ElapsedMilliseconds); } } catch (Exception ex) { Utils.ExceptionHandler.HandleException(ex); } finally { SynchronizationContext.Current?.OperationCompleted(); } }; await ExclusiveTaskFactory.StartNew(action, CancellationToken.None, TaskCreationOptions.DenyChildAttach, ExclusiveScheduler ).ConfigureAwait(false); }
/// <summary> /// Sets the page rule lines. /// </summary> /// <param name="pageSettings">PageSettings XML element</param> /// <param name="pageModel">page settings data model</param> protected void SetRuleLines(XElement pageSettings, IPageSettingsModel pageModel) { var ruleLines = pageSettings.Element(OneNS + "RuleLines"); if (ruleLines == null) { throw new ArgumentNullException( nameof(pageSettings), "one:RuleLines element not found"); } ruleLines.Descendants().Remove(); var visible = pageModel.RuleLinesVisible; if (visible) { ruleLines.Add( new XElement(OneNS + "Horizontal", new XAttribute(COLOR_ATTRIBUTE_NAME, pageModel .RuleLinesHorizontalColor.XmlEnumValue()), new XAttribute("spacing", pageModel .RuleLinesHorizontalSpacing.XmlEnumValue()) ) ); ruleLines.Add( new XElement(OneNS + "Margin", new XAttribute(COLOR_ATTRIBUTE_NAME, pageModel .RuleLinesMarginColor.XmlEnumValue()) ) ); } ruleLines.SetAttributeValue("visible", visible); }
async Task CreateNewPageAsync(IPageSettingsModel settings) { await Task.Run(() => { var context = new PageContext(Application, settings.Title); if (context.PageNotFound()) { var sw = Stopwatch.StartNew(); AddJournalPageContent(context, context.CreateNewPage(), settings); context.NavigateToPage(); RunKeyboardMacro(); Tracer.WriteInfoLine("CreateNewPageAsync elapsed time: {0} ms", sw.ElapsedMilliseconds); //DEBUG*/ context.SaveCurrentPageToDisk(); //DEBUG*/ context.SendCurrentPageToClipboard(); } else { context.NavigateToPage(); ScrollToBottomOfPage(); } }).ConfigureAwait(false); }
public override void ChangeJournalRuleLinesVisible(PageContext context, XDocument page, IPageSettingsModel settings) { Tracer.WriteTraceMethodLine(); SetRuleLines(page.Root.Element(OneNS + "PageSettings"), settings); context.UpdateMyJournal(page); }
public override void ChangeJournalPageTitle(PageContext context, XDocument page, IPageSettingsModel settings) { Tracer.WriteTraceMethodLine(); var created = (DateTime)page.Root.Attribute("dateTime"); var title = created.Format(settings.Title); SetPageTitle(page, title, PAGE_TITLE_FONT_STYLE); context.UpdateMyJournal(page); }
public override void ChangeJournalPageColor(PageContext context, XDocument page, IPageSettingsModel settings) { Tracer.WriteTraceMethodLine(); var element = page.Root.Element(OneNS + "PageSettings"); var value = settings.Color.XmlEnumValue(); element.SetAttributeValue(COLOR_ATTRIBUTE_NAME, value); context.UpdateMyJournal(page); }
public override void AddJournalPageContent(PageContext context, XDocument page, IPageSettingsModel settings) { Tracer.WriteTraceMethodLine(); InitPageSettings(page, settings); SetPageTitle(page, context.PageName, PAGE_TITLE_FONT_STYLE); page.Root.Add(CreatePageContent()); // DateTime.MinValue is used to tell OneNote to skip its optimistic // concurrency check; this is the initial page update. context.UpdateMyJournal(page, DateTime.MinValue); }
/// <summary> /// Sets the page size, rule lines style and background color. /// </summary> /// <param name="page">page content in XML format</param> /// <param name="pageModel">page settings data model</param> protected void InitPageSettings(XDocument page, IPageSettingsModel pageModel) { var pageSettings = page.Root.Element(OneNS + "PageSettings"); if (pageSettings == null) { throw new ArgumentNullException( nameof(page), "one:PageSettings element not found"); } SetPageSize(pageSettings); SetRuleLines(pageSettings, pageModel); // Set Page background color pageSettings.SetAttributeValue(COLOR_ATTRIBUTE_NAME, pageModel.Color.XmlEnumValue()); }
public override void ChangeJournalRuleLinesSpacing(PageContext context, XDocument page, IPageSettingsModel settings) { Tracer.WriteTraceMethodLine(); var pageSettings = page.Root.Element(OneNS + "PageSettings"); var ruleLines = pageSettings.Element(OneNS + "RuleLines"); if ((bool)ruleLines.Attribute("visible")) { var element = ruleLines.Element(OneNS + "Horizontal"); var value = settings.RuleLinesHorizontalSpacing.XmlEnumValue(); element.SetAttributeValue("spacing", value); } else { SetRuleLines(pageSettings, settings); } context.UpdateMyJournal(page); }
Task CreateNewPageTask(IPageSettingsModel settings) { Action action = () => { var context = new PageContext(Application, settings.Title); if (context.PageNotFound()) { AddJournalPageContent(context, context.CreateNewPage(), settings); context.NavigateToPage(); RunKeyboardMacro(); //DEBUG*/ context.SaveCurrentPageToDisk(); //DEBUG*/ context.SendCurrentPageToClipboard(); } else { context.NavigateToPage(); ScrollToBottomOfPage(); } }; var tcs = new TaskCompletionSource <object>(); var thread = new Thread(() => { try { action(); tcs.SetResult(null); } catch (Exception e) { tcs.SetException(e); } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); return(tcs.Task); }
internal static void Save(IPageSettingsModel pageSettings) { if (pageSettings.IsModified()) { Tracer.WriteTraceMethodLine(); try { var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true }; using (var writer = XmlWriter.Create(s_xmlFileName, settings)) { s_serializer.Serialize(writer, pageSettings); } } catch (Exception e) { Utils.ExceptionHandler.HandleException(e); } } }
/// <summary> /// The template method for changing the journal page title. /// </summary> public abstract void ChangeJournalPageTitle(PageContext context, XDocument page, IPageSettingsModel settings);
/// <summary> /// The template method for changing the journal page rule lines visibility. /// </summary> public abstract void ChangeJournalRuleLinesVisible(PageContext context, XDocument page, IPageSettingsModel settings);
/// <summary> /// The template method for changing the journal page rule lines margin color. /// </summary> public abstract void ChangeJournalRuleLinesMarginColor(PageContext context, XDocument page, IPageSettingsModel settings);
/// <summary> /// The template method for adding content to a journal page. /// </summary> public abstract void AddJournalPageContent(PageContext context, XDocument page, IPageSettingsModel settings);
public override void ChangeJournalRuleLinesMarginColor(PageContext context, XDocument page, IPageSettingsModel settings) { Tracer.WriteTraceMethodLine(); throw new NotImplementedException(); }