{"action":"created","issue":{"url":"https://api.github.com/repos/ocornut/imgui/issues/603","repository_url":"https://api.github.com/repos/ocornut/imgui","labels_url":"https://api.github.com/repos/ocornut/imgui/issues/603/labels{/name}","comments_url":"https://api.github.com/repos/ocornut/imgui/issues/603/comments","events_url":"https://api.github.com/repos/ocornut/imgui/issues/603/events","html_url":"https://github.com/ocornut/imgui/issues/603","id":150243330,"number":603,"title":"Detecting when popup goes from open to closed?","user":{"login":"Roflraging","id":3121968,"avatar_url":"https://avatars.githubusercontent.com/u/3121968?v=3","gravatar_id":"","url":"https://api.github.com/users/Roflraging","html_url":"https://github.com/Roflraging","followers_url":"https://api.github.com/users/Roflraging/followers","following_url":"https://api.github.com/users/Roflraging/following{/other_user}","gists_url":"https://api.github.com/users/Roflraging/gists{/gist_id}","starred_url":"https://api.github.com/users/Roflraging/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Roflraging/subscriptions","organizations_url":"https://api.github.com/users/Roflraging/orgs","repos_url":"https://api.github.com/users/Roflraging/repos","events_url":"https://api.github.com/users/Roflraging/events{/privacy}","received_events_url":"https://api.github.com/users/Roflraging/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-04-22T02:03:14Z","updated_at":"2016-04-22T06:43:31Z","closed_at":null,"body":"Sorry if I am missing something that is very obvious here, but it doesn't seem like I can detect when a popup gets closed through the popup API.\r\n\r\nhttps://github.com/ocornut/imgui/blob/7ce6c18bbe662e9c6a80dd6c702952152a166e33/imgui.h#L353-L361\r\n\r\n```\r\n    // Popups\r\n    IMGUI_API void          OpenPopup(const char* str_id);\r\n    IMGUI_API bool          BeginPopup(const char* str_id);\r\n    IMGUI_API bool          BeginPopupModal(const char* name, bool* p_opened = NULL, ImGuiWindowFlags extra_flags = 0);\r\n    IMGUI_API bool          BeginPopupContextItem(const char* str_id, int mouse_button = 1);\r\n    IMGUI_API bool          BeginPopupContextWindow(bool also_over_items = true, const char* str_id = NULL, int mouse_button = 1);\r\n    IMGUI_API bool          BeginPopupContextVoid(const char* str_id = NULL, int mouse_button = 1);\r\n    IMGUI_API void          EndPopup();\r\n    IMGUI_API void          CloseCurrentPopup();\r\n```\r\n\r\nI have code that allocates memory on the start of a popup to do work inside the popup.  I'm not sure how to detect when the popup is closed so that I can be sure to free the memory.\r\n\r\n```\r\n    if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(1))\r\n    {\r\n        ImGui::OpenPopup(\"batch edit\");\r\n        BatchEditBegin(gui, *desc, desc);\r\n    }\r\n\r\n    if (ImGui::BeginPopup(\"batch edit\"))\r\n    {\r\n        BatchEditUpdate(gui, desc);\r\n        ImGui::EndPopup();\r\n    }\r\n\r\n    // How do I know when to call BatchEditEnd() which should be called when the popup closes?\r\n```\r\n\r\nThe best I've come up with is:\r\n\r\n```\r\n    if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(1))\r\n    {\r\n        ImGui::OpenPopup(\"batch edit\");\r\n        BatchEditBegin(gui, *desc, desc);\r\n    }\r\n\r\n    if (ImGui::BeginPopup(\"batch edit\"))\r\n    {\r\n        BatchEditUpdate(gui, desc);\r\n\r\n        if (!ImGui::IsMouseHoveringWindow() && ImGui::IsMouseClicked(0))\r\n        {\r\n            BatchEditEnd(gui);\r\n        }\r\n\r\n        ImGui::EndPopup();\r\n    }\r\n```\r\n\r\nThis seems to work for me, but I wasn't sure if this is the best way to go about doing this?  My specific use case won't allow for Selectables() to close the popup, so I don't think I need to be worried about this here?\r\n\r\n-Dale Kim"},"comment":{"url":"https://api.github.com/repos/ocornut/imgui/issues/comments/213289237","html_url":"https://github.com/ocornut/imgui/issues/603#issuecomment-213289237","issue_url":"https://api.github.com/repos/ocornut/imgui/issues/603","id":213289237,"user":{"login":"ocornut","id":8225057,"avatar_url":"https://avatars.githubusercontent.com/u/8225057?v=3","gravatar_id":"","url":"https://api.github.com/users/ocornut","html_url":"https://github.com/ocornut","followers_url":"https://api.github.com/users/ocornut/followers","following_url":"https://api.github.com/users/ocornut/following{/other_user}","gists_url":"https://api.github.com/users/ocornut/gists{/gist_id}","starred_url":"https://api.github.com/users/ocornut/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ocornut/subscriptions","organizations_url":"https://api.github.com/users/ocornut/orgs","repos_url":"https://api.github.com/users/ocornut/repos","events_url":"https://api.github.com/users/ocornut/events{/privacy}","received_events_url":"https://api.github.com/users/ocornut/received_events","type":"User","site_admin":false},"created_at":"2016-04-22T06:43:31Z","updated_at":"2016-04-22T06:43:31Z","body":"One way would be to set a flag at the time of calling OpenPopup(), when BeginPopup() return false and that flag was set then you've noticed your popup closing.\r\n\r\nThat would still rely on you running the code, so you need to pay attention to that (perhaps if the wnidow/tool stops running you may need to cleanup is that flag was set).\r\n\r\nI could perhaps add a helper that would tell you if a window was just closed but similarly you'd have to run that code to cleanup, so storing a state flag may be easier/better for now?\r\n"}}