Add DialogSettings helper struct
This commit is contained in:
parent
e7410976f0
commit
867bb5a94e
9 changed files with 151 additions and 235 deletions
|
@ -66,26 +66,24 @@ void ConvertFilters(const Filters& filters,
|
|||
template <typename T>
|
||||
class FileDialog {
|
||||
public:
|
||||
FileDialog(const base::FilePath& default_path,
|
||||
const std::string& title,
|
||||
const std::string& button_label,
|
||||
const Filters& filters, int options) {
|
||||
FileDialog(const DialogSettings& settings, int options) {
|
||||
std::wstring file_part;
|
||||
if (!IsDirectory(default_path))
|
||||
file_part = default_path.BaseName().value();
|
||||
if (!IsDirectory(settings.default_path))
|
||||
file_part = settings.default_path.BaseName().value();
|
||||
|
||||
std::vector<std::wstring> buffer;
|
||||
std::vector<COMDLG_FILTERSPEC> filterspec;
|
||||
ConvertFilters(filters, &buffer, &filterspec);
|
||||
ConvertFilters(settings.filters, &buffer, &filterspec);
|
||||
|
||||
dialog_.reset(new T(file_part.c_str(), options, NULL,
|
||||
filterspec.data(), filterspec.size()));
|
||||
|
||||
if (!title.empty())
|
||||
GetPtr()->SetTitle(base::UTF8ToUTF16(title).c_str());
|
||||
if (!settings.title.empty())
|
||||
GetPtr()->SetTitle(base::UTF8ToUTF16(settings.title).c_str());
|
||||
|
||||
if (!button_label.empty())
|
||||
GetPtr()->SetOkButtonLabel(base::UTF8ToUTF16(button_label).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.
|
||||
|
@ -107,7 +105,7 @@ class FileDialog {
|
|||
}
|
||||
}
|
||||
|
||||
SetDefaultFolder(default_path);
|
||||
SetDefaultFolder(settings.default_path);
|
||||
}
|
||||
|
||||
bool Show(atom::NativeWindow* parent_window) {
|
||||
|
@ -160,31 +158,20 @@ bool CreateDialogThread(RunState* run_state) {
|
|||
}
|
||||
|
||||
void RunOpenDialogInNewThread(const RunState& run_state,
|
||||
atom::NativeWindow* parent,
|
||||
const std::string& title,
|
||||
const std::string& button_label,
|
||||
const base::FilePath& default_path,
|
||||
const Filters& filters,
|
||||
int properties,
|
||||
const DialogSettings& settings,
|
||||
const OpenDialogCallback& callback) {
|
||||
std::vector<base::FilePath> paths;
|
||||
bool result = ShowOpenDialog(parent, title, button_label, default_path,
|
||||
filters, properties, &paths);
|
||||
bool result = ShowOpenDialog(settings, &paths);
|
||||
run_state.ui_task_runner->PostTask(FROM_HERE,
|
||||
base::Bind(callback, result, paths));
|
||||
run_state.ui_task_runner->DeleteSoon(FROM_HERE, run_state.dialog_thread);
|
||||
}
|
||||
|
||||
void RunSaveDialogInNewThread(const RunState& run_state,
|
||||
atom::NativeWindow* parent,
|
||||
const std::string& title,
|
||||
const std::string& button_label,
|
||||
const base::FilePath& default_path,
|
||||
const Filters& filters,
|
||||
const DialogSettings& settings,
|
||||
const SaveDialogCallback& callback) {
|
||||
base::FilePath path;
|
||||
bool result = ShowSaveDialog(parent, title, button_label, default_path,
|
||||
filters, &path);
|
||||
bool result = ShowSaveDialog(settings, &path);
|
||||
run_state.ui_task_runner->PostTask(FROM_HERE,
|
||||
base::Bind(callback, result, path));
|
||||
run_state.ui_task_runner->DeleteSoon(FROM_HERE, run_state.dialog_thread);
|
||||
|
@ -192,26 +179,20 @@ void RunSaveDialogInNewThread(const RunState& run_state,
|
|||
|
||||
} // namespace
|
||||
|
||||
bool ShowOpenDialog(atom::NativeWindow* parent_window,
|
||||
const std::string& title,
|
||||
const std::string& button_label,
|
||||
const base::FilePath& default_path,
|
||||
const Filters& filters,
|
||||
int properties,
|
||||
bool ShowOpenDialog(const DialogSettings& settings,
|
||||
std::vector<base::FilePath>* paths) {
|
||||
int options = FOS_FORCEFILESYSTEM | FOS_FILEMUSTEXIST;
|
||||
if (properties & FILE_DIALOG_OPEN_DIRECTORY)
|
||||
if (settings.properties & FILE_DIALOG_OPEN_DIRECTORY)
|
||||
options |= FOS_PICKFOLDERS;
|
||||
if (properties & FILE_DIALOG_MULTI_SELECTIONS)
|
||||
if (settings.properties & FILE_DIALOG_MULTI_SELECTIONS)
|
||||
options |= FOS_ALLOWMULTISELECT;
|
||||
if (properties & FILE_DIALOG_SHOW_HIDDEN_FILES)
|
||||
if (settings.properties & FILE_DIALOG_SHOW_HIDDEN_FILES)
|
||||
options |= FOS_FORCESHOWHIDDEN;
|
||||
if (properties & FILE_DIALOG_PROMPT_TO_CREATE)
|
||||
if (settings.properties & FILE_DIALOG_PROMPT_TO_CREATE)
|
||||
options |= FOS_CREATEPROMPT;
|
||||
|
||||
FileDialog<CShellFileOpenDialog> open_dialog(
|
||||
default_path, title, button_label, filters, options);
|
||||
if (!open_dialog.Show(parent_window))
|
||||
FileDialog<CShellFileOpenDialog> open_dialog(settings, options);
|
||||
if (!open_dialog.Show(settings.parent_window))
|
||||
return false;
|
||||
|
||||
ATL::CComPtr<IShellItemArray> items;
|
||||
|
@ -244,12 +225,7 @@ bool ShowOpenDialog(atom::NativeWindow* parent_window,
|
|||
return true;
|
||||
}
|
||||
|
||||
void ShowOpenDialog(atom::NativeWindow* parent,
|
||||
const std::string& title,
|
||||
const std::string& button_label,
|
||||
const base::FilePath& default_path,
|
||||
const Filters& filters,
|
||||
int properties,
|
||||
void ShowOpenDialog(const DialogSettings& settings,
|
||||
const OpenDialogCallback& callback) {
|
||||
RunState run_state;
|
||||
if (!CreateDialogThread(&run_state)) {
|
||||
|
@ -259,20 +235,14 @@ void ShowOpenDialog(atom::NativeWindow* parent,
|
|||
|
||||
run_state.dialog_thread->task_runner()->PostTask(
|
||||
FROM_HERE,
|
||||
base::Bind(&RunOpenDialogInNewThread, run_state, parent, title,
|
||||
button_label, default_path, filters, properties, callback));
|
||||
base::Bind(&RunOpenDialogInNewThread, run_state, settings, callback));
|
||||
}
|
||||
|
||||
bool ShowSaveDialog(atom::NativeWindow* parent_window,
|
||||
const std::string& title,
|
||||
const std::string& button_label,
|
||||
const base::FilePath& default_path,
|
||||
const Filters& filters,
|
||||
bool ShowSaveDialog(const DialogSettings& settings,
|
||||
base::FilePath* path) {
|
||||
FileDialog<CShellFileSaveDialog> save_dialog(
|
||||
default_path, title, button_label, filters,
|
||||
FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_OVERWRITEPROMPT);
|
||||
if (!save_dialog.Show(parent_window))
|
||||
settings, FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_OVERWRITEPROMPT);
|
||||
if (!save_dialog.Show(settings.parent_window))
|
||||
return false;
|
||||
|
||||
wchar_t buffer[MAX_PATH];
|
||||
|
@ -284,11 +254,7 @@ bool ShowSaveDialog(atom::NativeWindow* parent_window,
|
|||
return true;
|
||||
}
|
||||
|
||||
void ShowSaveDialog(atom::NativeWindow* parent,
|
||||
const std::string& title,
|
||||
const std::string& button_label,
|
||||
const base::FilePath& default_path,
|
||||
const Filters& filters,
|
||||
void ShowSaveDialog(const DialogSettings& settings,
|
||||
const SaveDialogCallback& callback) {
|
||||
RunState run_state;
|
||||
if (!CreateDialogThread(&run_state)) {
|
||||
|
@ -298,8 +264,7 @@ void ShowSaveDialog(atom::NativeWindow* parent,
|
|||
|
||||
run_state.dialog_thread->task_runner()->PostTask(
|
||||
FROM_HERE,
|
||||
base::Bind(&RunSaveDialogInNewThread, run_state, parent, title,
|
||||
button_label, default_path, filters, callback));
|
||||
base::Bind(&RunSaveDialogInNewThread, run_state, settings, callback));
|
||||
}
|
||||
|
||||
} // namespace file_dialog
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue