Skip to content

Commit

Permalink
Merge pull request #12647 from brave/first_run_dialog_win
Browse files Browse the repository at this point in the history
Introduced Windows specific FirstRun dialog
  • Loading branch information
simonhong authored Mar 21, 2022
2 parents 1f8ff56 + 5c71433 commit 73096ec
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 23 deletions.
17 changes: 14 additions & 3 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -1656,9 +1656,20 @@ Are you sure you want to do this?
<message name="IDS_FIRSTRUN_DLG_COMPLETE_INSTALLATION_LABEL_BRAVE" desc="Label at top of Dialog noting what's going to happen">
To ensure the best privacy online, consider setting Brave as the default browser on your computer. With Brave as default, any web link you click will open with Brave's privacy protections.
</message>
<message name="IDS_FIRSTRUN_DIALOG_WINDOW_TITLE_BRAVE" desc="Window title of First Run dialog, displayed in title bar">
Welcome to Brave
</message>
<if expr="is_win">
<message name="IDS_FIRSTRUN_DLG_WIN_HEADER_TEXT" desc="Text for FirstRun dialog header">
Ready for the best privacy online?
</message>
<message name="IDS_FIRSTRUN_DLG_WIN_CONTENTS_TEXT" desc="Text for FirstRun dialog contents">
Set Brave as your default browser to get Brave's privacy protections on every web page you open.
</message>
<message name="IDS_FIRSTRUN_DLG_WIN_OK_BUTTON_LABEL" desc="Text for FirstRun dialog ok button">
Set Brave as default
</message>
<message name="IDS_FIRSTRUN_DLG_WIN_CANCEL_BUTTON_LABEL" desc="Text for FirstRun dialog cancel button">
Maybe later
</message>
</if>
<message name="IDS_SETTINGS_WALLET_NETWORKS_ITEM" desc="The label for opening wallet networks list">
Networks
</message>
Expand Down
6 changes: 3 additions & 3 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ source_set("ui") {
"views/web_discovery_dialog_view.h",
]

# Enable FirstRun dialog on Win.
# Use different FirstRun dialog UI on Win.
# Upstream only includes it on mac/Linux.
if (is_win) {
sources += [
"//chrome/browser/ui/views/first_run_dialog.cc",
"//chrome/browser/ui/views/first_run_dialog.h",
"views/first_run_dialog_win.cc",
"views/first_run_dialog_win.h",
]
}

Expand Down
111 changes: 111 additions & 0 deletions browser/ui/views/first_run_dialog_win.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/. */

#include "brave/browser/ui/views/first_run_dialog_win.h"

#include <memory>
#include <utility>

#include "base/bind.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "brave/browser/brave_shell_integration.h"
#include "brave/grit/brave_generated_resources.h"
#include "chrome/browser/first_run/first_run.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/font.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/window/dialog_delegate.h"

namespace first_run {

void ShowFirstRunDialog(Profile* profile) {
base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
FirstRunDialogWin::Show(run_loop.QuitClosure());
run_loop.Run();
}

} // namespace first_run

// static
void FirstRunDialogWin::Show(base::RepeatingClosure quit_runloop) {
FirstRunDialogWin* dialog = new FirstRunDialogWin(std::move(quit_runloop));
views::DialogDelegate::CreateDialogWidget(dialog, NULL, NULL)->Show();
}

FirstRunDialogWin::FirstRunDialogWin(base::RepeatingClosure quit_runloop)
: quit_runloop_(quit_runloop) {
set_should_ignore_snapping(true);
SetButtonLabel(
ui::DIALOG_BUTTON_OK,
l10n_util::GetStringUTF16(IDS_FIRSTRUN_DLG_WIN_OK_BUTTON_LABEL));
SetButtonLabel(
ui::DIALOG_BUTTON_CANCEL,
l10n_util::GetStringUTF16(IDS_FIRSTRUN_DLG_WIN_CANCEL_BUTTON_LABEL));
constexpr int kChildSpacing = 16;
constexpr int kPadding = 24;
constexpr int kTopPadding = 20;
constexpr int kBottomPadding = 55;

SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical,
gfx::Insets(kTopPadding, kPadding, kBottomPadding, kPadding),
kChildSpacing));

constexpr int kHeaderFontSize = 16;
int size_diff =
kHeaderFontSize - views::Label::GetDefaultFontList().GetFontSize();
views::Label::CustomFont header_font = {
views::Label::GetDefaultFontList()
.DeriveWithSizeDelta(size_diff)
.DeriveWithWeight(gfx::Font::Weight::SEMIBOLD)};
auto* header_label = AddChildView(std::make_unique<views::Label>(
l10n_util::GetStringUTF16(IDS_FIRSTRUN_DLG_WIN_HEADER_TEXT),
header_font));
header_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);

