Skip to content

Commit

Permalink
fix: make sure to update ui in its thread
Browse files Browse the repository at this point in the history
  • Loading branch information
atticus-lv committed Dec 6, 2024
1 parent 18a8199 commit 5d06f08
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/VirtualStreetSnap/Services/ScreenshotHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public static class ScreenshotHelper
/// </summary>
/// <param name="screenBounds">The bounds of the screen to capture.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the captured screen as a Bitmap.</returns>
public static async Task<Bitmap> CaptureFullScreenAsync(PixelRect screenBounds)
public static async Task<Bitmap> CaptureFullScreenAsync(PixelRect screenBounds,int msDelay = 10)
{
await Task.Delay(5); // This is a workaround to prevent the app from freezing when capturing the screen.
await Task.Delay(msDelay); // This is a workaround to prevent the app from freezing when capturing the screen.
using var bitmap = new System.Drawing.Bitmap(screenBounds.Width, screenBounds.Height);
using var g = Graphics.FromImage(bitmap);
g.CopyFromScreen(screenBounds.X, screenBounds.Y, 0, 0,
Expand Down
11 changes: 6 additions & 5 deletions src/VirtualStreetSnap/Views/SnapShotView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Platform;
using Avalonia.Threading;
using VirtualStreetSnap.Services;
using VirtualStreetSnap.ViewModels;
using VirtualStreetSnap.Models;
Expand Down Expand Up @@ -37,11 +39,10 @@ private async void SnapshotButton_Click(object? sender, RoutedEventArgs e)
{
GetCurrentScreenInfo();
if (_currentScreen == null) return;
Overlay.IsVisible = false;
await Dispatcher.UIThread.InvokeAsync(() => Overlay.IsVisible = false);
var currentScreen = _currentScreen;
Console.WriteLine(_window);
var screenshot = await ScreenshotHelper.CaptureFullScreenAsync(currentScreen.Bounds);
Overlay.IsVisible = true;
var screenshot = await Task.Run(() => ScreenshotHelper.CaptureFullScreenAsync(currentScreen.Bounds));
await Dispatcher.UIThread.InvokeAsync(() => Overlay.IsVisible = true);

// calculate capture area
var appScale = currentScreen.Scaling;
Expand Down Expand Up @@ -171,7 +172,7 @@ private void DisplayTextBlock_PointerPressed(object sender, PointerPressedEventA
}

private void InputElement_OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
{
var _window = TopLevel.GetTopLevel(this) as Window;
_window?.BeginMoveDrag(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/VirtualStreetSnap/VirtualStreetSnap.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
<Version>0.1.5</Version>
<Version>0.1.5.1</Version>
<Authors>Atticus</Authors>
<Description>A simple screen shot tool design for VirtualStreetSnap</Description>
</PropertyGroup>
Expand Down

0 comments on commit 5d06f08

Please sign in to comment.