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 17 ActionableView::ActionableView() 18 : has_capture_(false) { 19 set_focusable(true); 20 } 21 22 ActionableView::~ActionableView() { 23 } 24 25 void ActionableView::DrawBorder(gfx::Canvas* canvas, const gfx::Rect& bounds) { 26 gfx::Rect rect = bounds; 27 rect.Inset(1, 1, 3, 3); 28 canvas->DrawRect(rect, kFocusBorderColor); 29 } 30 31 const char* ActionableView::GetClassName() const { 32 return kViewClassName; 33 } 34 35 bool ActionableView::OnKeyPressed(const ui::KeyEvent& event) { 36 if (event.key_code() == ui::VKEY_SPACE || 37 event.key_code() == ui::VKEY_RETURN) { 38 return PerformAction(event); 39 } 40 return false; 41 } 42 43 bool ActionableView::OnMousePressed(const ui::MouseEvent& event) { 44 // Return true so that this view starts capturing the events. 45 has_capture_ = true; 46 return true; 47 } 48 49 void ActionableView::OnMouseReleased(const ui::MouseEvent& event) { 50 if (has_capture_ && GetLocalBounds().Contains(event.location())) 51 PerformAction(event); 52 } 53 54 void ActionableView::OnMouseCaptureLost() { 55 has_capture_ = false; 56 } 57 58 void ActionableView::SetAccessibleName(const base::string16& name) { 59 accessible_name_ = name; 60 } 61 62 void ActionableView::OnPaintFocusBorder(gfx::Canvas* canvas) { 63 if (HasFocus() && (focusable() || IsAccessibilityFocusable())) 64 DrawBorder(canvas, GetLocalBounds()); 65 } 66 67 void ActionableView::OnGestureEvent(ui::GestureEvent* event) { 68 if (event->type() == ui::ET_GESTURE_TAP && PerformAction(*event)) 69 event->SetHandled(); 70 } 71 72 void ActionableView::GetAccessibleState(ui::AccessibleViewState* state) { 73 state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON; 74 state->name = accessible_name_; 75 } 76 77 } // namespace internal 78 } // namespace ash 79