| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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/common/system/chromeos/palette/palette_tray.h" |
| 6 |
| 7 #include "ash/common/material_design/material_design_controller.h" |
| 8 #include "ash/common/shelf/shelf_constants.h" |
| 9 #include "ash/common/shelf/wm_shelf.h" |
| 10 #include "ash/common/shelf/wm_shelf_util.h" |
| 11 #include "ash/common/shell_window_ids.h" |
| 12 #include "ash/common/system/chromeos/palette/palette_tool.h" |
| 13 #include "ash/common/system/chromeos/palette/palette_tool_manager.h" |
| 14 #include "ash/common/system/tray/system_tray_delegate.h" |
| 15 #include "ash/common/system/tray/tray_bubble_wrapper.h" |
| 16 #include "ash/common/system/tray/tray_constants.h" |
| 17 #include "ash/common/system/tray/tray_popup_header_button.h" |
| 18 #include "ash/common/wm_lookup.h" |
| 19 #include "ash/common/wm_root_window_controller.h" |
| 20 #include "ash/common/wm_shell.h" |
| 21 #include "ash/common/wm_window.h" |
| 22 #include "base/sys_info.h" |
| 23 #include "grit/ash_resources.h" |
| 24 #include "grit/ash_strings.h" |
| 25 #include "ui/base/l10n/l10n_util.h" |
| 26 #include "ui/base/resource/resource_bundle.h" |
| 27 #include "ui/gfx/paint_vector_icon.h" |
| 28 #include "ui/views/controls/image_view.h" |
| 29 #include "ui/views/controls/label.h" |
| 30 #include "ui/views/controls/separator.h" |
| 31 #include "ui/views/layout/box_layout.h" |
| 32 #include "ui/views/layout/fill_layout.h" |
| 33 |
| 34 namespace ash { |
| 35 |
| 36 namespace { |
| 37 |
| 38 // Predefined padding for the icon used in this tray. These are to be set to the |
| 39 // border of the icon, depending on the current |shelf_alignment()|. |
| 40 const int kHorizontalShelfHorizontalPadding = 8; |
| 41 const int kHorizontalShelfVerticalPadding = 4; |
| 42 const int kVerticalShelfHorizontalPadding = 2; |
| 43 const int kVerticalShelfVerticalPadding = 5; |
| 44 |
| 45 // Width of the palette itself (dp). |
| 46 const int kPaletteWidth = 360; |
| 47 |
| 48 // Returns true if the command line flag is present that enables the palette. |
| 49 bool IsPaletteEnabled() { |
| 50 // TODO(jdufault): Hookup this function when the flag is in Chrome. |
| 51 return false; |
| 52 } |
| 53 |
| 54 // Creates a separator. |
| 55 views::Separator* CreateSeparator(views::Separator::Orientation orientation) { |
| 56 const int kSeparatorInset = 10; |
| 57 |
| 58 views::Separator* separator = |
| 59 new views::Separator(views::Separator::HORIZONTAL); |
| 60 separator->SetColor(ash::kBorderDarkColor); |
| 61 separator->SetBorder( |
| 62 views::Border::CreateEmptyBorder(kSeparatorInset, 0, kSeparatorInset, 0)); |
| 63 return separator; |
| 64 } |
| 65 |
| 66 // Creates the title-bar view. |
| 67 views::View* CreateTitleView() { |
| 68 auto& rb = ui::ResourceBundle::GetSharedInstance(); |
| 69 |
| 70 auto* root = new views::View(); |
| 71 auto* box_layout = |
| 72 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0); |
| 73 root->SetLayoutManager(box_layout); |
| 74 root->SetBorder(views::Border::CreateEmptyBorder( |
| 75 0, ash::kTrayPopupPaddingHorizontal, 0, 0)); |
| 76 |
| 77 views::Label* text_label = |
| 78 new views::Label(l10n_util::GetStringUTF16(IDS_ASH_PALETTE_TITLE)); |
| 79 text_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 80 text_label->SetFontList(rb.GetFontList(ui::ResourceBundle::BoldFont)); |
| 81 root->AddChildView(text_label); |
| 82 box_layout->SetFlexForView(text_label, 1); |
| 83 |
| 84 // TODO(jdufault): Use proper icons. |
| 85 ash::TrayPopupHeaderButton* help_button = new ash::TrayPopupHeaderButton( |
| 86 nullptr, IDR_AURA_UBER_TRAY_SHUTDOWN, IDR_AURA_UBER_TRAY_SHUTDOWN, |
| 87 IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER, IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER, |
| 88 IDS_ASH_STATUS_TRAY_SHUTDOWN); |
| 89 help_button->SetTooltipText( |
| 90 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SHUTDOWN)); |
| 91 root->AddChildView(help_button); |
| 92 |
| 93 root->AddChildView(CreateSeparator(views::Separator::VERTICAL)); |
| 94 |
| 95 // TODO(jdufault): Use proper icons. |
| 96 ash::TrayPopupHeaderButton* settings_button = new ash::TrayPopupHeaderButton( |
| 97 nullptr, IDR_AURA_UBER_TRAY_SHUTDOWN, IDR_AURA_UBER_TRAY_SHUTDOWN, |
| 98 IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER, IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER, |
| 99 IDS_ASH_STATUS_TRAY_SHUTDOWN); |
| 100 settings_button->SetTooltipText( |
| 101 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SHUTDOWN)); |
| 102 root->AddChildView(settings_button); |
| 103 |
| 104 return root; |
| 105 } |
| 106 |
| 107 } // namespace |
| 108 |
| 109 PaletteTray::PaletteTray(WmShelf* wm_shelf) |
| 110 : TrayBackgroundView(wm_shelf), |
| 111 palette_tool_manager_(new PaletteToolManager(this)) { |
| 112 PaletteTool::RegisterToolInstances(palette_tool_manager_.get()); |
| 113 |
| 114 SetContentsBackground(); |
| 115 |
| 116 SetLayoutManager(new views::FillLayout()); |
| 117 icon_ = new views::ImageView(); |
| 118 UpdateTrayIcon(); |
| 119 |
| 120 SetIconBorderForShelfAlignment(); |
| 121 tray_container()->AddChildView(icon_); |
| 122 |
| 123 WmShell::Get()->AddShellObserver(this); |
| 124 WmShell::Get()->GetSessionStateDelegate()->AddSessionStateObserver(this); |
| 125 |
| 126 UpdateIconVisibility(); |
| 127 } |
| 128 |
| 129 PaletteTray::~PaletteTray() { |
| 130 if (bubble_) |
| 131 bubble_->bubble_view()->reset_delegate(); |
| 132 |
| 133 WmShell::Get()->RemoveShellObserver(this); |
| 134 WmShell::Get()->GetSessionStateDelegate()->RemoveSessionStateObserver(this); |
| 135 } |
| 136 |
| 137 bool PaletteTray::PerformAction(const ui::Event& event) { |
| 138 if (bubble_) { |
| 139 bubble_.reset(); |
| 140 return true; |
| 141 } |
| 142 |
| 143 return OpenBubble(); |
| 144 } |
| 145 |
| 146 bool PaletteTray::OpenBubble() { |
| 147 views::TrayBubbleView::InitParams init_params( |
| 148 views::TrayBubbleView::ANCHOR_TYPE_TRAY, GetAnchorAlignment(), |
| 149 kPaletteWidth, kPaletteWidth); |
| 150 init_params.first_item_has_no_margin = true; |
| 151 init_params.can_activate = true; |
| 152 init_params.close_on_deactivate = true; |
| 153 |
| 154 DCHECK(tray_container()); |
| 155 |
| 156 // Create view, customize it. |
| 157 views::TrayBubbleView* bubble_view = |
| 158 views::TrayBubbleView::Create(tray_container(), this, &init_params); |
| 159 bubble_view->SetArrowPaintType(views::BubbleBorder::PAINT_NONE); |
| 160 bubble_view->UseCompactMargins(); |
| 161 bubble_view->set_margins(gfx::Insets(bubble_view->margins().top(), 0, 0, 0)); |
| 162 |
| 163 // Add child views. |
| 164 bubble_view->AddChildView(CreateTitleView()); |
| 165 bubble_view->AddChildView(CreateSeparator(views::Separator::HORIZONTAL)); |
| 166 AddToolsToView(bubble_view); |
| 167 |
| 168 // Show the bubble. |
| 169 bubble_.reset(new ash::TrayBubbleWrapper(this, bubble_view)); |
| 170 |
| 171 SetDrawBackgroundAsActive(true); |
| 172 |
| 173 return true; |
| 174 } |
| 175 |
| 176 void PaletteTray::AddToolsToView(views::View* host) { |
| 177 std::vector<PaletteToolView> views = palette_tool_manager_->CreateViews(); |
| 178 |
| 179 // There may not be any registered tools. |
| 180 if (!views.size()) |
| 181 return; |
| 182 |
| 183 PaletteGroup group = views[0].group; |
| 184 for (const PaletteToolView& view : views) { |
| 185 // If the group changes, add a separator. |
| 186 if (group != view.group) { |
| 187 group = view.group; |
| 188 host->AddChildView(CreateSeparator(views::Separator::HORIZONTAL)); |
| 189 } |
| 190 |
| 191 host->AddChildView(view.view); |
| 192 } |
| 193 } |
| 194 |
| 195 void PaletteTray::SessionStateChanged( |
| 196 SessionStateDelegate::SessionState state) { |
| 197 UpdateIconVisibility(); |
| 198 } |
| 199 |
| 200 void PaletteTray::ClickedOutsideBubble() { |
| 201 HidePalette(); |
| 202 } |
| 203 |
| 204 base::string16 PaletteTray::GetAccessibleNameForTray() { |
| 205 return l10n_util::GetStringUTF16(IDS_ASH_PALETTE_TITLE); |
| 206 } |
| 207 |
| 208 void PaletteTray::HideBubbleWithView(const views::TrayBubbleView* bubble_view) { |
| 209 if (bubble_->bubble_view() == bubble_view) |
| 210 bubble_.reset(); |
| 211 } |
| 212 |
| 213 void PaletteTray::BubbleViewDestroyed() { |
| 214 palette_tool_manager_->NotifyViewsDestroyed(); |
| 215 SetDrawBackgroundAsActive(false); |
| 216 } |
| 217 |
| 218 void PaletteTray::OnMouseEnteredView() {} |
| 219 |
| 220 void PaletteTray::OnMouseExitedView() {} |
| 221 |
| 222 base::string16 PaletteTray::GetAccessibleNameForBubble() { |
| 223 return GetAccessibleNameForTray(); |
| 224 } |
| 225 |
| 226 gfx::Rect PaletteTray::GetAnchorRect( |
| 227 views::Widget* anchor_widget, |
| 228 views::TrayBubbleView::AnchorType anchor_type, |
| 229 views::TrayBubbleView::AnchorAlignment anchor_alignment) const { |
| 230 gfx::Rect r = |
| 231 GetBubbleAnchorRect(anchor_widget, anchor_type, anchor_alignment); |
| 232 |
| 233 // Move the palette to the left so the right edge of the palette aligns with |
| 234 // the right edge of the tray button. |
| 235 if (IsHorizontalAlignment(shelf_alignment())) |
| 236 r.Offset(-r.width() + tray_container()->width(), 0); |
| 237 else |
| 238 r.Offset(0, -r.height() + tray_container()->height()); |
| 239 |
| 240 return r; |
| 241 } |
| 242 |
| 243 void PaletteTray::OnBeforeBubbleWidgetInit( |
| 244 views::Widget* anchor_widget, |
| 245 views::Widget* bubble_widget, |
| 246 views::Widget::InitParams* params) const { |
| 247 // Place the bubble in the same root window as |anchor_widget|. |
| 248 WmLookup::Get() |
| 249 ->GetWindowForWidget(anchor_widget) |
| 250 ->GetRootWindowController() |
| 251 ->ConfigureWidgetInitParamsForContainer( |
| 252 bubble_widget, kShellWindowId_SettingBubbleContainer, params); |
| 253 } |
| 254 |
| 255 void PaletteTray::HideBubble(const views::TrayBubbleView* bubble_view) { |
| 256 HideBubbleWithView(bubble_view); |
| 257 } |
| 258 |
| 259 void PaletteTray::HidePalette() { |
| 260 bubble_.reset(); |
| 261 } |
| 262 |
| 263 void PaletteTray::OnActiveToolChanged() { |
| 264 UpdateTrayIcon(); |
| 265 } |
| 266 |
| 267 WmWindow* PaletteTray::GetWindow() { |
| 268 return shelf()->GetWindow(); |
| 269 } |
| 270 |
| 271 void PaletteTray::SetShelfAlignment(ShelfAlignment alignment) { |
| 272 if (alignment == shelf_alignment()) |
| 273 return; |
| 274 |
| 275 TrayBackgroundView::SetShelfAlignment(alignment); |
| 276 SetIconBorderForShelfAlignment(); |
| 277 } |
| 278 |
| 279 void PaletteTray::AnchorUpdated() { |
| 280 if (bubble_) |
| 281 bubble_->bubble_view()->UpdateBubble(); |
| 282 } |
| 283 |
| 284 void PaletteTray::SetIconBorderForShelfAlignment() { |
| 285 // TODO(tdanderson): Ensure PaletteTray follows material design specs. See |
| 286 // crbug.com/630464. |
| 287 if (IsHorizontalAlignment(shelf_alignment())) { |
| 288 icon_->SetBorder(views::Border::CreateEmptyBorder(gfx::Insets( |
| 289 kHorizontalShelfVerticalPadding, kHorizontalShelfHorizontalPadding))); |
| 290 } else { |
| 291 icon_->SetBorder(views::Border::CreateEmptyBorder(gfx::Insets( |
| 292 kVerticalShelfVerticalPadding, kVerticalShelfHorizontalPadding))); |
| 293 } |
| 294 } |
| 295 |
| 296 void PaletteTray::UpdateTrayIcon() { |
| 297 gfx::VectorIconId icon = palette_tool_manager_->GetActiveTrayIcon( |
| 298 palette_tool_manager_->GetActiveTool(ash::PaletteGroup::MODE)); |
| 299 icon_->SetImage(CreateVectorIcon(icon, kShelfIconColor)); |
| 300 } |
| 301 |
| 302 void PaletteTray::UpdateIconVisibility() { |
| 303 if (!IsPaletteEnabled()) |
| 304 return; |
| 305 |
| 306 SessionStateDelegate* session_state_delegate = |
| 307 WmShell::Get()->GetSessionStateDelegate(); |
| 308 |
| 309 SetVisible(!session_state_delegate->IsScreenLocked() && |
| 310 session_state_delegate->GetSessionState() == |
| 311 SessionStateDelegate::SESSION_STATE_ACTIVE && |
| 312 WmShell::Get()->system_tray_delegate()->GetUserLoginStatus() != |
| 313 LoginStatus::KIOSK_APP); |
| 314 } |
| 315 |
| 316 } // namespace ash |
| OLD | NEW |