Skip to content

Commit

Permalink
chore: implement INotifyPropertyChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
ramezgerges committed Nov 20, 2024
1 parent 98d2b11 commit d982d10
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/AddIns/Uno.WinUI.Graphics3DGL/GLCanvasElement.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -37,7 +38,7 @@ namespace Uno.WinUI.Graphics3DGL;
/// This is only available on WinUI and on skia-based targets running with hardware acceleration.
/// This is currently only available on the WPF and X11 targets (and WinUI).
/// </remarks>
public abstract partial class GLCanvasElement : Grid, INativeContext
public abstract partial class GLCanvasElement : Grid, INativeContext, INotifyPropertyChanged
{
private const int BytesPerPixel = 4;
private static readonly Dictionary<XamlRoot, INativeOpenGLWrapper?> _xamlRootToWrapper = new();
Expand All @@ -46,6 +47,8 @@ public abstract partial class GLCanvasElement : Grid, INativeContext

private readonly Func<Window>? _getWindowFunc;

private bool? _isGlInitialized;

// valid if and only if GLCanvasElement was loaded at least once and OpenGL is available on the running platform
private INativeOpenGLWrapper? _nativeOpenGlWrapper;
// These are valid if and only if IsLoaded and _nativeOpenGlWrapper is not null
Expand Down Expand Up @@ -224,16 +227,25 @@ private void OnClosed(object _, object __)
/// Indicates whether this element was loaded successfully or not, including the OpenGL context creation and setup.
/// This property is only valid when the element is loaded. When the element is not loaded in the visual tree, the value will be null.
/// </summary>
public bool? LoadedSuccessfully { get; private set; }
public bool? IsGLInitialized
{
get => _isGlInitialized;
private set
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsGLInitialized)));
_isGlInitialized = value;
}
}

public event PropertyChangedEventHandler? PropertyChanged;

private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
LoadedSuccessfully = false;

_nativeOpenGlWrapper = GetOrCreateNativeOpenGlWrapper(XamlRoot!, _getWindowFunc);

if (_nativeOpenGlWrapper is null)
{
IsGLInitialized = false;
return;
}

Expand All @@ -260,12 +272,12 @@ private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
fe.Unloaded += OnClosed;
}

LoadedSuccessfully = true;
IsGLInitialized = true;
}

private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
{
LoadedSuccessfully = null;
IsGLInitialized = null;
if (_nativeOpenGlWrapper is null)
{
return;
Expand Down

0 comments on commit d982d10

Please sign in to comment.