constexpr int kContentFontSize = 15;
size_diff =
kContentFontSize - views::Label::GetDefaultFontList().GetFontSize();
views::Label::CustomFont contents_font = {
views::Label::GetDefaultFontList()
.DeriveWithSizeDelta(size_diff)
.DeriveWithWeight(gfx::Font::Weight::NORMAL)};
auto* contents_label = AddChildView(std::make_unique<views::Label>(
l10n_util::GetStringUTF16(IDS_FIRSTRUN_DLG_WIN_CONTENTS_TEXT),
contents_font));
contents_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
contents_label->SetMultiLine(true);
constexpr int kMaxWidth = 350;
contents_label->SetMaximumWidth(kMaxWidth);
}

FirstRunDialogWin::~FirstRunDialogWin() = default;

void FirstRunDialogWin::Done() {
CHECK(!quit_runloop_.is_null());
quit_runloop_.Run();
}

bool FirstRunDialogWin::Accept() {
GetWidget()->Hide();

base::MakeRefCounted<shell_integration::BraveDefaultBrowserWorker>()
->StartSetAsDefault(base::NullCallback());

Done();
return true;
}

void FirstRunDialogWin::WindowClosing() {
first_run::SetShouldShowWelcomePage();
Done();
}

BEGIN_METADATA(FirstRunDialogWin, views::DialogDelegateView)
END_METADATA
38 changes: 38 additions & 0 deletions browser/ui/views/first_run_dialog_win.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_UI_VIEWS_FIRST_RUN_DIALOG_WIN_H_
#define BRAVE_BROWSER_UI_VIEWS_FIRST_RUN_DIALOG_WIN_H_

#include "base/callback.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/views/window/dialog_delegate.h"

class FirstRunDialogWin : public views::DialogDelegateView {
public:
METADATA_HEADER(FirstRunDialogWin);

FirstRunDialogWin(const FirstRunDialogWin&) = delete;
FirstRunDialogWin& operator=(const FirstRunDialogWin&) = delete;

static void Show(base::RepeatingClosure quit_runloop);

private:
explicit FirstRunDialogWin(base::RepeatingClosure quit_runloop);
~FirstRunDialogWin() override;

// This terminates the nested message-loop.
void Done();

// views::DialogDelegate overrides:
bool Accept() override;

// views::WidgetDelegate overrides:
void WindowClosing() override;

base::RepeatingClosure quit_runloop_;
};

#endif // BRAVE_BROWSER_UI_VIEWS_FIRST_RUN_DIALOG_WIN_H_
1 change: 0 additions & 1 deletion chromium_src/chrome/browser/first_run/first_run_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace first_run {
// Enable first run dialog on Win also.
// Upstream only uses it for macOS/Linux.
void ShowFirstRunDialog(Profile* profile);
void ShowFirstRunDialogViews(Profile* profile);

} // namespace first_run

Expand Down
18 changes: 2 additions & 16 deletions chromium_src/chrome/browser/ui/views/first_run_dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <string>

#include "base/bind.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "brave/grit/brave_generated_resources.h"
#include "build/build_config.h"
Expand All @@ -31,10 +30,6 @@
#include "ui/views/widget/widget.h"
#include "ui/views/window/dialog_delegate.h"

#if BUILDFLAG(IS_WIN)
#include "brave/browser/brave_shell_integration.h"
#endif

namespace first_run {

void ShowFirstRunDialog(Profile* profile) {
Expand Down Expand Up @@ -77,7 +72,7 @@ FirstRunDialog::FirstRunDialog(base::RepeatingClosure learn_more_callback,
if (report_crashes_)
report_crashes_->GetChecked();

SetTitle(l10n_util::GetStringUTF16(IDS_FIRSTRUN_DIALOG_WINDOW_TITLE_BRAVE));
SetTitle(l10n_util::GetStringUTF16(IDS_FIRST_RUN_DIALOG_WINDOW_TITLE));
SetButtons(ui::DIALOG_BUTTON_OK);
SetExtraView(
std::make_unique<views::Link>(l10n_util::GetStringUTF16(IDS_LEARN_MORE)))
Expand Down Expand Up @@ -122,17 +117,8 @@ void FirstRunDialog::Done() {
bool FirstRunDialog::Accept() {
GetWidget()->Hide();

if (make_default_->GetChecked()) {
// shell_integration::SetAsDefaultBrowser() doesn't work on Windows 8+.
// Upstream will use DefaultBrowserWorker when it's available on all OSs.
// See the comments of shell_integration::SetAsDefaultBrowser().
#if BUILDFLAG(IS_WIN)
base::MakeRefCounted<shell_integration::BraveDefaultBrowserWorker>()
->StartSetAsDefault(base::NullCallback());
#else
if (make_default_->GetChecked())
shell_integration::SetAsDefaultBrowser();
#endif
}

Done();
return true;
Expand Down

0 comments on commit 73096ec

Please sign in to comment.