Skip to content

Commit

Permalink
fix(QuickMenu): Take into account the edge of the screen and show the…
Browse files Browse the repository at this point in the history
… quick menu properly

Fixes #735
  • Loading branch information
lpv11 authored Oct 30, 2021
1 parent 128981a commit bc9eccb
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions SoundSwitch.UI.Menu/Form/QuickMenu.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand Down Expand Up @@ -48,8 +49,6 @@ internal QuickMenu()
{
InitializeComponent();
_hideDisposeMethod = HideDispose;
Show();
SetLocationToCursor();
}

/// <summary>
Expand Down Expand Up @@ -113,6 +112,13 @@ public void SetData(IEnumerable<IconMenuItem<T>.DataContainer> payloads)

Height = 0;
}

if (!isLocationSet)
{
Show();
SetLocationToCursor();
isLocationSet = true;
}
}

private void DebounceHiding()
Expand All @@ -139,6 +145,8 @@ private void OnItemClicked(IconMenuItem<T> control)
SelectionChanged?.Invoke(control, new MenuClickedEvent(dataContainer));
}

private bool isLocationSet = false;

private async Task HideDispose()
{
_hiding = true;
Expand All @@ -153,6 +161,7 @@ private async Task HideDispose()

Hide();
Dispose();
isLocationSet = false;
}

private void ResetOpacity()
Expand All @@ -162,9 +171,16 @@ private void ResetOpacity()

private void SetLocationToCursor()
{
var position = Cursor.Position;
SetDesktopLocation(position.X, position.Y);
Location = position;
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
Point qmLoc = new Point(
Math.Min(Cursor.Position.X, screenWidth - Width),
Math.Min(Cursor.Position.Y, screenHeight - Height)
);

SetDesktopLocation(qmLoc.X, qmLoc.Y);
Location = qmLoc;
isLocationSet = true;
}
}
}

0 comments on commit bc9eccb

Please sign in to comment.