Repository: godotengine/godot
File: scene/gui/popup_menu.cpp (C++)

Code before:

		case NOTIFICATION_INTERNAL_PROCESS: {
			// Only used when using operating system windows.
			if (!activated_by_keyboard && !is_embedded() && autohide_areas.size()) {
				Point2 mouse_pos = DisplayServer::get_singleton()->mouse_get_position();
				mouse_pos -= get_position();

				for (const Rect2 &E : autohide_areas) {
					if (!Rect2(Point2(), get_size()).has_point(mouse_pos) && E.has_point(mouse_pos)) {
						_close_pressed();
						return;
					}
				}
			}
		} break;

		case NOTIFICATION_VISIBILITY_CHANGED: {
			if (!is_visible()) {
				if (mouse_over >= 0) {
					mouse_over = -1;
					control->queue_redraw();
				}

				for (int i = 0; i < items.size(); i++) {
					if (items[i].submenu.is_empty()) {
						continue;
					}

					Node *n = get_node(items[i].submenu);
					if (!n) {
						continue;
					}

					PopupMenu *pm = Object::cast_to<PopupMenu>(n);
					if (!pm || !pm->is_visible()) {
						continue;
					}

					pm->hide();
				}

				set_process_internal(false);
			} else {
				if (!is_embedded()) {
					set_process_internal(true);
				}

				// Set margin on the margin container
				margin_container->add_theme_constant_override("margin_left", theme_cache.panel_style->get_margin(Side::SIDE_LEFT));
				margin_container->add_theme_constant_override("margin_top", theme_cache.panel_style->get_margin(Side::SIDE_TOP));
				margin_container->add_theme_constant_override("margin_right", theme_cache.panel_style->get_margin(Side::SIDE_RIGHT));

Review comment (suggestion):
```suggestion
			if (gamepad_event_delay_ms <= 0) {
```

Code after:
		case NOTIFICATION_WM_MOUSE_ENTER: {
			grab_focus();
		} break;

		case NOTIFICATION_WM_MOUSE_EXIT: {
			if (mouse_over >= 0 && (items[mouse_over].submenu.is_empty() || submenu_over != -1)) {
				mouse_over = -1;
				control->queue_redraw();
			}
		} break;

		case NOTIFICATION_POST_POPUP: {
			initial_button_mask = Input::get_singleton()->get_mouse_button_mask();
			during_grabbed_click = (bool)initial_button_mask;
		} break;

		case NOTIFICATION_INTERNAL_PROCESS: {
			Input *input = Input::get_singleton();

			if (input->is_action_just_released("ui_up") || input->is_action_just_released("ui_down")) {
				gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
				set_process_internal(false);
				return;
			}
			gamepad_event_delay_ms -= get_process_delta_time();
			if (gamepad_event_delay_ms <= 0) {
				if (input->is_action_pressed("ui_down")) {
					gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
					int search_from = mouse_over + 1;
					if (search_from >= items.size()) {
						search_from = 0;
					}

					bool match_found = false;
					for (int i = search_from; i < items.size(); i++) {
						if (!items[i].separator && !items[i].disabled) {
							mouse_over = i;
							emit_signal(SNAME("id_focused"), i);
							scroll_to_item(i);
							control->queue_redraw();
							match_found = true;
							break;
						}
					}

					if (!match_found) {
						// If the last item is not selectable, try re-searching from the start.
						for (int i = 0; i < search_from; i++) {
							if (!items[i].separator && !items[i].disabled) {
								mouse_over = i;
								emit_signal(SNAME("id_focused"), i);