Skip to content

Commit

Permalink
[wpigui] Fix PFD file dialogs not closing after window closing (#5530)
Browse files Browse the repository at this point in the history
Also applies samhocevar/portable-file-dialogs#91 to properly retrieve the HWND on Windows.
  • Loading branch information
ohowe1 authored Aug 12, 2023
1 parent 86d7bbc commit e2e1b76
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
25 changes: 17 additions & 8 deletions wpigui/src/main/native/cpp/portable-file-dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,12 @@ HANDLE internal::platform::new_style_context::create()

// dialog implementation

internal::dialog::~dialog() {
if (m_async->m_running) {
kill();
}
}

bool internal::dialog::ready(int timeout /* = default_wait_timeout */) const
{
return m_async->ready(timeout);
Expand Down Expand Up @@ -804,7 +810,7 @@ class internal::file_dialog::Impl {
#if _WIN32
static int CALLBACK bffcallback(HWND hwnd, UINT uMsg, LPARAM, LPARAM pData);
#if PFD_HAS_IFILEDIALOG
std::string select_folder_vista(IFileDialog *ifd, bool force_path);
std::string select_folder_vista(IFileDialog *ifd, bool force_path, HWND active_window);
#endif

std::wstring m_wtitle;
Expand All @@ -831,8 +837,9 @@ internal::file_dialog::file_dialog(type in_type,
}
filter_list += '\0';

HWND active_window = GetActiveWindow();
m_async->start_func([this, in_type, title, default_path, filter_list,
options](int *exit_code) -> std::string
options, active_window](int *exit_code) -> std::string
{
(void)exit_code;
m_impl->m_wtitle = internal::str2wstr(title);
Expand All @@ -858,7 +865,7 @@ internal::file_dialog::file_dialog(type in_type,

// In case CoCreateInstance fails (which it should not), try legacy approach
if (SUCCEEDED(hr))
return m_impl->select_folder_vista(ifd, options & opt::force_path);
return m_impl->select_folder_vista(ifd, options & opt::force_path, active_window);
}
#endif

Expand Down Expand Up @@ -892,7 +899,7 @@ internal::file_dialog::file_dialog(type in_type,
OPENFILENAMEW ofn;
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAMEW);
ofn.hwndOwner = GetActiveWindow();
ofn.hwndOwner = active_window;

ofn.lpstrFilter = wfilter_list.c_str();

Expand Down Expand Up @@ -1169,7 +1176,7 @@ int CALLBACK internal::file_dialog::Impl::bffcallback(HWND hwnd, UINT uMsg,
}

#if PFD_HAS_IFILEDIALOG
std::string internal::file_dialog::Impl::select_folder_vista(IFileDialog *ifd, bool force_path)
std::string internal::file_dialog::Impl::select_folder_vista(IFileDialog *ifd, bool force_path, HWND active_window)
{
std::string result;

Expand Down Expand Up @@ -1206,7 +1213,7 @@ std::string internal::file_dialog::Impl::select_folder_vista(IFileDialog *ifd, b
ifd->SetOptions(FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM);
ifd->SetTitle(m_wtitle.c_str());

hr = ifd->Show(GetActiveWindow());
hr = ifd->Show(active_window);
if (SUCCEEDED(hr))
{
IShellItem* item;
Expand Down Expand Up @@ -1393,13 +1400,15 @@ message::message(std::string const &title,
m_mappings[IDRETRY] = button::retry;
m_mappings[IDIGNORE] = button::ignore;

m_async->start_func([text, title, style](int* exit_code) -> std::string
HWND active_window = GetActiveWindow();

m_async->start_func([text, title, style, active_window](int* exit_code) -> std::string
{
auto wtext = internal::str2wstr(text);
auto wtitle = internal::str2wstr(title);
// Apply new visual style (required for all Windows versions)
internal::platform::new_style_context ctx;
*exit_code = MessageBoxW(GetActiveWindow(), wtext.c_str(), wtitle.c_str(), style);
*exit_code = MessageBoxW(active_window, wtext.c_str(), wtitle.c_str(), style);
return "";
});

Expand Down
3 changes: 2 additions & 1 deletion wpigui/src/main/native/include/portable-file-dialogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class executor;
class dialog : protected settings
{
public:
virtual ~dialog();
bool ready(int timeout = default_wait_timeout) const;
bool kill() const;

Expand All @@ -146,7 +147,7 @@ class dialog : protected settings
std::shared_ptr<executor> m_async;
};

class file_dialog : public dialog
class file_dialog : public internal::dialog
{
protected:
enum type
Expand Down

0 comments on commit e2e1b76

Please sign in to comment.