Skip to content

Commit 3699705

Browse files
committed
Minor
1 parent e42bb10 commit 3699705

File tree

4 files changed

+66
-15
lines changed

4 files changed

+66
-15
lines changed

JL.Windows/GUI/MainWindow.xaml.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,11 @@ private void Window_MouseLeave(object sender, MouseEventArgs e)
15621562

15631563
private void Window_MouseEnter(object sender, MouseEventArgs e)
15641564
{
1565+
if (FirstPopupWindow.IsVisible)
1566+
{
1567+
return;
1568+
}
1569+
15651570
ConfigManager configManager = ConfigManager.Instance;
15661571
if (configManager.TextOnlyVisibleOnHover)
15671572
{
@@ -1573,8 +1578,7 @@ private void Window_MouseEnter(object sender, MouseEventArgs e)
15731578
Background.Opacity = OpacitySlider.Value / 100;
15741579
}
15751580

1576-
if (!FirstPopupWindow.IsVisible
1577-
&& configManager is { Focusable: true, MainWindowFocusOnHover: true })
1581+
if (configManager is { Focusable: true, MainWindowFocusOnHover: true })
15781582
{
15791583
if (configManager.RestoreFocusToPreviouslyActiveWindow)
15801584
{

JL.Windows/GUI/PopupWindow.xaml.cs

+46-5
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,16 @@ public Task LookupOnCharPosition(TextBox textBox, int charPosition, bool enableM
298298
if (configManager.RestoreFocusToPreviouslyActiveWindow && isFirstPopupWindow)
299299
{
300300
nint previousWindowHandle = WinApi.GetActiveWindowHandle();
301-
if (previousWindowHandle != mainWindow.WindowHandle)
301+
if (previousWindowHandle != mainWindow.WindowHandle && previousWindowHandle != WindowHandle)
302302
{
303303
WindowsUtils.LastActiveWindowHandle = previousWindowHandle;
304304
}
305305
}
306306

307-
_ = Activate();
307+
if (!Activate())
308+
{
309+
WinApi.StealFocus(WindowHandle);
310+
}
308311
}
309312

310313
_ = Focus();
@@ -396,7 +399,20 @@ public Task LookupOnSelect(TextBox textBox)
396399

397400
if (configManager.Focusable)
398401
{
399-
_ = Activate();
402+
MainWindow mainWindow = MainWindow.Instance;
403+
if (configManager.RestoreFocusToPreviouslyActiveWindow && this == mainWindow.FirstPopupWindow)
404+
{
405+
nint previousWindowHandle = WinApi.GetActiveWindowHandle();
406+
if (previousWindowHandle != mainWindow.WindowHandle && previousWindowHandle != WindowHandle)
407+
{
408+
WindowsUtils.LastActiveWindowHandle = previousWindowHandle;
409+
}
410+
}
411+
412+
if (!Activate())
413+
{
414+
WinApi.StealFocus(WindowHandle);
415+
}
400416
}
401417

402418
_ = Focus();
@@ -1545,7 +1561,19 @@ public Task HandleHotKey(KeyGesture keyGesture)
15451561

15461562
if (configManager.Focusable)
15471563
{
1548-
_ = Activate();
1564+
if (configManager.RestoreFocusToPreviouslyActiveWindow && this == mainWindow.FirstPopupWindow)
1565+
{
1566+
nint previousWindowHandle = WinApi.GetActiveWindowHandle();
1567+
if (previousWindowHandle != mainWindow.WindowHandle && previousWindowHandle != WindowHandle)
1568+
{
1569+
WindowsUtils.LastActiveWindowHandle = previousWindowHandle;
1570+
}
1571+
}
1572+
1573+
if (!Activate())
1574+
{
1575+
WinApi.StealFocus(WindowHandle);
1576+
}
15491577
}
15501578

15511579
_ = Focus();
@@ -2332,7 +2360,20 @@ public void ShowMiningModeResults()
23322360

23332361
if (configManager.Focusable)
23342362
{
2335-
_ = Activate();
2363+
MainWindow mainWindow = MainWindow.Instance;
2364+
if (configManager.RestoreFocusToPreviouslyActiveWindow && this == mainWindow.FirstPopupWindow)
2365+
{
2366+
nint previousWindowHandle = WinApi.GetActiveWindowHandle();
2367+
if (previousWindowHandle != mainWindow.WindowHandle && previousWindowHandle != WindowHandle)
2368+
{
2369+
WindowsUtils.LastActiveWindowHandle = previousWindowHandle;
2370+
}
2371+
}
2372+
2373+
if (!Activate())
2374+
{
2375+
WinApi.StealFocus(WindowHandle);
2376+
}
23362377
}
23372378

23382379
_ = Focus();

JL.Windows/GUI/PreferencesWindow.xaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
Cursor="Help" TextWrapping="Wrap" VerticalAlignment="Center"
133133
ToolTip="Restores focus to the non-JL window that was previously active if focus was automatically taken by the "Focus on hover" or "Focus on lookup" option,
134134

when no popup is open and the mouse is no longer over the main window.
135-

Note that JL may need to attach itself to another program's thread to do this, which could be seen as an intrusive action.
135+

Note that JL may need to attach itself to currently activated window's thread to do this, which could be seen as an intrusive action.
136136

Keep this in mind before enabling this option." />
137137
<CheckBox x:Name="RestoreFocusToPreviouslyActiveWindowCheckBox" HorizontalAlignment="Right" />
138138
</DockPanel>
@@ -483,7 +483,7 @@
483483
TextWrapping="Wrap" VerticalAlignment="Center"
484484
Cursor="Help"
485485
ToolTip="Attempts to steal focus from the currently active window on mouse hover.
486-
&#10;Note that JL may need to attach itself to another program's thread to do this, which could be seen as an intrusive action.
486+
&#10;Note that JL may need to attach itself to currently activated window's thread to do this, which could be seen as an intrusive action.
487487
&#10;Keep that in mind before enabling this option.
488488
&#10;If &quot;Focusable&quot; is set to false, this option will have no effect." />
489489
<CheckBox x:Name="MainWindowFocusOnHoverCheckBox" HorizontalAlignment="Right" />
@@ -760,6 +760,8 @@
760760
Style="{StaticResource TextBlockDefault}"
761761
Cursor="Help"
762762
ToolTip="Attempt to set focus on the popup window when you mouse over text.
763+
&#10;Note that JL may need to attach itself to currently activated window's thread to do this, which could be seen as an intrusive action.
764+
&#10;Keep this in mind before enabling this option.
763765
&#10;If Focusable is set to false, this option will have no effect." />
764766
<CheckBox x:Name="PopupFocusOnLookupCheckBox" HorizontalAlignment="Right" />
765767
</DockPanel>

JL.Windows/WinApi.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -367,19 +367,23 @@ public static void GiveFocusToWindow(nint windowHandle)
367367
{
368368
uint currentThreadId = GetCurrentThreadId();
369369
uint foregroundThread = GetWindowThreadProcessId(windowHandle, out _);
370-
_ = AttachThreadInput(currentThreadId, foregroundThread, true);
371-
_ = SetForegroundWindow(windowHandle);
372-
_ = AttachThreadInput(currentThreadId, foregroundThread, false);
370+
if (AttachThreadInput(currentThreadId, foregroundThread, true))
371+
{
372+
_ = SetForegroundWindow(windowHandle);
373+
_ = AttachThreadInput(currentThreadId, foregroundThread, false);
374+
}
373375
}
374376
}
375377

376378
public static void StealFocus(nint windowHandle)
377379
{
378380
uint currentThreadId = GetCurrentThreadId();
379381
uint foregroundThread = GetWindowThreadProcessId(GetForegroundWindow(), out _);
380-
_ = AttachThreadInput(currentThreadId, foregroundThread, true);
381-
_ = SetForegroundWindow(windowHandle);
382-
_ = AttachThreadInput(currentThreadId, foregroundThread, false);
382+
if (AttachThreadInput(currentThreadId, foregroundThread, true))
383+
{
384+
_ = SetForegroundWindow(windowHandle);
385+
_ = AttachThreadInput(currentThreadId, foregroundThread, false);
386+
}
383387
}
384388

385389
private nint WndProc(nint hwnd, int msg, nint wParam, nint lParam, ref bool handled)

0 commit comments

Comments
 (0)