Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

Commit

Permalink
Add More Tray Stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
drasticactions committed Jan 2, 2022
1 parent 39d1157 commit fce8325
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 48 deletions.
34 changes: 17 additions & 17 deletions DrasticMaui.Sample/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ public App()
{
this.InitializeComponent();

var menuItems = new List<DrasticTrayMenuItem>
{
new DrasticTrayMenuItem("Exit"),
new DrasticTrayMenuItem("Test"),
new DrasticTrayMenuItem("Test 2"),
};

var stream = MauiProgram.GetResourceFileContent("Icon.favicon.ico");
if (stream is null)
{
throw new Exception("Couldn't set up tray image");
}

this.icon = new DrasticTrayIcon("Maui", stream, menuItems);
this.icon.MenuClicked += this.TrayIcon_MenuClicked;
//var menuItems = new List<DrasticTrayMenuItem>
// {
// new DrasticTrayMenuItem("Exit"),
// new DrasticTrayMenuItem("Test"),
// new DrasticTrayMenuItem("Test 2"),
// };

//var stream = MauiProgram.GetResourceFileContent("Icon.favicon.ico");
//if (stream is null)
//{
// throw new Exception("Couldn't set up tray image");
//}

//this.icon = new DrasticTrayIcon("Maui", stream, menuItems);
//this.icon.MenuClicked += this.TrayIcon_MenuClicked;
}

private void TrayIcon_MenuClicked(object? sender, DrasticTrayMenuClickedEventArgs e)
Expand All @@ -50,6 +50,6 @@ private void TrayIcon_MenuClicked(object? sender, DrasticTrayMenuClickedEventArg

/// <inheritdoc/>
protected override Window CreateWindow(IActivationState? activationState)
//=> new DrasticMauiSampleWindow() { Page = new NavigationPage(new MainPage()) };
=> new DrasticMauiSampleTrayWindow(this.icon) { Page = new TraySample() };
=> new DrasticMauiSampleWindow() { Page = new NavigationPage(new MainPage()) };
//=> new DrasticMauiSampleTrayWindow(this.icon) { Page = new TraySample() };
}
20 changes: 20 additions & 0 deletions DrasticMaui.Sample/DrasticMauiSampleWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading.Tasks;
using DrasticMaui.Models;
using DrasticMaui.Overlays;
using DrasticMaui.Tools;
using Microsoft.Maui.Handlers;

namespace DrasticMaui.Sample
Expand Down Expand Up @@ -44,6 +45,25 @@ protected override void OnCreated()
this.AddOverlay(this.DragAndDropOverlay);
}

public async Task TestTrayIcon()
{
#if __MACCATALYST__
if (this.Handler is WindowHandler handler)
{
await handler.NativeView.SetFrameForUIWindow(new CoreGraphics.CGRect(0, 0, 100, 100));
}
#endif
}

public async Task EnableTrayApp()
{
if (this.TrayIcon is not null)
{
var trayWindow = new DrasticTrayWindow(this.TrayIcon) { Page = new TraySample() };
Application.Current?.OpenWindow(trayWindow);
}
}

public void SetupTrayIcon()
{
if (this.TrayIcon is not null)
Expand Down
2 changes: 2 additions & 0 deletions DrasticMaui.Sample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<VerticalStackLayout Spacing="10" VerticalOptions="Center">
<Button Text="Toggle Full Screen" Clicked="OnFullScreen"></Button>
<Button Text="Setup Tray Icon" Clicked="OnTrayIcon"></Button>
<Button Text="Try Get Status Icon Frame Location" Clicked="OnStatusIcon"></Button>
<Button Text="New Window" Clicked="OnNewWindow"></Button>
<Button Text="New Tray App" Clicked="OnNewTrayApp"></Button>
<Button Text="Add Page Overlay" Clicked="OnPageOverlay"></Button>
<Button Text="Navigate page" Clicked="OnPageNavigate"></Button>
</VerticalStackLayout>
Expand Down
10 changes: 10 additions & 0 deletions DrasticMaui.Sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ private void OnTrayIcon(object sender, EventArgs e)
this.window?.SetupTrayIcon();
}

private async void OnStatusIcon(object sender, EventArgs e)
{
await this.window?.TestTrayIcon();
}

