popup.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /**************************************************************************/
  2. /* popup.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "popup.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "core/config/project_settings.h"
  33. #endif
  34. #include "scene/gui/panel.h"
  35. #include "scene/resources/style_box_flat.h"
  36. #include "scene/theme/theme_db.h"
  37. void Popup::_input_from_window(const Ref<InputEvent> &p_event) {
  38. if ((ac_popup || get_flag(FLAG_POPUP)) && p_event->is_action_pressed(SNAME("ui_cancel"), false, true)) {
  39. hide_reason = HIDE_REASON_CANCELED; // ESC pressed, mark as canceled unconditionally.
  40. _close_pressed();
  41. }
  42. Window::_input_from_window(p_event);
  43. }
  44. void Popup::_initialize_visible_parents() {
  45. if (is_embedded()) {
  46. visible_parents.clear();
  47. Window *parent_window = this;
  48. while (parent_window) {
  49. parent_window = parent_window->get_parent_visible_window();
  50. if (parent_window) {
  51. visible_parents.push_back(parent_window);
  52. parent_window->connect(SceneStringName(focus_entered), callable_mp(this, &Popup::_parent_focused));
  53. parent_window->connect(SceneStringName(tree_exited), callable_mp(this, &Popup::_deinitialize_visible_parents));
  54. }
  55. }
  56. }
  57. }
  58. void Popup::_deinitialize_visible_parents() {
  59. if (is_embedded()) {
  60. for (Window *parent_window : visible_parents) {
  61. parent_window->disconnect(SceneStringName(focus_entered), callable_mp(this, &Popup::_parent_focused));
  62. parent_window->disconnect(SceneStringName(tree_exited), callable_mp(this, &Popup::_deinitialize_visible_parents));
  63. }
  64. visible_parents.clear();
  65. }
  66. }
  67. void Popup::_notification(int p_what) {
  68. switch (p_what) {
  69. case NOTIFICATION_VISIBILITY_CHANGED: {
  70. if (!is_in_edited_scene_root()) {
  71. if (is_visible()) {
  72. _initialize_visible_parents();
  73. } else {
  74. _deinitialize_visible_parents();
  75. if (hide_reason == HIDE_REASON_NONE) {
  76. hide_reason = HIDE_REASON_CANCELED;
  77. }
  78. emit_signal(SNAME("popup_hide"));
  79. popped_up = false;
  80. }
  81. }
  82. } break;
  83. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  84. if (!is_in_edited_scene_root()) {
  85. if (has_focus()) {
  86. popped_up = true;
  87. hide_reason = HIDE_REASON_NONE;
  88. }
  89. }
  90. } break;
  91. case NOTIFICATION_UNPARENTED:
  92. case NOTIFICATION_EXIT_TREE: {
  93. if (!is_in_edited_scene_root()) {
  94. _deinitialize_visible_parents();
  95. }
  96. } break;
  97. case NOTIFICATION_WM_CLOSE_REQUEST: {
  98. if (!is_in_edited_scene_root()) {
  99. if (hide_reason == HIDE_REASON_NONE) {
  100. hide_reason = HIDE_REASON_UNFOCUSED;
  101. }
  102. _close_pressed();
  103. }
  104. } break;
  105. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  106. if (!is_in_edited_scene_root() && (get_flag(FLAG_POPUP) || ac_popup)) {
  107. if (hide_reason == HIDE_REASON_NONE) {
  108. hide_reason = HIDE_REASON_UNFOCUSED;
  109. }
  110. _close_pressed();
  111. }
  112. } break;
  113. }
  114. }
  115. void Popup::_parent_focused() {
  116. if (popped_up && (get_flag(FLAG_POPUP) || ac_popup)) {
  117. if (hide_reason == HIDE_REASON_NONE) {
  118. hide_reason = HIDE_REASON_UNFOCUSED;
  119. }
  120. _close_pressed();
  121. }
  122. }
  123. void Popup::_close_pressed() {
  124. popped_up = false;
  125. _deinitialize_visible_parents();
  126. callable_mp((Window *)this, &Window::hide).call_deferred();
  127. }
  128. void Popup::_post_popup() {
  129. Window::_post_popup();
  130. popped_up = true;
  131. }
  132. void Popup::_validate_property(PropertyInfo &p_property) const {
  133. if (
  134. p_property.name == "transient" ||
  135. p_property.name == "exclusive" ||
  136. p_property.name == "popup_window" ||
  137. p_property.name == "unfocusable") {
  138. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  139. }
  140. }
  141. Rect2i Popup::_popup_adjust_rect() const {
  142. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  143. Rect2i parent_rect = get_usable_parent_rect();
  144. if (parent_rect == Rect2i()) {
  145. return Rect2i();
  146. }
  147. Rect2i current(get_position(), get_size());
  148. if (!is_embedded() && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS)) {
  149. // We're fine as is, the Display Server will take care of that for us.
  150. return current;
  151. }
  152. if (current.position.x + current.size.x > parent_rect.position.x + parent_rect.size.x) {
  153. current.position.x = parent_rect.position.x + parent_rect.size.x - current.size.x;
  154. }
  155. if (current.position.x < parent_rect.position.x) {
  156. current.position.x = parent_rect.position.x;
  157. }
  158. if (current.position.y + current.size.y > parent_rect.position.y + parent_rect.size.y) {
  159. current.position.y = parent_rect.position.y + parent_rect.size.y - current.size.y;
  160. }
  161. if (current.position.y < parent_rect.position.y) {
  162. current.position.y = parent_rect.position.y;
  163. }
  164. if (current.size.y > parent_rect.size.y) {
  165. current.size.y = parent_rect.size.y;
  166. }
  167. if (current.size.x > parent_rect.size.x) {
  168. current.size.x = parent_rect.size.x;
  169. }
  170. // Early out if max size not set.
  171. Size2i popup_max_size = get_max_size();
  172. if (popup_max_size <= Size2()) {
  173. return current;
  174. }
  175. if (current.size.x > popup_max_size.x) {
  176. current.size.x = popup_max_size.x;
  177. }
  178. if (current.size.y > popup_max_size.y) {
  179. current.size.y = popup_max_size.y;
  180. }
  181. return current;
  182. }
  183. void Popup::_bind_methods() {
  184. ADD_SIGNAL(MethodInfo("popup_hide"));
  185. }
  186. Popup::Popup() {
  187. set_wrap_controls(true);
  188. set_visible(false);
  189. set_transient(true);
  190. set_flag(FLAG_BORDERLESS, true);
  191. set_flag(FLAG_RESIZE_DISABLED, true);
  192. set_flag(FLAG_MINIMIZE_DISABLED, true);
  193. set_flag(FLAG_MAXIMIZE_DISABLED, true);
  194. set_flag(FLAG_POPUP, true);
  195. set_flag(FLAG_POPUP_WM_HINT, true);
  196. }
  197. Popup::~Popup() {
  198. }
  199. #ifdef TOOLS_ENABLED
  200. PackedStringArray PopupPanel::get_configuration_warnings() const {
  201. PackedStringArray warnings = Popup::get_configuration_warnings();
  202. if (!DisplayServer::get_singleton()->is_window_transparency_available() && !GLOBAL_GET_CACHED(bool, "display/window/subwindows/embed_subwindows")) {
  203. Ref<StyleBoxFlat> sb = theme_cache.panel_style;
  204. if (sb.is_valid() && (sb->get_shadow_size() > 0 || sb->get_corner_radius(CORNER_TOP_LEFT) > 0 || sb->get_corner_radius(CORNER_TOP_RIGHT) > 0 || sb->get_corner_radius(CORNER_BOTTOM_LEFT) > 0 || sb->get_corner_radius(CORNER_BOTTOM_RIGHT) > 0)) {
  205. warnings.push_back(RTR("The current theme style has shadows and/or rounded corners for popups, but those won't display correctly if \"display/window/per_pixel_transparency/allowed\" isn't enabled in the Project Settings, nor if it isn't supported."));
  206. }
  207. }
  208. return warnings;
  209. }
  210. #endif
  211. void PopupPanel::_input_from_window(const Ref<InputEvent> &p_event) {
  212. if (p_event.is_valid()) {
  213. if (!get_flag(FLAG_POPUP)) {
  214. return;
  215. }
  216. Ref<InputEventMouseButton> b = p_event;
  217. // Hide it if the shadows have been clicked.
  218. if (b.is_valid() && b->is_pressed() && b->get_button_index() == MouseButton::LEFT) {
  219. Rect2 panel_area = panel->get_global_rect();
  220. float win_scale = get_content_scale_factor();
  221. panel_area.position *= win_scale;
  222. panel_area.size *= win_scale;
  223. if (!panel_area.has_point(b->get_position())) {
  224. _close_pressed();
  225. }
  226. }
  227. } else {
  228. WARN_PRINT_ONCE("PopupPanel has received an invalid InputEvent. Consider filtering out invalid events.");
  229. }
  230. Popup::_input_from_window(p_event);
  231. }
  232. Size2 PopupPanel::_get_contents_minimum_size() const {
  233. Size2 ms;
  234. for (int i = 0; i < get_child_count(); i++) {
  235. Control *c = Object::cast_to<Control>(get_child(i));
  236. if (!c || c == panel) {
  237. continue;
  238. }
  239. if (c->is_set_as_top_level()) {
  240. continue;
  241. }
  242. Size2 cms = c->get_combined_minimum_size();
  243. ms = cms.max(ms);
  244. }
  245. // Take shadows into account.
  246. ms.width += panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT);
  247. ms.height += panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM);
  248. return ms + theme_cache.panel_style->get_minimum_size();
  249. }
  250. Rect2i PopupPanel::_popup_adjust_rect() const {
  251. Rect2i current = Popup::_popup_adjust_rect();
  252. if (current == Rect2i()) {
  253. return current;
  254. }
  255. pre_popup_rect = current;
  256. _update_shadow_offsets();
  257. _update_child_rects();
  258. if (is_layout_rtl()) {
  259. current.position -= Vector2(-panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP)) * get_content_scale_factor();
  260. } else {
  261. current.position -= Vector2(panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP)) * get_content_scale_factor();
  262. }
  263. current.size += Vector2(panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM)) * get_content_scale_factor();
  264. return current;
  265. }
  266. void PopupPanel::_update_shadow_offsets() const {
  267. if (!DisplayServer::get_singleton()->is_window_transparency_available() && !is_embedded()) {
  268. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  269. return;
  270. }
  271. const Ref<StyleBoxFlat> sb = theme_cache.panel_style;
  272. if (sb.is_null()) {
  273. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  274. return;
  275. }
  276. const int shadow_size = sb->get_shadow_size();
  277. if (shadow_size == 0) {
  278. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  279. return;
  280. }
  281. // Offset the background panel so it leaves space inside the window for the shadows to be drawn.
  282. const Point2 shadow_offset = sb->get_shadow_offset();
  283. if (is_layout_rtl()) {
  284. panel->set_offset(SIDE_LEFT, MAX(0, shadow_size + shadow_offset.x));
  285. panel->set_offset(SIDE_RIGHT, MIN(0, -shadow_size + shadow_offset.x));
  286. } else {
  287. panel->set_offset(SIDE_LEFT, MAX(0, shadow_size - shadow_offset.x));
  288. panel->set_offset(SIDE_RIGHT, MIN(0, -shadow_size - shadow_offset.x));
  289. }
  290. panel->set_offset(SIDE_TOP, MAX(0, shadow_size - shadow_offset.y));
  291. panel->set_offset(SIDE_BOTTOM, MIN(0, -shadow_size - shadow_offset.y));
  292. }
  293. void PopupPanel::_update_child_rects() const {
  294. Vector2 cpos(theme_cache.panel_style->get_offset());
  295. cpos += Vector2(is_layout_rtl() ? -panel->get_offset(SIDE_RIGHT) : panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP));
  296. Vector2 csize = Vector2(get_size()) / get_content_scale_factor() - theme_cache.panel_style->get_minimum_size();
  297. // Trim shadows.
  298. csize.width -= panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT);
  299. csize.height -= panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM);
  300. for (int i = 0; i < get_child_count(); i++) {
  301. Control *c = Object::cast_to<Control>(get_child(i));
  302. if (!c || c == panel) {
  303. continue;
  304. }
  305. if (c->is_set_as_top_level()) {
  306. continue;
  307. }
  308. c->set_position(cpos);
  309. c->set_size(csize);
  310. }
  311. }
  312. void PopupPanel::_notification(int p_what) {
  313. switch (p_what) {
  314. case NOTIFICATION_THEME_CHANGED: {
  315. panel->add_theme_style_override(SceneStringName(panel), theme_cache.panel_style);
  316. if (is_visible()) {
  317. _update_shadow_offsets();
  318. }
  319. _update_child_rects();
  320. #ifdef TOOLS_ENABLED
  321. update_configuration_warnings();
  322. #endif
  323. } break;
  324. case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  325. if (is_visible()) {
  326. _update_shadow_offsets();
  327. }
  328. } break;
  329. case NOTIFICATION_VISIBILITY_CHANGED: {
  330. if (!is_visible()) {
  331. // Remove the extra space used by the shadows, so they can be ignored when the popup is hidden.
  332. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  333. _update_child_rects();
  334. if (pre_popup_rect != Rect2i()) {
  335. set_position(pre_popup_rect.position);
  336. set_size(pre_popup_rect.size);
  337. pre_popup_rect = Rect2i();
  338. }
  339. } else if (pre_popup_rect == Rect2i()) {
  340. // The popup was made visible directly (without `popup_*()`), so just update the offsets without touching the rect.
  341. _update_shadow_offsets();
  342. _update_child_rects();
  343. }
  344. } break;
  345. case NOTIFICATION_WM_SIZE_CHANGED: {
  346. _update_child_rects();
  347. if (is_visible()) {
  348. const Vector2i offsets = Vector2i(panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM));
  349. // Check if the size actually changed.
  350. if (pre_popup_rect.size + offsets != get_size()) {
  351. // Play safe, and stick with the new size.
  352. pre_popup_rect = Rect2i();
  353. }
  354. }
  355. } break;
  356. }
  357. }
  358. void PopupPanel::_bind_methods() {
  359. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupPanel, panel_style, "panel");
  360. }
  361. PopupPanel::PopupPanel() {
  362. set_flag(FLAG_TRANSPARENT, true);
  363. panel = memnew(Panel);
  364. panel->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  365. add_child(panel, false, INTERNAL_MODE_FRONT);
  366. #ifdef TOOLS_ENABLED
  367. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp((Node *)this, &Node::update_configuration_warnings));
  368. #endif
  369. }