Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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 "chrome/browser/ui/views/frame/app_non_client_frame_view_aura.h" | |
| 6 | |
| 7 #include "base/debug/stack_trace.h" | |
| 8 #include "chrome/browser/ui/views/frame/browser_frame.h" | |
| 9 #include "chrome/browser/ui/views/frame/browser_view.h" | |
| 10 #include "grit/ui_resources.h" | |
| 11 #include "ui/aura/window.h" | |
| 12 #include "ui/base/animation/slide_animation.h" | |
| 13 #include "ui/base/hit_test.h" | |
| 14 #include "ui/base/resource/resource_bundle.h" | |
| 15 #include "ui/gfx/canvas.h" | |
| 16 #include "ui/gfx/compositor/layer.h" | |
| 17 #include "ui/gfx/image/image.h" | |
| 18 #include "ui/gfx/point.h" | |
| 19 #include "ui/gfx/rect.h" | |
| 20 #include "ui/gfx/size.h" | |
| 21 #include "ui/views/controls/button/image_button.h" | |
| 22 #include "ui/views/widget/widget.h" | |
| 23 #include "ui/views/window/non_client_view.h" | |
| 24 | |
| 25 namespace { | |
| 26 // The number of pixels to use as a hover zone at the top of the screen. | |
| 27 const int kTopMargin = 1; | |
| 28 // How long the hover animation takes if uninterrupted. | |
| 29 const int kHoverFadeDurationMs = 250; | |
|
sky
2012/02/09 01:04:17
This seems like a long time.
DaveMoore
2012/02/10 23:05:55
Cole sez 130ms.
On 2012/02/09 01:04:17, sky wrote:
| |
| 30 // The number of pixels within the shadow to draw the buttons. | |
| 31 const int kShadowStart = 28; | |
| 32 } | |
| 33 | |
| 34 class AppNonClientFrameViewAura::ControlView | |
| 35 : public views::View, public views::ButtonListener { | |
| 36 public: | |
| 37 explicit ControlView(AppNonClientFrameViewAura* owner) : | |
| 38 owner_(owner), | |
| 39 close_button_(CreateImageButton(IDR_AURA_WINDOW_FULLSCREEN_CLOSE)), | |
| 40 restore_button_(CreateImageButton(IDR_AURA_WINDOW_FULLSCREEN_RESTORE)) { | |
| 41 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 42 separator_ = rb.GetBitmapNamed(IDR_AURA_WINDOW_FULLSCREEN_SEPARATOR); | |
|
sky
2012/02/09 01:04:17
Isn't GetBitmapNamed deprecated?
DaveMoore
2012/02/10 23:05:55
Done.
| |
| 43 shadow_ = rb.GetBitmapNamed(IDR_AURA_WINDOW_FULLSCREEN_SHADOW); | |
| 44 AddChildView(close_button_); | |
| 45 AddChildView(restore_button_); | |
| 46 } | |
| 47 | |
| 48 virtual void Layout() OVERRIDE { | |
| 49 restore_button_->SetPosition(gfx::Point(kShadowStart, 0)); | |
| 50 close_button_->SetPosition( | |
| 51 gfx::Point(kShadowStart + close_button_->width() + separator_->width(), | |
| 52 0)); | |
| 53 } | |
| 54 | |
| 55 virtual gfx::Size GetPreferredSize() OVERRIDE { | |
| 56 return gfx::Size(shadow_->width(), shadow_->height()); | |
| 57 } | |
| 58 | |
| 59 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
| 60 views::View::OnPaint(canvas); | |
| 61 canvas->DrawBitmapInt( | |
| 62 *separator_, restore_button_->x() + restore_button_->width(), 0); | |
| 63 canvas->DrawBitmapInt(*shadow_, 0, 0); | |
| 64 } | |
| 65 | |
| 66 void ButtonPressed( | |
| 67 views::Button* sender, const views::Event& event) OVERRIDE { | |
|
sky
2012/02/09 01:04:17
nit: each param on its own line.
DaveMoore
2012/02/10 23:05:55
Done.
| |
| 68 if (sender == close_button_) | |
| 69 owner_->Close(); | |
| 70 else if (sender == restore_button_) | |
| 71 owner_->Restore(); | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 // Gets an image representing 3 bitmaps laied out horizontally that will be | |
|
sky
2012/02/09 01:04:17
laied -> laid
DaveMoore
2012/02/10 23:05:55
Done.
| |
| 76 // used as the normal, hot and pushed states for the created button. | |
| 77 views::ImageButton* CreateImageButton(int resource_id) { | |
| 78 views::ImageButton* button = new views::ImageButton(this); | |
| 79 const SkBitmap* all_images = | |
| 80 ResourceBundle::GetSharedInstance().GetBitmapNamed(resource_id); | |
| 81 int width = all_images->width() / 3; | |
| 82 int height = all_images->height(); | |
| 83 | |
| 84 SkBitmap normal, hot, pushed; | |
| 85 all_images->extractSubset( | |
|
sky
2012/02/09 01:04:17
This seems tedious. Is there a reason we're going
DaveMoore
2012/02/10 23:05:55
I like this approach more. It reduces the number o
| |
| 86 &normal, | |
| 87 SkIRect::MakeXYWH(0, 0, width, height)); | |
| 88 all_images->extractSubset( | |
| 89 &hot, | |
| 90 SkIRect::MakeXYWH(width, 0, width, height)); | |
| 91 all_images->extractSubset( | |
| 92 &pushed, | |
| 93 SkIRect::MakeXYWH(2 * width, 0, width, height)); | |
| 94 button->SetImage(views::CustomButton::BS_NORMAL, &normal); | |
| 95 button->SetImage(views::CustomButton::BS_HOT, &hot); | |
| 96 button->SetImage(views::CustomButton::BS_PUSHED, &pushed); | |
| 97 | |
| 98 button->SizeToPreferredSize(); | |
| 99 return button; | |
| 100 } | |
| 101 | |
| 102 AppNonClientFrameViewAura* owner_; | |
| 103 views::ImageButton* close_button_; | |
| 104 views::ImageButton* restore_button_; | |
| 105 const SkBitmap* separator_; | |
|
sky
2012/02/09 01:04:17
Generally we prefer values for SkBitmap and not po
DaveMoore
2012/02/10 23:05:55
Done.
| |
| 106 const SkBitmap* shadow_; | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(ControlView); | |
| 109 }; | |
| 110 | |
| 111 class AppNonClientFrameViewAura::Host : public views::MouseWatcherHost { | |
| 112 public: | |
| 113 explicit Host(AppNonClientFrameViewAura* owner) : owner_(owner) {} | |
| 114 virtual ~Host() {} | |
| 115 | |
| 116 virtual bool Contains( | |
| 117 const gfx::Point& screen_point, | |
| 118 views::MouseWatcherHost::MouseEventType type) { | |
| 119 gfx::Rect top_margin = owner_->GetScreenBounds(); | |
| 120 top_margin.set_height(kTopMargin); | |
| 121 gfx::Rect control_bounds = owner_->GetControlBounds(); | |
| 122 control_bounds.Inset(kShadowStart, 0, 0, kShadowStart); | |
| 123 bool ret = top_margin.Contains(screen_point) || | |
|
sky
2012/02/09 01:04:17
Since you don't use this bool, just return the res
DaveMoore
2012/02/10 23:05:55
Done.
| |
| 124 control_bounds.Contains(screen_point); | |
| 125 return ret; | |
| 126 } | |
| 127 | |
| 128 AppNonClientFrameViewAura* owner_; | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(Host); | |
| 131 }; | |
| 132 | |
| 133 AppNonClientFrameViewAura::AppNonClientFrameViewAura( | |
| 134 BrowserFrame* frame, BrowserView* browser_view) | |
| 135 : BrowserNonClientFrameView(frame, browser_view), | |
| 136 control_view_(new ControlView(this)), | |
| 137 control_widget_(NULL), | |
| 138 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 139 show_animation_(new ui::SlideAnimation(this))), | |
| 140 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 141 mouse_watcher_(new Host(this), this)) { | |
| 142 show_animation_->SetSlideDuration(kHoverFadeDurationMs); | |
| 143 } | |
| 144 | |
| 145 AppNonClientFrameViewAura::~AppNonClientFrameViewAura() { | |
| 146 if (control_widget_) { | |
| 147 control_widget_->Close(); | |
| 148 } | |
| 149 } | |
| 150 gfx::Rect AppNonClientFrameViewAura::GetBoundsForClientView() const { | |
| 151 gfx::Rect bounds = GetLocalBounds(); | |
| 152 bounds.Inset(0, kTopMargin, 0, 0); | |
| 153 return bounds; | |
| 154 } | |
| 155 | |
| 156 gfx::Rect AppNonClientFrameViewAura::GetWindowBoundsForClientBounds( | |
| 157 const gfx::Rect& client_bounds) const { | |
| 158 gfx::Rect bounds = client_bounds; | |
| 159 bounds.Inset(0, -kTopMargin, 0, 0); | |
| 160 return bounds; | |
| 161 } | |
| 162 | |
| 163 int AppNonClientFrameViewAura::NonClientHitTest( | |
| 164 const gfx::Point& point) { | |
| 165 return bounds().Contains(point) ? HTCLIENT : HTNOWHERE; | |
| 166 } | |
| 167 | |
| 168 void AppNonClientFrameViewAura::GetWindowMask(const gfx::Size& size, | |
| 169 gfx::Path* window_mask) { | |
| 170 } | |
| 171 | |
| 172 void AppNonClientFrameViewAura::ResetWindowControls() { | |
| 173 } | |
| 174 | |
| 175 void AppNonClientFrameViewAura::UpdateWindowIcon() { | |
| 176 } | |
| 177 | |
| 178 gfx::Rect AppNonClientFrameViewAura::GetBoundsForTabStrip( | |
| 179 views::View* tabstrip) const { | |
| 180 return gfx::Rect(); | |
| 181 } | |
| 182 | |
| 183 int AppNonClientFrameViewAura::GetHorizontalTabStripVerticalOffset( | |
| 184 bool restored) const { | |
| 185 return 0; | |
| 186 } | |
| 187 | |
| 188 void AppNonClientFrameViewAura::UpdateThrobber(bool running) { | |
| 189 } | |
| 190 | |
| 191 void AppNonClientFrameViewAura::OnMouseEntered( | |
| 192 const views::MouseEvent& event) { | |
| 193 if (show_animation_->IsShowing()) | |
| 194 return; | |
| 195 | |
| 196 if (!control_widget_) { | |
| 197 control_widget_ = new views::Widget; | |
| 198 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); | |
| 199 params.parent = browser_view()->GetNativeHandle(); | |
| 200 params.transparent = true; | |
| 201 control_widget_->Init(params); | |
| 202 control_widget_->SetContentsView(control_view_); | |
| 203 } | |
| 204 gfx::Rect control_bounds = GetControlBounds(); | |
| 205 control_bounds.set_y(control_bounds.y() - control_bounds.height()); | |
| 206 control_widget_->SetBounds(control_bounds); | |
| 207 control_widget_->Show(); | |
| 208 show_animation_->Show(); | |
| 209 mouse_watcher_.Start(); | |
| 210 } | |
| 211 | |
| 212 void AppNonClientFrameViewAura::AnimationProgressed( | |
| 213 const ui::Animation* animation) { | |
| 214 if (animation == show_animation_.get()) { | |
|
sky
2012/02/09 01:04:17
Did you try using the layer animation code? In the
DaveMoore
2012/02/10 23:05:55
I didn't, but this didn't seem very difficult. Are
sky
2012/02/13 16:01:32
It should be cleaner, and hopefully at some point
| |
| 215 double value = show_animation_->GetCurrentValue(); | |
| 216 gfx::Rect control_bounds = GetControlBounds(); | |
| 217 int y = control_bounds.y() - (1.0 - value) * control_bounds.height(); | |
| 218 control_bounds.set_y(y); | |
| 219 control_widget_->GetNativeWindow()->layer()->SetOpacity(value); | |
| 220 control_widget_->SetBounds(control_bounds); | |
| 221 return; | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 void AppNonClientFrameViewAura::MouseMovedOutOfHost() { | |
| 226 show_animation_->Hide(); | |
| 227 } | |
| 228 | |
| 229 gfx::Rect AppNonClientFrameViewAura::GetControlBounds() const { | |
| 230 gfx::Size preferred = control_view_->GetPreferredSize(); | |
| 231 gfx::Point location(width() - preferred.width(), 0); | |
| 232 ConvertPointToWidget(this, &location); | |
| 233 return gfx::Rect( | |
| 234 location.x(), location.y(), | |
| 235 preferred.width(), preferred.height()); | |
| 236 } | |
| 237 | |
| 238 void AppNonClientFrameViewAura::Close() { | |
| 239 control_widget_->Close(); | |
|
sky
2012/02/09 01:04:17
I suspect explicitly closing the widget here and i
| |
| 240 control_widget_ = NULL; | |
| 241 mouse_watcher_.Stop(); | |
| 242 frame()->Close(); | |
| 243 } | |
| 244 | |
| 245 void AppNonClientFrameViewAura::Restore() { | |
| 246 control_widget_->Close(); | |
| 247 control_widget_ = NULL; | |
| 248 mouse_watcher_.Stop(); | |
| 249 frame()->Restore(); | |
| 250 } | |
| OLD | NEW |