Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add correct tray menu provider #126

Merged
merged 24 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions KeeTrayTOTP.Tests/Menu/LegacyTrayMenuTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Windows.Forms;
using FluentAssertions;
using KeePass.App.Configuration;
using KeePass.Forms;
using KeePass.Plugins;
using KeePass.UI;
using KeeTrayTOTP.Menu;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace KeeTrayTOTP.Tests.Menu
{
[TestClass]
public class LegacyTrayMenuTests
{
[TestMethod]
public void LegacyTrayMenuItemProvider_ShouldReturnNull()
{
var plugin = CreatePluginHostMock(out var host);
var legacyTrayMenuItemProvider = new LegacyTrayMenuItemProvider(plugin, host.Object);

var sut = legacyTrayMenuItemProvider.ProvideMenuItem();

sut.Should().BeNull();
}

[TestMethod]
public void LegacyTrayMenuItemProvider_ShouldAddItemsDirectlyToMainWindowsTrayContextMenu()
{
var plugin = CreatePluginHostMock(out var host);
var oldItemCount = host.Object.MainWindow.TrayContextMenu.Items.Count;
var legacyTrayMenuItemProvider = new LegacyTrayMenuItemProvider(plugin, host.Object);

var sut = host.Object.MainWindow.TrayContextMenu.Items.Count;

sut.Should().Be(oldItemCount + 2);
}

private static KeeTrayTOTPExt CreatePluginHostMock(out Mock<IPluginHost> host)
dannoe marked this conversation as resolved.
Show resolved Hide resolved
{
var plugin = new KeeTrayTOTPExt();
host = new Mock<IPluginHost>(MockBehavior.Strict);

var mainForm = new MainForm();
host.SetupGet(c => c.MainWindow).Returns(mainForm);

return plugin;
}
}
}
17 changes: 16 additions & 1 deletion KeeTrayTOTP/FormSettings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions KeeTrayTOTP/FormSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ private void WorkerLoad_DoWork(object sender, DoWorkEventArgs e)
CheckBoxShowCopyTOTPEntryMenu.Checked = _plugin.PluginHost.CustomConfig.GetBool(KeeTrayTOTPExt.setname_bool_EntryContextCopy_Visible, true);
CheckBoxShowSetupTOTPEntryMenu.Checked = _plugin.PluginHost.CustomConfig.GetBool(KeeTrayTOTPExt.setname_bool_EntryContextSetup_Visible, true);
CheckBoxShowTOTPEntriesTrayMenu.Checked = _plugin.PluginHost.CustomConfig.GetBool(KeeTrayTOTPExt.setname_bool_NotifyContext_Visible, true);
CheckBoxEnableLegacyTrayMenuProvider.Checked = _plugin.PluginHost.CustomConfig.GetBool(KeeTrayTOTPExt.setname_bool_LegacyTrayMenuProvider_Enable, false);
CheckBoxTrimTrayText.Checked = _plugin.PluginHost.CustomConfig.GetBool(KeeTrayTOTPExt.setname_bool_TrimTrayText, false);
if (WorkerLoad.CancellationPending) { e.Cancel = true; return; }

Expand Down Expand Up @@ -360,6 +361,7 @@ private void WorkerSave_DoWork(object sender, DoWorkEventArgs e)
_plugin.PluginHost.CustomConfig.SetBool(KeeTrayTOTPExt.setname_bool_EntryContextCopy_Visible, CheckBoxShowCopyTOTPEntryMenu.Checked);
_plugin.PluginHost.CustomConfig.SetBool(KeeTrayTOTPExt.setname_bool_EntryContextSetup_Visible, CheckBoxShowSetupTOTPEntryMenu.Checked);
_plugin.PluginHost.CustomConfig.SetBool(KeeTrayTOTPExt.setname_bool_NotifyContext_Visible, CheckBoxShowTOTPEntriesTrayMenu.Checked);
_plugin.PluginHost.CustomConfig.SetBool(KeeTrayTOTPExt.setname_bool_LegacyTrayMenuProvider_Enable, CheckBoxEnableLegacyTrayMenuProvider.Checked);
_plugin.PluginHost.CustomConfig.SetBool(KeeTrayTOTPExt.setname_bool_TrimTrayText, CheckBoxTrimTrayText.Checked);
if (WorkerSave.CancellationPending) { e.Cancel = true; return; }

Expand Down
3 changes: 3 additions & 0 deletions KeeTrayTOTP/FormSettings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@
<metadata name="GroupBoxTrayMenu.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="CheckBoxEnableLegacyTrayMenuProvider.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="CheckBoxTrimTrayText.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
Expand Down
31 changes: 31 additions & 0 deletions KeeTrayTOTP/Helpers/DocumentHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KeePass.Plugins;
using KeePass.UI;

