-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/tray-icon-support' into tmp-master
# Conflicts: # src/Avalonia.Controls/ApiCompatBaseline.txt
- Loading branch information
1 parent
a7d5c9a
commit 7eaf18c
Showing
31 changed files
with
1,062 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// trayicon.h | ||
// Avalonia.Native.OSX | ||
// | ||
// Created by Dan Walmsley on 09/09/2021. | ||
// Copyright © 2021 Avalonia. All rights reserved. | ||
// | ||
|
||
#ifndef trayicon_h | ||
#define trayicon_h | ||
|
||
#include "common.h" | ||
|
||
class AvnTrayIcon : public ComSingleObject<IAvnTrayIcon, &IID_IAvnTrayIcon> | ||
{ | ||
private: | ||
NSStatusItem* _native; | ||
ComPtr<IAvnTrayIconEvents> _events; | ||
|
||
public: | ||
FORWARD_IUNKNOWN() | ||
|
||
AvnTrayIcon(IAvnTrayIconEvents* events); | ||
|
||
~AvnTrayIcon (); | ||
|
||
virtual HRESULT SetIcon (void* data, size_t length) override; | ||
|
||
virtual HRESULT SetMenu (IAvnMenu* menu) override; | ||
|
||
virtual HRESULT SetIsVisible (bool isVisible) override; | ||
}; | ||
|
||
#endif /* trayicon_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include "common.h" | ||
#include "trayicon.h" | ||
#include "menu.h" | ||
|
||
extern IAvnTrayIcon* CreateTrayIcon(IAvnTrayIconEvents* cb) | ||
{ | ||
@autoreleasepool | ||
{ | ||
return new AvnTrayIcon(cb); | ||
} | ||
} | ||
|
||
AvnTrayIcon::AvnTrayIcon(IAvnTrayIconEvents* events) | ||
{ | ||
_events = events; | ||
|
||
_native = [[NSStatusBar systemStatusBar] statusItemWithLength: NSSquareStatusItemLength]; | ||
|
||
} | ||
|
||
AvnTrayIcon::~AvnTrayIcon() | ||
{ | ||
if(_native != nullptr) | ||
{ | ||
[[_native statusBar] removeStatusItem:_native]; | ||
_native = nullptr; | ||
} | ||
} | ||
|
||
HRESULT AvnTrayIcon::SetIcon (void* data, size_t length) | ||
{ | ||
START_COM_CALL; | ||
|
||
@autoreleasepool | ||
{ | ||
if(data != nullptr) | ||
{ | ||
NSData *imageData = [NSData dataWithBytes:data length:length]; | ||
NSImage *image = [[NSImage alloc] initWithData:imageData]; | ||
|
||
NSSize originalSize = [image size]; | ||
|
||
NSSize size; | ||
size.height = [[NSFont menuFontOfSize:0] pointSize] * 1.333333; | ||
|
||
auto scaleFactor = size.height / originalSize.height; | ||
size.width = originalSize.width * scaleFactor; | ||
|
||
[image setSize: size]; | ||
[_native setImage:image]; | ||
} | ||
else | ||
{ | ||
[_native setImage:nullptr]; | ||
} | ||
return S_OK; | ||
} | ||
} | ||
|
||
HRESULT AvnTrayIcon::SetMenu (IAvnMenu* menu) | ||
{ | ||
START_COM_CALL; | ||
|
||
@autoreleasepool | ||
{ | ||
auto appMenu = dynamic_cast<AvnAppMenu*>(menu); | ||
|
||
if(appMenu != nullptr) | ||
{ | ||
[_native setMenu:appMenu->GetNative()]; | ||
} | ||
} | ||
|
||
return S_OK; | ||
} | ||
|
||
HRESULT AvnTrayIcon::SetIsVisible(bool isVisible) | ||
{ | ||
START_COM_CALL; | ||
|
||
@autoreleasepool | ||
{ | ||
[_native setVisible:isVisible]; | ||
} | ||
|
||
return S_OK; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Avalonia; | ||
using Avalonia.Controls.ApplicationLifetimes; | ||
using MiniMvvm; | ||
|
||
namespace ControlCatalog.ViewModels | ||
{ | ||
public class ApplicationViewModel : ViewModelBase | ||
{ | ||
public ApplicationViewModel() | ||
{ | ||
ExitCommand = MiniCommand.Create(() => | ||
{ | ||
if(Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime) | ||
{ | ||
lifetime.Shutdown(); | ||
} | ||
}); | ||
|
||
ToggleCommand = MiniCommand.Create(() => { }); | ||
} | ||
|
||
public MiniCommand ExitCommand { get; } | ||
|
||
public MiniCommand ToggleCommand { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 14 additions & 3 deletions
17
src/Avalonia.Controls/Platform/ITopLevelNativeMenuExporter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using Avalonia.Controls; | ||
using Avalonia.Controls.Platform; | ||
|
||
#nullable enable | ||
|
||
namespace Avalonia.Platform | ||
{ | ||
public interface ITrayIconImpl : IDisposable | ||
{ | ||
/// <summary> | ||
/// Sets the icon of this tray icon. | ||
/// </summary> | ||
void SetIcon(IWindowIconImpl? icon); | ||
|
||
/// <summary> | ||
/// Sets the icon of this tray icon. | ||
/// </summary> | ||
void SetToolTipText(string? text); | ||
|
||
/// <summary> | ||
/// Sets if the tray icon is visible or not. | ||
/// </summary> | ||
void SetIsVisible (bool visible); | ||
|
||
/// <summary> | ||
/// Gets the MenuExporter to allow native menus to be exported to the TrayIcon. | ||
/// </summary> | ||
INativeMenuExporter? MenuExporter { get; } | ||
|
||
/// <summary> | ||
/// Gets or Sets the Action that is called when the TrayIcon is clicked. | ||
/// </summary> | ||
Action? OnClicked { get; set; } | ||
} | ||
} |
Oops, something went wrong.