private void OnNewTrayApp(object sender, EventArgs e)
{
this.window?.EnableTrayApp();
}

private void OnCounterClicked(object sender, EventArgs e)
{
}
Expand Down
10 changes: 9 additions & 1 deletion DrasticMaui.Sample/TraySample.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
x:Class="DrasticMaui.Sample.TraySample"
Title="TraySample">
<ScrollView>
<Grid RowSpacing="25" RowDefinitions="Auto,Auto,Auto,Auto,*"
<Grid RowSpacing="25" RowDefinitions="Auto,Auto,Auto,Auto,*,Auto"
Padding="{OnPlatform iOS='30,60,30,30', Default='30'}">

<Label
Expand Down Expand Up @@ -38,6 +38,14 @@
Clicked="OnCounterClicked"
HorizontalOptions="Center" />

<Button
Text="Reset View"
FontAttributes="Bold"
Grid.Row="5"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnReset"
HorizontalOptions="Center" />

<Image Grid.Row="4"
Source="dotnet_bot.png"
SemanticProperties.Description="Cute dot net bot waving hi to you!"
Expand Down
8 changes: 8 additions & 0 deletions DrasticMaui.Sample/TraySample.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public TraySample()
InitializeComponent();
}

private void OnReset(object sender, EventArgs e)
{
if (this.GetParentWindow() is DrasticTrayWindow win)
{
win.Setup();
}
}

private void OnCounterClicked(object sender, EventArgs e)
{
count++;
Expand Down
8 changes: 6 additions & 2 deletions DrasticMaui/DrasticTrayWindow.MacCatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ private async void SetupWindow()

this.nsWindow = await this.uiWindow.GetNSWindowFromUIWindow();



await this.uiWindow.ToggleTitleBarButtons(true);

if (this.uiWindow.RootViewController is null)
Expand All @@ -60,6 +58,12 @@ private async void SetupWindow()
protected override void OnHandlerChanged()
{
base.OnHandlerChanged();
//this.SetupWindow();
//this.SetupTrayIcon();
}

public void Setup()
{
this.SetupWindow();
this.SetupTrayIcon();
}
Expand Down
4 changes: 4 additions & 0 deletions DrasticMaui/DrasticTrayWindow.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ private void Window_VisibilityChanged(object sender, Microsoft.UI.Xaml.WindowVis
}
}

public void Setup()
{
}

private void ShowWindow()
{
if (this.appWindow is null)
Expand Down
4 changes: 4 additions & 0 deletions DrasticMaui/DrasticTrayWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ private void ShowWindow()
private void HideWindow()
{
}

public void Setup()
{
}
#endif
}
}
47 changes: 45 additions & 2 deletions DrasticMaui/Tools/PlatformExtensions.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ public static CATransform3D TransformToAncestor(this CALayer fromLayer, CALayer
}

#if __MACCATALYST__

public static NSObject? GetNSStatusBar()
=> Runtime.GetNSObject(Class.GetHandle("NSStatusBar"));

/// <summary>
/// Get NSWindow from UIWindow.
/// </summary>
Expand Down Expand Up @@ -323,9 +327,38 @@ public static async Task SetFrameForUIWindow(this UIWindow window, CGRect rect)
return;
}

var newRect = NSValue.FromCGRect(rect);
var testFrame = window.GetFrame();

var attachedWindow = nsWindow.ValueForKey(new NSString("attachedWindow"));

if (attachedWindow is null)
{
return;
}

var windowFrame = (NSValue)attachedWindow.ValueForKey(new NSString("frame"));

var originalOne = windowFrame.CGRectValue;

var newRect = NSValue.FromCGRect(new CGRect(originalOne.X, originalOne.Y, originalOne.Width, originalOne.Height));

// var point = NSValue.FromCGPoint(new CGPoint(rect.X, rect.Y));

//void_objc_msgSend_IntPtr_bool(attachedWindow.Handle, Selector.GetHandle("setFrameTopLeftPoint:"), point.Handle, false);

//attachedWindow.PerformSelector(new Selector("setFrame:display:"), newRect, NSNumber.FromBoolean(false));

void_objc_msgSend_IntPtr_bool(nsWindow.Handle, Selector.GetHandle("setFrame:display:"), newRect.Handle, false);
//void_objc_msgSend_IntPtr_bool(attachedWindow.Handle, Selector.GetHandle("setFrame:display:"), windowFrame.Handle, true);

