Skip to content

Commit bc9eccb

Browse files
authored
fix(QuickMenu): Take into account the edge of the screen and show the quick menu properly
Fixes #735
1 parent 128981a commit bc9eccb

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

SoundSwitch.UI.Menu/Form/QuickMenu.cs

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
4+
using System.Drawing;
45
using System.Linq;
56
using System.Threading.Tasks;
67
using System.Windows.Forms;
@@ -48,8 +49,6 @@ internal QuickMenu()
4849
{
4950
InitializeComponent();
5051
_hideDisposeMethod = HideDispose;
51-
Show();
52-
SetLocationToCursor();
5352
}
5453

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

114113
Height = 0;
115114
}
115+
116+
if (!isLocationSet)
117+
{
118+
Show();
119+
SetLocationToCursor();
120+
isLocationSet = true;
121+
}
116122
}
117123

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

148+
private bool isLocationSet = false;
149+
142150
private async Task HideDispose()
143151
{
144152
_hiding = true;
@@ -153,6 +161,7 @@ private async Task HideDispose()
153161

154162
Hide();
155163
Dispose();
164+
isLocationSet = false;
156165
}
157166

158167
private void ResetOpacity()
@@ -162,9 +171,16 @@ private void ResetOpacity()
162171

163172
private void SetLocationToCursor()
164173
{
165-
var position = Cursor.Position;
166-
SetDesktopLocation(position.X, position.Y);
167-
Location = position;
174+
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
175+
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
176+
Point qmLoc = new Point(
177+
Math.Min(Cursor.Position.X, screenWidth - Width),
178+
Math.Min(Cursor.Position.Y, screenHeight - Height)
179+
);
180+
181+
SetDesktopLocation(qmLoc.X, qmLoc.Y);
182+
Location = qmLoc;
183+
isLocationSet = true;
168184
}
169185
}
170186
}

0 commit comments

Comments
 (0)