-
Notifications
You must be signed in to change notification settings - Fork 891
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
a91a6cb
commit e9bef8c
Showing
63 changed files
with
3,178 additions
and
12 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
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,44 @@ | ||
# 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/. | ||
|
||
import("//brave/components/ntp_background_images/buildflags/buildflags.gni") | ||
|
||
assert(!is_android) | ||
assert(enable_custom_background) | ||
|
||
source_set("brave_new_tab") { | ||
sources = [ | ||
"custom_image_chooser.cc", | ||
"custom_image_chooser.h", | ||
"new_tab_page_handler.cc", | ||
"new_tab_page_handler.h", | ||
"new_tab_ui.cc", | ||
"new_tab_ui.h", | ||
"update_observer.cc", | ||
"update_observer.h", | ||
] | ||
|
||
deps = [ | ||
"//brave/browser:browser_process", | ||
"//brave/browser/ntp_background", | ||
"//brave/components/brave_new_tab/common", | ||
"//brave/components/brave_new_tab/common:mojom", | ||
"//brave/components/brave_new_tab/resources:generated_resources", | ||
"//brave/components/l10n/common", | ||
"//brave/components/ntp_background_images/browser", | ||
"//brave/components/ntp_background_images/common", | ||
"//brave/components/resources:static_resources", | ||
"//brave/components/resources:strings", | ||
"//chrome/app:generated_resources", | ||
"//chrome/browser:browser_public_dependencies", | ||
"//chrome/browser/profiles:profile", | ||
"//chrome/browser/ui/webui:webui_util", | ||
"//components/prefs", | ||
"//components/strings:components_strings", | ||
"//ui/base", | ||
"//ui/shell_dialogs", | ||
"//ui/webui", | ||
] | ||
} |
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.