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

Commit

Permalink
Revamp Tray Icons
Browse files Browse the repository at this point in the history
  • Loading branch information
drasticactions committed Jan 1, 2022
1 parent bae5c3f commit ba943d3
Show file tree
Hide file tree
Showing 18 changed files with 404 additions and 454 deletions.
45 changes: 27 additions & 18 deletions DrasticMaui.Sample/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,52 @@

namespace DrasticMaui.Sample;

using DrasticMaui.Tray;
using DrasticMaui.Events;

/// <summary>
/// App.
/// </summary>
public partial class App : Application
{
private TrayService tray;
private DrasticTrayIcon icon;

/// <summary>
/// Initializes a new instance of the <see cref="App"/> class.
/// </summary>
public App()
{
this.InitializeComponent();
this.HandlerChanged += App_HandlerChanged;
}

private void App_HandlerChanged(object? sender, EventArgs e)
{
//var handler = this.Handler.MauiContext;
//var stream = MauiProgram.GetResourceFileContent("Icon.favicon.ico");
//if (stream is not null && handler is not null)
//{
// this.tray = new TrayService("DrasticMaui", stream, handler);
// this.tray.SetupPage(new TraySample());
// this.Dispatcher.Dispatch(this.tray.SetupTrayIcon);
//}
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;
}

/// <inheritdoc/>
protected override void OnStart()
private void TrayIcon_MenuClicked(object? sender, DrasticTrayMenuClickedEventArgs e)
{
base.OnStart();
if (e.MenuItem.Text == "Exit")
{
#if WINDOWS
Microsoft.UI.Xaml.Application.Current.Exit();
return;
#endif
}
}

/// <inheritdoc/>
protected override Window CreateWindow(IActivationState? activationState)
=> new DrasticMauiSampleWindow() { Page = new NavigationPage(new MainPage()) };
// => new DrasticMauiSampleWindow() { Page = new NavigationPage(new MainPage()) };
=> new DrasticMauiSampleTrayWindow(this.icon) { Page = new TraySample() };
}
21 changes: 21 additions & 0 deletions DrasticMaui.Sample/DrasticMauiSampleTrayWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// <copyright file="DrasticMauiSampleTrayWindow.cs" company="Drastic Actions">
// Copyright (c) Drastic Actions. All rights reserved.
// </copyright>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DrasticMaui.Options;

namespace DrasticMaui.Sample
{
public class DrasticMauiSampleTrayWindow : DrasticTrayWindow
{
public DrasticMauiSampleTrayWindow(DrasticTrayIcon icon, DrasticTrayWindowOptions? options = null)
: base(icon, options)
{
}
}
}
27 changes: 17 additions & 10 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 Microsoft.Maui.Handlers;

namespace DrasticMaui.Sample
{
Expand Down Expand Up @@ -50,27 +51,33 @@ public void SetupTrayIcon()
return;
}

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

this.TrayIcon = new DrasticTrayIcon("Maui", stream);
this.TrayIcon.Clicked += this.TrayIcon_Clicked;
var menuItems = new List<DrasticTrayMenuItem>
{
new DrasticTrayMenuItem("Exit"),
new DrasticTrayMenuItem("Test"),
new DrasticTrayMenuItem("Test 2")
};

this.TrayIcon = new DrasticTrayIcon("Maui", stream, menuItems);
this.TrayIcon.LeftClicked += this.TrayIcon_Clicked;
this.TrayIcon.MenuClicked += TrayIcon_MenuClicked;
}

public void MoveWindowToTray()
private void TrayIcon_MenuClicked(object? sender, Events.DrasticTrayMenuClickedEventArgs e)
{
#if __MACCATALYST__
var uiWindow = this.Handler.NativeView as UIKit.UIWindow;
if (uiWindow is null)
if (e.MenuItem.Text == "Exit")
{
#if WINDOWS
Microsoft.UI.Xaml.Application.Current.Exit();
return;
}

this.TrayIcon?.MoveUIWindowToTray(uiWindow);
#endif
}
}

