Skip to content

Commit 461bcda

Browse files
fix: focus window on show
1 parent bac80d7 commit 461bcda

File tree

5 files changed

+43
-34
lines changed

5 files changed

+43
-34
lines changed

src/shell/contextmenu/hooks.cc

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -116,47 +116,45 @@ void mb_shell::context_menu_hooks::install_SHCreateDefaultContextMenu_hook() {
116116
std::cerr << "Failed to find CreateWindowExW in user32.dll" << std::endl;
117117
}
118118
static auto CreateWindowExWHook = CreateWindowExWFunc->inline_hook();
119-
CreateWindowExWHook->install(+[](DWORD dwExStyle, LPCWSTR lpClassName,
120-
LPCWSTR lpWindowName, DWORD dwStyle, int X,
121-
int Y, int nWidth, int nHeight,
122-
HWND hWndParent, HMENU hMenu,
123-
HINSTANCE hInstance,
124-
LPVOID lpParam) -> HWND {
125-
std::wstring class_name = [&]{
126-
if (!lpClassName) {
119+
CreateWindowExWHook->install(
120+
+[](DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR lpWindowName,
121+
DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent,
122+
HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam) -> HWND {
123+
std::wstring class_name = [&] {
124+
if (!lpClassName) {
127125
return std::wstring(L"");
128-
}
129-
if (blook::Pointer((void*)lpClassName).try_read<int>()) {
126+
}
127+
if (blook::Pointer((void *)lpClassName).try_read<int>()) {
130128
return std::wstring(lpClassName);
131-
} else {
129+
} else {
132130
// read as registered class
133131
wchar_t class_name_buffer[256];
134132
if (GetClassNameW((HWND)lpClassName, class_name_buffer, 256) > 0) {
135-
return std::wstring(class_name_buffer);
133+
return std::wstring(class_name_buffer);
136134
} else {
137-
return std::wstring(L"");
135+
return std::wstring(L"");
138136
}
139-
}
140-
}();
137+
}
138+
}();
141139

142-
bool should_close =
143-
close_next_create_window_exw_window &&
144-
class_name.starts_with(L"HwndWrapper[OneCommander.exe");
140+
bool should_close =
141+
close_next_create_window_exw_window &&
142+
class_name.starts_with(L"HwndWrapper[OneCommander.exe");
145143

146-
if (should_close) {
147-
dwStyle &= ~WS_VISIBLE;
148-
}
144+
if (should_close) {
145+
dwStyle &= ~WS_VISIBLE;
146+
}
149147

150-
auto res = CreateWindowExWHook->call_trampoline<HWND>(
151-
dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight,
152-
hWndParent, hMenu, hInstance, lpParam);
153-
if (res && should_close) {
154-
close_next_create_window_exw_window = false;
155-
PostMessageW(res, WM_CLOSE, 0, 0);
156-
CloseWindow(res);
157-
}
158-
return res;
159-
});
148+
auto res = CreateWindowExWHook->call_trampoline<HWND>(
149+
dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth,
150+
nHeight, hWndParent, hMenu, hInstance, lpParam);
151+
if (res && should_close) {
152+
close_next_create_window_exw_window = false;
153+
PostMessageW(res, WM_CLOSE, 0, 0);
154+
CloseWindow(res);
155+
}
156+
return res;
157+
});
160158

161159
/**
162160
prototype: SHSTDAPI SHCreateDefaultContextMenu(

src/shell/contextmenu/menu_render.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "menu_render.h"
2+
#define GLFW_EXPOSE_NATIVE_WIN32
3+
#include "GLFW/glfw3native.h"
24
#include "Windows.h"
35
#include "menu_widget.h"
46

@@ -18,14 +20,14 @@ menu_render menu_render::create(int x, int y, menu menu) {
1820
return {nullptr, std::nullopt};
1921
}
2022

21-
constexpr int l_pad = 100, t_pad = -1;
23+
constexpr int l_pad = -1, t_pad = -1;
2224
static auto rt = []() {
2325
auto rt = std::make_shared<ui::render_target>();
2426
rt->transparent = true;
2527
rt->no_focus = false;
2628
rt->capture_all_input = true;
2729
rt->decorated = false;
28-
// rt->topmost = true;
30+
rt->topmost = true;
2931
rt->vsync = config::current->context_menu.vsync;
3032

3133
if (config::current->avoid_resize_ui) {
@@ -74,6 +76,7 @@ menu_render menu_render::create(int x, int y, menu menu) {
7476
glfwSwapInterval(config::current->context_menu.vsync ? 1 : 0);
7577

7678
rt->show();
79+
rt->focus();
7780
auto menu_wid = std::make_shared<mouse_menu_widget_main>(
7881
menu,
7982
// convert the x and y to the window coordinates

src/shell/entry.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void main() {
109109
std::filesystem::path exe_path(executable_path);
110110

111111
fix_win11_menu::install();
112-
112+
113113
context_menu_hooks::install_common_hook();
114114
if (exe_path.filename() == "OneCommander.exe") {
115115
context_menu_hooks::install_SHCreateDefaultContextMenu_hook();

src/ui/ui.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,11 @@ void render_target::post_loop_thread_task(std::function<void()> task) {
401401
std::lock_guard lock(loop_thread_tasks_lock);
402402
loop_thread_tasks.push(std::move(task));
403403
}
404+
void render_target::focus() {
405+
if (this->window) {
406+
glfwFocusWindow(this->window);
407+
SetActiveWindow(glfwGetWin32Window(this->window));
408+
SetFocus(glfwGetWin32Window(this->window));
409+
}
410+
}
404411
} // namespace ui

src/ui/ui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ struct render_target {
120120
void close();
121121
void hide();
122122
void show();
123+
void focus();
123124
void hide_as_close();
124125
bool should_loop_stop_hide_as_close = false;
125126
std::optional<std::function<void(bool)>> on_focus_changed;

0 commit comments

Comments
 (0)