//void_objc_msgSend_IntPtr(attachedWindow.Handle, Selector.GetHandle("setFrameOrigin:"), point.Handle);

windowFrame = (NSValue)attachedWindow.ValueForKey(new NSString("frame"));

// void_objc_msgSend_IntPtr(attachedWindow.Handle, Selector.GetHandle("setFrameOrigin:"), point.Handle);

// void_objc_msgSend_IntPtr(nsWindow.Handle, Selector.GetHandle("makeKeyAndOrderFront:"), nsWindow.Handle);

//NSApplicationActivateIgnoringOtherApps();
}

public static async Task ToggleTitleBarButtons(this UIWindow window, bool hideButtons)
Expand Down Expand Up @@ -408,12 +441,22 @@ public static void NSApplicationActivateIgnoringOtherApps(bool ignoreSetting = t
[DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
internal static extern void void_objc_msgSend_IntPtr(IntPtr receiver, IntPtr selector, IntPtr arg1);

[DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
internal static extern void void_objc_msgSend_bool_IntPtr(IntPtr receiver, IntPtr selector, bool arg1, IntPtr arg2);

[DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
internal static extern void void_objc_msgSend_IntPtr_IntPtr(IntPtr receiver, IntPtr selector, IntPtr arg1, IntPtr arg2);

[DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
internal static extern void void_objc_msgSend_IntPtr_bool(IntPtr receiver, IntPtr selector, IntPtr arg1, bool arg2);

[DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
internal static extern void void_objc_msgSend_IntPtr_bool_bool(IntPtr receiver, IntPtr selector, IntPtr arg1, bool arg2, bool arg3);

[DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
internal static extern void void_objc_msgSend_bool(IntPtr receiver, IntPtr selector, bool arg1);


[DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
internal static extern void void_objc_msgSend_ulong(IntPtr receiver, IntPtr selector, ulong arg1);
#endif
Expand Down
74 changes: 50 additions & 24 deletions DrasticMaui/Tray/DrasticTrayIcon.MacCatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,63 @@ namespace DrasticMaui
{
public partial class DrasticTrayIcon : NSObject
{
private bool setupFrames;
private AppKit.NSImage? statusBarImage;
private NSObject? systemStatusBarObj;
private NSObject? statusBarObj;
private NSObject? statusBarItem;
private NSObject? statusBarButton;

public CGRect GetFrame()
{
if (this.statusBarButton is null)
if (this.statusBarItem is null)
{
return new CGRect(0, 0, 0, 0);
}

var statusBarButton = Runtime.GetNSObject(PlatformExtensions.IntPtr_objc_msgSend(this.statusBarItem.Handle, Selector.GetHandle("button")));
if (statusBarButton is null)
{
return new CGRect(0, 0, 1, 1);
return new CGRect(0, 0, 0, 0);
}

var buttonValue = Runtime.GetNSObject<NSValue>(PlatformExtensions.IntPtr_objc_msgSend(this.statusBarButton.Handle, Selector.GetHandle("frame")));
if (buttonValue is null)
var nsButtonWindow = Runtime.GetNSObject(PlatformExtensions.IntPtr_objc_msgSend(statusBarButton.Handle, Selector.GetHandle("window")));
if (nsButtonWindow is null)
{
return new CGRect(0, 0, 1, 1);
return new CGRect(0, 0, 0, 0);
}

//var cgRectWindowFrame = Runtime.GetNSObject(PlatformExtensions.IntPtr_objc_msgSend(buttonWindow.Handle, Selector.GetHandle("frame")));
//if (cgRectWindowFrame is null)
//{
// return new CGRect(0, 0, 1, 1);
//}
var windowFrame = (NSValue)nsButtonWindow.ValueForKey(new NSString("frame"));

return new CGRect(0, 0, 1, 1);
return windowFrame.CGRectValue;
}

private void SetupStatusBarButton()
{
this.statusBarObj = Runtime.GetNSObject(Class.GetHandle("NSStatusBar"));
if (this.statusBarObj is null)
var statusBarObj = PlatformExtensions.GetNSStatusBar();
if (statusBarObj is null)
{
return;
}

this.systemStatusBarObj = this.statusBarObj.PerformSelector(new Selector("systemStatusBar"));
this.statusBarItem = Runtime.GetNSObject(PlatformExtensions.IntPtr_objc_msgSend_nfloat(this.systemStatusBarObj.Handle, Selector.GetHandle("statusItemWithLength:"), -1));
var systemStatusBarObj = statusBarObj.PerformSelector(new Selector("systemStatusBar"));
this.statusBarItem = Runtime.GetNSObject(PlatformExtensions.IntPtr_objc_msgSend_nfloat(systemStatusBarObj.Handle, Selector.GetHandle("statusItemWithLength:"), -1));
if (this.statusBarItem is null)
{
return;
}

this.statusBarButton = Runtime.GetNSObject(PlatformExtensions.IntPtr_objc_msgSend(this.statusBarItem.Handle, Selector.GetHandle("button")));
var statusBarButton = Runtime.GetNSObject(PlatformExtensions.IntPtr_objc_msgSend(this.statusBarItem.Handle, Selector.GetHandle("button")));

if (this.statusBarButton is not null && this.statusBarImage is not null)
if (statusBarButton is not null && statusBarImage is not null)
{
PlatformExtensions.void_objc_msgSend_IntPtr(this.statusBarButton.Handle, Selector.GetHandle("setImage:"), this.statusBarImage.Handle);
PlatformExtensions.void_objc_msgSend_IntPtr(statusBarButton.Handle, Selector.GetHandle("setImage:"), statusBarImage.Handle);
PlatformExtensions.void_objc_msgSend_bool(this.statusBarImage.Handle, Selector.GetHandle("setTemplate:"), true);
this.statusBarImage.Size = new CoreGraphics.CGSize(32, 32);
}

if (this.statusBarButton is not null)
if (statusBarButton is not null)
{
// Handle click
PlatformExtensions.void_objc_msgSend_IntPtr(this.statusBarButton.Handle, Selector.GetHandle("setTarget:"), this.Handle);
PlatformExtensions.void_objc_msgSend_IntPtr(this.statusBarButton.Handle, Selector.GetHandle("setAction:"), new Selector("handleButtonClick:").Handle);
PlatformExtensions.void_objc_msgSend_IntPtr(statusBarButton.Handle, Selector.GetHandle("setTarget:"), this.Handle);
PlatformExtensions.void_objc_msgSend_IntPtr(statusBarButton.Handle, Selector.GetHandle("setAction:"), new Selector("handleButtonClick:").Handle);
}
}

Expand All @@ -87,6 +87,32 @@ private void SetupStatusBarImage()
PlatformExtensions.IntPtr_objc_msgSend_IntPtr(this.statusBarImage.Handle, Selector.GetHandle("initWithData:"), imageStream.Handle);
}

private void SetupButtonFrames()
{
if (this.statusBarItem is null)
{
return;
}

var statusBarButton = Runtime.GetNSObject(PlatformExtensions.IntPtr_objc_msgSend(this.statusBarItem.Handle, Selector.GetHandle("button")));
if (statusBarButton is null)
{
return;
}

var nsButtonWindow = Runtime.GetNSObject(PlatformExtensions.IntPtr_objc_msgSend(statusBarButton.Handle, Selector.GetHandle("window")));
if (nsButtonWindow is null)
{
return;
}

var windowFrame = (NSValue)nsButtonWindow.ValueForKey(new NSString("frame"));
var buttonFrame = (NSValue)statusBarButton.ValueForKey(new NSString("frame"));

var what = windowFrame.CGRectValue;
//this.setupFrames = true;
}

[Export("handleButtonClick:")]
private void HandleClick(NSObject senderStatusBarButton)
{
Expand Down
4 changes: 2 additions & 2 deletions DrasticMaui/Tray/DrasticTrayUIViewController.MacCatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public async void ToggleVisibility()
return;
}

//var frame = this.trayIcon.GetFrame();
//await this.window.SetFrameForUIWindow(new CGRect(500, 500, 1000, 1000));
var buttonBounds = this.trayIcon.GetFrame();
await this.window.SetFrameForUIWindow(buttonBounds);
var viewController = this.contentController;

if (viewController.PresentingViewController is not null)
Expand Down

0 comments on commit fce8325

Please sign in to comment.