| 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 "ash/wm/dialog_frame_view.h" | |
| 6 | |
| 7 #include "grit/ui_resources.h" | |
| 8 #include "ui/base/hit_test.h" | |
| 9 #include "ui/base/resource/resource_bundle.h" | |
| 10 #include "ui/gfx/canvas.h" | |
| 11 #include "ui/gfx/font.h" | |
| 12 #include "ui/gfx/image/image.h" | |
| 13 #include "ui/gfx/insets.h" | |
| 14 #include "ui/views/background.h" | |
| 15 #include "ui/views/border.h" | |
| 16 #include "ui/views/controls/button/image_button.h" | |
| 17 #include "ui/views/widget/widget.h" | |
| 18 #include "ui/views/widget/widget_delegate.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // TODO(benrg): Make frame shadow and stroke agree with the spec. | |
| 23 | |
| 24 // TODO(benrg): Title bar text should be #222, not black. Text in the client | |
| 25 // area should also be #222, but is currently usually black. Punting on this | |
| 26 // until the overhaul of color handling that will be happening Real Soon Now. | |
| 27 const SkColor kDialogTitleColor = SK_ColorBLACK; | |
| 28 const SkColor kDialogBackgroundColor = SK_ColorWHITE; | |
| 29 | |
| 30 // Dialog-size-dependent padding values from the spec, in pixels. | |
| 31 const int kGoogleSmallDialogVPadding = 16; | |
| 32 const int kGoogleSmallDialogHPadding = 20; | |
| 33 const int kGoogleMediumDialogVPadding = 28; | |
| 34 const int kGoogleMediumDialogHPadding = 32; | |
| 35 const int kGoogleLargeDialogVPadding = 30; | |
| 36 const int kGoogleLargeDialogHPadding = 42; | |
| 37 | |
| 38 // Dialog layouts themselves contain some padding, which should be ignored in | |
| 39 // favor of the padding values above. Currently we hack it by nudging the | |
| 40 // client area outward. | |
| 41 const int kDialogHPaddingNudge = 13; | |
| 42 const int kDialogTopPaddingNudge = 5; | |
| 43 const int kDialogBottomPaddingNudge = 10; | |
| 44 | |
| 45 // TODO(benrg): There are no examples in the spec of close boxes on medium- or | |
| 46 // small-size dialogs, and the close button size is not factored into other | |
| 47 // sizing decisions. This value could cause problems with smaller dialogs. | |
| 48 const int kCloseButtonSize = 44; | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 52 namespace ash { | |
| 53 namespace internal { | |
| 54 | |
| 55 // static | |
| 56 const char DialogFrameView::kViewClassName[] = "ash/wm/DialogFrameView"; | |
| 57 | |
| 58 //////////////////////////////////////////////////////////////////////////////// | |
| 59 // DialogFrameView, public: | |
| 60 | |
| 61 DialogFrameView::DialogFrameView() { | |
| 62 set_background(views::Background::CreateSolidBackground( | |
| 63 kDialogBackgroundColor)); | |
| 64 | |
| 65 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 66 title_font_.reset(new gfx::Font(rb.GetFont(ui::ResourceBundle::MediumFont))); | |
| 67 close_button_ = new views::ImageButton(this); | |
| 68 close_button_->SetImage(views::CustomButton::STATE_NORMAL, | |
| 69 rb.GetImageNamed(IDR_CLOSE_BAR).ToImageSkia()); | |
| 70 close_button_->SetImage(views::CustomButton::STATE_HOVERED, | |
| 71 rb.GetImageNamed(IDR_CLOSE_BAR_H).ToImageSkia()); | |
| 72 close_button_->SetImage(views::CustomButton::STATE_PRESSED, | |
| 73 rb.GetImageNamed(IDR_CLOSE_BAR_P).ToImageSkia()); | |
| 74 close_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, | |
| 75 views::ImageButton::ALIGN_MIDDLE); | |
| 76 AddChildView(close_button_); | |
| 77 } | |
| 78 | |
| 79 DialogFrameView::~DialogFrameView() { | |
| 80 } | |
| 81 | |
| 82 //////////////////////////////////////////////////////////////////////////////// | |
| 83 // DialogFrameView, views::NonClientFrameView: | |
| 84 | |
| 85 gfx::Rect DialogFrameView::GetBoundsForClientView() const { | |
| 86 gfx::Rect client_bounds = GetLocalBounds(); | |
| 87 client_bounds.Inset(GetClientInsets()); | |
| 88 return client_bounds; | |
| 89 } | |
| 90 | |
| 91 gfx::Rect DialogFrameView::GetWindowBoundsForClientBounds( | |
| 92 const gfx::Rect& client_bounds) const { | |
| 93 gfx::Rect window_bounds = client_bounds; | |
| 94 window_bounds.Inset(-GetClientInsets()); | |
| 95 return window_bounds; | |
| 96 } | |
| 97 | |
| 98 int DialogFrameView::NonClientHitTest(const gfx::Point& point) { | |
| 99 if (close_button_->GetMirroredBounds().Contains(point)) | |
| 100 return HTCLOSE; | |
| 101 return point.y() < GetClientInsets().top() ? HTCAPTION : HTCLIENT; | |
| 102 } | |
| 103 | |
| 104 void DialogFrameView::GetWindowMask(const gfx::Size& size, | |
| 105 gfx::Path* window_mask) { | |
| 106 // Nothing to do. | |
| 107 } | |
| 108 | |
| 109 void DialogFrameView::ResetWindowControls() { | |
| 110 // Nothing to do. | |
| 111 } | |
| 112 | |
| 113 void DialogFrameView::UpdateWindowIcon() { | |
| 114 // Nothing to do. | |
| 115 } | |
| 116 | |
| 117 void DialogFrameView::UpdateWindowTitle() { | |
| 118 // Nothing to do. | |
| 119 } | |
| 120 | |
| 121 //////////////////////////////////////////////////////////////////////////////// | |
| 122 // DialogFrameView, views::View overrides: | |
| 123 | |
| 124 std::string DialogFrameView::GetClassName() const { | |
| 125 return kViewClassName; | |
| 126 } | |
| 127 | |
| 128 void DialogFrameView::Layout() { | |
| 129 title_display_rect_ = GetLocalBounds(); | |
| 130 title_display_rect_.Inset(GetPaddingInsets()); | |
| 131 title_display_rect_.set_height(title_font_->GetHeight()); | |
| 132 | |
| 133 // The hot rectangle for the close button is flush with the upper right of the | |
| 134 // dialog. The close button image is smaller, and is centered in the hot rect. | |
| 135 close_button_->SetBounds( | |
| 136 width() - kCloseButtonSize, | |
| 137 0, kCloseButtonSize, kCloseButtonSize); | |
| 138 } | |
| 139 | |
| 140 void DialogFrameView::OnPaint(gfx::Canvas* canvas) { | |
| 141 views::View::OnPaint(canvas); | |
| 142 views::WidgetDelegate* delegate = GetWidget()->widget_delegate(); | |
| 143 if (!delegate) | |
| 144 return; | |
| 145 canvas->DrawStringInt(delegate->GetWindowTitle(), *title_font_.get(), | |
| 146 kDialogTitleColor, title_display_rect_); | |
| 147 } | |
| 148 | |
| 149 //////////////////////////////////////////////////////////////////////////////// | |
| 150 // DialogFrameView, views::ButtonListener overrides: | |
| 151 | |
| 152 void DialogFrameView::ButtonPressed(views::Button* sender, | |
| 153 const ui::Event& event) { | |
| 154 if (sender == close_button_) | |
| 155 GetWidget()->Close(); | |
| 156 } | |
| 157 | |
| 158 //////////////////////////////////////////////////////////////////////////////// | |
| 159 // DialogFrameView, private: | |
| 160 | |
| 161 gfx::Insets DialogFrameView::GetPaddingInsets() const { | |
| 162 // These three discrete padding sizes come from the spec. If we ever wanted | |
| 163 // our Google-style dialogs to be resizable, we would probably need to | |
| 164 // smoothly interpolate the padding size instead. | |
| 165 int v_padding, h_padding; | |
| 166 if (width() <= 280) { | |
| 167 v_padding = kGoogleSmallDialogVPadding; | |
| 168 h_padding = kGoogleSmallDialogHPadding; | |
| 169 } else if (width() <= 480) { | |
| 170 v_padding = kGoogleMediumDialogVPadding; | |
| 171 h_padding = kGoogleMediumDialogHPadding; | |
| 172 } else { | |
| 173 v_padding = kGoogleLargeDialogVPadding; | |
| 174 h_padding = kGoogleLargeDialogHPadding; | |
| 175 } | |
| 176 return gfx::Insets(v_padding, h_padding, v_padding, h_padding); | |
| 177 } | |
| 178 | |
| 179 gfx::Insets DialogFrameView::GetClientInsets() const { | |
| 180 gfx::Insets insets = GetPaddingInsets(); | |
| 181 // The title should be separated from the client area by 1 em (dialog spec), | |
| 182 // and one em is equal to the font size (CSS spec). | |
| 183 insets += gfx::Insets( | |
| 184 title_font_->GetHeight() + title_font_->GetFontSize() - | |
| 185 kDialogTopPaddingNudge, | |
| 186 -kDialogHPaddingNudge, | |
| 187 -kDialogBottomPaddingNudge, | |
| 188 -kDialogHPaddingNudge); | |
| 189 return insets; | |
| 190 } | |
| 191 | |
| 192 } // namespace internal | |
| 193 } // namespace views | |
| OLD | NEW |