Skip to content

Commit

Permalink
Commands.WinForms: Added feed-specific branding to progress dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianeicher committed Jan 15, 2022
1 parent e672e6c commit 14df6dc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
51 changes: 51 additions & 0 deletions src/Commands.WinForms/FeedBranding.cs
@@ -0,0 +1,51 @@
// Copyright Bastian Eicher et al.
// Licensed under the GNU Lesser Public License

using System.Windows.Forms;
using NanoByte.Common;
using NanoByte.Common.Storage;
using NanoByte.Common.Tasks;
using ZeroInstall.Commands.Properties;
using ZeroInstall.DesktopIntegration;
using ZeroInstall.Model;
using ZeroInstall.Store.Configuration;
using ZeroInstall.Store.Feeds;
using ZeroInstall.Store.Icons;
using ZeroInstall.Store.Trust;

namespace ZeroInstall.Commands.WinForms
{
/// <summary>
/// Feed-specific visual branding information.
/// </summary>
public class FeedBranding
{
/// <summary>
/// The title of the window.
/// </summary>
public string Title { get; }

/// <summary>
/// The title of the window.
/// </summary>
public System.Drawing.Icon Icon { get; }

/// <summary>
/// Loads visual branding information for a specific <paramref name="feedUri"/> or falls back to defaults.
/// </summary>
public FeedBranding(FeedUri? feedUri)
{
var feed = feedUri?.To(FeedCaches.Default(OpenPgp.Verifying()).GetFeed);

Icon = feed
?.Icons.GetIcon(ZeroInstall.Model.Icon.MimeTypeIco)
?.To(IconStores.DesktopIntegration(Config.LoadSafe(), new SilentTaskHandler(), machineWide: false).GetCached)
?.To(path => new System.Drawing.Icon(path))
?? System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath)!;

Title = feed?.To(x => $"{x.Name} (Zero Install)")
?? "Zero Install";
if (Locations.IsPortable) Title += @" - " + Resources.PortableMode;
}
}
}
17 changes: 7 additions & 10 deletions src/Commands.WinForms/GuiCommandHandler.cs
Expand Up @@ -12,10 +12,8 @@
using NanoByte.Common.Collections;
using NanoByte.Common.Controls;
using NanoByte.Common.Native;
using NanoByte.Common.Storage;
using NanoByte.Common.Tasks;
using NanoByte.Common.Threading;
using ZeroInstall.Commands.Properties;
using ZeroInstall.DesktopIntegration.ViewModel;
using ZeroInstall.Model;
using ZeroInstall.Model.Selection;
Expand All @@ -33,17 +31,16 @@ namespace ZeroInstall.Commands.WinForms
public sealed class GuiCommandHandler : GuiTaskHandlerBase, ICommandHandler
{
#region Resources
private static readonly System.Drawing.Icon _icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath)!;
private readonly Lazy<FeedBranding> _feedBranding;
private readonly AsyncFormWrapper<ProgressForm> _wrapper;

public GuiCommandHandler()
{
_feedBranding = new(() => new FeedBranding(FeedUri));

_wrapper = new AsyncFormWrapper<ProgressForm>(delegate
{
string title = "Zero Install";
if (Locations.IsPortable) title += @" - " + Resources.PortableMode;

var form = new ProgressForm(title, _icon, CancellationTokenSource);
var form = new ProgressForm(_feedBranding.Value, CancellationTokenSource);
if (Background) form.ShowTrayIcon();
else form.Show();
return form;
Expand Down Expand Up @@ -218,17 +215,17 @@ public override void Output<T>(string title, IEnumerable<T> data)
/// </summary>
/// <param name="title">The title of the message.</param>
/// <param name="message">The message text.</param>
private static void OutputNotification(string title, string message)
private void OutputNotification(string title, string message)
{
if (WindowsUtils.IsWindows10 && ZeroInstallInstance.IsIntegrated)
ShowModernNotification(title, message);
else
ShowLegacyNotification(title, message);
}

private static void ShowLegacyNotification(string title, string message)
private void ShowLegacyNotification(string title, string message)
{
var icon = new NotifyIcon {Visible = true, Icon = _icon};
var icon = new NotifyIcon {Visible = true, Icon = _feedBranding.Value.Icon};
icon.ShowBalloonTip(10000, title, message, ToolTipIcon.Info);
}

Expand Down
10 changes: 4 additions & 6 deletions src/Commands.WinForms/ProgressForm.cs
Expand Up @@ -3,7 +3,6 @@

using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand All @@ -28,17 +27,16 @@ public sealed partial class ProgressForm : Form
/// <summary>
/// Creates a new progress tracking window.
/// </summary>
/// <param name="title">The title of the window.</param>
/// <param name="icon">The icon of the window</param>
/// <param name="feedBranding">Branding to apply to the window.</param>
/// <param name="cancellationTokenSource">Used to signal when the user wishes to cancel the current process.</param>
public ProgressForm(string title, Icon icon, CancellationTokenSource cancellationTokenSource)
public ProgressForm(FeedBranding feedBranding, CancellationTokenSource cancellationTokenSource)
{
_cancellationTokenSource = cancellationTokenSource ?? throw new ArgumentNullException(nameof(cancellationTokenSource));

InitializeComponent();

Text = notifyIcon.Text = title;
Icon = notifyIcon.Icon = icon;
Text = notifyIcon.Text = feedBranding.Title;
Icon = notifyIcon.Icon = feedBranding.Icon;

buttonCustomizeSelectionsDone.Text = Resources.Done;
buttonHide.Text = Resources.Hide;
Expand Down

0 comments on commit 14df6dc

Please sign in to comment.