Skip to content

Commit 6471650

Browse files
maxkatz6grokys
authored andcommitted
Implement MacOSProperties.IsTemplateIcon attached property on TrayIcon (#14348)
* Implement MacOS.IsTemplateIcon attached property on TrayIcon * Use MacOS.IsTemplateIcon in the ControlCatalog * Rename MacOS to MacOSProperties * Extract IsTemplateIcon to ITrayIconWithIsTemplateImpl
1 parent 75aea9d commit 6471650

File tree

9 files changed

+82
-5
lines changed

9 files changed

+82
-5
lines changed

native/Avalonia.Native/src/OSX/trayicon.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class AvnTrayIcon : public ComSingleObject<IAvnTrayIcon, &IID_IAvnTrayIcon>
1515
{
1616
private:
1717
NSStatusItem* _native;
18-
18+
bool _isTemplateIcon;
19+
1920
public:
2021
FORWARD_IUNKNOWN()
2122

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

3031
virtual HRESULT SetIsVisible (bool isVisible) override;
31-
32+
3233
virtual HRESULT SetToolTipText (char* text) override;
34+
35+
virtual HRESULT SetIsTemplateIcon (bool isTemplateIcon) override;
3336
};
3437

3538
#endif /* trayicon_h */

native/Avalonia.Native/src/OSX/trayicon.mm

+22
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
size.width = originalSize.width * scaleFactor;
4646

4747
[image setSize: size];
48+
[image setTemplate: _isTemplateIcon];
4849
[_native setImage:image];
4950
}
5051
else
@@ -98,3 +99,24 @@
9899

99100
return S_OK;
100101
}
102+
103+
HRESULT AvnTrayIcon::SetIsTemplateIcon(bool isTemplateIcon)
104+
{
105+
START_COM_CALL;
106+
107+
@autoreleasepool
108+
{
109+
if (_isTemplateIcon != isTemplateIcon)
110+
{
111+
_isTemplateIcon = isTemplateIcon;
112+
113+
NSImage *image = [_native image];
114+
if (image)
115+
{
116+
[image setTemplate: _isTemplateIcon];
117+
}
118+
}
119+
}
120+
121+
return S_OK;
122+
}

samples/ControlCatalog/App.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
</Application.Styles>
6565
<TrayIcon.Icons>
6666
<TrayIcons>
67-
<TrayIcon Icon="/Assets/test_icon.ico" ToolTipText="Avalonia Tray Icon ToolTip">
67+
<TrayIcon Icon="/Assets/test_icon.ico" MacOSProperties.IsTemplateIcon="true" ToolTipText="Avalonia Tray Icon ToolTip">
6868
<TrayIcon.Menu>
6969
<NativeMenu>
7070
<NativeMenuItem Header="Settings">

src/Avalonia.Controls/Platform/ITrayIconImpl.cs

+9
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,13 @@ public interface ITrayIconImpl : IDisposable
3232
/// </summary>
3333
Action? OnClicked { get; set; }
3434
}
35+
36+
[Unstable]
37+
public interface ITrayIconWithIsTemplateImpl : ITrayIconImpl
38+
{
39+
/// <summary>
40+
/// Sets if the tray icon has a template/monochrome icon or not.
41+
/// </summary>
42+
void SetIsTemplateIcon(bool isTemplateIcon);
43+
}
3544
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Avalonia.Platform;
2+
3+
namespace Avalonia.Controls;
4+
5+
/// <summary>
6+
/// Set of MacOS specific attached properties that allow deeper customization of the application per platform.
7+
/// </summary>
8+
public class MacOSProperties
9+
{
10+
static MacOSProperties()
11+
{
12+
IsTemplateIconProperty.Changed.AddClassHandler<TrayIcon>(TrayIconIsTemplateIconChanged);
13+
}
14+
15+
/// <summary>
16+
/// Defines the IsTemplateIcon attached property.
17+
/// </summary>
18+
public static readonly AttachedProperty<bool> IsTemplateIconProperty =
19+
AvaloniaProperty.RegisterAttached<MacOSProperties, TrayIcon, bool>("IsTemplateIcon");
20+
21+
/// <summary>
22+
/// A Boolean value that determines whether the TrayIcon image represents a template image.
23+
/// </summary>
24+
public static void SetIsTemplateIcon(TrayIcon obj, bool value) => obj.SetValue(IsTemplateIconProperty, value);
25+
26+
/// <summary>
27+
/// Returns a Boolean value that indicates whether the TrayIcon image is a template image.
28+
/// </summary>
29+
public static bool GetIsTemplateIcon(TrayIcon obj) => obj.GetValue(IsTemplateIconProperty);
30+
31+
private static void TrayIconIsTemplateIconChanged(TrayIcon trayIcon, AvaloniaPropertyChangedEventArgs args)
32+
{
33+
(trayIcon.Impl as ITrayIconWithIsTemplateImpl)?.SetIsTemplateIcon(args.GetNewValue<bool>());
34+
}
35+
}

src/Avalonia.Controls/TrayIcon.cs

+2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ public bool IsVisible
182182

183183
public INativeMenuExporter? NativeMenuExporter => _impl?.MenuExporter;
184184

185+
internal ITrayIconImpl? Impl => _impl;
186+
185187
private static void Lifetime_Exit(object? sender, ControlledApplicationLifetimeExitEventArgs e)
186188
{
187189
var app = Application.Current ?? throw new InvalidOperationException("Application not yet initialized.");

src/Avalonia.Native/TrayIconImpl.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Avalonia.Native
1010
{
11-
internal class TrayIconImpl : ITrayIconImpl
11+
internal class TrayIconImpl : ITrayIconWithIsTemplateImpl
1212
{
1313
private readonly IAvnTrayIcon _native;
1414

@@ -58,6 +58,11 @@ public void SetIsVisible(bool visible)
5858
_native.SetIsVisible(visible.AsComBool());
5959
}
6060

61+
public void SetIsTemplateIcon(bool isTemplateIcon)
62+
{
63+
_native.SetIsTemplateIcon(isTemplateIcon.AsComBool());
64+
}
65+
6166
public INativeMenuExporter? MenuExporter { get; }
6267
}
6368
}

src/Avalonia.Native/avn.idl

+1
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@ interface IAvnTrayIcon : IUnknown
999999
HRESULT SetMenu(IAvnMenu* menu);
10001000
HRESULT SetIsVisible(bool isVisible);
10011001
HRESULT SetToolTipText(char* text);
1002+
HRESULT SetIsTemplateIcon(bool text);
10021003
}
10031004

10041005
[uuid(a7724dc1-cf6b-4fa8-9d23-228bf2593edc)]

src/Windows/Avalonia.Win32/TrayIconImpl.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void SetIsVisible(bool visible)
7979
{
8080
UpdateIcon(!visible);
8181
}
82-
82+
8383
/// <inheritdoc />
8484
public void SetToolTipText(string? text)
8585
{

0 commit comments

Comments
 (0)