private void TrayIcon_Clicked(object? sender, EventArgs e)
Expand Down
1 change: 0 additions & 1 deletion DrasticMaui.Sample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<VerticalStackLayout Spacing="10" VerticalOptions="Center">
<Button Text="Toggle Full Screen" Clicked="OnFullScreen"></Button>
<Button Text="Setup Tray Icon" Clicked="OnTrayIcon"></Button>
<Button Text="Move Window to Tray" Clicked="OnTrayWindow"></Button>
<Button Text="New Window" Clicked="OnNewWindow"></Button>
<Button Text="Add Page Overlay" Clicked="OnPageOverlay"></Button>
<Button Text="Navigate page" Clicked="OnPageNavigate"></Button>
Expand Down
5 changes: 0 additions & 5 deletions DrasticMaui.Sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ private void OnTrayIcon(object sender, EventArgs e)
this.window?.SetupTrayIcon();
}

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

private void OnCounterClicked(object sender, EventArgs e)
{
}
Expand Down
86 changes: 42 additions & 44 deletions DrasticMaui.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
// <copyright file="MauiProgram.cs" company="Drastic Actions">
// Copyright (c) Drastic Actions. All rights reserved.
// </copyright>

using DrasticMaui.Controls;
using DrasticMaui.Tray;
using System.Reflection;

namespace DrasticMaui.Sample;

/// <summary>
/// Maui Program.
/// </summary>
public static class MauiProgram
{
/// <summary>
/// Create Maui App.
/// </summary>
/// <returns>MauiApp.</returns>
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});

return builder.Build();
}

public static Stream? GetResourceFileContent(string fileName)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "DrasticMaui.Sample." + fileName;
if (assembly is null)
{
return null;
}

return assembly.GetManifestResourceStream(resourceName);
}
}
// <copyright file="MauiProgram.cs" company="Drastic Actions">
// Copyright (c) Drastic Actions. All rights reserved.
// </copyright>

using System.Reflection;

namespace DrasticMaui.Sample;

/// <summary>
/// Maui Program.
/// </summary>
public static class MauiProgram
{
/// <summary>
/// Create Maui App.
/// </summary>
/// <returns>MauiApp.</returns>
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});

return builder.Build();
}

public static Stream? GetResourceFileContent(string fileName)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "DrasticMaui.Sample." + fileName;
if (assembly is null)
{
return null;
}

return assembly.GetManifestResourceStream(resourceName);
}
}
49 changes: 44 additions & 5 deletions DrasticMaui.Sample/TraySample.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,48 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DrasticMaui.Sample.TraySample"
Title="TraySample">
<VerticalStackLayout>
<Label Text="Welcome to .NET MAUI!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</VerticalStackLayout>
<ScrollView>
<Grid RowSpacing="25" RowDefinitions="Auto,Auto,Auto,Auto,*"
Padding="{OnPlatform iOS='30,60,30,30', Default='30'}">

<Label
Text="Hello, World!"
Grid.Row="0"
SemanticProperties.HeadingLevel="Level1"
FontSize="32"
HorizontalOptions="Center" />

<Label
Text="Welcome to .NET Multi-platform App UI"
Grid.Row="1"
SemanticProperties.HeadingLevel="Level1"
SemanticProperties.Description="Welcome to dot net Multi platform App U I"
FontSize="18"
HorizontalOptions="Center" />

<Label
Text="Current count: 0"
Grid.Row="2"
FontSize="18"
FontAttributes="Bold"
x:Name="CounterLabel"
HorizontalOptions="Center" />

<Button
Text="Click me"
FontAttributes="Bold"
Grid.Row="3"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Center" />

<Image Grid.Row="4"
Source="dotnet_bot.png"
SemanticProperties.Description="Cute dot net bot waving hi to you!"
WidthRequest="250"
HeightRequest="310"
HorizontalOptions="Center" />

</Grid>
</ScrollView>
</ContentPage>
24 changes: 17 additions & 7 deletions DrasticMaui.Sample/TraySample.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@

namespace DrasticMaui.Sample
{
public partial class TraySample : ContentPage
{
public TraySample()
{
InitializeComponent();
}
}
public partial class TraySample : ContentPage
{
int count = 0;

public TraySample()
{
InitializeComponent();
}

private void OnCounterClicked(object sender, EventArgs e)
{
count++;
CounterLabel.Text = $"Current count: {count}";

SemanticScreenReader.Announce(CounterLabel.Text);
}
}
}
Loading

0 comments on commit ba943d3

Please sign in to comment.