-
-
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 pull request #6560 from AvaloniaUI/feature/tray-icon-support
Feature/tray icon support # Conflicts: # src/Avalonia.Controls/ApiCompatBaseline.txt
- Loading branch information
1 parent
a088549
commit 081c89c
Showing
36 changed files
with
1,440 additions
and
51 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,33 @@ | ||
// | ||
// 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; | ||
|
||
public: | ||
FORWARD_IUNKNOWN() | ||
|
||
AvnTrayIcon(); | ||
|
||
~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,85 @@ | ||
#include "common.h" | ||
#include "trayicon.h" | ||
#include "menu.h" | ||
|
||
extern IAvnTrayIcon* CreateTrayIcon() | ||
{ | ||
@autoreleasepool | ||
{ | ||
return new AvnTrayIcon(); | ||
} | ||
} | ||
|
||
AvnTrayIcon::AvnTrayIcon() | ||
{ | ||
_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
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
Oops, something went wrong.