Skip to content

Commit

Permalink
Fixing wrong dropdown location in a rare multi screen setup (#162)
Browse files Browse the repository at this point in the history
This should finally fix #160.
  • Loading branch information
dannoe committed May 17, 2020
1 parent 6302156 commit cd77b20
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
49 changes: 49 additions & 0 deletions KeeTrayTOTP/Helpers/ToolStripMenuItemEx.cs
@@ -0,0 +1,49 @@
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace KeeTrayTOTP.Helpers
{
[DesignerCategory("")]
public class ToolStripMenuItemEx : ToolStripMenuItem
{
public ToolStripMenuItemEx()
{
}

public ToolStripMenuItemEx(string text) : base(text)
{
}

public ToolStripMenuItemEx(string text, Image image) : base(text, image)
{
}

public ToolStripMenuItemEx(string text, Image image, EventHandler onClick) : base(text, image, onClick)
{
}

/// <summary>
/// This is required, because there is a bug in the .NET Framework, which calculates the position of dropdown menus wrong,
/// if there is a multi-monitor setup with two monitors stacked above with the taskbar on the bottom of the top screen.
/// </summary>
protected override Point DropDownLocation
{
get
{
var dropDownLocation = base.DropDownLocation;

var screenOfDropDown = Screen.FromPoint(dropDownLocation);
var screenOfParentMenu = Screen.FromControl(Parent);

if (!screenOfParentMenu.Equals(screenOfDropDown))
{
dropDownLocation.Offset(0, -DropDown.Height);
}

return dropDownLocation;
}
}
}
}
1 change: 1 addition & 0 deletions KeeTrayTOTP/KeeTrayTOTP.csproj
Expand Up @@ -76,6 +76,7 @@
<Compile Include="Helpers\DocumentExtensions.cs" />
<Compile Include="Helpers\MenuItemHelper.cs" />
<Compile Include="Helpers\ImageExtensions.cs" />
<Compile Include="Helpers\ToolStripMenuItemEx.cs" />
<Compile Include="Helpers\TOTPEntryValidator.cs" />
<Compile Include="ISettings.cs" />
<Compile Include="Menu\EntryMenuItemProvider.cs" />
Expand Down
8 changes: 4 additions & 4 deletions KeeTrayTOTP/Menu/TrayMenuItemProvider.cs
Expand Up @@ -37,7 +37,7 @@ private void TrayContextMenu_Opened(object sender, EventArgs e)

public virtual ToolStripMenuItem ProvideMenuItem()
{
var rootTrayMenuItem = new ToolStripMenuItem(Localization.Strings.TrayTOTPPlugin, Resources.TOTP);
var rootTrayMenuItem = new ToolStripMenuItemEx(Localization.Strings.TrayTOTPPlugin, Resources.TOTP);

rootTrayMenuItem.DropDownItems.Add(CreatePseudoToolStripMenuItem());
rootTrayMenuItem.DropDownOpening += OnRootDropDownOpening;
Expand Down Expand Up @@ -85,17 +85,17 @@ internal IEnumerable<ToolStripMenuItem> BuildMenuItemsForRootDropDown(List<PwDoc

private ToolStripMenuItem CreateDatabaseMenuItemForDocument(PwDocument document)
{
ToolStripMenuItem mainDropDownItem;
ToolStripMenuItemEx mainDropDownItem;
if (!document.Database.IsOpen)
{
var documentName = UrlUtil.GetFileName(document.LockedIoc.Path);
documentName += " [" + Localization.Strings.Locked + "]";
mainDropDownItem = new ToolStripMenuItem(documentName, Resources.TOTP_Error, OnClickOpenDatabase);
mainDropDownItem = new ToolStripMenuItemEx(documentName, Resources.TOTP_Error, OnClickOpenDatabase);
}
else
{
var documentName = UrlUtil.GetFileName(document.Database.IOConnectionInfo.Path);
mainDropDownItem = new ToolStripMenuItem(documentName, ImageExtensions.CreateImageFromColor(document.Database.Color));
mainDropDownItem = new ToolStripMenuItemEx(documentName, ImageExtensions.CreateImageFromColor(document.Database.Color));
mainDropDownItem.DropDownOpening += OnDatabaseDropDownOpening;
mainDropDownItem.DropDownOpening += MenuItemHelper.OnDatabaseDropDownOpening;
mainDropDownItem.DropDownClosed += OnDropDownClosed;
Expand Down

0 comments on commit cd77b20

Please sign in to comment.