Skip to content

Commit 7df34c4

Browse files
committed
[windows] Implement focus & blur methods
1 parent ce6167b commit 7df34c4

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

README-ZH.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ class MainFlutterWindow: NSWindow {
259259

260260
| Method | Description | Linux | macOS | Windows |
261261
| ------------------ | ---------------------------------------------------------------------------------------------------------- | ----- | ----- | ------- |
262-
| `focus` | Focuses on the window. | ✔️ | ✔️ | |
263-
| `blur` | Removes focus from the window. || ✔️ | |
262+
| `focus` | Focuses on the window. | ✔️ | ✔️ | ✔️ |
263+
| `blur` | Removes focus from the window. || ✔️ | ✔️ |
264264
| `show` | Shows and gives focus to the window. | ✔️ | ✔️ | ✔️ |
265265
| `hide` | Hides the window. | ✔️ | ✔️ | ✔️ |
266266
| `isVisible` | Returns `bool` - Whether the window is visible to the user. | ✔️ | ✔️ | ✔️ |

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ class MainFlutterWindow: NSWindow {
259259

260260
| Method | Description | Linux | macOS | Windows |
261261
| ------------------ | ---------------------------------------------------------------------------------------------------------- | ----- | ----- | ------- |
262-
| `focus` | Focuses on the window. | ✔️ | ✔️ | |
263-
| `blur` | Removes focus from the window. || ✔️ | |
262+
| `focus` | Focuses on the window. | ✔️ | ✔️ | ✔️ |
263+
| `blur` | Removes focus from the window. || ✔️ | ✔️ |
264264
| `show` | Shows and gives focus to the window. | ✔️ | ✔️ | ✔️ |
265265
| `hide` | Hides the window. | ✔️ | ✔️ | ✔️ |
266266
| `isVisible` | Returns `bool` - Whether the window is visible to the user. | ✔️ | ✔️ | ✔️ |

windows/window_manager.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,29 @@ void WindowManager::WaitUntilReadyToShow()
134134

135135
void WindowManager::Focus()
136136
{
137-
SetForegroundWindow(GetMainWindow());
137+
HWND hWnd = GetMainWindow();
138+
if (IsMinimized())
139+
{
140+
Restore();
141+
}
142+
143+
::SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
144+
SetForegroundWindow(hWnd);
138145
}
139146

140147
void WindowManager::Blur()
141148
{
149+
HWND hWnd = GetMainWindow();
150+
HWND next_hwnd = ::GetNextWindow(hWnd, GW_HWNDNEXT);
151+
while (next_hwnd)
152+
{
153+
if (::IsWindowVisible(next_hwnd))
154+
{
155+
::SetForegroundWindow(next_hwnd);
156+
return;
157+
}
158+
next_hwnd = ::GetNextWindow(next_hwnd, GW_HWNDNEXT);
159+
}
142160
}
143161

144162
void WindowManager::Show()

windows/window_manager_plugin.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ void WindowManagerPlugin::HandleMethodCall(const flutter::MethodCall<flutter::En
243243
window_manager->Focus();
244244
result->Success(flutter::EncodableValue(true));
245245
}
246+
else if (method_name.compare("blur") == 0)
247+
{
248+
window_manager->Blur();
249+
result->Success(flutter::EncodableValue(true));
250+
}
246251
else if (method_name.compare("show") == 0)
247252
{
248253
window_manager->Show();

0 commit comments

Comments
 (0)