summaryrefslogtreecommitdiffstats
path: root/ash/launcher/app_launcher_button.cc
blob: 8bcac90349d1bab57fb36b3b495feea1b758d20d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/launcher/app_launcher_button.h"

#include <algorithm>

#include "ash/launcher/launcher_button_host.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "ui/gfx/canvas_skia.h"

namespace ash {

namespace internal {

const int kImageSize = 32;

AppLauncherButton::AppLauncherButton(views::ButtonListener* listener,
                                     LauncherButtonHost* host)
    : views::ImageButton(listener),
      host_(host) {
  SetImageAlignment(views::ImageButton::ALIGN_CENTER,
                    views::ImageButton::ALIGN_MIDDLE);
  set_accessibility_focusable(true);
}

AppLauncherButton::~AppLauncherButton() {
}

void AppLauncherButton::SetAppImage(const SkBitmap& image) {
  if (image.empty()) {
    // TODO: need an empty image.
    SetImage(BS_NORMAL, &image);
    return;
  }
  // Resize the image maintaining our aspect ratio.
  int pref = kImageSize;
  float aspect_ratio =
      static_cast<float>(image.width()) / static_cast<float>(image.height());
  int height = pref;
  int width = static_cast<int>(aspect_ratio * height);
  if (width > pref) {
    width = pref;
    height = static_cast<int>(width / aspect_ratio);
  }
  if (width == image.width() && height == image.height()) {
    SetImage(BS_NORMAL, &image);
    return;
  }
  gfx::CanvasSkia canvas(gfx::Size(width, height), false);
  canvas.DrawBitmapInt(image, 0, 0, image.width(), image.height(),
                       0, 0, width, height, false);
  SkBitmap resized_image(canvas.ExtractBitmap());
  SetImage(BS_NORMAL, &resized_image);
}

bool AppLauncherButton::OnMousePressed(const views::MouseEvent& event) {
  ImageButton::OnMousePressed(event);
  host_->MousePressedOnButton(this, event);
  return true;
}

void AppLauncherButton::OnMouseReleased(const views::MouseEvent& event) {
  host_->MouseReleasedOnButton(this, false);
  ImageButton::OnMouseReleased(event);
}

void AppLauncherButton::OnMouseCaptureLost() {
  host_->MouseReleasedOnButton(this, true);
  ImageButton::OnMouseCaptureLost();
}

bool AppLauncherButton::OnMouseDragged(const views::MouseEvent& event) {
  ImageButton::OnMouseDragged(event);
  host_->MouseDraggedOnButton(this, event);
  return true;
}

void AppLauncherButton::OnMouseExited(const views::MouseEvent& event) {
  ImageButton::OnMouseExited(event);
  host_->MouseExitedButton(this);
}

void AppLauncherButton::GetAccessibleState(ui::AccessibleViewState* state) {
  state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
  state->name = host_->GetAccessibleName(this);
}

}  // namespace internal
}  // namespace ash