Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Native File Dialogs] Refocus last focused window on close. #80952

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion platform/linuxbsd/freedesktop_portal_desktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ bool FreeDesktopPortalDesktop::file_chooser_parse_response(DBusMessageIter *p_it
return true;
}

Error FreeDesktopPortalDesktop::file_dialog_show(const String &p_xid, const String &p_title, const String &p_current_directory, const String &p_filename, DisplayServer::FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback) {
Error FreeDesktopPortalDesktop::file_dialog_show(DisplayServer::WindowID p_window_id, const String &p_xid, const String &p_title, const String &p_current_directory, const String &p_filename, DisplayServer::FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback) {
if (unsupported) {
return FAILED;
}
Expand All @@ -277,6 +277,7 @@ Error FreeDesktopPortalDesktop::file_dialog_show(const String &p_xid, const Stri
// Open connection and add signal handler.
FileDialogData fd;
fd.callback = p_callback;
fd.prev_focus = p_window_id;

CryptoCore::RandomGenerator rng;
ERR_FAIL_COND_V_MSG(rng.init(), FAILED, "Failed to initialize random number generator.");
Expand Down Expand Up @@ -416,6 +417,9 @@ void FreeDesktopPortalDesktop::_thread_file_dialog_monitor(void *p_ud) {
Variant *v_args[2] = { &v_status, &v_files };
fd.callback.call_deferredp((const Variant **)&v_args, 2);
}
if (fd.prev_focus != DisplayServer::INVALID_WINDOW_ID) {
callable_mp(DisplayServer::get_singleton(), &DisplayServer::window_move_to_foreground).call_deferred(fd.prev_focus);
}
}
dbus_message_unref(msg);

Expand Down
3 changes: 2 additions & 1 deletion platform/linuxbsd/freedesktop_portal_desktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class FreeDesktopPortalDesktop {

struct FileDialogData {
DBusConnection *connection = nullptr;
DisplayServer::WindowID prev_focus = DisplayServer::INVALID_WINDOW_ID;
Callable callback;
String path;
};
Expand All @@ -73,7 +74,7 @@ class FreeDesktopPortalDesktop {

bool is_supported() { return !unsupported; }

Error file_dialog_show(const String &p_xid, const String &p_title, const String &p_current_directory, const String &p_filename, DisplayServer::FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback);
Error file_dialog_show(DisplayServer::WindowID p_window_id, const String &p_xid, const String &p_title, const String &p_current_directory, const String &p_filename, DisplayServer::FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback);

// Retrieve the system's preferred color scheme.
// 0: No preference or unknown.
Expand Down
4 changes: 2 additions & 2 deletions platform/linuxbsd/x11/display_server_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,14 @@ bool DisplayServerX11::is_dark_mode() const {
}

Error DisplayServerX11::file_dialog_show(const String &p_title, const String &p_current_directory, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback) {
WindowID window_id = _get_focused_window_or_popup();
WindowID window_id = last_focused_window;

if (!windows.has(window_id)) {
window_id = MAIN_WINDOW_ID;
}

String xid = vformat("x11:%x", (uint64_t)windows[window_id].x11_window);
return portal_desktop->file_dialog_show(xid, p_title, p_current_directory, p_filename, p_mode, p_filters, p_callback);
return portal_desktop->file_dialog_show(last_focused_window, xid, p_title, p_current_directory, p_filename, p_mode, p_filters, p_callback);
}

#endif
Expand Down
8 changes: 8 additions & 0 deletions platform/macos/display_server_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,8 @@
}
}

WindowID prev_focus = last_focused_window;

Callable callback = p_callback; // Make a copy for async completion handler.
switch (p_mode) {
case FILE_DIALOG_MODE_SAVE_FILE: {
Expand Down Expand Up @@ -1952,6 +1954,9 @@
callback.callp((const Variant **)&v_args, 2, ret, ce);
}
}
if (prev_focus != INVALID_WINDOW_ID) {
callable_mp(DisplayServer::get_singleton(), &DisplayServer::window_move_to_foreground).call_deferred(prev_focus);
}
}];
} break;
case FILE_DIALOG_MODE_OPEN_ANY:
Expand Down Expand Up @@ -2031,6 +2036,9 @@
callback.callp((const Variant **)&v_args, 2, ret, ce);
}
}
if (prev_focus != INVALID_WINDOW_ID) {
callable_mp(DisplayServer::get_singleton(), &DisplayServer::window_move_to_foreground).call_deferred(prev_focus);
}
}];
} break;
}
Expand Down
5 changes: 5 additions & 0 deletions platform/windows/display_server_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ Error DisplayServerWindows::file_dialog_show(const String &p_title, const String
filters.push_back({ (LPCWSTR)filter_names[i].ptr(), (LPCWSTR)filter_exts[i].ptr() });
}

WindowID prev_focus = last_focused_window;

HRESULT hr = S_OK;
IFileDialog *pfd = nullptr;
if (p_mode == FILE_DIALOG_MODE_SAVE_FILE) {
Expand Down Expand Up @@ -339,6 +341,9 @@ Error DisplayServerWindows::file_dialog_show(const String &p_title, const String
}
}
pfd->Release();
if (prev_focus != INVALID_WINDOW_ID) {
callable_mp(DisplayServer::get_singleton(), &DisplayServer::window_move_to_foreground).call_deferred(prev_focus);
}

return OK;
} else {
Expand Down