Skip to content

Commit 98c9392

Browse files
feat: popup menu ignore initial mouse state
1 parent 10ea6d4 commit 98c9392

3 files changed

Lines changed: 32 additions & 24 deletions

File tree

src/shell/contextmenu/menu_widget.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,8 @@ BOOL IsCursorVisible() {
705705

706706
mb_shell::mouse_menu_widget_main::mouse_menu_widget_main(menu menu_data,
707707
float x, float y)
708-
: widget(), anchor_x(x), anchor_y(y) {
708+
: widget(), anchor_x(x), anchor_y(y),
709+
ignore_outside_click_until_mouse_release(true) {
709710
menu_wid = std::make_shared<menu_widget>(true);
710711
menu_wid->init_from_data(menu_data);
711712

@@ -731,14 +732,20 @@ void mb_shell::mouse_menu_widget_main::update(ui::update_context &ctx) {
731732
menu_wid->update(ctx);
732733

733734
auto using_touchscreen = !IsCursorVisible();
735+
auto has_pressed_mouse_button = (GetAsyncKeyState(VK_LBUTTON) & 0x8000) ||
736+
(GetAsyncKeyState(VK_RBUTTON) & 0x8000);
737+
738+
if (ignore_outside_click_until_mouse_release && !has_pressed_mouse_button) {
739+
ignore_outside_click_until_mouse_release = false;
740+
}
734741

735742
if (ctx.hovered_widgets->empty()) {
736743
glfwSetWindowAttrib(ctx.rt.window, GLFW_MOUSE_PASSTHROUGH,
737744
using_touchscreen ? GLFW_FALSE : GLFW_TRUE);
738745

739-
if ((ctx.mouse_clicked || ctx.right_mouse_clicked) ||
740-
GetAsyncKeyState(VK_LBUTTON) & 0x8000 ||
741-
GetAsyncKeyState(VK_RBUTTON) & 0x8000) {
746+
if (!ignore_outside_click_until_mouse_release &&
747+
((ctx.mouse_clicked || ctx.right_mouse_clicked) ||
748+
has_pressed_mouse_button)) {
742749
ctx.rt.hide_as_close();
743750
}
744751
} else {

src/shell/contextmenu/menu_widget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ struct mouse_menu_widget_main : public ui::widget {
156156
float anchor_x = 0, anchor_y = 0;
157157
mouse_menu_widget_main(menu menu_data, float x, float y);
158158
bool position_calibrated = false, direction_calibrated = false;
159+
bool ignore_outside_click_until_mouse_release = false;
159160
popup_direction direction;
160161
std::shared_ptr<menu_widget> menu_wid;
161162

src/shell/script/binding_types.cc

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,26 +1796,26 @@ void menu_controller::show_at(int x, int y) {
17961796
if (!valid() || !$menu_detached)
17971797
return;
17981798

1799-
std::promise<void> prom;
1800-
auto fut = prom.get_future();
1801-
std::thread([x, y, this, &prom]() mutable {
1802-
std::ignore = mb_shell::track_popup_menu(
1803-
menu{
1804-
.is_top_level = true,
1805-
},
1806-
x, y,
1807-
[this, &prom](menu_render &render) {
1808-
auto menu_new =
1809-
render.rt->root->get_child<mouse_menu_widget_main>()
1810-
->menu_wid;
1811-
menu_new->children = $menu_detached->children;
1812-
menu_new->children = $menu_detached->children;
1813-
$menu = menu_new;
1814-
$menu_detached = nullptr;
1815-
prom.set_value();
1816-
},
1817-
false);
1818-
}).detach();
1799+
std::promise<void> prom;
1800+
auto fut = prom.get_future();
1801+
std::thread([x, y, this, &prom]() mutable {
1802+
std::ignore = mb_shell::track_popup_menu(
1803+
menu{
1804+
.is_top_level = true,
1805+
},
1806+
x, y,
1807+
[this, &prom](menu_render &render) {
1808+
auto menu_new =
1809+
render.rt->root->get_child<mouse_menu_widget_main>()
1810+
->menu_wid;
1811+
menu_new->children = $menu_detached->children;
1812+
menu_new->children = $menu_detached->children;
1813+
$menu = menu_new;
1814+
$menu_detached = nullptr;
1815+
prom.set_value();
1816+
},
1817+
false);
1818+
}).detach();
18191819
fut.wait();
18201820
}
18211821
void menu_controller::show_at_cursor() {

0 commit comments

Comments
 (0)