Commit fbe81f60 authored by jdufault's avatar jdufault Committed by Commit bot

Add pref to enable/disable palette tray.

BUG=616112
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:closure_compilation

Review-Url: https://codereview.chromium.org/2258553004
Cr-Commit-Position: refs/heads/master@{#414733}
parent 85f30039
......@@ -7,6 +7,7 @@
#include "ash/ash_export.h"
#include "base/callback.h"
#include "base/callback_list.h"
#include "base/macros.h"
#include "ui/events/devices/stylus_state.h"
......@@ -16,10 +17,19 @@ namespace ash {
// Chrome-specific actions.
class PaletteDelegate {
public:
using EnableListener = base::Callback<void(bool)>;
using EnableListenerSubscription =
base::CallbackList<void(bool)>::Subscription;
using OnStylusStateChangedCallback = base::Callback<void(ui::StylusState)>;
virtual ~PaletteDelegate() {}
// Sets callback function that will receive the current state of the palette
// enabled pref. The callback will be invoked once the initial pref value is
// available.
virtual std::unique_ptr<EnableListenerSubscription> AddPaletteEnableListener(
const EnableListener& on_state_changed) = 0;
// Create a new note.
virtual void CreateNote() = 0;
......
......@@ -16,8 +16,10 @@ ScopedSessionStateObserver::ScopedSessionStateObserver(
}
ScopedSessionStateObserver::~ScopedSessionStateObserver() {
WmShell::Get()->GetSessionStateDelegate()->RemoveSessionStateObserver(
observer_);
if (WmShell::Get()) {
WmShell::Get()->GetSessionStateDelegate()->RemoveSessionStateObserver(
observer_);
}
}
} // namespace ash
......@@ -4,7 +4,6 @@
#include "ash/common/system/chromeos/palette/palette_tray.h"
#include "ash/common/palette_delegate.h"
#include "ash/common/shelf/shelf_constants.h"
#include "ash/common/shelf/wm_shelf.h"
#include "ash/common/shelf/wm_shelf_util.h"
......@@ -124,6 +123,9 @@ PaletteTray::PaletteTray(WmShelf* wm_shelf)
: TrayBackgroundView(wm_shelf),
palette_tool_manager_(new PaletteToolManager(this)),
weak_factory_(this) {
// PaletteTray should only be instantiated if the palette feature is enabled.
DCHECK(IsPaletteFeatureEnabled());
PaletteTool::RegisterToolInstances(palette_tool_manager_.get());
SetContentsBackground();
......@@ -143,7 +145,12 @@ PaletteTray::PaletteTray(WmShelf* wm_shelf)
weak_factory_.GetWeakPtr()));
}
UpdateIconVisibility();
// OnPaletteEnabledPrefChanged will get called with the initial pref value,
// which will take care of showing the palette.
palette_enabled_subscription_ =
WmShell::Get()->palette_delegate()->AddPaletteEnableListener(
base::Bind(&PaletteTray::OnPaletteEnabledPrefChanged,
weak_factory_.GetWeakPtr()));
}
PaletteTray::~PaletteTray() {
......@@ -333,10 +340,14 @@ void PaletteTray::OnStylusStateChanged(ui::StylusState stylus_state) {
bubble_.reset();
}
void PaletteTray::UpdateIconVisibility() {
if (!IsPaletteEnabled())
return;
void PaletteTray::OnPaletteEnabledPrefChanged(bool enabled) {
if (!enabled)
SetVisible(false);
else
UpdateIconVisibility();
}
void PaletteTray::UpdateIconVisibility() {
SessionStateDelegate* session_state_delegate =
WmShell::Get()->GetSessionStateDelegate();
......
......@@ -9,13 +9,13 @@
#include <memory>
#include "ash/ash_export.h"
#include "ash/common/palette_delegate.h"
#include "ash/common/session/session_state_observer.h"
#include "ash/common/shell_observer.h"
#include "ash/common/system/chromeos/palette/palette_tool_manager.h"
#include "ash/common/system/tray/tray_background_view.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "ui/events/devices/input_device_manager.h"
namespace views {
class ImageView;
......@@ -90,12 +90,20 @@ class ASH_EXPORT PaletteTray : public TrayBackgroundView,
// Called when a stylus inserted or removed event is received.
void OnStylusStateChanged(ui::StylusState stylus_state);
// Called when the palette enabled pref has changed.
void OnPaletteEnabledPrefChanged(bool enabled);
bool OpenBubble();
void AddToolsToView(views::View* host);
std::unique_ptr<PaletteToolManager> palette_tool_manager_;
std::unique_ptr<TrayBubbleWrapper> bubble_;
// Manages the callback OnPaletteEnabledPrefChanged callback registered to
// the PaletteDelegate instance.
std::unique_ptr<PaletteDelegate::EnableListenerSubscription>
palette_enabled_subscription_;
// Weak pointer, will be parented by TrayContainer for its lifetime.
views::ImageView* icon_;
......
......@@ -9,7 +9,7 @@
namespace ash {
bool IsPaletteEnabled() {
bool IsPaletteFeatureEnabled() {
return base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kAshEnablePalette);
}
......
......@@ -9,8 +9,9 @@
namespace ash {
// Returns true if the palette is enabled.
ASH_EXPORT bool IsPaletteEnabled();
// Returns true if the palette feature is enabled. The palette itself may have
// been disabled by the user.
ASH_EXPORT bool IsPaletteFeatureEnabled();
} // namespace ash
......
......@@ -22,6 +22,7 @@
#if defined(OS_CHROMEOS)
#include "ash/common/system/chromeos/ime_menu/ime_menu_tray.h"
#include "ash/common/system/chromeos/palette/palette_tray.h"
#include "ash/common/system/chromeos/palette/palette_utils.h"
#include "ash/common/system/chromeos/session/logout_button_tray.h"
#include "ash/common/system/chromeos/virtual_keyboard/virtual_keyboard_tray.h"
#endif
......@@ -200,7 +201,8 @@ void StatusAreaWidget::SchedulePaint() {
virtual_keyboard_tray_->SchedulePaint();
logout_button_tray_->SchedulePaint();
ime_menu_tray_->SchedulePaint();
palette_tray_->SchedulePaint();
if (palette_tray_)
palette_tray_->SchedulePaint();
#endif
overview_button_tray_->SchedulePaint();
}
......@@ -224,7 +226,8 @@ void StatusAreaWidget::UpdateShelfItemBackground(int alpha) {
virtual_keyboard_tray_->UpdateShelfItemBackground(alpha);
logout_button_tray_->UpdateShelfItemBackground(alpha);
ime_menu_tray_->UpdateShelfItemBackground(alpha);
palette_tray_->UpdateShelfItemBackground(alpha);
if (palette_tray_)
palette_tray_->UpdateShelfItemBackground(alpha);
#endif
overview_button_tray_->UpdateShelfItemBackground(alpha);
}
......@@ -248,8 +251,10 @@ void StatusAreaWidget::AddLogoutButtonTray() {
}
void StatusAreaWidget::AddPaletteTray() {
palette_tray_ = new PaletteTray(wm_shelf_);
status_area_widget_delegate_->AddTray(palette_tray_);
if (IsPaletteFeatureEnabled()) {
palette_tray_ = new PaletteTray(wm_shelf_);
status_area_widget_delegate_->AddTray(palette_tray_);
}
}
void StatusAreaWidget::AddVirtualKeyboardTray() {
......
......@@ -10,6 +10,12 @@ TestPaletteDelegate::TestPaletteDelegate() {}
TestPaletteDelegate::~TestPaletteDelegate() {}
std::unique_ptr<PaletteDelegate::EnableListenerSubscription>
TestPaletteDelegate::AddPaletteEnableListener(
const EnableListener& on_state_changed) {
return nullptr;
}
void TestPaletteDelegate::CreateNote() {
++create_note_count_;
}
......
......@@ -36,6 +36,8 @@ class TestPaletteDelegate : public PaletteDelegate {
private:
// PaletteDelegate:
std::unique_ptr<EnableListenerSubscription> AddPaletteEnableListener(
const EnableListener& on_state_changed) override;
void CreateNote() override;
bool HasNoteApp() override;
void SetPartialMagnifierState(bool enabled) override;
......
......@@ -85,6 +85,11 @@ class PaletteDelegateImpl : public PaletteDelegate {
~PaletteDelegateImpl() override {}
// PaletteDelegate:
std::unique_ptr<EnableListenerSubscription> AddPaletteEnableListener(
const EnableListener& on_state_changed) override {
on_state_changed.Run(false);
return nullptr;
}
void CreateNote() override {}
bool HasNoteApp() override { return false; }
void SetPartialMagnifierState(bool enabled) override {}
......
......@@ -69,7 +69,7 @@ const extensions::Extension* GetApp(Profile* profile) {
bool IsNoteTakingAppAvailable(Profile* profile) {
DCHECK(profile);
return ash::IsPaletteEnabled() && GetApp(profile);
return ash::IsPaletteFeatureEnabled() && GetApp(profile);
}
void LaunchNoteTakingAppForNewNote(Profile* profile,
......
......@@ -281,6 +281,9 @@ void Preferences::RegisterProfilePrefs(
language_prefs::kXkbAutoRepeatIntervalInMs,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
registry->RegisterBooleanPref(
prefs::kEnableStylusTools, true,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
registry->RegisterBooleanPref(
prefs::kLaunchPaletteOnEjectEvent, true,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
......
......@@ -254,13 +254,17 @@ const PrefsUtil::TypedPrefMap& PrefsUtil::GetWhitelistedKeys() {
(*s_whitelist)["settings.enable_screen_lock"] =
settings_private::PrefType::PREF_TYPE_BOOLEAN;
// Ash settings.
(*s_whitelist)["settings.enable_stylus_tools"] =
settings_private::PrefType::PREF_TYPE_BOOLEAN;
(*s_whitelist)["settings.launch_palette_on_eject_event"] =
settings_private::PrefType::PREF_TYPE_BOOLEAN;
// Input method settings.
(*s_whitelist)["settings.language.preload_engines"] =
settings_private::PrefType::PREF_TYPE_STRING;
(*s_whitelist)["settings.language.enabled_extension_imes"] =
settings_private::PrefType::PREF_TYPE_STRING;
(*s_whitelist)["settings.launch_palette_on_eject_event"] =
settings_private::PrefType::PREF_TYPE_BOOLEAN;
// Device settings.
(*s_whitelist)["settings.touchpad.enable_tap_to_click"] =
......
......@@ -4,6 +4,12 @@
<div class="content-area">
<div class="option-control-table">
<div class="option-name">
<div class="checkbox controlled-setting-with-label">
<label>
<input pref="settings.enable_stylus_tools" type="checkbox">
<span i18n-content="stylusEnableStylusTools"></span>
</label>
</div>
<div class="checkbox controlled-setting-with-label">
<label>
<input pref="settings.launch_palette_on_eject_event"
......
......@@ -8,6 +8,13 @@
<style include="settings-shared"></style>
<div class="settings-box">
<settings-checkbox
pref="{{prefs.settings.enable_stylus_tools}}"
label="$i18n{stylusEnableStylusTools}">
</settings-checkbox>
</div>
<div class="settings-box continuation">
<settings-checkbox
pref="{{prefs.settings.launch_palette_on_eject_event}}"
label="$i18n{stylusAutoOpenStylusTools}">
......
......@@ -9,22 +9,27 @@
#include "ash/screenshot_delegate.h"
#include "ash/shell.h"
#include "ash/utility/screenshot_controller.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/note_taking_app_utils.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "ui/events/devices/input_device_manager.h"
namespace chromeos {
namespace {
Profile* GetProfile() {
return ProfileManager::GetActiveUserProfile();
}
} // namespace
PaletteDelegateChromeOS::PaletteDelegateChromeOS() {
registrar_.Add(this, chrome::NOTIFICATION_SESSION_STARTED,
content::NotificationService::AllSources());
registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
content::NotificationService::AllSources());
ui::InputDeviceManager::GetInstance()->AddObserver(this);
}
......@@ -32,12 +37,83 @@ PaletteDelegateChromeOS::~PaletteDelegateChromeOS() {
ui::InputDeviceManager::GetInstance()->RemoveObserver(this);
}
std::unique_ptr<PaletteDelegateChromeOS::EnableListenerSubscription>
PaletteDelegateChromeOS::AddPaletteEnableListener(
const EnableListener& on_state_changed) {
auto subscription = palette_enabled_callback_list_.Add(on_state_changed);
OnPaletteEnabledPrefChanged();
return subscription;
}
void PaletteDelegateChromeOS::CreateNote() {
chromeos::LaunchNoteTakingAppForNewNote(GetProfile(), base::FilePath());
if (!profile_)
return;
chromeos::LaunchNoteTakingAppForNewNote(profile_, base::FilePath());
}
bool PaletteDelegateChromeOS::HasNoteApp() {
return chromeos::IsNoteTakingAppAvailable(GetProfile());
if (!profile_)
return false;
return chromeos::IsNoteTakingAppAvailable(profile_);
}
void PaletteDelegateChromeOS::ActiveUserChanged(const AccountId& account_id) {
const user_manager::User* user =
user_manager::UserManager::Get()->FindUser(account_id);
Profile* profile = ProfileHelper::Get()->GetProfileByUser(user);
SetProfile(profile);
}
void PaletteDelegateChromeOS::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_SESSION_STARTED:
// Update |profile_| when entering a session.
SetProfile(ProfileManager::GetActiveUserProfile());
// Add a session state observer to be able to monitor session changes.
if (!session_state_observer_.get() && ash::Shell::HasInstance()) {
session_state_observer_.reset(
new ash::ScopedSessionStateObserver(this));
}
break;
case chrome::NOTIFICATION_PROFILE_DESTROYED: {
// Update |profile_| when exiting a session or shutting down.
Profile* profile = content::Source<Profile>(source).ptr();
if (profile_ == profile)
SetProfile(nullptr);
break;
}
}
}
void PaletteDelegateChromeOS::OnPaletteEnabledPrefChanged() {
if (profile_) {
palette_enabled_callback_list_.Notify(
profile_->GetPrefs()->GetBoolean(prefs::kEnableStylusTools));
}
}
void PaletteDelegateChromeOS::SetProfile(Profile* profile) {
profile_ = profile;
pref_change_registrar_.reset();
if (!profile_)
return;
PrefService* prefs = profile_->GetPrefs();
pref_change_registrar_.reset(new PrefChangeRegistrar);
pref_change_registrar_->Init(prefs);
pref_change_registrar_->Add(
prefs::kEnableStylusTools,
base::Bind(&PaletteDelegateChromeOS::OnPaletteEnabledPrefChanged,
base::Unretained(this)));
// Run listener with new pref value, if any.
OnPaletteEnabledPrefChanged();
}
void PaletteDelegateChromeOS::SetPartialMagnifierState(bool enabled) {
......@@ -52,8 +128,10 @@ void PaletteDelegateChromeOS::SetStylusStateChangedCallback(
}
bool PaletteDelegateChromeOS::ShouldAutoOpenPalette() {
return GetProfile()->GetPrefs()->GetBoolean(
prefs::kLaunchPaletteOnEjectEvent);
if (!profile_)
return false;
return profile_->GetPrefs()->GetBoolean(prefs::kLaunchPaletteOnEjectEvent);
}
void PaletteDelegateChromeOS::TakeScreenshot() {
......
......@@ -8,22 +8,36 @@
#include <string>
#include "ash/common/palette_delegate.h"
#include "base/compiler_specific.h"
#include "ash/common/session/session_state_observer.h"
#include "base/callback_list.h"
#include "base/macros.h"
#include "base/values.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "ui/events/devices/input_device_event_observer.h"
class PrefChangeRegistrar;
class Profile;
namespace ash {
class ScopedSessionStateObserver;
}
namespace chromeos {
// A class which allows the Ash palette to perform chrome actions.
class PaletteDelegateChromeOS : public ash::PaletteDelegate,
public ui::InputDeviceEventObserver {
public ui::InputDeviceEventObserver,
public ash::SessionStateObserver,
public content::NotificationObserver {
public:
PaletteDelegateChromeOS();
~PaletteDelegateChromeOS() override;
private:
// ash::PaletteDelegate:
std::unique_ptr<EnableListenerSubscription> AddPaletteEnableListener(
const EnableListener& on_state_changed) override;
void CreateNote() override;
bool HasNoteApp() override;
void SetPartialMagnifierState(bool enabled) override;
......@@ -33,11 +47,31 @@ class PaletteDelegateChromeOS : public ash::PaletteDelegate,
void TakeScreenshot() override;
void TakePartialScreenshot() override;
// ash::SessionStateObserver:
void ActiveUserChanged(const AccountId& account_id) override;
// content::NotificationObserver:
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
// ui::InputDeviceObserver:
void OnStylusStateChanged(ui::StylusState state) override;
// Called when the palette enabled pref has changed.
void OnPaletteEnabledPrefChanged();
void SetProfile(Profile* profile);
base::CallbackList<void(bool)> palette_enabled_callback_list_;
OnStylusStateChangedCallback on_stylus_state_changed_;
// Unowned pointer to the active profile.
Profile* profile_ = nullptr;
std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
std::unique_ptr<ash::ScopedSessionStateObserver> session_state_observer_;
content::NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(PaletteDelegateChromeOS);
};
......
......@@ -27,11 +27,15 @@ void OptionsStylusHandler::GetLocalizedValues(
localized_strings->SetString(
"stylusAutoOpenStylusTools",
l10n_util::GetStringUTF16(IDS_SETTINGS_STYLUS_AUTO_OPEN_STYLUS_TOOLS));
localized_strings->SetString(
"stylusEnableStylusTools",
l10n_util::GetStringUTF16(IDS_SETTINGS_STYLUS_ENABLE_STYLUS_TOOLS));
localized_strings->SetString(
"stylusFindMoreApps",
l10n_util::GetStringUTF16(IDS_SETTINGS_STYLUS_FIND_MORE_APPS));
localized_strings->SetBoolean("showStylusSettings", ash::IsPaletteEnabled());
localized_strings->SetBoolean("showStylusSettings",
ash::IsPaletteFeatureEnabled());
}
void OptionsStylusHandler::InitializePage() {}
......
......@@ -526,14 +526,13 @@ void AddDeviceStrings(content::WebUIDataSource* html_source) {
arraysize(keyboard_strings));
LocalizedString stylus_strings[] = {
{"stylusTitle", IDS_SETTINGS_STYLUS_TITLE},
{"stylusAutoOpenStylusTools", IDS_SETTINGS_STYLUS_AUTO_OPEN_STYLUS_TOOLS},
{"stylusFindMoreApps", IDS_SETTINGS_STYLUS_FIND_MORE_APPS}
};
{"stylusTitle", IDS_SETTINGS_STYLUS_TITLE},
{"stylusEnableStylusTools", IDS_SETTINGS_STYLUS_ENABLE_STYLUS_TOOLS},
{"stylusAutoOpenStylusTools", IDS_SETTINGS_STYLUS_AUTO_OPEN_STYLUS_TOOLS},
{"stylusFindMoreApps", IDS_SETTINGS_STYLUS_FIND_MORE_APPS}};
AddLocalizedStringsBulk(html_source, stylus_strings,
arraysize(stylus_strings));
LocalizedString display_strings[] = {
{"displayTitle", IDS_SETTINGS_DISPLAY_TITLE},
{"displayArrangement", IDS_SETTINGS_DISPLAY_ARRANGEMENT},
......
......@@ -118,7 +118,7 @@ MdSettingsUI::MdSettingsUI(content::WebUI* web_ui, const GURL& url)
if (easy_unlock_handler)
AddSettingsPageUIHandler(easy_unlock_handler);
html_source->AddBoolean("stylusAllowed", ash::IsPaletteEnabled());
html_source->AddBoolean("stylusAllowed", ash::IsPaletteFeatureEnabled());
html_source->AddBoolean("quickUnlockEnabled",
chromeos::IsQuickUnlockEnabled());
#endif
......
......@@ -674,6 +674,9 @@ const char kSecondaryDisplays[] = "settings.display.secondary_displays";
// display orientation, for the internal display.
const char kDisplayRotationLock[] = "settings.display.rotation_lock";
// A boolean pref that specifies if the stylus tools should be enabled/disabled.
const char kEnableStylusTools[] = "settings.enable_stylus_tools";
// A boolean pref that specifies if the ash palette should be launched after an
// eject input event has been received.
const char kLaunchPaletteOnEjectEvent[] =
......
......@@ -245,6 +245,7 @@ extern const char kDisplayPowerState[];
extern const char kDisplayProperties[];
extern const char kSecondaryDisplays[];
extern const char kDisplayRotationLock[];
extern const char kEnableStylusTools[];
extern const char kLaunchPaletteOnEjectEvent[];
extern const char kSessionUserActivitySeen[];
extern const char kSessionStartTime[];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment