Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions src/dao/browser/dao_pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@

#include "dao/browser/dao_pref_names.h"

#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface_iterator.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_contents.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
#include "third_party/blink/public/mojom/css/preferred_color_scheme.mojom.h"
#include "ui/native_theme/native_theme.h"

namespace dao::prefs {

Expand All @@ -15,6 +25,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(kDaoEnhancedPipEnabled, true);
registry->RegisterBooleanPref(kDaoWelcomeShown, false);
registry->RegisterBooleanPref(kDaoLittleDaoEnabled, true);
registry->RegisterBooleanPref(kDaoForceDarkModeEnabled, false);
registry->RegisterBooleanPref(kDaoEnhancedCommandBarSuggestionsEnabled,
false);
registry->RegisterDictionaryPref(kDaoLittleDaoWindowSize);
Expand All @@ -24,3 +35,97 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
}

} // namespace dao::prefs

namespace dao {

namespace {

Profile* GetStorageProfile(Profile* profile) {
return profile ? profile->GetOriginalProfile() : nullptr;
}

bool UsesSameStorageProfile(Profile* lhs, Profile* rhs) {
return GetStorageProfile(lhs) == GetStorageProfile(rhs);
}

} // namespace

bool IsSystemDarkMode() {
ui::NativeTheme* theme = ui::NativeTheme::GetInstanceForNativeUi();
return theme && theme->preferred_color_scheme() ==
ui::NativeTheme::PreferredColorScheme::kDark;
}

bool IsForceDarkModeUserEnabled(Profile* profile) {
Profile* storage_profile = GetStorageProfile(profile);
return storage_profile && storage_profile->GetPrefs()->GetBoolean(
prefs::kDaoForceDarkModeEnabled);
}

bool IsForceDarkModeAvailable() {
return IsSystemDarkMode();
}

bool IsForceDarkModeEffective(Profile* profile) {
return IsForceDarkModeAvailable() && IsForceDarkModeUserEnabled(profile);
}

void SetForceDarkModeUserEnabled(Profile* profile, bool enabled) {
Profile* storage_profile = GetStorageProfile(profile);
if (!storage_profile) {
return;
}
storage_profile->GetPrefs()->SetBoolean(prefs::kDaoForceDarkModeEnabled,
enabled);
NotifyForceDarkModeChanged(storage_profile);
}

void ApplyForceDarkModePreferences(
Profile* profile,
blink::web_pref::WebPreferences* web_preferences) {
if (!web_preferences) {
return;
}

const bool force_dark_mode = IsForceDarkModeEffective(profile);
web_preferences->force_dark_mode_enabled = force_dark_mode;
if (!force_dark_mode) {
return;
}

web_preferences->preferred_color_scheme =
blink::mojom::PreferredColorScheme::kDark;
web_preferences->preferred_root_scrollbar_color_scheme =
blink::mojom::PreferredColorScheme::kDark;
}

void NotifyForceDarkModeChanged(Profile* profile) {
Profile* storage_profile = GetStorageProfile(profile);
if (!storage_profile) {
return;
}

for (BrowserWindowInterface* browser_window :
GetAllBrowserWindowInterfaces()) {
Browser* browser =
browser_window ? browser_window->GetBrowserForMigrationOnly() : nullptr;
if (!browser ||
!UsesSameStorageProfile(browser->profile(), storage_profile)) {
continue;
}

TabStripModel* tab_strip_model = browser->tab_strip_model();
if (!tab_strip_model) {
continue;
}

for (int i = 0; i < tab_strip_model->count(); ++i) {
content::WebContents* contents = tab_strip_model->GetWebContentsAt(i);
if (contents) {
contents->OnWebPreferencesChanged();
}
}
}
}

} // namespace dao
27 changes: 27 additions & 0 deletions src/dao/browser/dao_pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
#ifndef DAO_BROWSER_DAO_PREF_NAMES_H_
#define DAO_BROWSER_DAO_PREF_NAMES_H_

class Profile;

namespace blink::web_pref {
struct WebPreferences;
}

namespace user_prefs {
class PrefRegistrySyncable;
}
Expand Down Expand Up @@ -40,6 +46,12 @@ inline constexpr char kDaoWelcomeShown[] = "dao.welcome_shown";
// When false, external links use the regular full browser window.
inline constexpr char kDaoLittleDaoEnabled[] = "dao.little_dao_enabled";

// Boolean pref that remembers whether Dao should use Chromium's Auto Dark Mode
// pipeline for web contents. It only takes effect while the system appearance
// is dark.
inline constexpr char kDaoForceDarkModeEnabled[] =
"dao.force_dark_mode_enabled";

// Boolean pref that enables the richer Arc-style command bar suggestion
// pipeline. Kept off by default while the ranking model is still maturing.
inline constexpr char kDaoEnhancedCommandBarSuggestionsEnabled[] =
Expand Down Expand Up @@ -68,4 +80,19 @@ inline constexpr char kDaoDreamExcludedDomains[] =

} // namespace dao::prefs

namespace dao {

bool IsSystemDarkMode();
bool IsForceDarkModeUserEnabled(Profile* profile);
bool IsForceDarkModeAvailable();
bool IsForceDarkModeEffective(Profile* profile);

void SetForceDarkModeUserEnabled(Profile* profile, bool enabled);
void ApplyForceDarkModePreferences(
Profile* profile,
blink::web_pref::WebPreferences* web_preferences);
void NotifyForceDarkModeChanged(Profile* profile);

} // namespace dao

#endif // DAO_BROWSER_DAO_PREF_NAMES_H_
15 changes: 15 additions & 0 deletions src/dao/browser/strings/dao_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@
<message name="IDS_DAO_CONTROL_CENTER_BACK_BUTTON_ACCESSIBLE_NAME" desc="Accessible name for the back button in the Control Center 'More' submenu (no arrow, screen reader friendly).">
Back
</message>
<message name="IDS_DAO_CONTROL_CENTER_SHARE" desc="Label on the button in the Control Center 'More' submenu that opens the native share sheet for the current page.">
Share
</message>
<message name="IDS_DAO_CONTROL_CENTER_CLEAR_CACHE" desc="Label on the button in the Control Center 'More' submenu that clears the browser cache. Idle state.">
Clear Cache
</message>
Expand All @@ -345,6 +348,18 @@
<message name="IDS_DAO_CONTROL_CENTER_CLEARING" desc="Transient label shown on a Clear Cache or Clear Cookies button while the operation is in progress.">
Clearing...
</message>
<message name="IDS_DAO_FORCE_DARK_MODE_LABEL" desc="Accessible label for a toggle button that remembers whether Dao should force websites into dark mode while the system appearance is dark.">
Force dark mode
</message>
<message name="IDS_DAO_FORCE_DARK_MODE_SHORT_LABEL" desc="Short label shown under the Control Center utility button for the force dark mode toggle.">
Dark
</message>
<message name="IDS_DAO_FORCE_DARK_MODE_TOOLTIP" desc="Tooltip for the Force dark mode toggle when it can be changed because the system appearance is dark.">
Darken websites with Auto Dark Mode while the system is in dark mode.
</message>
<message name="IDS_DAO_FORCE_DARK_MODE_DISABLED_TOOLTIP" desc="Tooltip for the Force dark mode toggle when it is disabled because the system appearance is light.">
Available when the system is in dark mode.
</message>

<!-- QR code dialog (after scanning a QR via right-click on an image) -->
<message name="IDS_DAO_CONTEXT_MENU_DECODE_QR_CODE" desc="Image right-click context menu item that decodes any QR code embedded in the image and shows the result.">
Expand Down
Loading
Loading