OLD | NEW |
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 Loading... |
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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 | 150 |
150 DISALLOW_COPY_AND_ASSIGN(TitleView); | 151 DISALLOW_COPY_AND_ASSIGN(TitleView); |
151 }; | 152 }; |
152 | 153 |
153 } // namespace | 154 } // namespace |
154 | 155 |
155 PaletteTray::PaletteTray(WmShelf* wm_shelf) | 156 PaletteTray::PaletteTray(WmShelf* wm_shelf) |
156 : TrayBackgroundView(wm_shelf), | 157 : TrayBackgroundView(wm_shelf), |
157 palette_tool_manager_(new PaletteToolManager(this)), | 158 palette_tool_manager_(new PaletteToolManager(this)), |
158 weak_factory_(this) { | 159 weak_factory_(this) { |
159 // PaletteTray should only be instantiated if the palette feature is enabled. | |
160 DCHECK(IsPaletteFeatureEnabled()); | |
161 | |
162 PaletteTool::RegisterToolInstances(palette_tool_manager_.get()); | 160 PaletteTool::RegisterToolInstances(palette_tool_manager_.get()); |
163 | 161 |
164 if (MaterialDesignController::IsShelfMaterial()) { | 162 if (MaterialDesignController::IsShelfMaterial()) { |
165 SetInkDropMode(InkDropMode::ON); | 163 SetInkDropMode(InkDropMode::ON); |
166 SetContentsBackground(false); | 164 SetContentsBackground(false); |
167 } else { | 165 } else { |
168 SetContentsBackground(true); | 166 SetContentsBackground(true); |
169 } | 167 } |
170 | 168 |
171 SetLayoutManager(new views::FillLayout()); | 169 SetLayoutManager(new views::FillLayout()); |
172 icon_ = new views::ImageView(); | 170 icon_ = new views::ImageView(); |
173 UpdateTrayIcon(); | 171 UpdateTrayIcon(); |
174 | 172 |
175 SetIconBorderForShelfAlignment(); | 173 SetIconBorderForShelfAlignment(); |
176 tray_container()->AddChildView(icon_); | 174 tray_container()->AddChildView(icon_); |
177 | 175 |
178 WmShell::Get()->AddShellObserver(this); | 176 WmShell::Get()->AddShellObserver(this); |
179 WmShell::Get()->GetSessionStateDelegate()->AddSessionStateObserver(this); | 177 WmShell::Get()->GetSessionStateDelegate()->AddSessionStateObserver(this); |
180 if (WmShell::Get()->palette_delegate()) { | 178 ui::InputDeviceManager::GetInstance()->AddObserver(this); |
181 WmShell::Get()->palette_delegate()->SetStylusStateChangedCallback( | |
182 base::Bind(&PaletteTray::OnStylusStateChanged, | |
183 weak_factory_.GetWeakPtr())); | |
184 } | |
185 } | 179 } |
186 | 180 |
187 PaletteTray::~PaletteTray() { | 181 PaletteTray::~PaletteTray() { |
188 if (bubble_) | 182 if (bubble_) |
189 bubble_->bubble_view()->reset_delegate(); | 183 bubble_->bubble_view()->reset_delegate(); |
190 | 184 |
| 185 ui::InputDeviceManager::GetInstance()->RemoveObserver(this); |
191 WmShell::Get()->RemoveShellObserver(this); | 186 WmShell::Get()->RemoveShellObserver(this); |
192 WmShell::Get()->GetSessionStateDelegate()->RemoveSessionStateObserver(this); | 187 WmShell::Get()->GetSessionStateDelegate()->RemoveSessionStateObserver(this); |
193 } | 188 } |
194 | 189 |
195 bool PaletteTray::PerformAction(const ui::Event& event) { | 190 bool PaletteTray::PerformAction(const ui::Event& event) { |
196 if (bubble_) { | 191 if (bubble_) { |
197 if (num_actions_in_bubble_ == 0) | 192 if (num_actions_in_bubble_ == 0) |
198 RecordPaletteOptionsUsage(PaletteTrayOptions::PALETTE_CLOSED_NO_ACTION); | 193 RecordPaletteOptionsUsage(PaletteTrayOptions::PALETTE_CLOSED_NO_ACTION); |
199 HidePalette(); | 194 HidePalette(); |
200 return true; | 195 return true; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 | 281 |
287 base::string16 PaletteTray::GetAccessibleNameForTray() { | 282 base::string16 PaletteTray::GetAccessibleNameForTray() { |
288 return l10n_util::GetStringUTF16(IDS_ASH_STYLUS_TOOLS_TITLE); | 283 return l10n_util::GetStringUTF16(IDS_ASH_STYLUS_TOOLS_TITLE); |
289 } | 284 } |
290 | 285 |
291 void PaletteTray::HideBubbleWithView(const views::TrayBubbleView* bubble_view) { | 286 void PaletteTray::HideBubbleWithView(const views::TrayBubbleView* bubble_view) { |
292 if (bubble_->bubble_view() == bubble_view) | 287 if (bubble_->bubble_view() == bubble_view) |
293 HidePalette(); | 288 HidePalette(); |
294 } | 289 } |
295 | 290 |
| 291 void PaletteTray::OnTouchscreenDeviceConfigurationChanged() { |
| 292 UpdateIconVisibility(); |
| 293 } |
| 294 |
| 295 void PaletteTray::OnStylusStateChanged(ui::StylusState stylus_state) { |
| 296 PaletteDelegate* palette_delegate = WmShell::Get()->palette_delegate(); |
| 297 |
| 298 // Don't do anything if the palette should not be shown or if the user has |
| 299 // disabled it all-together. |
| 300 if (!IsInUserSession() || !palette_delegate->ShouldShowPalette()) |
| 301 return; |
| 302 |
| 303 // Auto show/hide the palette if allowed by the user. |
| 304 if (palette_delegate->ShouldAutoOpenPalette()) { |
| 305 if (stylus_state == ui::StylusState::REMOVED && !bubble_) { |
| 306 is_bubble_auto_opened_ = true; |
| 307 ShowPalette(); |
| 308 } else if (stylus_state == ui::StylusState::INSERTED && bubble_) { |
| 309 HidePalette(); |
| 310 } |
| 311 } |
| 312 |
| 313 // Disable any active modes if the stylus has been inserted. |
| 314 if (stylus_state == ui::StylusState::INSERTED) |
| 315 palette_tool_manager_->DisableActiveTool(PaletteGroup::MODE); |
| 316 } |
| 317 |
296 void PaletteTray::BubbleViewDestroyed() { | 318 void PaletteTray::BubbleViewDestroyed() { |
297 palette_tool_manager_->NotifyViewsDestroyed(); | 319 palette_tool_manager_->NotifyViewsDestroyed(); |
298 SetIsActive(false); | 320 SetIsActive(false); |
299 } | 321 } |
300 | 322 |
301 void PaletteTray::OnMouseEnteredView() {} | 323 void PaletteTray::OnMouseEnteredView() {} |
302 | 324 |
303 void PaletteTray::OnMouseExitedView() {} | 325 void PaletteTray::OnMouseExitedView() {} |
304 | 326 |
305 base::string16 PaletteTray::GetAccessibleNameForBubble() { | 327 base::string16 PaletteTray::GetAccessibleNameForBubble() { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 TrayBackgroundView::SetShelfAlignment(alignment); | 400 TrayBackgroundView::SetShelfAlignment(alignment); |
379 SetIconBorderForShelfAlignment(); | 401 SetIconBorderForShelfAlignment(); |
380 } | 402 } |
381 | 403 |
382 void PaletteTray::AnchorUpdated() { | 404 void PaletteTray::AnchorUpdated() { |
383 if (bubble_) | 405 if (bubble_) |
384 bubble_->bubble_view()->UpdateBubble(); | 406 bubble_->bubble_view()->UpdateBubble(); |
385 } | 407 } |
386 | 408 |
387 void PaletteTray::Initialize() { | 409 void PaletteTray::Initialize() { |
| 410 PaletteDelegate* delegate = WmShell::Get()->palette_delegate(); |
| 411 // |delegate| can be null in tests. |
| 412 if (!delegate) |
| 413 return; |
| 414 |
388 // OnPaletteEnabledPrefChanged will get called with the initial pref value, | 415 // OnPaletteEnabledPrefChanged will get called with the initial pref value, |
389 // which will take care of showing the palette. | 416 // which will take care of showing the palette. |
390 palette_enabled_subscription_ = | 417 palette_enabled_subscription_ = delegate->AddPaletteEnableListener(base::Bind( |
391 WmShell::Get()->palette_delegate()->AddPaletteEnableListener( | 418 &PaletteTray::OnPaletteEnabledPrefChanged, weak_factory_.GetWeakPtr())); |
392 base::Bind(&PaletteTray::OnPaletteEnabledPrefChanged, | |
393 weak_factory_.GetWeakPtr())); | |
394 } | 419 } |
395 | 420 |
396 void PaletteTray::SetIconBorderForShelfAlignment() { | 421 void PaletteTray::SetIconBorderForShelfAlignment() { |
397 // TODO(tdanderson): Ensure PaletteTray follows material design specs. See | 422 // TODO(tdanderson): Ensure PaletteTray follows material design specs. See |
398 // crbug.com/630464. | 423 // crbug.com/630464. |
399 if (IsHorizontalAlignment(shelf_alignment())) { | 424 if (IsHorizontalAlignment(shelf_alignment())) { |
400 icon_->SetBorder(views::CreateEmptyBorder(gfx::Insets( | 425 icon_->SetBorder(views::CreateEmptyBorder(gfx::Insets( |
401 kHorizontalShelfVerticalPadding, kHorizontalShelfHorizontalPadding))); | 426 kHorizontalShelfVerticalPadding, kHorizontalShelfHorizontalPadding))); |
402 } else { | 427 } else { |
403 icon_->SetBorder(views::CreateEmptyBorder(gfx::Insets( | 428 icon_->SetBorder(views::CreateEmptyBorder(gfx::Insets( |
404 kVerticalShelfVerticalPadding, kVerticalShelfHorizontalPadding))); | 429 kVerticalShelfVerticalPadding, kVerticalShelfHorizontalPadding))); |
405 } | 430 } |
406 } | 431 } |
407 | 432 |
408 void PaletteTray::UpdateTrayIcon() { | 433 void PaletteTray::UpdateTrayIcon() { |
409 icon_->SetImage(CreateVectorIcon( | 434 icon_->SetImage(CreateVectorIcon( |
410 palette_tool_manager_->GetActiveTrayIcon( | 435 palette_tool_manager_->GetActiveTrayIcon( |
411 palette_tool_manager_->GetActiveTool(ash::PaletteGroup::MODE)), | 436 palette_tool_manager_->GetActiveTool(ash::PaletteGroup::MODE)), |
412 kTrayIconSize, kShelfIconColor)); | 437 kTrayIconSize, kShelfIconColor)); |
413 } | 438 } |
414 | 439 |
415 void PaletteTray::OnStylusStateChanged(ui::StylusState stylus_state) { | |
416 PaletteDelegate* palette_delegate = WmShell::Get()->palette_delegate(); | |
417 | |
418 // Don't do anything if the palette should not be shown or if the user has | |
419 // disabled it all-together. | |
420 if (!IsInUserSession() || !palette_delegate->ShouldShowPalette()) | |
421 return; | |
422 | |
423 // Auto show/hide the palette if allowed by the user. | |
424 if (palette_delegate->ShouldAutoOpenPalette()) { | |
425 if (stylus_state == ui::StylusState::REMOVED && !bubble_) { | |
426 is_bubble_auto_opened_ = true; | |
427 ShowPalette(); | |
428 } else if (stylus_state == ui::StylusState::INSERTED && bubble_) { | |
429 HidePalette(); | |
430 } | |
431 } | |
432 | |
433 // Disable any active modes if the stylus has been inserted. | |
434 if (stylus_state == ui::StylusState::INSERTED) | |
435 palette_tool_manager_->DisableActiveTool(PaletteGroup::MODE); | |
436 } | |
437 | |
438 void PaletteTray::OnPaletteEnabledPrefChanged(bool enabled) { | 440 void PaletteTray::OnPaletteEnabledPrefChanged(bool enabled) { |
439 is_palette_enabled_ = enabled; | 441 is_palette_enabled_ = enabled; |
440 | 442 |
441 if (!enabled) { | 443 if (!enabled) { |
442 SetVisible(false); | 444 SetVisible(false); |
443 palette_tool_manager_->DisableActiveTool(PaletteGroup::MODE); | 445 palette_tool_manager_->DisableActiveTool(PaletteGroup::MODE); |
444 } else { | 446 } else { |
445 UpdateIconVisibility(); | 447 UpdateIconVisibility(); |
446 } | 448 } |
447 } | 449 } |
448 | 450 |
449 void PaletteTray::UpdateIconVisibility() { | 451 void PaletteTray::UpdateIconVisibility() { |
450 SetVisible(is_palette_enabled_ && IsInUserSession()); | 452 SetVisible(is_palette_enabled_ && palette_utils::HasStylusInput() && |
| 453 IsInUserSession()); |
451 } | 454 } |
452 | 455 |
453 } // namespace ash | 456 } // namespace ash |
OLD | NEW |