-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NTP Next] Add updated NTP with background support
- Loading branch information
1 parent
ba95b0a
commit 58bd175
Showing
53 changed files
with
2,684 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#include "brave/browser/ui/webui/brave_new_tab/custom_image_chooser.h" | ||
|
||
#include <utility> | ||
|
||
#include "brave/components/l10n/common/localization_util.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "chrome/browser/ui/chrome_select_file_policy.h" | ||
#include "chrome/grit/generated_resources.h" | ||
#include "content/public/browser/web_contents.h" | ||
#include "ui/shell_dialogs/selected_file_info.h" | ||
|
||
namespace brave_new_tab { | ||
|
||
CustomImageChooser::CustomImageChooser(content::WebContents* web_contents) | ||
: web_contents_(web_contents), | ||
profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())) { | ||
} | ||
|
||
CustomImageChooser::~CustomImageChooser() = default; | ||
|
||
void CustomImageChooser::ShowDialog(ShowDialogCallback callback) { | ||
if (callback_) { | ||
std::move(callback_).Run({}); | ||
} | ||
|
||
callback_ = std::move(callback); | ||
|
||
if (dialog_) { | ||
return; | ||
} | ||
|
||
dialog_ = ui::SelectFileDialog::Create( | ||
this, std::make_unique<ChromeSelectFilePolicy>(web_contents_)); | ||
|
||
ui::SelectFileDialog::FileTypeInfo file_types; | ||
file_types.allowed_paths = ui::SelectFileDialog::FileTypeInfo::NATIVE_PATH; | ||
file_types.extensions.push_back( | ||
{{FILE_PATH_LITERAL("jpg"), FILE_PATH_LITERAL("jpeg"), | ||
FILE_PATH_LITERAL("png"), FILE_PATH_LITERAL("gif")}}); | ||
file_types.extension_description_overrides.push_back( | ||
brave_l10n::GetLocalizedResourceUTF16String(IDS_UPLOAD_IMAGE_FORMAT)); | ||
|
||
dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE, | ||
std::u16string(), profile_->last_selected_directory(), | ||
&file_types, 0, base::FilePath::StringType(), | ||
web_contents_->GetTopLevelNativeWindow(), nullptr); | ||
} | ||
|
||
void CustomImageChooser::FileSelected(const ui::SelectedFileInfo& file, | ||
int index) { | ||
dialog_ = nullptr; | ||
profile_->set_last_selected_directory(file.path().DirName()); | ||
if (callback_) { | ||
std::move(callback_).Run({file.path()}); | ||
} | ||
} | ||
|
||
void CustomImageChooser::MultiFilesSelected( | ||
const std::vector<ui::SelectedFileInfo>& files) { | ||
dialog_ = nullptr; | ||
if (!files.empty()) { | ||
profile_->set_last_selected_directory(files.back().path().DirName()); | ||
} | ||
std::vector<base::FilePath> paths; | ||
paths.reserve(files.size()); | ||
for (auto& file : files) { | ||
paths.push_back(file.path()); | ||
} | ||
if (callback_) { | ||
std::move(callback_).Run(std::move(paths)); | ||
} | ||
} | ||
|
||
void CustomImageChooser::FileSelectionCanceled() { | ||
dialog_ = nullptr; | ||
if (callback_) { | ||
std::move(callback_).Run({}); | ||
} | ||
} | ||
|
||
} // namespace brave_new_tab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#ifndef BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_CUSTOM_IMAGE_CHOOSER_H_ | ||
#define BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_CUSTOM_IMAGE_CHOOSER_H_ | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
#include "base/files/file_path.h" | ||
#include "base/functional/callback.h" | ||
#include "base/memory/raw_ptr.h" | ||
#include "ui/shell_dialogs/select_file_dialog.h" | ||
|
||
class Profile; | ||
|
||
namespace content { | ||
class WebContents; | ||
} | ||
|
||
namespace brave_new_tab { | ||
|
||
class CustomImageChooser : public ui::SelectFileDialog::Listener { | ||
public: | ||
explicit CustomImageChooser(content::WebContents* web_contents); | ||
~CustomImageChooser() override; | ||
|
||
CustomImageChooser(const CustomImageChooser&) = delete; | ||
CustomImageChooser& operator=(const CustomImageChooser&) = delete; | ||
|
||
using ShowDialogCallback = | ||
base::OnceCallback<void(std::vector<base::FilePath>)>; | ||
|
||
void ShowDialog(ShowDialogCallback callback); | ||
|
||
// ui::SelectFileDialog::Listener: | ||
void FileSelected(const ui::SelectedFileInfo& file, int index) override; | ||
void MultiFilesSelected( | ||
const std::vector<ui::SelectedFileInfo>& files) override; | ||
void FileSelectionCanceled() override; | ||
|
||
private: | ||
raw_ptr<content::WebContents> web_contents_ = nullptr; | ||
raw_ptr<Profile> profile_ = nullptr; | ||
scoped_refptr<ui::SelectFileDialog> dialog_; | ||
ShowDialogCallback callback_; | ||
}; | ||
|
||
} // namespace brave_new_tab | ||
|
||
#endif // BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_CUSTOM_IMAGE_CHOOSER_H_ |
Oops, something went wrong.