Skip to content

Commit

Permalink
Merge pull request #14714 from brave/add_to_dock_when_brave_is_default
Browse files Browse the repository at this point in the history
Add Keep in dock option to first run dialog and settings
  • Loading branch information
simonhong authored Oct 28, 2022
2 parents 692184f + 3acb0aa commit 685f1a3
Show file tree
Hide file tree
Showing 24 changed files with 204 additions and 50 deletions.
2 changes: 2 additions & 0 deletions app/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import("//brave/browser/shell_integrations/buildflags/buildflags.gni")
import("//brave/components/brave_vpn/buildflags/buildflags.gni")
import("//brave/components/sidebar/buildflags/buildflags.gni")
import("//brave/components/speedreader/common/buildflags.gni")
Expand All @@ -14,6 +15,7 @@ brave_grit("brave_generated_resources_grit") {
"enable_sidebar=$enable_sidebar",
"enable_speedreader=$enable_speedreader",
"enable_brave_vpn=$enable_brave_vpn",
"enable_pin_shortcut=$enable_pin_shortcut",
]
source = "brave_generated_resources.grd"
output_dir = "$root_gen_dir/brave"
Expand Down
16 changes: 12 additions & 4 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -938,11 +938,19 @@ Or change later at <ph name="SETTINGS_EXTENIONS_LINK">$2<ex>brave://settings/ext
Maybe later
</message>

<if expr="is_win">
<message name="IDS_FIRSTRUN_DLG_PIN_SHORTCUT_TEXT" desc="Text for pin to taskbar checkbox">
Pin to taskbar
</message>
<if expr="enable_pin_shortcut">
<if expr="is_win">
<message name="IDS_FIRSTRUN_DLG_PIN_SHORTCUT_TEXT" desc="Text for pin to taskbar checkbox">
Pin to taskbar
</message>
</if>
<if expr="is_macosx">
<message name="IDS_FIRSTRUN_DLG_PIN_SHORTCUT_TEXT" desc="Text for pin to taskbar checkbox">
Keep in Dock
</message>
</if>
</if>

<!-- Importer -->
<message name="IDS_BRAVE_IMPORT_FROM_EDGE" desc="browser combo box: Microsoft Edge Legacy">
Microsoft Edge Legacy
Expand Down
33 changes: 23 additions & 10 deletions app/brave_settings_strings.grdp
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,29 @@
</message>

<!-- Settings / Pin shortcut-->
<if expr="is_win">
<message name="IDS_SETTINGS_CAN_PIN_SHORTCUT">
Pin to taskbar
</message>
<message name="IDS_SETTINGS_PIN_SHORTCUT">
Pin
</message>
<message name="IDS_SETTINGS_SHORTCUT_PINNED">
Brave is already pinned
</message>
<if expr="enable_pin_shortcut">
<if expr="is_win">
<message name="IDS_SETTINGS_CAN_PIN_SHORTCUT" desc="The label for pin to taskbar settings">
Pin to taskbar
</message>
<message name="IDS_SETTINGS_PIN_SHORTCUT" desc="The label for pin to taskbar button">
Pin
</message>
<message name="IDS_SETTINGS_SHORTCUT_PINNED" desc="The label for pinned state description">
Brave is already pinned
</message>
</if>
<if expr="is_macosx">
<message name="IDS_SETTINGS_CAN_PIN_SHORTCUT" desc="The label for keep in dock settings">
Keep in Dock
</message>
<message name="IDS_SETTINGS_PIN_SHORTCUT" desc="The label for dock button">
Dock
</message>
<message name="IDS_SETTINGS_SHORTCUT_PINNED" desc="The label for docked state description">
Brave is already in Dock
</message>
</if>
</if>

<!-- Settings / Shields -->
Expand Down
27 changes: 27 additions & 0 deletions browser/brave_shell_integration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,39 @@

#include <utility>

#include "brave/browser/brave_shell_integration_win.h"
#include "brave/browser/default_protocol_handler_utils_win.h"
#include "chrome/installer/util/shell_util.h"
#endif

#if BUILDFLAG(IS_MAC)
#include "brave/browser/brave_shell_integration_mac.h"
#endif