namespace KeeTrayTOTP.Helpers
{
public static class DocumentHelper
{
public static bool IsSingleDatabaseOpenAndUnlocked(this DocumentManagerEx documentManager)
{
var documents = documentManager.Documents;
return documents.Count == 1 && documents[0].Database.IsOpen;
}

/// <summary>
/// The DocumentManager of keepass will always return a list with at least a single "pseudo" document.
/// This method checks if this single document is the pseudo document.
/// </summary>
/// <returns>Returns true if there is no real document open.</returns>
public static bool IsNotAtLeastOneDocumentOpen(this DocumentManagerEx documentManager)
{
var documents = documentManager.Documents;
return ((documents.Count == 1) && (!documents[0].Database.IsOpen) &&
(documents[0].LockedIoc.Path.Length == 0));
}
}
}
23 changes: 23 additions & 0 deletions KeeTrayTOTP/Helpers/ImageHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KeePass.UI;

namespace KeeTrayTOTP.Helpers
{
public static class ImageHelper
{
public static Image CreateImageFromColor(Color color)
{
if (color != Color.Empty)
{
return UIUtil.CreateColorBitmap24(16, 16, color);
}

return null;
}
}
}
55 changes: 55 additions & 0 deletions KeeTrayTOTP/Helpers/MenuItemHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KeeTrayTOTP.Helpers
{
public static class MenuItemHelper
{
/// <summary>
/// Prevent ToolStripMenuItems from jumping to second screen
/// see: https://stackoverflow.com/a/49626530/1627022
/// </summary>
internal static void OnDatabaseDropDownOpening(object sender, EventArgs e)
{
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
if (menuItem == null || menuItem.HasDropDownItems == false)
{
return; // not a drop down item
}

//get position of current menu item
var pos = new Point(menuItem.GetCurrentParent().Left, menuItem.GetCurrentParent().Top);

// Current bounds of the current monitor
Rectangle bounds = Screen.GetWorkingArea(pos);
Screen currentScreen = Screen.FromPoint(pos);

// Find the width of sub-menu
int maxWidth = 0;
foreach (var subItem in menuItem.DropDownItems)
{
if (subItem.GetType() == typeof(ToolStripMenuItem))
{
var mnu = (ToolStripMenuItem)subItem;
maxWidth = Math.Max(mnu.Width, maxWidth);
}
}

maxWidth += 10; // Add a little wiggle room

int farRight = pos.X + menuItem.Width + maxWidth;
int farLeft = pos.X - maxWidth;

//get left and right distance to compare
int leftGap = farLeft - currentScreen.Bounds.Left;
int rightGap = currentScreen.Bounds.Right - farRight;

menuItem.DropDownDirection = leftGap >= rightGap ? ToolStripDropDownDirection.Left : ToolStripDropDownDirection.Right;
}
}
}
9 changes: 9 additions & 0 deletions KeeTrayTOTP/KeeTrayTOTP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Include="Helpers\DocumentHelper.cs" />
<Compile Include="Helpers\MenuItemHelper.cs" />
<Compile Include="Helpers\ImageHelper.cs" />
<Compile Include="Menu\EntryMenuItemProvider.cs" />
<Compile Include="Menu\MainMenuItemProvider.cs" />
<Compile Include="Menu\MenuItemProvider.cs" />
<Compile Include="Menu\LegacyTrayMenuItemProvider.cs" />
<Compile Include="Libraries\DropDownLocationCalculator.cs" />
<Compile Include="FormAbout.cs">
<SubType>Form</SubType>
Expand All @@ -91,6 +98,8 @@
<Compile Include="Libraries\Base32.cs" />
<Compile Include="Libraries\TimeCorrectionProvider.cs" />
<Compile Include="Libraries\TOTPEncoder.cs" />
<Compile Include="Menu\MenuItemProviderBase.cs" />
<Compile Include="Menu\TrayMenuItemProvider.cs" />
<Compile Include="SetupTOTP.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
18 changes: 18 additions & 0 deletions KeeTrayTOTP/Localization/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions KeeTrayTOTP/Localization/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@
<data name="Help" xml:space="preserve">
<value>Help</value>
</data>
<data name="Locked" xml:space="preserve">
<value>Locked</value>
</data>
<data name="NoDatabaseIsOpened" xml:space="preserve">
<value>No database is opened!</value>
</data>
<data name="NoTOTPSeedFound" xml:space="preserve">
<value>No TOTP Seed found!</value>
</data>
Expand Down