Skip to content

Commit 5df7362

Browse files
committed
- Add Logic in settingwindow
1 parent ab40e7a commit 5df7362

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

Flow.Launcher/SettingWindow.xaml.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,45 @@ public void InitializePosition()
112112
{
113113
if (_settings.SettingWindowTop == null || _settings.SettingWindowLeft == null)
114114
{
115-
Top = WindowTop();
116-
Left = WindowLeft();
115+
SetWindowPosition(WindowTop(), WindowLeft());
117116
}
118117
else
119118
{
120-
Top = _settings.SettingWindowTop.Value;
121-
Left = _settings.SettingWindowLeft.Value;
119+
double left = _settings.SettingWindowLeft.Value;
120+
double top = _settings.SettingWindowTop.Value;
121+
AdjustWindowPosition(ref top, ref left);
122+
SetWindowPosition(top, left);
122123
}
123124
WindowState = _settings.SettingWindowState;
124125
}
125126

127+
private void SetWindowPosition(double top, double left)
128+
{
129+
// Ensure window does not exceed screen boundaries
130+
top = Math.Max(top, SystemParameters.VirtualScreenTop);
131+
left = Math.Max(left, SystemParameters.VirtualScreenLeft);
132+
top = Math.Min(top, SystemParameters.VirtualScreenHeight - ActualHeight);
133+
left = Math.Min(left, SystemParameters.VirtualScreenWidth - ActualWidth);
134+
135+
Top = top;
136+
Left = left;
137+
}
138+
139+
private void AdjustWindowPosition(ref double top, ref double left)
140+
{
141+
// Adjust window position if it exceeds screen boundaries
142+
top = Math.Max(top, SystemParameters.VirtualScreenTop);
143+
left = Math.Max(left, SystemParameters.VirtualScreenLeft);
144+
top = Math.Min(top, SystemParameters.VirtualScreenHeight - ActualHeight);
145+
left = Math.Min(left, SystemParameters.VirtualScreenWidth - ActualWidth);
146+
}
147+
126148
private double WindowLeft()
127149
{
128150
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
129151
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0);
130152
var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0);
131-
var left = (dip2.X - this.ActualWidth) / 2 + dip1.X;
153+
var left = (dip2.X - ActualWidth) / 2 + dip1.X;
132154
return left;
133155
}
134156

@@ -137,7 +159,7 @@ private double WindowTop()
137159
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
138160
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y);
139161
var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Height);
140-
var top = (dip2.Y - this.ActualHeight) / 2 + dip1.Y - 20;
162+
var top = (dip2.Y - ActualHeight) / 2 + dip1.Y;
141163
return top;
142164
}
143165

0 commit comments

Comments
 (0)