1 // Copyright 2014 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.
5 #include "athena/activity/activity_frame_view.h"
7 #include "athena/activity/public/activity_view_model.h"
8 #include "athena/wm/public/window_manager.h"
9 #include "ui/base/hit_test.h"
10 #include "ui/gfx/canvas.h"
11 #include "ui/gfx/image/image_skia.h"
12 #include "ui/views/background.h"
13 #include "ui/views/controls/image_view.h"
14 #include "ui/views/controls/label.h"
15 #include "ui/views/view.h"
16 #include "ui/views/widget/widget.h"
17 #include "ui/views/widget/widget_delegate.h"
18 #include "ui/views/window/client_view.h"
24 const int kIconSize
= 32;
26 // The distance between the icon and the title when the icon is visible.
27 const int kIconTitleSpacing
= 10;
29 // The height of the top border necessary to display the title without the icon.
30 const int kDefaultTitleHeight
= 25;
32 // The height of the top border in overview mode.
33 const int kOverviewTitleHeight
= 55;
35 // The height of the top border for fullscreen and frameless activities in
37 const int kOverviewShortTitleHeight
= 30;
39 // The thickness of the left, right and bottom borders in overview mode.
40 const int kOverviewBorderThickness
= 5;
45 const char ActivityFrameView::kViewClassName
[] = "ActivityFrameView";
47 ActivityFrameView::ActivityFrameView(views::Widget
* frame
,
48 ActivityViewModel
* view_model
)
50 view_model_(view_model
),
51 title_(new views::Label
),
52 icon_(new views::ImageView
),
54 title_
->SetEnabledColor(SkColorSetA(SK_ColorBLACK
, 0xe5));
62 view_model_
->SetActivityView(this);
64 WindowManager::Get()->AddObserver(this);
67 ActivityFrameView::~ActivityFrameView() {
68 WindowManager::Get()->RemoveObserver(this);
70 // |view_model_| is already destroyed at this time. So do not attempt to reset
71 // the activity-view by calling SetActivityView(nullptr);
72 // http://crbug.com/427113
75 gfx::Rect
ActivityFrameView::GetBoundsForClientView() const {
76 gfx::Rect client_bounds
= bounds();
77 client_bounds
.Inset(NonClientBorderInsets());
81 gfx::Rect
ActivityFrameView::GetWindowBoundsForClientBounds(
82 const gfx::Rect
& client_bounds
) const {
83 gfx::Rect window_bounds
= client_bounds
;
84 window_bounds
.Inset(-NonClientBorderInsets());
88 int ActivityFrameView::NonClientHitTest(const gfx::Point
& point
) {
89 if (!bounds().Contains(point
))
91 int client_hit_test
= frame_
->client_view()->NonClientHitTest(point
);
92 if (client_hit_test
!= HTNOWHERE
)
93 return client_hit_test
;
95 GetHTComponentForFrame(point
, 0, NonClientBorderThickness(), 0, 0, false);
96 return (window_hit_test
== HTNOWHERE
) ? HTCAPTION
: client_hit_test
;
99 void ActivityFrameView::GetWindowMask(const gfx::Size
& size
,
100 gfx::Path
* window_mask
) {
103 void ActivityFrameView::ResetWindowControls() {
106 void ActivityFrameView::UpdateWindowIcon() {
107 // The activity has a frame in overview mode regardless of the value of
108 // ActivityViewModel::UsesFrame().
109 SkColor bgcolor
= view_model_
->GetRepresentativeColor();
110 set_background(views::Background::CreateSolidBackground(bgcolor
));
111 title_
->SetBackgroundColor(bgcolor
);
113 if (view_model_
->UsesFrame())
114 icon_
->SetImage(view_model_
->GetIcon());
118 void ActivityFrameView::UpdateWindowTitle() {
119 if (!view_model_
->UsesFrame())
121 title_
->SetText(frame_
->widget_delegate()->GetWindowTitle());
125 void ActivityFrameView::SizeConstraintsChanged() {
128 gfx::Size
ActivityFrameView::GetPreferredSize() const {
129 gfx::Size pref
= frame_
->client_view()->GetPreferredSize();
130 gfx::Rect
bounds(0, 0, pref
.width(), pref
.height());
131 return frame_
->non_client_view()
132 ->GetWindowBoundsForClientBounds(bounds
)
136 const char* ActivityFrameView::GetClassName() const {
137 return kViewClassName
;
140 void ActivityFrameView::Layout() {
141 if (frame_
->IsFullscreen() || !view_model_
->UsesFrame()) {
142 title_
->SetVisible(false);
143 icon_
->SetVisible(false);
147 title_
->SetVisible(true);
148 icon_
->SetVisible(in_overview_
);
150 gfx::Size preferred_title_size
= title_
->GetPreferredSize();
151 int top_height
= NonClientTopBorderHeight();
154 int edge
= (top_height
- kIconSize
) / 2;
155 icon_
->SetBounds(edge
, edge
, kIconSize
, kIconSize
);
157 title_x
= icon_
->bounds().right() + kIconTitleSpacing
;
159 title_x
= (width() - preferred_title_size
.width()) / 2;
162 title_
->SetBounds(title_x
,
163 (top_height
- preferred_title_size
.height()) / 2,
164 preferred_title_size
.width(),
165 preferred_title_size
.height());
168 void ActivityFrameView::OnPaintBackground(gfx::Canvas
* canvas
) {
169 View::OnPaintBackground(canvas
);
171 // Paint a border around the client view.
172 gfx::Rect border_bounds
= GetLocalBounds();
173 border_bounds
.Inset(NonClientBorderInsets());
174 border_bounds
.Inset(-1, -1, 0, 0);
175 canvas
->DrawRect(border_bounds
, SkColorSetA(SK_ColorGRAY
, 0x7f));
178 void ActivityFrameView::UpdateTitle() {
182 void ActivityFrameView::UpdateIcon() {
186 void ActivityFrameView::UpdateRepresentativeColor() {
190 void ActivityFrameView::OnOverviewModeEnter() {
191 view_model_
->PrepareContentsForOverview();
194 frame_
->client_view()->InvalidateLayout();
195 frame_
->GetRootView()->Layout();
199 void ActivityFrameView::OnOverviewModeExit() {
200 in_overview_
= false;
202 frame_
->client_view()->InvalidateLayout();
203 frame_
->GetRootView()->Layout();
205 view_model_
->ResetContentsView();
208 void ActivityFrameView::OnSplitViewModeEnter() {
211 void ActivityFrameView::OnSplitViewModeExit() {
214 gfx::Insets
ActivityFrameView::NonClientBorderInsets() const {
215 int border_thickness
= NonClientBorderThickness();
216 return gfx::Insets(NonClientTopBorderHeight(),
222 int ActivityFrameView::NonClientBorderThickness() const {
223 return in_overview_
? kOverviewBorderThickness
: 0;
226 int ActivityFrameView::NonClientTopBorderHeight() const {
227 if (frame_
->IsFullscreen() || !view_model_
->UsesFrame())
228 return in_overview_
? kOverviewShortTitleHeight
: 0;
229 return in_overview_
? kOverviewTitleHeight
: kDefaultTitleHeight
;
232 } // namespace athena