summaryrefslogtreecommitdiffstats
path: root/views/bubble/border_contents_view.cc
blob: 158275186cb87910fef656df28445b8ab33eecdb (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) 2011 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 "views/bubble/border_contents_view.h"

#include <algorithm>

#include "ui/gfx/screen.h"

static const int kTopMargin = 6;
static const int kLeftMargin = 6;
static const int kBottomMargin = 6;
static const int kRightMargin = 6;

namespace {

// Computes how much |window_bounds| is off-screen of the monitor bounds
// |monitor_bounds| and puts the values in |offscreen_insets|.
// Returns false if |window_bounds| is actually contained in |monitor_bounds|,
// in which case |offscreen_insets| is not modified.
bool ComputeOffScreenInsets(const gfx::Rect& monitor_bounds,
                            const gfx::Rect& window_bounds,
                            gfx::Insets* offscreen_insets) {
  if (monitor_bounds.Contains(window_bounds))
    return false;

  if (!offscreen_insets)
    return true;

  //  window_bounds
  //  +-------------------------------+
  //  |             top               |
  //  |      +----------------+       |
  //  | left | monitor_bounds | right |
  //  |      +----------------+       |
  //  |            bottom             |
  //  +-------------------------------+
  int top = std::max(0, monitor_bounds.y() - window_bounds.y());
  int left = std::max(0, monitor_bounds.x() - window_bounds.x());
  int bottom = std::max(0, window_bounds.bottom() - monitor_bounds.bottom());
  int right = std::max(0, window_bounds.right() - monitor_bounds.right());

  offscreen_insets->Set(top, left, bottom, right);
  return true;
}

// Convenience method that returns the height of |insets| if |vertical| is
// true, its width otherwise.
int GetInsetsLength(const gfx::Insets& insets, bool vertical) {
  return vertical ? insets.height() : insets.width();
}

}  // namespace

namespace views {

BorderContentsView::BorderContentsView()
    : bubble_border_(NULL),
      content_margins_(kTopMargin, kLeftMargin, kBottomMargin, kRightMargin) {
}

BorderContentsView::BorderContentsView(int top_margin,
                                       int left_margin,
                                       int bottom_margin,
                                       int right_margin)
    : bubble_border_(NULL),
      content_margins_(top_margin, left_margin, bottom_margin, right_margin) {
}

BorderContentsView::~BorderContentsView() {}

void BorderContentsView::Init() {
  // Default arrow location.
  BubbleBorder::ArrowLocation arrow_location =
      BubbleBorder::TOP_LEFT;
  if (base::i18n::IsRTL())
    arrow_location = BubbleBorder::horizontal_mirror(arrow_location);
  DCHECK(!bubble_border_);

  bubble_border_ = new BubbleBorder(arrow_location);
  set_border(bubble_border_);
  set_background(new BubbleBackground(bubble_border_));
}

void BorderContentsView::SetBackgroundColor(SkColor color) {
  bubble_border_->set_background_color(color);
}

void BorderContentsView::SizeAndGetBounds(
    const gfx::Rect& position_relative_to,
    BubbleBorder::ArrowLocation arrow_location,
    bool allow_bubble_offscreen,
    const gfx::Size& contents_size,
    gfx::Rect* contents_bounds,
    gfx::Rect* window_bounds) {
  if (base::i18n::IsRTL())
    arrow_location = BubbleBorder::horizontal_mirror(arrow_location);
  bubble_border_->set_arrow_location(arrow_location);
  // Set the border.
  set_border(bubble_border_);

  // Give the contents a margin.
  gfx::Size local_contents_size(contents_size);
  local_contents_size.Enlarge(content_margins_.width(),
                              content_margins_.height());

  // Try putting the arrow in its initial location, and calculating the bounds.
  *window_bounds =
      bubble_border_->GetBounds(position_relative_to, local_contents_size);
  if (!allow_bubble_offscreen) {
    gfx::Rect monitor_bounds = GetMonitorBounds(position_relative_to);
    if (!monitor_bounds.IsEmpty()) {
      // Try to resize vertically if this does not fit on the screen.
      MirrorArrowIfOffScreen(true,  // |vertical|.
                             position_relative_to, monitor_bounds,
                             local_contents_size, &arrow_location,
                             window_bounds);
      // Then try to resize horizontally if it still does not fit on the screen.
      MirrorArrowIfOffScreen(false,  // |vertical|.
                             position_relative_to, monitor_bounds,
                             local_contents_size, &arrow_location,
                             window_bounds);
    }
  }

  // Calculate the bounds of the contained contents (in window coordinates) by
  // subtracting the border dimensions and margin amounts.
  *contents_bounds = gfx::Rect(gfx::Point(), window_bounds->size());
  gfx::Insets insets;
  bubble_border_->GetInsets(&insets);
  insets += content_margins_;
  contents_bounds->Inset(insets);
}

gfx::Rect BorderContentsView::GetMonitorBounds(const gfx::Rect& rect) {
  return gfx::Screen::GetMonitorWorkAreaNearestPoint(rect.CenterPoint());
}

void BorderContentsView::MirrorArrowIfOffScreen(
    bool vertical,
    const gfx::Rect& position_relative_to,
    const gfx::Rect& monitor_bounds,
    const gfx::Size& local_contents_size,
    BubbleBorder::ArrowLocation* arrow_location,
    gfx::Rect* window_bounds) {
  // If the bounds don't fit, move the arrow to its mirrored position to see if
  // it improves things.
  gfx::Insets offscreen_insets;
  if (ComputeOffScreenInsets(monitor_bounds, *window_bounds,
                             &offscreen_insets) &&
      GetInsetsLength(offscreen_insets, vertical) > 0) {
    BubbleBorder::ArrowLocation original_arrow_location =
        *arrow_location;
     *arrow_location =
         vertical ? BubbleBorder::vertical_mirror(*arrow_location) :
                    BubbleBorder::horizontal_mirror(*arrow_location);

    // Change the arrow and get the new bounds.
    bubble_border_->set_arrow_location(*arrow_location);
    *window_bounds = bubble_border_->GetBounds(position_relative_to,
                                               local_contents_size);
    gfx::Insets new_offscreen_insets;
    // If there is more of the window offscreen, we'll keep the old arrow.
    if (ComputeOffScreenInsets(monitor_bounds, *window_bounds,
                               &new_offscreen_insets) &&
        GetInsetsLength(new_offscreen_insets, vertical) >=
            GetInsetsLength(offscreen_insets, vertical)) {
      *arrow_location = original_arrow_location;
      bubble_border_->set_arrow_location(*arrow_location);
      *window_bounds = bubble_border_->GetBounds(position_relative_to,
                                                 local_contents_size);
    }
  }
}

}  // namespace views