1 // Copyright 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "ash/system/tray/actionable_view.h" 6 7 #include "ash/ash_constants.h" 8 #include "ui/base/accessibility/accessible_view_state.h" 9 #include "ui/gfx/canvas.h" 10 11 namespace ash { 12 namespace internal { 13 14 // static 15 const char ActionableView::kViewClassName[] = "tray/ActionableView"; 16 ActionableView()17ActionableView::ActionableView() 18 : has_capture_(false) { 19 SetFocusable(true); 20 } 21 ~ActionableView()22ActionableView::~ActionableView() { 23 } 24 OnPaintFocus(gfx::Canvas * canvas)25void ActionableView::OnPaintFocus(gfx::Canvas* canvas) { 26 gfx::Rect rect(GetFocusBounds()); 27 rect.Inset(1, 1, 3, 2); 28 canvas->DrawSolidFocusRect(rect, kFocusBorderColor); 29 } 30 GetFocusBounds()31gfx::Rect ActionableView::GetFocusBounds() { 32 return GetLocalBounds(); 33 } 34 GetClassName() const35const char* ActionableView::GetClassName() const { 36 return kViewClassName; 37 } 38 OnKeyPressed(const ui::KeyEvent & event)39bool ActionableView::OnKeyPressed(const ui::KeyEvent& event) { 40 if (event.key_code() == ui::VKEY_SPACE || 41 event.key_code() == ui::VKEY_RETURN) { 42 return PerformAction(event); 43 } 44 return false; 45 } 46 OnMousePressed(const ui::MouseEvent & event)47bool ActionableView::OnMousePressed(const ui::MouseEvent& event) { 48 // Return true so that this view starts capturing the events. 49 has_capture_ = true; 50 return true; 51 } 52 OnMouseReleased(const ui::MouseEvent & event)53void ActionableView::OnMouseReleased(const ui::MouseEvent& event) { 54 if (has_capture_ && GetLocalBounds().Contains(event.location())) 55 PerformAction(event); 56 } 57 OnMouseCaptureLost()58void ActionableView::OnMouseCaptureLost() { 59 has_capture_ = false; 60 } 61 SetAccessibleName(const base::string16 & name)62void ActionableView::SetAccessibleName(const base::string16& name) { 63 accessible_name_ = name; 64 } 65 GetAccessibleState(ui::AccessibleViewState * state)66void ActionableView::GetAccessibleState(ui::AccessibleViewState* state) { 67 state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON; 68 state->name = accessible_name_; 69 } 70 OnPaint(gfx::Canvas * canvas)71void ActionableView::OnPaint(gfx::Canvas* canvas) { 72 View::OnPaint(canvas); 73 if (HasFocus()) 74 OnPaintFocus(canvas); 75 } 76 OnFocus()77void ActionableView::OnFocus() { 78 View::OnFocus(); 79 // We render differently when focused. 80 SchedulePaint(); 81 } 82 OnBlur()83void ActionableView::OnBlur() { 84 View::OnBlur(); 85 // We render differently when focused. 86 SchedulePaint(); 87 } 88 OnGestureEvent(ui::GestureEvent * event)89void ActionableView::OnGestureEvent(ui::GestureEvent* event) { 90 if (event->type() == ui::ET_GESTURE_TAP && PerformAction(*event)) 91 event->SetHandled(); 92 } 93 94 } // namespace internal 95 } // namespace ash 96