Skip to content

Commit

Permalink
Merge pull request #1270 from stevencohn/1258-optimize-image-adjustment
Browse files Browse the repository at this point in the history
optimize image adjustment
  • Loading branch information
stevencohn authored Jan 20, 2024
2 parents 6572e4d + cdbe7c5 commit 6bf4db5
Show file tree
Hide file tree
Showing 33 changed files with 1,110 additions and 969 deletions.
9 changes: 4 additions & 5 deletions OneMore/AddInCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public async Task AddFormulaCmd(IRibbonControl control)
=> await factory.Run<AddFormulaCommand>();


[Command("ribAdjustImagesButton_Label", Keys.None, "ribImagesMenu")]
public async Task AdjustImagesCmd(IRibbonControl control)
=> await factory.Run<AdjustImagesCommand>();

public async Task AnalyzeCmd(IRibbonControl control)
=> await factory.Run<AnalyzeCommand>();

Expand Down Expand Up @@ -707,11 +711,6 @@ public async Task ReportRemindersCmd(IRibbonControl control)
=> await factory.Run<ReportRemindersCommand>();


[Command("ribResizeImagesButton_Label", Keys.None, "ribImagesMenu")]
public async Task ResizeImagesCmd(IRibbonControl control)
=> await factory.Run<ResizeImagesCommand>();


[Command("ribRestoreAutosizeButton_Label", Keys.None, "ribCleanMenu")]
public async Task RestoreAutosizeCmd(IRibbonControl control)
=> await factory.Run<RestoreAutosizeCommand>();
Expand Down
141 changes: 141 additions & 0 deletions OneMore/Commands/Images/AdjustImagesCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//************************************************************************************************
// Copyright © 2020 Steven M Cohn. All rights reserved.
//************************************************************************************************

namespace River.OneMoreAddIn.Commands
{
using River.OneMoreAddIn.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Resx = Properties.Resources;


/// <summary>
/// Resize and adjust images on the page
/// </summary>
internal class AdjustImagesCommand : Command
{
public AdjustImagesCommand()
{
}


public override async Task Execute(params object[] args)
{
using var one = new OneNote(out var page, out var ns, OneNote.PageDetail.All);

// starting at Outline should exclude all background images
var elements = page.Root
.Elements(ns + "Outline")
.Descendants(ns + "Image")?
.Where(e => e.Attribute("selected")?.Value == "all")
.ToList();

if ((elements == null) || !elements.Any())
{
elements = page.Root
.Elements(ns + "Outline").Descendants(ns + "Image")
.ToList();
}

if (elements.Any())
{
var updated = elements.Count == 1
// single selected image
? ResizeOne(elements[0])
// multiple selections or all if none selected
: ResizeMany(elements, page, page.Root.Elements(ns + "Image").Any());

if (updated)
{
await one.Update(page);
}
}
else
{
UIHelper.ShowMessage(Resx.AdjustImagesDialog_noImages);
}
}


private bool ResizeOne(XElement element)
{
var wrapper = new OneImage(element);
using var image = wrapper.ReadImage();

using var dialog = new AdjustImagesDialog(image, wrapper.Width, wrapper.Height);
var result = dialog.ShowDialog(owner);
if (result == DialogResult.OK)
{
var editor = dialog.GetImageEditor(image);
if (editor.IsReady)
{
editor.Apply(wrapper);
return true;
}
}

return false;
}


private bool ResizeMany(IEnumerable<XElement> elements, Page page, bool hasBgImages)
{
using var dialog = new AdjustImagesDialog(hasBgImages);

var result = dialog.ShowDialog(owner);
if (result != DialogResult.OK)
{
return false;
}

var updated = false;
foreach (var element in elements)
{
var wrapper = new OneImage(element);
using var image = wrapper.ReadImage();

var editor = dialog.GetImageEditor(image);

// when pasting an image onto the page, width or height can be zero
// OneNote ignores both if either is zero so we'll do the same...
var viewWidth = wrapper.Width;
if (viewWidth == 0)
{
viewWidth = image.Width;
}

if (editor.IsReady)
{
if (editor.AutoSize ||
editor.Constraint == ImageEditor.SizeConstraint.All ||
(
editor.Constraint == ImageEditor.SizeConstraint.OnlyShrink &&
viewWidth > editor.Size.Width) ||
(
editor.Constraint == ImageEditor.SizeConstraint.OnlyEnlarge &&
viewWidth < editor.Size.Width))
{
using var edit = editor.Apply(wrapper);
updated = true;
}
else
{
logger.WriteLine($"skipped image, size=[{wrapper.Width} x {wrapper.Width}]");
}
}
}

if (dialog.RepositionImages)
{
new StackBackgroundImagesCommand().StackImages(page);
updated = true;
}

return updated;
}
}
}

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

Loading

0 comments on commit 6bf4db5

Please sign in to comment.