Skip to content

Commit

Permalink
fix Ambilight freeze when property changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Oct 30, 2024
1 parent 3552578 commit 1f2acc7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,6 @@ public void Dispose()
{
_desktopDuplicator = null;
}
WindowListener.Dispose();
_windowListenerReference.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ internal sealed class GdiScreenCapture : IScreenCapture
{
public event EventHandler<Bitmap>? ScreenshotTaken;

private Bitmap? _bitmap;
private Bitmap? _lastBitmap;
private Graphics _graphics = Graphics.FromImage(new Bitmap(8, 8));
private readonly WindowListener.WindowListenerReference _windowListenerReference = new();

public WindowListener WindowListener => _windowListenerReference.WindowListener;

public void Capture(Rectangle desktopRegion, Bitmap bitmap)
{
if (_bitmap != bitmap)
if (_lastBitmap != bitmap)
{
_graphics.Dispose();
_graphics = Graphics.FromImage(bitmap);
Expand All @@ -30,7 +30,7 @@ public void Capture(Rectangle desktopRegion, Bitmap bitmap)
_graphics.SmoothingMode = SmoothingMode.HighSpeed;
_graphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;

_bitmap = bitmap;
_lastBitmap = bitmap;
}
_graphics.CopyFromScreen(desktopRegion.Location, Point.Empty, desktopRegion.Size);

Expand All @@ -44,8 +44,7 @@ public IEnumerable<string> GetDisplays() =>
public void Dispose()
{
_graphics.Dispose();
_bitmap?.Dispose();
_bitmap = null;
WindowListener.Dispose();
_lastBitmap = null;
_windowListenerReference.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public override void Default()
[DoNotNotify]
public sealed class AmbilightLayerHandler : LayerHandler<AmbilightLayerHandlerProperties>
{
private Temporary<IScreenCapture> _screenCapture;
private readonly Temporary<IScreenCapture> _screenCapture;

private readonly SmartThreadPool _captureWorker;
private readonly WorkItemCallback _screenshotWork;
Expand Down Expand Up @@ -426,35 +426,41 @@ protected override void PropertiesChanged(object? sender, PropertyChangedEventAr
_imageAttributes.SetColorMatrix(new ColorMatrix(mtx));
_imageAttributes.SetWrapMode(WrapMode.Clamp);

var activeProcessMonitor = ProcessesModule.ActiveProcessMonitor.Result;
activeProcessMonitor.ActiveProcessChanged -= ProcessChanged;
_screenCapture.Value.WindowListener.WindowCreated -= WindowsChanged;
_screenCapture.Value.WindowListener.WindowDestroyed -= WindowsChanged;
switch (Properties.AmbilightCaptureType)
switch (args.PropertyName)
{
case AmbilightCaptureType.SpecificProcess when !string.IsNullOrWhiteSpace(Properties.SpecificProcess):
UpdateSpecificProcessHandle(Properties.SpecificProcess);
case nameof(Properties.AmbilightCaptureType):
{
var activeProcessMonitor = ProcessesModule.ActiveProcessMonitor.Result;
activeProcessMonitor.ActiveProcessChanged -= ProcessChanged;
_screenCapture.Value.WindowListener.WindowCreated -= WindowsChanged;
_screenCapture.Value.WindowListener.WindowDestroyed -= WindowsChanged;
switch (Properties.AmbilightCaptureType)
{
case AmbilightCaptureType.SpecificProcess when !string.IsNullOrWhiteSpace(Properties.SpecificProcess):
UpdateSpecificProcessHandle(Properties.SpecificProcess);

_screenCapture.Value.WindowListener.WindowCreated += WindowsChanged;
_screenCapture.Value.WindowListener.WindowDestroyed += WindowsChanged;
break;
case AmbilightCaptureType.ForegroundApp:
_specificProcessHandle = User32.GetForegroundWindow();
activeProcessMonitor.ActiveProcessChanged += ProcessChanged;
_screenCapture.Value.WindowListener.WindowCreated += WindowsChanged;
_screenCapture.Value.WindowListener.WindowDestroyed += WindowsChanged;
break;
case AmbilightCaptureType.ForegroundApp:
_specificProcessHandle = User32.GetForegroundWindow();
activeProcessMonitor.ActiveProcessChanged += ProcessChanged;
break;
case AmbilightCaptureType.Coordinates:
case AmbilightCaptureType.EntireMonitor:
default:
break;
}

// the instance will be recreated
_screenCapture.Dispose();
break;
case AmbilightCaptureType.Coordinates:
case AmbilightCaptureType.EntireMonitor:
default:
}
case nameof(Properties.ExperimentalMode):
// the instance will be recreated
_screenCapture.Dispose();
break;
}

_screenCapture.Dispose();
_screenCapture = new Temporary<IScreenCapture>(() =>
{
IScreenCapture screenCapture = Properties.ExperimentalMode ? new DxScreenCapture() : new GdiScreenCapture();
screenCapture.ScreenshotTaken += ScreenshotAction;
return screenCapture;
});
}

private void ProcessChanged(object? sender, EventArgs e)
Expand Down
1 change: 1 addition & 0 deletions Project-Aurora/Project-Aurora/Utils/Temporary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void Dispose()
{
disposable.Dispose();
}
_value = null;
}

public async ValueTask DisposeAsync()
Expand Down

0 comments on commit 1f2acc7

Please sign in to comment.