Skip to content

Commit

Permalink
cancel resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed Nov 3, 2024
1 parent 27c2347 commit 576aa87
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
13 changes: 9 additions & 4 deletions Source/ImageGlass/FrmMain/FrmMain.IGMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3289,13 +3289,18 @@ public void IG_OpenResizeTool()
{
if (Local.IsBusy) return;

using var frm = new FrmResize();
if (frm.ShowDialog() != DialogResult.OK) return;

if (Local.FrmResize.IsDisposed)
{
Local.FrmResize = new();
}

if (Local.FrmResize.ShowDialog() != DialogResult.OK) return;


if (frm.Result != null)
if (Local.FrmResize.Result != null)
{
LoadClipboardImage(frm.Result);
LoadClipboardImage(Local.FrmResize.Result);
}
}

Expand Down
10 changes: 10 additions & 0 deletions Source/ImageGlass/Local.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class Local
{
private static CancellationTokenSource? _gcTokenSrc;
private static FrmSettings _frmSetting;
private static FrmResize _frmResize;

public static FrmMain? FrmMain;

Expand Down Expand Up @@ -274,6 +275,15 @@ public static FrmSettings FrmSettings
set => _frmSetting = value;
}

/// <summary>
/// Gets Resize tool window.
/// </summary>
public static FrmResize FrmResize
{
get => LazyInitializer.EnsureInitialized(ref _frmResize);
set => _frmResize = value;
}

#endregion


Expand Down
2 changes: 1 addition & 1 deletion Source/ImageGlass/Tools/FrmResize.Designer.cs

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

34 changes: 22 additions & 12 deletions Source/ImageGlass/Tools/FrmResize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace ImageGlass.Tools;
public partial class FrmResize : DialogForm
{
private readonly IProgress<ImageResizedEventArgs> _uiReporter;
private CancellationTokenSource? _tokenSrc;
private Size _inputSize;
private Size _outputSize;

Expand Down Expand Up @@ -64,23 +65,21 @@ protected override void OnLoad(EventArgs e)
ApplyLanguage();


var workingArea = Screen.FromControl(this).WorkingArea;
if (Bottom > workingArea.Bottom) Top = workingArea.Bottom - Height;


// load initial data
_inputSize =
_outputSize = new(
(int)Local.FrmMain.PicMain.SourceWidth,
(int)Local.FrmMain.PicMain.SourceHeight);

LoadResamplingMethods();
LblCurrentSizeValue.Text = LblNewSizeValue.Text =
$"{_outputSize.Width:n0} x {_outputSize.Height:n0} px"; // thousands place for number
// thousands place for number
LblCurrentSizeValue.Text = $"{_outputSize.Width:n0} x {_outputSize.Height:n0} px";


// set type of resizing
LblSizeUnit.Text = "px";
UpdateSize(_inputSize.Width, _inputSize.Height);
LoadResamplingMethods();

var initSize = RadResizeByPixels.Checked ? _inputSize : new(100, 100);
UpdateSize(initSize.Width, initSize.Height);

NumWidth.Minimum = NumHeight.Minimum = 1;
NumWidth.Maximum = Const.MAX_IMAGE_DIMENSION;
Expand Down Expand Up @@ -159,11 +158,17 @@ protected override void OnAcceptButtonClicked()
return;
}


_tokenSrc?.Cancel();
_tokenSrc = new();

_ = BHelper.RunAsThread(async () => await SubmitResizeAsync(finalSize, samplingMethod));
}

protected override void OnCancelButtonClicked()
{
_tokenSrc?.Cancel();

base.OnCancelButtonClicked();
}

Expand Down Expand Up @@ -201,7 +206,8 @@ private void ApplyLanguage()

private void LoadResamplingMethods()
{
CmbResample.Items.Clear();
// use cache for the next open
if (CmbResample.Items.Count > 0) return;

foreach (ImageResamplingMethod method in Enum.GetValues<ImageResamplingMethod>())
{
Expand Down Expand Up @@ -280,7 +286,11 @@ private async Task SubmitResizeAsync(Size size, ImageResamplingMethod samplingMe


// perform resizing
var resizedBmp = await BHelper.ResizeImageAsync(bmp, size.Width, size.Height, samplingMethod);
WicBitmapSource? resizedBmp = null;
if (!_tokenSrc.IsCancellationRequested)
{
resizedBmp = await BHelper.ResizeImageAsync(bmp, size.Width, size.Height, samplingMethod);
}

_uiReporter.Report(new("resizer:end", resizedBmp));
}
Expand All @@ -296,7 +306,7 @@ private void ReportToUIThread(ImageResizedEventArgs e)

if (e.Type == "resizer:end")
{
if (e.Image != null)
if (e.Image != null && !_tokenSrc.IsCancellationRequested)
{
Result = e.Image;
base.OnAcceptButtonClicked();
Expand Down

0 comments on commit 576aa87

Please # to comment.