Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(492)

Side by Side Diff: ash/common/system/chromeos/palette/palette_tray.cc

Issue 2644713002: cros: Use runtime stylus detection for ash palette. (Closed)
Patch Set: Initial upload Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/common/system/chromeos/palette/palette_tray.h" 5 #include "ash/common/system/chromeos/palette/palette_tray.h"
6 6
7 #include "ash/common/material_design/material_design_controller.h" 7 #include "ash/common/material_design/material_design_controller.h"
8 #include "ash/common/session/session_state_delegate.h" 8 #include "ash/common/session/session_state_delegate.h"
9 #include "ash/common/shelf/shelf_constants.h" 9 #include "ash/common/shelf/shelf_constants.h"
10 #include "ash/common/shelf/wm_shelf.h" 10 #include "ash/common/shelf/wm_shelf.h"
(...skipping 11 matching lines...) Expand all
22 #include "ash/common/wm_shell.h" 22 #include "ash/common/wm_shell.h"
23 #include "ash/common/wm_window.h" 23 #include "ash/common/wm_window.h"
24 #include "ash/public/cpp/shell_window_ids.h" 24 #include "ash/public/cpp/shell_window_ids.h"
25 #include "ash/resources/vector_icons/vector_icons.h" 25 #include "ash/resources/vector_icons/vector_icons.h"
26 #include "ash/root_window_controller.h" 26 #include "ash/root_window_controller.h"
27 #include "base/metrics/histogram_macros.h" 27 #include "base/metrics/histogram_macros.h"
28 #include "grit/ash_resources.h" 28 #include "grit/ash_resources.h"
29 #include "grit/ash_strings.h" 29 #include "grit/ash_strings.h"
30 #include "ui/base/l10n/l10n_util.h" 30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h" 31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/events/devices/input_device_manager.h"
32 #include "ui/events/devices/stylus_state.h" 33 #include "ui/events/devices/stylus_state.h"
33 #include "ui/gfx/color_palette.h" 34 #include "ui/gfx/color_palette.h"
34 #include "ui/gfx/paint_vector_icon.h" 35 #include "ui/gfx/paint_vector_icon.h"
35 #include "ui/gfx/vector_icons_public.h" 36 #include "ui/gfx/vector_icons_public.h"
36 #include "ui/views/controls/image_view.h" 37 #include "ui/views/controls/image_view.h"
37 #include "ui/views/controls/label.h" 38 #include "ui/views/controls/label.h"
38 #include "ui/views/controls/separator.h" 39 #include "ui/views/controls/separator.h"
39 #include "ui/views/layout/box_layout.h" 40 #include "ui/views/layout/box_layout.h"
40 #include "ui/views/layout/fill_layout.h" 41 #include "ui/views/layout/fill_layout.h"
41 42
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 161
161 DISALLOW_COPY_AND_ASSIGN(TitleView); 162 DISALLOW_COPY_AND_ASSIGN(TitleView);
162 }; 163 };
163 164
164 } // namespace 165 } // namespace
165 166
166 PaletteTray::PaletteTray(WmShelf* wm_shelf) 167 PaletteTray::PaletteTray(WmShelf* wm_shelf)
167 : TrayBackgroundView(wm_shelf), 168 : TrayBackgroundView(wm_shelf),
168 palette_tool_manager_(new PaletteToolManager(this)), 169 palette_tool_manager_(new PaletteToolManager(this)),
169 weak_factory_(this) { 170 weak_factory_(this) {
170 // PaletteTray should only be instantiated if the palette feature is enabled.
171 DCHECK(IsPaletteFeatureEnabled());
172
173 PaletteTool::RegisterToolInstances(palette_tool_manager_.get()); 171 PaletteTool::RegisterToolInstances(palette_tool_manager_.get());
174 172
175 if (MaterialDesignController::IsShelfMaterial()) { 173 if (MaterialDesignController::IsShelfMaterial()) {
176 SetInkDropMode(InkDropMode::ON); 174 SetInkDropMode(InkDropMode::ON);
177 SetContentsBackground(false); 175 SetContentsBackground(false);
178 } else { 176 } else {
179 SetContentsBackground(true); 177 SetContentsBackground(true);
180 } 178 }
181 179
182 SetLayoutManager(new views::FillLayout()); 180 SetLayoutManager(new views::FillLayout());
183 icon_ = new views::ImageView(); 181 icon_ = new views::ImageView();
184 UpdateTrayIcon(); 182 UpdateTrayIcon();
185 183
186 SetIconBorderForShelfAlignment(); 184 SetIconBorderForShelfAlignment();
187 tray_container()->AddChildView(icon_); 185 tray_container()->AddChildView(icon_);
188 186
189 WmShell::Get()->AddShellObserver(this); 187 WmShell::Get()->AddShellObserver(this);
190 WmShell::Get()->GetSessionStateDelegate()->AddSessionStateObserver(this); 188 WmShell::Get()->GetSessionStateDelegate()->AddSessionStateObserver(this);
191 if (WmShell::Get()->palette_delegate()) { 189 ui::InputDeviceManager::GetInstance()->AddObserver(this);
192 WmShell::Get()->palette_delegate()->SetStylusStateChangedCallback(
193 base::Bind(&PaletteTray::OnStylusStateChanged,
194 weak_factory_.GetWeakPtr()));
195 }
196 } 190 }
197 191
198 PaletteTray::~PaletteTray() { 192 PaletteTray::~PaletteTray() {
199 if (bubble_) 193 if (bubble_)
200 bubble_->bubble_view()->reset_delegate(); 194 bubble_->bubble_view()->reset_delegate();
201 195
196 ui::InputDeviceManager::GetInstance()->RemoveObserver(this);
202 WmShell::Get()->RemoveShellObserver(this); 197 WmShell::Get()->RemoveShellObserver(this);
203 WmShell::Get()->GetSessionStateDelegate()->RemoveSessionStateObserver(this); 198 WmShell::Get()->GetSessionStateDelegate()->RemoveSessionStateObserver(this);
204 } 199 }
205 200
206 bool PaletteTray::PerformAction(const ui::Event& event) { 201 bool PaletteTray::PerformAction(const ui::Event& event) {
207 if (bubble_) { 202 if (bubble_) {
208 if (num_actions_in_bubble_ == 0) 203 if (num_actions_in_bubble_ == 0)
209 RecordPaletteOptionsUsage(PaletteTrayOptions::PALETTE_CLOSED_NO_ACTION); 204 RecordPaletteOptionsUsage(PaletteTrayOptions::PALETTE_CLOSED_NO_ACTION);
210 HidePalette(); 205 HidePalette();
211 return true; 206 return true;
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 292
298 base::string16 PaletteTray::GetAccessibleNameForTray() { 293 base::string16 PaletteTray::GetAccessibleNameForTray() {
299 return l10n_util::GetStringUTF16(IDS_ASH_STYLUS_TOOLS_TITLE); 294 return l10n_util::GetStringUTF16(IDS_ASH_STYLUS_TOOLS_TITLE);
300 } 295 }
301 296
302 void PaletteTray::HideBubbleWithView(const views::TrayBubbleView* bubble_view) { 297 void PaletteTray::HideBubbleWithView(const views::TrayBubbleView* bubble_view) {
303 if (bubble_->bubble_view() == bubble_view) 298 if (bubble_->bubble_view() == bubble_view)
304 HidePalette(); 299 HidePalette();
305 } 300 }
306 301
302 void PaletteTray::OnTouchscreenDeviceConfigurationChanged() {
303 UpdateIconVisibility();
304 }
305
306 void PaletteTray::OnStylusStateChanged(ui::StylusState stylus_state) {
307 PaletteDelegate* palette_delegate = WmShell::Get()->palette_delegate();
308
309 // Don't do anything if the palette should not be shown or if the user has
310 // disabled it all-together.
311 if (!IsInUserSession() || !palette_delegate->ShouldShowPalette())
312 return;
313
314 // Auto show/hide the palette if allowed by the user.
315 if (palette_delegate->ShouldAutoOpenPalette()) {
316 if (stylus_state == ui::StylusState::REMOVED && !bubble_) {
317 is_bubble_auto_opened_ = true;
318 ShowPalette();
319 } else if (stylus_state == ui::StylusState::INSERTED && bubble_) {
320 HidePalette();
321 }
322 }
323
324 // Disable any active modes if the stylus has been inserted.
325 if (stylus_state == ui::StylusState::INSERTED)
326 palette_tool_manager_->DisableActiveTool(PaletteGroup::MODE);
327 }
328
307 void PaletteTray::BubbleViewDestroyed() { 329 void PaletteTray::BubbleViewDestroyed() {
308 palette_tool_manager_->NotifyViewsDestroyed(); 330 palette_tool_manager_->NotifyViewsDestroyed();
309 SetIsActive(false); 331 SetIsActive(false);
310 } 332 }
311 333
312 void PaletteTray::OnMouseEnteredView() {} 334 void PaletteTray::OnMouseEnteredView() {}
313 335
314 void PaletteTray::OnMouseExitedView() {} 336 void PaletteTray::OnMouseExitedView() {}
315 337
316 base::string16 PaletteTray::GetAccessibleNameForBubble() { 338 base::string16 PaletteTray::GetAccessibleNameForBubble() {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 TrayBackgroundView::SetShelfAlignment(alignment); 411 TrayBackgroundView::SetShelfAlignment(alignment);
390 SetIconBorderForShelfAlignment(); 412 SetIconBorderForShelfAlignment();
391 } 413 }
392 414
393 void PaletteTray::AnchorUpdated() { 415 void PaletteTray::AnchorUpdated() {
394 if (bubble_) 416 if (bubble_)
395 bubble_->bubble_view()->UpdateBubble(); 417 bubble_->bubble_view()->UpdateBubble();
396 } 418 }
397 419
398 void PaletteTray::Initialize() { 420 void PaletteTray::Initialize() {
399 // OnPaletteEnabledPrefChanged will get called with the initial pref value, 421 PaletteDelegate* delegate = WmShell::Get()->palette_delegate();
400 // which will take care of showing the palette. 422 if (delegate) {
stevenjb 2017/01/21 00:23:50 if (!delegate) return;
jdufault 2017/01/25 00:53:15 Done.
401 palette_enabled_subscription_ = 423 // OnPaletteEnabledPrefChanged will get called with the initial pref value,
402 WmShell::Get()->palette_delegate()->AddPaletteEnableListener( 424 // which will take care of showing the palette.
403 base::Bind(&PaletteTray::OnPaletteEnabledPrefChanged, 425 palette_enabled_subscription_ = delegate->AddPaletteEnableListener(
404 weak_factory_.GetWeakPtr())); 426 base::Bind(&PaletteTray::OnPaletteEnabledPrefChanged,
427 weak_factory_.GetWeakPtr()));
428 }
405 } 429 }
406 430
407 void PaletteTray::SetIconBorderForShelfAlignment() { 431 void PaletteTray::SetIconBorderForShelfAlignment() {
408 // TODO(tdanderson): Ensure PaletteTray follows material design specs. See 432 // TODO(tdanderson): Ensure PaletteTray follows material design specs. See
409 // crbug.com/630464. 433 // crbug.com/630464.
410 if (IsHorizontalAlignment(shelf_alignment())) { 434 if (IsHorizontalAlignment(shelf_alignment())) {
411 icon_->SetBorder(views::CreateEmptyBorder(gfx::Insets( 435 icon_->SetBorder(views::CreateEmptyBorder(gfx::Insets(
412 kHorizontalShelfVerticalPadding, kHorizontalShelfHorizontalPadding))); 436 kHorizontalShelfVerticalPadding, kHorizontalShelfHorizontalPadding)));
413 } else { 437 } else {
414 icon_->SetBorder(views::CreateEmptyBorder(gfx::Insets( 438 icon_->SetBorder(views::CreateEmptyBorder(gfx::Insets(
415 kVerticalShelfVerticalPadding, kVerticalShelfHorizontalPadding))); 439 kVerticalShelfVerticalPadding, kVerticalShelfHorizontalPadding)));
416 } 440 }
417 } 441 }
418 442
419 void PaletteTray::UpdateTrayIcon() { 443 void PaletteTray::UpdateTrayIcon() {
420 icon_->SetImage(CreateVectorIcon( 444 icon_->SetImage(CreateVectorIcon(
421 palette_tool_manager_->GetActiveTrayIcon( 445 palette_tool_manager_->GetActiveTrayIcon(
422 palette_tool_manager_->GetActiveTool(ash::PaletteGroup::MODE)), 446 palette_tool_manager_->GetActiveTool(ash::PaletteGroup::MODE)),
423 kTrayIconSize, kShelfIconColor)); 447 kTrayIconSize, kShelfIconColor));
424 } 448 }
425 449
426 void PaletteTray::OnStylusStateChanged(ui::StylusState stylus_state) {
427 PaletteDelegate* palette_delegate = WmShell::Get()->palette_delegate();
428
429 // Don't do anything if the palette should not be shown or if the user has
430 // disabled it all-together.
431 if (!IsInUserSession() || !palette_delegate->ShouldShowPalette())
432 return;
433
434 // Auto show/hide the palette if allowed by the user.
435 if (palette_delegate->ShouldAutoOpenPalette()) {
436 if (stylus_state == ui::StylusState::REMOVED && !bubble_) {
437 is_bubble_auto_opened_ = true;
438 ShowPalette();
439 } else if (stylus_state == ui::StylusState::INSERTED && bubble_) {
440 HidePalette();
441 }
442 }
443
444 // Disable any active modes if the stylus has been inserted.
445 if (stylus_state == ui::StylusState::INSERTED)
446 palette_tool_manager_->DisableActiveTool(PaletteGroup::MODE);
447 }
448
449 void PaletteTray::OnPaletteEnabledPrefChanged(bool enabled) { 450 void PaletteTray::OnPaletteEnabledPrefChanged(bool enabled) {
450 is_palette_enabled_ = enabled; 451 is_palette_enabled_ = enabled;
451 452
452 if (!enabled) { 453 if (!enabled) {
453 SetVisible(false); 454 SetVisible(false);
454 palette_tool_manager_->DisableActiveTool(PaletteGroup::MODE); 455 palette_tool_manager_->DisableActiveTool(PaletteGroup::MODE);
455 } else { 456 } else {
456 UpdateIconVisibility(); 457 UpdateIconVisibility();
457 } 458 }
458 } 459 }
459 460
460 void PaletteTray::UpdateIconVisibility() { 461 void PaletteTray::UpdateIconVisibility() {
461 SetVisible(is_palette_enabled_ && IsInUserSession()); 462 SetVisible(is_palette_enabled_ && HasStylusInput() && IsInUserSession());
462 } 463 }
463 464
464 } // namespace ash 465 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698