Skip to content

Commit

Permalink
Merge pull request #1199 from stevencohn/1196-color-all-pages
Browse files Browse the repository at this point in the history
1196 color all pages
  • Loading branch information
stevencohn authored Dec 2, 2023
2 parents 4a2dd34 + 81fadef commit be265dc
Show file tree
Hide file tree
Showing 18 changed files with 245 additions and 82 deletions.
54 changes: 29 additions & 25 deletions OneMore/Commands/Styles/ApplyStylesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ internal class ApplyStylesCommand : Command

private Page page;
private XNamespace ns;
private readonly Theme theme;
private readonly Stylizer stylizer;


public ApplyStylesCommand()
{
// using blank Style just so we have a valid Stylizer
stylizer = new Stylizer(new Style());
theme = new ThemeProvider().Theme;
}


Expand All @@ -44,31 +46,46 @@ public ApplyStylesCommand()
public override async Task Execute(params object[] args)
{
using var one = new OneNote(out page, out ns);
if (ApplyCurrentTheme())

// apply theme page color..

var changed = false;
if (!string.IsNullOrWhiteSpace(theme.Color))
{
var cmd = new PageColorCommand();
cmd.SetLogger(logger);
changed = cmd.UpdatePageColor(page, theme.Color);
}

// apply theme styles...

if (ApplyThemeStyles())
{
changed = true;
}

if (changed)
{
await one.Update(page);
}
}


/// <summary>
/// Invoked from PageColorCommand
/// Invoked from PageColorCommand. Sets only the theme styles, presuming
/// PageColorCommand has already set the page background color.
/// </summary>
/// <param name="page"></param>
public void Apply(Page page)
/// <param name="page">The page to modify</param>
public void ApplyTheme(Page page)
{
this.page = page;
ns = page.Namespace;
ApplyCurrentTheme();
ApplyThemeStyles();
}


private bool ApplyCurrentTheme()
private bool ApplyThemeStyles()
{
var theme = new ThemeProvider().Theme;

var changed = ApplyPageColor(theme.Color);

var styles = theme.GetStyles();
if (ApplyStyles(styles))
{
Expand All @@ -81,23 +98,10 @@ private bool ApplyCurrentTheme()
ApplyToHyperlinks();
}

changed = true;
}

return changed;
}


private bool ApplyPageColor(string color)
{
if (string.IsNullOrWhiteSpace(color))
{
return false;
return true;
}

var cmd = new PageColorCommand();
cmd.SetLogger(logger);
return cmd.UpdatePageColor(page, color);
return false;
}


Expand Down
101 changes: 73 additions & 28 deletions OneMore/Commands/Styles/PageColorCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ namespace River.OneMoreAddIn.Commands
/// </summary>
internal class PageColorCommand : Command
{
private string pageColor;
private ApplyStylesCommand styler;


public PageColorCommand()
{
}
Expand All @@ -39,22 +43,74 @@ public override async Task Execute(params object[] args)
}

using var dialog = new PageColorDialog(color, new ThemeProvider().Theme.Name);
if (dialog.ShowDialog(owner) == DialogResult.OK)
if (dialog.ShowDialog(owner) != DialogResult.OK)
{
UpdatePageColor(page, MakePageColor(dialog.Color));
return;
}

if (dialog.ApplyStyle)
{
ThemeProvider.RecordTheme(dialog.ThemeKey);
pageColor = MakePageColor(dialog.Color);

var cmd = new ApplyStylesCommand();
cmd.SetLogger(logger);
cmd.Apply(page);
}
if (dialog.ApplyStyle)
{
ThemeProvider.RecordTheme(dialog.ThemeKey);
styler = new ApplyStylesCommand();
styler.SetLogger(logger);
}

if (dialog.Scope == OneNote.Scope.Self)
{
UpdatePageColor(page, pageColor);
styler?.ApplyTheme(page);

using var one = new OneNote();
await one.Update(page);
}
else
{
await ColorPages(dialog.Scope, pageColor);
}
}


private async Task ColorPages(OneNote.Scope scope, string pageColor)
{

using var one = new OneNote();

var root = scope == OneNote.Scope.Pages
? one.GetSection()
: await one.GetNotebook(OneNote.Scope.Pages);

var ns = root.GetNamespaceOfPrefix(OneNote.Prefix);

var nodes = root.Descendants(ns + "Page")
.Where(e => e.Attribute("isInRecycleBin") == null);

using var progress = new UI.ProgressDialog();
progress.SetMaximum(nodes.Count());

progress.ShowDialogWithCancel(async (dialog, token) =>
{
foreach (var node in nodes)
{
var page = one.GetPage(node.Attribute("ID").Value, OneNote.PageDetail.Basic);
if (token.IsCancellationRequested) break;
progress.SetMessage(page.Title);
UpdatePageColor(page, pageColor);
styler?.ApplyTheme(page);
if (token.IsCancellationRequested) break;
await one.Update(page);
progress.Increment();
if (token.IsCancellationRequested) break;
}
return true;
});
}


Expand Down Expand Up @@ -88,36 +144,25 @@ public bool UpdatePageColor(Page page, string color)

var changed = false;

var attr = element.Attribute("color");
if (attr != null)
var attribute = element.Attribute("color");
if (attribute == null)
{
if (attr.Value != color)
{
attr.Value = color;
changed = true;
}
element.Add(new XAttribute("color", color));
changed = true;
}
else
else if (attribute.Value != color)
{
element.Add(new XAttribute("color", color));
attribute.Value = color;
changed = true;
}

if (changed)
{
// if light->dark or dark->light, apply appropriate theme...

var dark = false;
if (color != StyleBase.Automatic)
{
dark = ColorTranslator.FromHtml(color).IsDark();
}

logger.WriteLine($"color set to {color} (dark:{dark})");
logger.WriteLine($"page color set to {color} for {page.Title}");
}
else
{
logger.WriteLine($"page color unchanged");
logger.WriteLine($"page color unchanged on {page.Title}");
}

return changed;
Expand Down
50 changes: 40 additions & 10 deletions OneMore/Commands/Styles/PageColorDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit be265dc

Please sign in to comment.