Browse Source

handle platforms that return null for the trayiconimpl.

pull/6560/head
Dan Walmsley 5 years ago
parent
commit
786375aff9
  1. 4
      src/Avalonia.Controls/Platform/IWindowingPlatform.cs
  2. 5
      src/Avalonia.Controls/Platform/PlatformManager.cs
  3. 23
      src/Avalonia.Controls/TrayIcon.cs
  4. 2
      src/Avalonia.DesignerSupport/Remote/PreviewerWindowingPlatform.cs
  5. 40
      src/Avalonia.DesignerSupport/Remote/TrayIconStub.cs
  6. 5
      src/Avalonia.Headless/AvaloniaHeadlessPlatform.cs
  7. 2
      src/iOS/Avalonia.iOS/Stubs.cs
  8. 2
      tests/Avalonia.Controls.UnitTests/WindowingPlatformMock.cs
  9. 2
      tests/Avalonia.UnitTests/MockWindowingPlatform.cs

4
src/Avalonia.Controls/Platform/IWindowingPlatform.cs

@ -1,3 +1,5 @@
#nullable enable
namespace Avalonia.Platform
{
public interface IWindowingPlatform
@ -6,6 +8,6 @@ namespace Avalonia.Platform
IWindowImpl CreateEmbeddableWindow();
ITrayIconImpl CreateTrayIcon();
ITrayIconImpl? CreateTrayIcon();
}
}

5
src/Avalonia.Controls/Platform/PlatformManager.cs

@ -1,8 +1,9 @@
using System;
using System.Reactive.Disposables;
using Avalonia.Media;
using Avalonia.Platform;
#nullable enable
namespace Avalonia.Controls.Platform
{
public static partial class PlatformManager
@ -22,7 +23,7 @@ namespace Avalonia.Controls.Platform
{
}
public static ITrayIconImpl CreateTrayIcon ()
public static ITrayIconImpl? CreateTrayIcon ()
{
var platform = AvaloniaLocator.Current.GetService<IWindowingPlatform>();

23
src/Avalonia.Controls/TrayIcon.cs

@ -16,15 +16,18 @@ namespace Avalonia.Controls
public class TrayIcon : AvaloniaObject, INativeMenuExporterProvider, IDisposable
{
private readonly ITrayIconImpl _impl;
private readonly ITrayIconImpl? _impl;
private TrayIcon(ITrayIconImpl impl)
private TrayIcon(ITrayIconImpl? impl)
{
_impl = impl;
if (impl != null)
{
_impl = impl;
_impl.SetIsVisible(IsVisible);
_impl.SetIsVisible(IsVisible);
_impl.OnClicked = () => Clicked?.Invoke(this, EventArgs.Empty);
_impl.OnClicked = () => Clicked?.Invoke(this, EventArgs.Empty);
}
}
public TrayIcon () : this(PlatformManager.CreateTrayIcon())
@ -160,25 +163,25 @@ namespace Avalonia.Controls
if(change.Property == IconProperty)
{
_impl.SetIcon(Icon.PlatformImpl);
_impl?.SetIcon(Icon.PlatformImpl);
}
else if (change.Property == IsVisibleProperty)
{
_impl.SetIsVisible(change.NewValue.GetValueOrDefault<bool>());
_impl?.SetIsVisible(change.NewValue.GetValueOrDefault<bool>());
}
else if (change.Property == ToolTipTextProperty)
{
_impl.SetToolTipText(change.NewValue.GetValueOrDefault<string?>());
_impl?.SetToolTipText(change.NewValue.GetValueOrDefault<string?>());
}
else if (change.Property == MenuProperty)
{
_impl.MenuExporter?.SetNativeMenu(change.NewValue.GetValueOrDefault<NativeMenu>());
_impl?.MenuExporter?.SetNativeMenu(change.NewValue.GetValueOrDefault<NativeMenu>());
}
}
/// <summary>
/// Disposes the tray icon (removing it from the tray area).
/// </summary>
public void Dispose() => _impl.Dispose();
public void Dispose() => _impl?.Dispose();
}
}

2
src/Avalonia.DesignerSupport/Remote/PreviewerWindowingPlatform.cs

@ -17,7 +17,7 @@ namespace Avalonia.DesignerSupport.Remote
private static PreviewerWindowImpl s_lastWindow;
public static List<object> PreFlightMessages = new List<object>();
public ITrayIconImpl CreateTrayIcon() => new TrayIconStub();
public ITrayIconImpl CreateTrayIcon() => null;
public IWindowImpl CreateWindow() => new WindowStub();

40
src/Avalonia.DesignerSupport/Remote/TrayIconStub.cs

@ -1,40 +0,0 @@
using System;
using Avalonia.Controls;
using Avalonia.Controls.Platform;
using Avalonia.Platform;
namespace Avalonia.DesignerSupport.Remote
{
class TrayIconStub : ITrayIconImpl
{
public Action Clicked { get; set; }
public Action DoubleClicked { get; set; }
public Action RightClicked { get; set; }
public INativeMenuExporter MenuExporter => null;
public Action OnClicked { get; set; }
public void Dispose()
{
throw new NotImplementedException();
}
public void SetIcon(IWindowIconImpl icon)
{
}
public void SetIsVisible(bool visible)
{
}
public void SetMenu(NativeMenu menu)
{
throw new NotImplementedException();
}
public void SetToolTipText(string text)
{
}
}
}

5
src/Avalonia.Headless/AvaloniaHeadlessPlatform.cs

@ -52,10 +52,7 @@ namespace Avalonia.Headless
public IPopupImpl CreatePopup() => new HeadlessWindowImpl(true);
public ITrayIconImpl CreateTrayIcon()
{
throw new NotImplementedException();
}
public ITrayIconImpl CreateTrayIcon() => return null;
}
internal static void Initialize()

2
src/iOS/Avalonia.iOS/Stubs.cs

@ -22,7 +22,7 @@ namespace Avalonia.iOS
public IWindowImpl CreateEmbeddableWindow() => throw new NotSupportedException();
public ITrayIconImpl CreateTrayIcon() => throw new NotSupportedException();
public ITrayIconImpl CreateTrayIcon() => null;
}
class PlatformIconLoaderStub : IPlatformIconLoader

2
tests/Avalonia.Controls.UnitTests/WindowingPlatformMock.cs

@ -27,7 +27,7 @@ namespace Avalonia.Controls.UnitTests
public ITrayIconImpl CreateTrayIcon()
{
throw new NotImplementedException();
return null;
}
public IPopupImpl CreatePopup() => _popupImpl?.Invoke() ?? Mock.Of<IPopupImpl>(x => x.RenderScaling == 1);

2
tests/Avalonia.UnitTests/MockWindowingPlatform.cs

@ -128,7 +128,7 @@ namespace Avalonia.UnitTests
public ITrayIconImpl CreateTrayIcon()
{
throw new NotImplementedException();
return null;
}
private static void SetupToplevel<T>(Mock<T> mock) where T : class, ITopLevelImpl

Loading…
Cancel
Save