зеркало из https://github.com/electron/electron.git
Merge pull request #13463 from electron/remove_wtl
refactor: Using win32 file open api instead of WTL
This commit is contained in:
Коммит
3d4487ccfd
1
BUILD.gn
1
BUILD.gn
|
@ -266,7 +266,6 @@ static_library("electron_lib") {
|
|||
}
|
||||
if (is_win) {
|
||||
deps += [
|
||||
"//third_party/wtl",
|
||||
"//third_party/breakpad:client",
|
||||
]
|
||||
}
|
||||
|
|
|
@ -6,12 +6,10 @@
|
|||
|
||||
#include <windows.h> // windows.h must be included first
|
||||
|
||||
#include <atlbase.h> // atlbase.h must be included before atlapp.h
|
||||
#include <atlbase.h> // atlbase.h for CComPtr
|
||||
|
||||
#include <atlapp.h>
|
||||
#include <atldlgs.h>
|
||||
#include <commdlg.h>
|
||||
#include <shlobj.h>
|
||||
#include <shobjidl.h>
|
||||
|
||||
#include "atom/browser/native_window_views.h"
|
||||
#include "atom/browser/unresponsive_suppressor.h"
|
||||
|
@ -24,9 +22,6 @@
|
|||
#include "base/threading/thread_task_runner_handle.h"
|
||||
#include "base/win/registry.h"
|
||||
|
||||
using WTL::CShellFileOpenDialog;
|
||||
using WTL::CShellFileSaveDialog;
|
||||
|
||||
namespace file_dialog {
|
||||
|
||||
DialogSettings::DialogSettings() = default;
|
||||
|
@ -69,87 +64,6 @@ void ConvertFilters(const Filters& filters,
|
|||
}
|
||||
}
|
||||
|
||||
// Generic class to delegate common open/save dialog's behaviours, users need to
|
||||
// call interface methods via GetPtr().
|
||||
template <typename T>
|
||||
class FileDialog {
|
||||
public:
|
||||
FileDialog(const DialogSettings& settings, int options) {
|
||||
std::wstring file_part;
|
||||
if (!IsDirectory(settings.default_path))
|
||||
file_part = settings.default_path.BaseName().value();
|
||||
|
||||
std::vector<std::wstring> buffer;
|
||||
std::vector<COMDLG_FILTERSPEC> filterspec;
|
||||
ConvertFilters(settings.filters, &buffer, &filterspec);
|
||||
|
||||
dialog_.reset(new T(file_part.c_str(), options, NULL, filterspec.data(),
|
||||
filterspec.size()));
|
||||
|
||||
if (!settings.title.empty())
|
||||
GetPtr()->SetTitle(base::UTF8ToUTF16(settings.title).c_str());
|
||||
|
||||
if (!settings.button_label.empty())
|
||||
GetPtr()->SetOkButtonLabel(
|
||||
base::UTF8ToUTF16(settings.button_label).c_str());
|
||||
|
||||
// By default, *.* will be added to the file name if file type is "*.*". In
|
||||
// Electron, we disable it to make a better experience.
|
||||
//
|
||||
// From MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/
|
||||
// bb775970(v=vs.85).aspx
|
||||
//
|
||||
// If SetDefaultExtension is not called, the dialog will not update
|
||||
// automatically when user choose a new file type in the file dialog.
|
||||
//
|
||||
// We set file extension to the first none-wildcard extension to make
|
||||
// sure the dialog will update file extension automatically.
|
||||
for (size_t i = 0; i < filterspec.size(); ++i) {
|
||||
if (std::wstring(filterspec[i].pszSpec) != L"*.*") {
|
||||
// SetFileTypeIndex is regarded as one-based index.
|
||||
GetPtr()->SetFileTypeIndex(i + 1);
|
||||
GetPtr()->SetDefaultExtension(filterspec[i].pszSpec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (settings.default_path.IsAbsolute()) {
|
||||
SetDefaultFolder(settings.default_path);
|
||||
}
|
||||
}
|
||||
|
||||
bool Show(atom::NativeWindow* parent_window) {
|
||||
atom::UnresponsiveSuppressor suppressor;
|
||||
HWND window = parent_window
|
||||
? static_cast<atom::NativeWindowViews*>(parent_window)
|
||||
->GetAcceleratedWidget()
|
||||
: NULL;
|
||||
return dialog_->DoModal(window) == IDOK;
|
||||
}
|
||||
|
||||
T* GetDialog() { return dialog_.get(); }
|
||||
|
||||
IFileDialog* GetPtr() const { return dialog_->GetPtr(); }
|
||||
|
||||
private:
|
||||
// Set up the initial directory for the dialog.
|
||||
void SetDefaultFolder(const base::FilePath file_path) {
|
||||
std::wstring directory = IsDirectory(file_path)
|
||||
? file_path.value()
|
||||
: file_path.DirName().value();
|
||||
|
||||
ATL::CComPtr<IShellItem> folder_item;
|
||||
HRESULT hr = SHCreateItemFromParsingName(directory.c_str(), NULL,
|
||||
IID_PPV_ARGS(&folder_item));
|
||||
if (SUCCEEDED(hr))
|
||||
GetPtr()->SetFolder(folder_item);
|
||||
}
|
||||
|
||||
std::unique_ptr<T> dialog_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(FileDialog);
|
||||
};
|
||||
|
||||
struct RunState {
|
||||
base::Thread* dialog_thread;
|
||||
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner;
|
||||
|
@ -189,9 +103,110 @@ void RunSaveDialogInNewThread(const RunState& run_state,
|
|||
|
||||
} // namespace
|
||||
|
||||
static HRESULT GetFileNameFromShellItem(IShellItem* pShellItem,
|
||||
SIGDN type,
|
||||
LPWSTR lpstr,
|
||||
size_t cchLength) {
|
||||
assert(pShellItem != NULL);
|
||||
|
||||
LPWSTR lpstrName = NULL;
|
||||
HRESULT hRet = pShellItem->GetDisplayName(type, &lpstrName);
|
||||
|
||||
if (SUCCEEDED(hRet)) {
|
||||
if (wcslen(lpstrName) < cchLength) {
|
||||
wcscpy_s(lpstr, cchLength, lpstrName);
|
||||
} else {
|
||||
NOTREACHED();
|
||||
hRet = DISP_E_BUFFERTOOSMALL;
|
||||
}
|
||||
|
||||
::CoTaskMemFree(lpstrName);
|
||||
}
|
||||
|
||||
return hRet;
|
||||
}
|
||||
|
||||
static void SetDefaultFolder(IFileDialog* dialog,
|
||||
const base::FilePath file_path) {
|
||||
std::wstring directory =
|
||||
IsDirectory(file_path) ? file_path.value() : file_path.DirName().value();
|
||||
|
||||
ATL::CComPtr<IShellItem> folder_item;
|
||||
HRESULT hr = SHCreateItemFromParsingName(directory.c_str(), NULL,
|
||||
IID_PPV_ARGS(&folder_item));
|
||||
if (SUCCEEDED(hr))
|
||||
dialog->SetFolder(folder_item);
|
||||
}
|
||||
|
||||
static HRESULT ShowFileDialog(IFileDialog* dialog,
|
||||
const DialogSettings& settings) {
|
||||
atom::UnresponsiveSuppressor suppressor;
|
||||
HWND parent_window =
|
||||
settings.parent_window
|
||||
? static_cast<atom::NativeWindowViews*>(settings.parent_window)
|
||||
->GetAcceleratedWidget()
|
||||
: NULL;
|
||||
|
||||
return dialog->Show(parent_window);
|
||||
}
|
||||
|
||||
static void ApplySettings(IFileDialog* dialog,
|
||||
const DialogSettings& settings) {
|
||||
std::wstring file_part;
|
||||
|
||||
if (!IsDirectory(settings.default_path))
|
||||
file_part = settings.default_path.BaseName().value();
|
||||
|
||||
dialog->SetFileName(file_part.c_str());
|
||||
|
||||
if (!settings.title.empty())
|
||||
dialog->SetTitle(base::UTF8ToUTF16(settings.title).c_str());
|
||||
|
||||
if (!settings.button_label.empty())
|
||||
dialog->SetOkButtonLabel(base::UTF8ToUTF16(settings.button_label).c_str());
|
||||
|
||||
std::vector<std::wstring> buffer;
|
||||
std::vector<COMDLG_FILTERSPEC> filterspec;
|
||||
ConvertFilters(settings.filters, &buffer, &filterspec);
|
||||
|
||||
if (!filterspec.empty()) {
|
||||
dialog->SetFileTypes(filterspec.size(), filterspec.data());
|
||||
}
|
||||
|
||||
// By default, *.* will be added to the file name if file type is "*.*". In
|
||||
// Electron, we disable it to make a better experience.
|
||||
//
|
||||
// From MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/
|
||||
// bb775970(v=vs.85).aspx
|
||||
//
|
||||
// If SetDefaultExtension is not called, the dialog will not update
|
||||
// automatically when user choose a new file type in the file dialog.
|
||||
//
|
||||
// We set file extension to the first none-wildcard extension to make
|
||||
// sure the dialog will update file extension automatically.
|
||||
for (size_t i = 0; i < filterspec.size(); ++i) {
|
||||
if (std::wstring(filterspec[i].pszSpec) != L"*.*") {
|
||||
// SetFileTypeIndex is regarded as one-based index.
|
||||
dialog->SetFileTypeIndex(i + 1);
|
||||
dialog->SetDefaultExtension(filterspec[i].pszSpec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (settings.default_path.IsAbsolute()) {
|
||||
SetDefaultFolder(dialog, settings.default_path);
|
||||
}
|
||||
}
|
||||
|
||||
bool ShowOpenDialog(const DialogSettings& settings,
|
||||
std::vector<base::FilePath>* paths) {
|
||||
int options = FOS_FORCEFILESYSTEM | FOS_FILEMUSTEXIST;
|
||||
ATL::CComPtr<IFileOpenDialog> file_open_dialog;
|
||||
HRESULT hr = file_open_dialog.CoCreateInstance(CLSID_FileOpenDialog);
|
||||
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
DWORD options = FOS_FORCEFILESYSTEM | FOS_FILEMUSTEXIST;
|
||||
if (settings.properties & FILE_DIALOG_OPEN_DIRECTORY)
|
||||
options |= FOS_PICKFOLDERS;
|
||||
if (settings.properties & FILE_DIALOG_MULTI_SELECTIONS)
|
||||
|
@ -200,14 +215,15 @@ bool ShowOpenDialog(const DialogSettings& settings,
|
|||
options |= FOS_FORCESHOWHIDDEN;
|
||||
if (settings.properties & FILE_DIALOG_PROMPT_TO_CREATE)
|
||||
options |= FOS_CREATEPROMPT;
|
||||
file_open_dialog->SetOptions(options);
|
||||
|
||||
FileDialog<CShellFileOpenDialog> open_dialog(settings, options);
|
||||
if (!open_dialog.Show(settings.parent_window))
|
||||
ApplySettings(file_open_dialog, settings);
|
||||
hr = ShowFileDialog(file_open_dialog, settings);
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
ATL::CComPtr<IShellItemArray> items;
|
||||
HRESULT hr =
|
||||
static_cast<IFileOpenDialog*>(open_dialog.GetPtr())->GetResults(&items);
|
||||
hr = file_open_dialog->GetResults(&items);
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
|
@ -224,8 +240,8 @@ bool ShowOpenDialog(const DialogSettings& settings,
|
|||
return false;
|
||||
|
||||
wchar_t file_name[MAX_PATH];
|
||||
hr = CShellFileOpenDialog::GetFileNameFromShellItem(item, SIGDN_FILESYSPATH,
|
||||
file_name, MAX_PATH);
|
||||
hr = GetFileNameFromShellItem(item, SIGDN_FILESYSPATH, file_name, MAX_PATH);
|
||||
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
|
@ -249,17 +265,32 @@ void ShowOpenDialog(const DialogSettings& settings,
|
|||
}
|
||||
|
||||
bool ShowSaveDialog(const DialogSettings& settings, base::FilePath* path) {
|
||||
FileDialog<CShellFileSaveDialog> save_dialog(
|
||||
settings, FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_OVERWRITEPROMPT);
|
||||
if (!save_dialog.Show(settings.parent_window))
|
||||
return false;
|
||||
|
||||
wchar_t buffer[MAX_PATH];
|
||||
HRESULT hr = save_dialog.GetDialog()->GetFilePath(buffer, MAX_PATH);
|
||||
ATL::CComPtr<IFileSaveDialog> file_save_dialog;
|
||||
HRESULT hr = file_save_dialog.CoCreateInstance(CLSID_FileSaveDialog);
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
*path = base::FilePath(buffer);
|
||||
file_save_dialog->SetOptions(FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST |
|
||||
FOS_OVERWRITEPROMPT);
|
||||
ApplySettings(file_save_dialog, settings);
|
||||
hr = ShowFileDialog(file_save_dialog, settings);
|
||||
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
CComPtr<IShellItem> pItem;
|
||||
hr = file_save_dialog->GetResult(&pItem);
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
PWSTR result_path = nullptr;
|
||||
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &result_path);
|
||||
if (!SUCCEEDED(hr))
|
||||
return false;
|
||||
|
||||
*path = base::FilePath(result_path);
|
||||
CoTaskMemFree(result_path);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -155,10 +155,7 @@
|
|||
'WEBRTC_MAC',
|
||||
],
|
||||
}], # OS=="mac"
|
||||
['OS=="win"', {
|
||||
'include_dirs': [
|
||||
'<(libchromiumcontent_src_dir)/third_party/wtl/include',
|
||||
],
|
||||
['OS=="win"', {
|
||||
'defines': [
|
||||
'_WIN32_WINNT=0x0602',
|
||||
'WINVER=0x0602',
|
||||
|
|
|
@ -364,6 +364,7 @@
|
|||
'link_settings': {
|
||||
'libraries': [
|
||||
'-limm32.lib',
|
||||
'-lgdi32.lib',
|
||||
'-loleacc.lib',
|
||||
'-lcomctl32.lib',
|
||||
'-lcomdlg32.lib',
|
||||
|
|
Загрузка…
Ссылка в новой задаче