Skip to content

Commit

Permalink
More code cleanup and double layer exception fox on application shutd…
Browse files Browse the repository at this point in the history
…own (#8)
  • Loading branch information
denisabt authored Feb 13, 2022
1 parent a0e52d4 commit 6dc7097
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 79 deletions.
16 changes: 8 additions & 8 deletions SharedProject1/AssistImpl/Genius.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Linq;
using Recoding.ClippyVSPackage;
using System.Diagnostics;
using System.Windows.Media.Effects;
using System.Windows.Resources;
using Microsoft.VisualStudio.PlatformUI;
using Newtonsoft.Json;
Expand Down Expand Up @@ -40,12 +39,12 @@ public class Genius : AssistantBase
/// <summary>
/// The height of the frame
/// </summary>
public static int ClipHeight { get; set; } = 93;
public static int ClipHeight { get; } = 93;

/// <summary>
/// The with of the frame
/// </summary>
public static int ClipWidth { get; set; } = 124;
public static int ClipWidth { get; } = 124;

/// <summary>
/// The image that holds the sprite
Expand Down Expand Up @@ -298,11 +297,12 @@ private void XDoubleAnimation_Completed(object sender, EventArgs e)
{
IsAnimating = false;
ClippedImage.Visibility = Visibility.Visible;
(ClippedImage.Parent as Canvas).Visibility = Visibility.Visible;
if (ClippedImage.Parent is Canvas canvas)
canvas.Visibility = Visibility.Visible;

// Rememver to modify canvas, not image!
_clippedImage1.Visibility = Visibility.Hidden;
(_clippedImage1.Parent as Canvas).Visibility=Visibility.Hidden;
if (_clippedImage1.Parent is Canvas canvas1)
canvas1.Visibility=Visibility.Hidden;
}

/// <summary>
Expand Down Expand Up @@ -360,8 +360,8 @@ public async System.Threading.Tasks.Task StartAnimationAsync(GeniusAnimations an
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
// well have to skip this (leave collapsed) if only one layer
if (animLayers > 1) {
_clippedImage1.Visibility = Visibility.Visible;
(_clippedImage1.Parent as Canvas).Visibility = Visibility.Visible;
_clippedImage1.Visibility = Visibility.Visible;
((Canvas) _clippedImage1.Parent).Visibility = Visibility.Visible;
}
ClippedImage.BeginAnimation(Canvas.LeftProperty, animation.KeyFrames.Item1);
ClippedImage.BeginAnimation(Canvas.TopProperty, animation.KeyFrames.Item2);
Expand Down
10 changes: 5 additions & 5 deletions SharedProject1/AssistImpl/Merlin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public class Merlin : AssistantBase
/// <summary>
/// The height of the frame
/// </summary>
public static int ClipHeight { get; set; } = 128;
public static int ClipHeight { get; } = 128;

/// <summary>
/// The with of the frame
/// </summary>
public static int ClipWidth { get; set; } = 128;
public static int ClipWidth { get; } = 128;

/// <summary>
/// The list of all the available animations
Expand All @@ -54,7 +54,7 @@ public class Merlin : AssistantBase
/// <summary>
/// All the animations that represents an Idle state
/// </summary>
public static List<MerlinAnimations> IdleAnimations = new List<MerlinAnimations>() {
private static readonly List<MerlinAnimations> IdleAnimations = new List<MerlinAnimations>() {
MerlinAnimations.MoveLeft,
MerlinAnimations.Idle3_2,
MerlinAnimations.Idle3_1,
Expand Down Expand Up @@ -140,7 +140,7 @@ private void RegisterAnimations()

double timeOffset = 0;

foreach (Recoding.ClippyVSPackage.Configurations.Frame frame in animation.Frames)
foreach (var frame in animation.Frames)
{
if (frame.ImagesOffsets != null)
{
Expand Down Expand Up @@ -239,7 +239,7 @@ public async System.Threading.Tasks.Task StartAnimationAsync(MerlinAnimations an
ClippedImage.BeginAnimation(Canvas.TopProperty, animation.Item2);
}
}
catch (Exception e)
catch (Exception)
{
Debug.WriteLine("StartAnimAsyncException Merlin " + animationType);
}
Expand Down
10 changes: 4 additions & 6 deletions SharedProject1/AssistantBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ public class AssistantBase : IDisposable
/// <summary>
/// When is true it means an animation is actually running
/// </summary>
public bool IsAnimating { get; set; }

protected bool IsAnimating { get; set; }

/// <summary>
/// Reads the content of a stream into a string
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static string StreamToString(Stream stream)
protected static string StreamToString(Stream stream)
{
string streamString;
stream.Position = 0;
Expand All @@ -52,9 +51,8 @@ public static string StreamToString(Stream stream)
}

public void Dispose()
{
if (WpfAnimationsDispatcher != null)
WpfAnimationsDispatcher.Stop();
{
WpfAnimationsDispatcher?.Stop();
}
}
}
24 changes: 8 additions & 16 deletions SharedProject1/Clippy.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.VisualStudio.Shell;
using Recoding.ClippyVSPackage.Configurations;
using Recoding.ClippyVSPackage.Configurations;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
Expand Down Expand Up @@ -33,12 +33,12 @@ public class Clippy : AssistantBase
/// <summary>
/// The height of the frame
/// </summary>
public static int ClipHeight { get; set; } = 93;
public static int ClipHeight => 93;

/// <summary>
/// The with of the frame
/// </summary>
public static int ClipWidth { get; set; } = 124;
public static int ClipWidth => 124;

/// <summary>
/// The list of all the available animations
Expand Down Expand Up @@ -74,7 +74,7 @@ public Clippy(Canvas canvas)
#endif
#if Dev22
#endif
this.Sprite = new BitmapImage(new Uri(spResUri, UriKind.RelativeOrAbsolute));
Sprite = new BitmapImage(new Uri(spResUri, UriKind.RelativeOrAbsolute));

ClippedImage = new Image
{
Expand All @@ -90,8 +90,6 @@ public Clippy(Canvas canvas)
if (_animations == null)
RegisterAnimations();


//XX Requires testing..
AllAnimations = new List<ClippyAnimation>();
var values = Enum.GetValues(typeof(ClippyAnimation));
AllAnimations.AddRange(values.Cast<ClippyAnimation>());
Expand Down Expand Up @@ -161,16 +159,10 @@ private void RegisterAnimations()
_animations.Add(animation.Name,
new Tuple<DoubleAnimationUsingKeyFrames, DoubleAnimationUsingKeyFrames>(xDoubleAnimation,
yDoubleAnimation));
xDoubleAnimation.Changed += XDoubleAnimation_Changed;
xDoubleAnimation.Completed += xDoubleAnimation_Completed;
}
}

private void XDoubleAnimation_Changed(object sender, EventArgs e)
{
// Debug.WriteLine("Clippy: Animation changing");
}

/// <summary>
/// Callback to execute at the end of an animation
/// </summary>
Expand Down Expand Up @@ -205,7 +197,7 @@ private void WPFAnimationsDispatcher_Tick(object sender, EventArgs e)

public void StartAnimation(ClippyAnimation animations, bool byPassCurrentAnimation = false)
{
ThreadHelper.JoinableTaskFactory.Run(
Microsoft.VisualStudio.Shell.ThreadHelper.JoinableTaskFactory.Run(
async delegate
{
await StartAnimationAsync(animations, byPassCurrentAnimation);
Expand All @@ -217,12 +209,12 @@ async delegate
/// </summary>
/// <param name="animationType"></param>
/// <param name="byPassCurrentAnimation"></param>
public async System.Threading.Tasks.Task StartAnimationAsync(ClippyAnimation animationType, bool byPassCurrentAnimation = false)
public async Task StartAnimationAsync(ClippyAnimation animationType, bool byPassCurrentAnimation = false)
{
if (!IsAnimating || byPassCurrentAnimation)
{
IsAnimating = true;
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
await Microsoft.VisualStudio.Shell.ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if (_animations.ContainsKey(animationType.ToString()))
{
ClippedImage.BeginAnimation(Canvas.LeftProperty, _animations[animationType.ToString()].Item1);
Expand Down
4 changes: 2 additions & 2 deletions SharedProject1/ClippyVSPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Recoding.ClippyVSPackage

public sealed class ClippyVisualStudioPackage : AsyncPackage
{
public SpriteContainer SpriteContainer { get; set; }
private SpriteContainer SpriteContainer { get; set; }

private IClippyVsSettings Settings { get; set; }

Expand Down Expand Up @@ -107,7 +107,7 @@ internal void ReviveMerlinCommand()
}


internal void ReviveClippyCommand()
private void ReviveClippyCommand()
{
var visibleAssistants = Application.Current.Windows.OfType<SpriteContainer>();
if (!visibleAssistants.Any())
Expand Down
15 changes: 3 additions & 12 deletions SharedProject1/Command1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,12 @@ internal sealed class Command1
/// <summary>
/// Command ID.
/// </summary>
public const int CommandId = 4129;
private const int CommandId = 4129;

/// <summary>
/// Command menu group (command set GUID).
/// </summary>
public static readonly Guid CommandSet = new Guid("fbed79a9-1faa-4dc3-9f96-9fb39d31bfdb");

/// <summary>
/// Gets the instance of the command.
/// </summary>
public static Command1 Instance
{
get;
private set;
}
private static readonly Guid CommandSet = new Guid("fbed79a9-1faa-4dc3-9f96-9fb39d31bfdb");

/// <summary>
/// Initializes a new instance of the <see cref="Command1"/> class.
Expand All @@ -56,7 +47,7 @@ public static async Task InitializeAsync(AsyncPackage package)
//await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);

OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
Instance = new Command1(commandService);
_ = new Command1(commandService);
}

/// <summary>
Expand Down
16 changes: 4 additions & 12 deletions SharedProject1/Command2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ internal sealed class Command2
/// <summary>
/// Command ID.
/// </summary>
public const int CommandId = 4130;
private const int CommandId = 4130;

/// <summary>
/// Command menu group (command set GUID).
/// </summary>
public static readonly Guid CommandSet = new Guid("fbed79a9-1faa-4dc3-9f96-9fb39d31bfdb");
private static readonly Guid CommandSet = new Guid("fbed79a9-1faa-4dc3-9f96-9fb39d31bfdb");

/// <summary>
/// VS Package that provides this command, not null.
Expand All @@ -30,22 +30,14 @@ internal sealed class Command2
/// <param name="commandService">Command service to add command to, not null.</param>
private Command2(AsyncPackage package, OleMenuCommandService commandService)
{
this._package = package ?? throw new ArgumentNullException(nameof(package));
_package = package ?? throw new ArgumentNullException(nameof(package));
commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));

var menuCommandId = new CommandID(CommandSet, CommandId);
var menuItem = new MenuCommand(this.Execute, menuCommandId);
commandService.AddCommand(menuItem);
}

/// <summary>
/// Gets the instance of the command.
/// </summary>
public static Command2 Instance
{
get;
private set;
}

/// <summary>
/// Gets the service provider from the owner package.
Expand All @@ -63,7 +55,7 @@ public static async System.Threading.Tasks.Task InitializeAsync(AsyncPackage pac
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);

OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
Instance = new Command2(package, commandService);
_ = new Command2(package, commandService);
}

/// <summary>
Expand Down
18 changes: 3 additions & 15 deletions SharedProject1/CommandGenius.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Microsoft.VisualStudio.Shell;
using System;
using System.ComponentModel.Design;
using System.Globalization;
using System.Windows.Forms;
using Task = System.Threading.Tasks.Task;

namespace Recoding.ClippyVSPackage
Expand All @@ -15,27 +13,18 @@ internal sealed class CommandGenius
/// <summary>
/// Command ID.
/// </summary>
public const int CommandId = 4131;
private const int CommandId = 4131;

/// <summary>
/// Command menu group (command set GUID).
/// </summary>
public static readonly Guid CommandSet = new Guid("fbed79a9-1faa-4dc3-9f96-9fb39d31bfdb");
private static readonly Guid CommandSet = new Guid("fbed79a9-1faa-4dc3-9f96-9fb39d31bfdb");

/// <summary>
/// VS Package that provides this command, not null.
/// </summary>
private readonly AsyncPackage _package;

/// <summary>
/// Gets the instance of the command.
/// </summary>
public static CommandGenius Instance
{
get;
private set;
}

/// <summary>
/// Initializes a new instance of the <see cref="Command1"/> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
Expand All @@ -62,8 +51,7 @@ public static async Task InitializeAsync(AsyncPackage package)
//await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);

OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;

Instance = new CommandGenius(commandService, package);
_ = new CommandGenius(commandService, package);
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions SharedProject1/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ namespace Recoding.ClippyVSPackage
static class Constants
{
public const string GuidClippyVsPkgString = "6aae52ac-8840-421f-bb35-60b52ab431e2";
public const string GuidClippyVsCmdSetString = "fbed79a9-1faa-4dc3-9f96-9fb39d31bfdb";
private const string GuidClippyVsCmdSetString = "fbed79a9-1faa-4dc3-9f96-9fb39d31bfdb";
public const string GuidOptionsPage = "663638f6-c16c-4401-84db-8bc093182cf8";

public static readonly Guid GuidClippyVsCmdSet = new Guid(GuidClippyVsCmdSetString);

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion SharedProject1/OptionsPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class OptionsPage : DialogPage
[Category("General")]
[Description("If true shows Clippy at the VS startup")]
[DisplayName("Show at startup")]
public bool ShowAtStartup { get; set; }
private bool ShowAtStartup { get; set; }

public OptionsPage()
{
Expand Down

0 comments on commit 6dc7097

Please sign in to comment.