namespace shell_integration {

void PinShortcut(Profile* profile,
base::OnceCallback<void(bool)> result_callback) {
#if BUILDFLAG(IS_WIN)
win::PinToTaskbar(profile, std::move(result_callback));
#elif BUILDFLAG(IS_MAC)
// Mac doesn't support profile specific icon in dock.
mac::AddIconToDock(std::move(result_callback));
#elif BUILDFLAG(IS_LINUX)
NOTREACHED() << "Not supported on linux yet.";
#endif
}

void IsShortcutPinned(base::OnceCallback<void(bool)> result_callback) {
#if BUILDFLAG(IS_WIN)
win::IsShortcutPinned(std::move(result_callback));
#elif BUILDFLAG(IS_MAC)
mac::IsIconAddedToDock(std::move(result_callback));
#elif BUILDFLAG(IS_LINUX)
NOTREACHED() << "Not supported on linux yet.";
#endif
}

BraveDefaultBrowserWorker::BraveDefaultBrowserWorker() = default;
BraveDefaultBrowserWorker::~BraveDefaultBrowserWorker() = default;

Expand Down
10 changes: 10 additions & 0 deletions browser/brave_shell_integration.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@
#ifndef BRAVE_BROWSER_BRAVE_SHELL_INTEGRATION_H_
#define BRAVE_BROWSER_BRAVE_SHELL_INTEGRATION_H_

#include "base/callback.h"
#include "base/callback_helpers.h"
#include "chrome/browser/shell_integration.h"

class Profile;

namespace shell_integration {

void PinShortcut(
Profile* profile = nullptr,
base::OnceCallback<void(bool)> result_callback = base::DoNothing());
void IsShortcutPinned(
base::OnceCallback<void(bool)> result_callback = base::DoNothing());

class BraveDefaultBrowserWorker : public DefaultBrowserWorker {
public:
BraveDefaultBrowserWorker();
Expand Down
20 changes: 20 additions & 0 deletions browser/brave_shell_integration_mac.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* 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_BRAVE_SHELL_INTEGRATION_MAC_H_
#define BRAVE_BROWSER_BRAVE_SHELL_INTEGRATION_MAC_H_

#include "base/callback.h"
#include "base/callback_helpers.h"

namespace shell_integration::mac {

// No-op if already added.
void AddIconToDock(
base::OnceCallback<void(bool)> result_callback = base::DoNothing());
void IsIconAddedToDock(base::OnceCallback<void(bool)> result_callback);
} // namespace shell_integration::mac

#endif // BRAVE_BROWSER_BRAVE_SHELL_INTEGRATION_MAC_H_
25 changes: 25 additions & 0 deletions browser/brave_shell_integration_mac.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* 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/brave_shell_integration_mac.h"

#include "base/mac/bundle_locations.h"
#include "chrome/browser/mac/dock.h"

namespace shell_integration::mac {

void AddIconToDock(base::OnceCallback<void(bool)> result_callback) {
dock::AddIcon([base::mac::MainBundle() bundlePath], nullptr);

std::move(result_callback)
.Run(dock::ChromeIsInTheDock() == dock::ChromeInDockTrue);
}

void IsIconAddedToDock(base::OnceCallback<void(bool)> result_callback) {
std::move(result_callback)
.Run(dock::ChromeIsInTheDock() == dock::ChromeInDockTrue);
}

} // namespace shell_integration::mac
2 changes: 2 additions & 0 deletions browser/resources/settings/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# you can obtain one at http://mozilla.org/MPL/2.0/.

import("//brave/browser/resources/settings/sources.gni")
import("//brave/browser/shell_integrations/buildflags/buildflags.gni")
import("//brave/build/config.gni")
import("//brave/components/brave_vpn/buildflags/buildflags.gni")
import("//brave/components/brave_wayback_machine/buildflags/buildflags.gni")
Expand Down Expand Up @@ -70,6 +71,7 @@ preprocess_if_expr("preprocess") {
"enable_brave_wayback_machine=$enable_brave_wayback_machine",
"enable_brave_vpn=$enable_brave_vpn",
"enable_extensions=$enable_extensions",
"enable_pin_shortcut=$enable_pin_shortcut",
]
in_folder = "./"
out_folder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<settings-animated-pages id="pages" section="getStarted">
<div route-path="default">
<settings-default-browser-page></settings-default-browser-page>
<if expr="is_win">
<if expr="enable_pin_shortcut">
<settings-pin-shortcut-page></settings-pin-shortcut-page>
</if>
<div class="settings-box">$i18n{onStartup}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import '../default_browser_page/default_browser_page.js'
import '../on_startup_page/on_startup_page.js'
import {getTemplate} from './getting_started.html.js'

// <if expr="is_win">
// <if expr="enable_pin_shortcut">
import '../pin_shortcut_page/pin_shortcut_page.js'
// </if>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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/. -->

<if expr="is_win">
<if expr="enable_pin_shortcut">
<style include="cr-shared-style settings-shared iron-flex">
</style>
<template is="dom-if" if="[[!pinned_]]">
Expand Down
3 changes: 2 additions & 1 deletion browser/resources/settings/settings.gni
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import("//brave/browser/shell_integrations/buildflags/buildflags.gni")
import("//chrome/browser/resources/settings/settings.gni")

settings_namespace_rewrites = [
Expand All @@ -15,7 +16,7 @@ settings_namespace_rewrites = [
"settings.Router|Router",
]

if (is_win) {
if (enable_pin_shortcut) {
settings_namespace_rewrites += [
"settings.PinShortcutPageBrowserProxyImpl|PinShortcutPageBrowserProxyImpl",
]
Expand Down
12 changes: 10 additions & 2 deletions browser/resources/settings/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# 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/.

import("//brave/browser/shell_integrations/buildflags/buildflags.gni")

# Provide our file list to chromium's settings page build
# Since our components start in a different path, we'll do the first step (move them to
# the generated preprocessed path via preprocess_if_expr, but let chromium handle the rest of the
Expand Down Expand Up @@ -44,7 +46,6 @@ brave_settings_web_component_files = [
"default_brave_shields_page/components/brave_adblock_subscribe_dropdown.ts",
"default_brave_shields_page/components/brave_adblock_editor.ts",
"getting_started_page/getting_started.ts",
"pin_shortcut_page/pin_shortcut_page.ts",
"social_blocking_page/social_blocking_page.ts",
]
brave_settings_non_web_component_files = [
Expand Down Expand Up @@ -91,9 +92,16 @@ brave_settings_non_web_component_files = [
"brave_wallet_page/brave_wallet_browser_proxy.ts",
"default_brave_shields_page/brave_adblock_browser_proxy.ts",
"default_brave_shields_page/default_brave_shields_browser_proxy.ts",
"pin_shortcut_page/pin_shortcut_page_browser_proxy.ts",
"brave_routes.ts",
]

if (enable_pin_shortcut) {
brave_settings_web_component_files +=
[ "pin_shortcut_page/pin_shortcut_page.ts" ]
brave_settings_non_web_component_files +=
[ "pin_shortcut_page/pin_shortcut_page_browser_proxy.ts" ]
}

brave_settings_icons_html_files = [ "brave_icons.html" ]
brave_settings_ts_definitions = [
"//brave/components/definitions/chrome_brave_theme.d.ts",
Expand Down
12 changes: 12 additions & 0 deletions browser/shell_integrations/buildflags/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 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/.

import("//build/buildflag_header.gni")
import("buildflags.gni")

buildflag_header("buildflags") {
header = "buildflags.h"
flags = [ "ENABLE_PIN_SHORTCUT=$enable_pin_shortcut" ]
}
6 changes: 6 additions & 0 deletions browser/shell_integrations/buildflags/buildflags.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 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/.

enable_pin_shortcut = is_win || is_mac
2 changes: 2 additions & 0 deletions browser/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ if (is_mac) {
brave_chrome_browser_sources += [
"//brave/browser/brave_browser_main_parts_mac.h",
"//brave/browser/brave_browser_main_parts_mac.mm",
"//brave/browser/brave_shell_integration_mac.h",
"//brave/browser/brave_shell_integration_mac.mm",
]
}

Expand Down
4 changes: 3 additions & 1 deletion browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import("//brave/browser/ethereum_remote_client/buildflags/buildflags.gni")
import("//brave/browser/shell_integrations/buildflags/buildflags.gni")
import("//brave/build/config.gni")
import("//brave/components/brave_vpn/buildflags/buildflags.gni")
import("//brave/components/brave_wayback_machine/buildflags/buildflags.gni")
Expand Down Expand Up @@ -159,7 +160,7 @@ source_set("ui") {
"webui/speedreader/speedreader_panel_ui.h",
]

if (is_win) {
if (enable_pin_shortcut) {
sources += [
"webui/settings/pin_shortcut_handler.cc",
"webui/settings/pin_shortcut_handler.h",
Expand Down Expand Up @@ -331,6 +332,7 @@ source_set("ui") {
# //chrome/browser/ui depends on //brave/browser/ui, add this target here
# to pull in dependencies needed for the overwrite codes in chromium_src.
"//brave/browser/resources/federated_internals:resources",
"//brave/browser/shell_integrations/buildflags",
"//brave/browser/ui/brave_ads",
"//brave/browser/ui/brave_tooltips",
"//brave/browser/ui/webui/brave_federated:mojo_bindings",
Expand Down
15 changes: 7 additions & 8 deletions browser/ui/views/brave_first_run_dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
#include "ui/views/layout/box_layout.h"
#include "ui/views/window/dialog_delegate.h"

#if BUILDFLAG(IS_WIN)
#if BUILDFLAG(ENABLE_PIN_SHORTCUT)
#include "brave/browser/brave_shell_integration.h"
#include "brave/browser/brave_shell_integration_win.h"
#else
#else // BUILDFLAG(ENABLE_PIN_SHORTCUT)
#include "chrome/browser/shell_integration.h"
#endif

Expand All @@ -40,7 +39,7 @@ void ShowBraveFirstRunDialogViews(Profile* profile) {
run_loop.Run();
}

#if BUILDFLAG(IS_WIN)
#if BUILDFLAG(ENABLE_PIN_SHORTCUT)
class PinShortcutCheckbox : public views::Checkbox {
public:
METADATA_HEADER(PinShortcutCheckbox);
Expand Down Expand Up @@ -133,7 +132,7 @@ BraveFirstRunDialog::BraveFirstRunDialog(base::RepeatingClosure quit_runloop)
constexpr int kMaxWidth = 350;
contents_label->SetMaximumWidth(kMaxWidth);

#if BUILDFLAG(IS_WIN)
#if BUILDFLAG(ENABLE_PIN_SHORTCUT)
pin_shortcut_checkbox_ =
AddChildView(std::make_unique<PinShortcutCheckbox>());
#endif
Expand All @@ -143,7 +142,7 @@ BraveFirstRunDialog::BraveFirstRunDialog(base::RepeatingClosure quit_runloop)
constexpr int kTopPadding = 20;
int kBottomPadding = 55;

#if BUILDFLAG(IS_WIN)
#if BUILDFLAG(ENABLE_PIN_SHORTCUT)
kBottomPadding -= pin_shortcut_checkbox_->GetPreferredSize().height();
#endif

Expand All @@ -163,15 +162,15 @@ void BraveFirstRunDialog::Done() {
bool BraveFirstRunDialog::Accept() {
GetWidget()->Hide();

#if BUILDFLAG(IS_WIN)
#if BUILDFLAG(ENABLE_PIN_SHORTCUT)
base::MakeRefCounted<shell_integration::BraveDefaultBrowserWorker>()
->StartSetAsDefault(base::BindOnce(
[](bool pin_to_shortcut,
shell_integration::DefaultWebClientState state) {
if (pin_to_shortcut &&
state == shell_integration::DefaultWebClientState::IS_DEFAULT) {
// Try to pin to taskbar when Brave is set as a default browser.
shell_integration::win::PinToTaskbar();
shell_integration::PinShortcut();
}
},
pin_shortcut_checkbox_->GetChecked()));
Expand Down
Loading

0 comments on commit 685f1a3

Please sign in to comment.