summaryrefslogtreecommitdiffstats
path: root/ui/views/controls/button/label_button_border.cc
blob: 4839716f1cde8dc168357c4e67d04b4af5356399 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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 "ui/views/controls/button/label_button_border.h"

#include "base/logging.h"
#include "ui/base/animation/animation.h"
#include "ui/base/native_theme/native_theme.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/rect.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/native_theme_delegate.h"

namespace views {

namespace {

// Preferred padding between content and edge.
static const int kPreferredPaddingHorizontal = 6;
static const int kPreferredPaddingVertical = 5;

// Preferred padding between content and edge for NativeTheme border.
static const int kPreferredNativeThemePaddingHorizontal = 12;
static const int kPreferredNativeThemePaddingVertical = 5;

views::CustomButton::ButtonState GetButtonState(ui::NativeTheme::State state) {
  switch(state) {
    case ui::NativeTheme::kDisabled: return views::CustomButton::STATE_DISABLED;
    case ui::NativeTheme::kHovered:  return views::CustomButton::STATE_HOVERED;
    case ui::NativeTheme::kNormal:   return views::CustomButton::STATE_NORMAL;
    case ui::NativeTheme::kPressed:  return views::CustomButton::STATE_PRESSED;
    case ui::NativeTheme::kMaxState: NOTREACHED() << "Unknown state: " << state;
  }
  return views::CustomButton::STATE_NORMAL;
}

// A helper function to paint the native theme or images as appropriate.
void PaintHelper(LabelButtonBorder* border,
                 gfx::Canvas* canvas,
                 const ui::NativeTheme* theme,
                 ui::NativeTheme::Part part,
                 ui::NativeTheme::State state,
                 const gfx::Rect& rect,
                 const ui::NativeTheme::ExtraParams& extra) {
  if (border->native_theme())
    theme->Paint(canvas->sk_canvas(), part, state, rect, extra);
  else
    border->GetImages(GetButtonState(state))->Paint(canvas, rect.size());
}

}  // namespace

LabelButtonBorder::LabelButtonBorder() : native_theme_(false) {
  SetImages(CustomButton::STATE_HOVERED, BorderImages(BorderImages::kHot));
  SetImages(CustomButton::STATE_PRESSED, BorderImages(BorderImages::kPushed));
}

LabelButtonBorder::~LabelButtonBorder() {}

void LabelButtonBorder::Paint(const View& view, gfx::Canvas* canvas) {
  const NativeThemeDelegate* native_theme_delegate =
      static_cast<const LabelButton*>(&view);
  ui::NativeTheme::Part part = native_theme_delegate->GetThemePart();
  gfx::Rect rect(native_theme_delegate->GetThemePaintRect());
  ui::NativeTheme::State state;
  ui::NativeTheme::ExtraParams extra;
  const ui::NativeTheme* theme = view.GetNativeTheme();
  const ui::Animation* animation = native_theme_delegate->GetThemeAnimation();

  if (animation && animation->is_animating()) {
    // Paint the background state.
    state = native_theme_delegate->GetBackgroundThemeState(&extra);
    PaintHelper(this, canvas, theme, part, state, rect, extra);

    // Composite the foreground state above the background state.
    const int alpha = animation->CurrentValueBetween(0, 255);
    canvas->SaveLayerAlpha(static_cast<uint8>(alpha));
    state = native_theme_delegate->GetForegroundThemeState(&extra);
    PaintHelper(this, canvas, theme, part, state, rect, extra);
    canvas->Restore();
  } else {
    state = native_theme_delegate->GetThemeState(&extra);
    PaintHelper(this, canvas, theme, part, state, rect, extra);
  }

  // Draw the Views focus border for the native theme style.
  if (native_theme() && view.focus_border() && extra.button.is_focused)
    view.focus_border()->Paint(view, canvas);
}

gfx::Insets LabelButtonBorder::GetInsets() const {
  if (native_theme()) {
    return gfx::Insets(kPreferredNativeThemePaddingVertical,
                       kPreferredNativeThemePaddingHorizontal,
                       kPreferredNativeThemePaddingVertical,
                       kPreferredNativeThemePaddingHorizontal);
  } else {
    return gfx::Insets(kPreferredPaddingVertical, kPreferredPaddingHorizontal,
                       kPreferredPaddingVertical, kPreferredPaddingHorizontal);
  }
}

BorderImages* LabelButtonBorder::GetImages(CustomButton::ButtonState state) {
  return &images_[state];
}

void LabelButtonBorder::SetImages(CustomButton::ButtonState state,
                                  const BorderImages& set) {
  images_[state] = set;
}

}  // namespace views