Skip to content

Commit

Permalink
Implement MacOSProperties.IsTemplateIcon attached property on TrayIcon (
Browse files Browse the repository at this point in the history
#14348)

* Implement MacOS.IsTemplateIcon attached property on TrayIcon

* Use MacOS.IsTemplateIcon in the ControlCatalog

* Rename MacOS to MacOSProperties

* Extract IsTemplateIcon to ITrayIconWithIsTemplateImpl
  • Loading branch information
maxkatz6 authored and grokys committed Jun 12, 2024
1 parent 75aea9d commit 6471650
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 5 deletions.
7 changes: 5 additions & 2 deletions native/Avalonia.Native/src/OSX/trayicon.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class AvnTrayIcon : public ComSingleObject<IAvnTrayIcon, &IID_IAvnTrayIcon>
{
private:
NSStatusItem* _native;

bool _isTemplateIcon;

public:
FORWARD_IUNKNOWN()

Expand All @@ -28,8 +29,10 @@ class AvnTrayIcon : public ComSingleObject<IAvnTrayIcon, &IID_IAvnTrayIcon>
virtual HRESULT SetMenu (IAvnMenu* menu) override;

virtual HRESULT SetIsVisible (bool isVisible) override;

virtual HRESULT SetToolTipText (char* text) override;

virtual HRESULT SetIsTemplateIcon (bool isTemplateIcon) override;
};

#endif /* trayicon_h */
22 changes: 22 additions & 0 deletions native/Avalonia.Native/src/OSX/trayicon.mm
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
size.width = originalSize.width * scaleFactor;

[image setSize: size];
[image setTemplate: _isTemplateIcon];
[_native setImage:image];
}
else
Expand Down Expand Up @@ -98,3 +99,24 @@

return S_OK;
}

HRESULT AvnTrayIcon::SetIsTemplateIcon(bool isTemplateIcon)
{
START_COM_CALL;

@autoreleasepool
{
if (_isTemplateIcon != isTemplateIcon)
{
_isTemplateIcon = isTemplateIcon;

NSImage *image = [_native image];
if (image)
{
[image setTemplate: _isTemplateIcon];
}
}
}

return S_OK;
}
2 changes: 1 addition & 1 deletion samples/ControlCatalog/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
</Application.Styles>
<TrayIcon.Icons>
<TrayIcons>
<TrayIcon Icon="/Assets/test_icon.ico" ToolTipText="Avalonia Tray Icon ToolTip">
<TrayIcon Icon="/Assets/test_icon.ico" MacOSProperties.IsTemplateIcon="true" ToolTipText="Avalonia Tray Icon ToolTip">
<TrayIcon.Menu>
<NativeMenu>
<NativeMenuItem Header="Settings">
Expand Down
9 changes: 9 additions & 0 deletions src/Avalonia.Controls/Platform/ITrayIconImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ public interface ITrayIconImpl : IDisposable
/// </summary>
Action? OnClicked { get; set; }
}

[Unstable]
public interface ITrayIconWithIsTemplateImpl : ITrayIconImpl
{
/// <summary>
/// Sets if the tray icon has a template/monochrome icon or not.
/// </summary>
void SetIsTemplateIcon(bool isTemplateIcon);
}
}
35 changes: 35 additions & 0 deletions src/Avalonia.Controls/Platform/MacOSProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Avalonia.Platform;

namespace Avalonia.Controls;

/// <summary>
/// Set of MacOS specific attached properties that allow deeper customization of the application per platform.
/// </summary>
public class MacOSProperties
{
static MacOSProperties()
{
IsTemplateIconProperty.Changed.AddClassHandler<TrayIcon>(TrayIconIsTemplateIconChanged);
}

/// <summary>
/// Defines the IsTemplateIcon attached property.
/// </summary>
public static readonly AttachedProperty<bool> IsTemplateIconProperty =
AvaloniaProperty.RegisterAttached<MacOSProperties, TrayIcon, bool>("IsTemplateIcon");

/// <summary>
/// A Boolean value that determines whether the TrayIcon image represents a template image.
/// </summary>
public static void SetIsTemplateIcon(TrayIcon obj, bool value) => obj.SetValue(IsTemplateIconProperty, value);

/// <summary>
/// Returns a Boolean value that indicates whether the TrayIcon image is a template image.
/// </summary>
public static bool GetIsTemplateIcon(TrayIcon obj) => obj.GetValue(IsTemplateIconProperty);

private static void TrayIconIsTemplateIconChanged(TrayIcon trayIcon, AvaloniaPropertyChangedEventArgs args)
{
(trayIcon.Impl as ITrayIconWithIsTemplateImpl)?.SetIsTemplateIcon(args.GetNewValue<bool>());
}
}
2 changes: 2 additions & 0 deletions src/Avalonia.Controls/TrayIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ public bool IsVisible

public INativeMenuExporter? NativeMenuExporter => _impl?.MenuExporter;

internal ITrayIconImpl? Impl => _impl;

private static void Lifetime_Exit(object? sender, ControlledApplicationLifetimeExitEventArgs e)
{
var app = Application.Current ?? throw new InvalidOperationException("Application not yet initialized.");
Expand Down
7 changes: 6 additions & 1 deletion src/Avalonia.Native/TrayIconImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Avalonia.Native
{
internal class TrayIconImpl : ITrayIconImpl
internal class TrayIconImpl : ITrayIconWithIsTemplateImpl
{
private readonly IAvnTrayIcon _native;

Expand Down Expand Up @@ -58,6 +58,11 @@ public void SetIsVisible(bool visible)
_native.SetIsVisible(visible.AsComBool());
}

public void SetIsTemplateIcon(bool isTemplateIcon)
{
_native.SetIsTemplateIcon(isTemplateIcon.AsComBool());
}

public INativeMenuExporter? MenuExporter { get; }
}
}
1 change: 1 addition & 0 deletions src/Avalonia.Native/avn.idl
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,7 @@ interface IAvnTrayIcon : IUnknown
HRESULT SetMenu(IAvnMenu* menu);
HRESULT SetIsVisible(bool isVisible);
HRESULT SetToolTipText(char* text);
HRESULT SetIsTemplateIcon(bool text);
}

[uuid(a7724dc1-cf6b-4fa8-9d23-228bf2593edc)]
Expand Down
2 changes: 1 addition & 1 deletion src/Windows/Avalonia.Win32/TrayIconImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void SetIsVisible(bool visible)
{
UpdateIcon(!visible);
}

/// <inheritdoc />
public void SetToolTipText(string? text)
{
Expand Down

0 comments on commit 6471650

Please sign in to comment.