Chromium Code Reviews| 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/gfx/vector_icons_public.h" | |
| 29 #include "ui/views/controls/image_view.h" | |
| 30 #include "ui/views/controls/label.h" | |
| 31 #include "ui/views/controls/separator.h" | |
| 32 #include "ui/views/layout/box_layout.h" | |
| 33 | |
| 34 #include "ui/views/layout/fill_layout.h" | |
| 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. | |
| 46 const int kPaletteWidth = 360; | |
| 47 | |
| 48 const int kSeparatorInset = 10; | |
| 49 | |
| 50 const int kBubbleViewTopMargin = 7; | |
| 51 | |
| 52 // Returns the icon that should be used in the status tray for the given tool. | |
| 53 gfx::VectorIconId GetTrayIconForTool(ash::PaletteToolId tool_id) { | |
| 54 switch (tool_id) { | |
| 55 case ash::PaletteToolId::PRESENTATION: | |
| 56 return gfx::VectorIconId::PALETTE_PRESENTATION_BEHAVIOR; | |
| 57 case ash::PaletteToolId::MAGNIFY: | |
| 58 return gfx::VectorIconId::PALETTE_MAGNIFY_BEHAVIOR; | |
| 59 default: | |
| 60 return gfx::VectorIconId::PALETTE_DEFAULT_BEHAVIOR; | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 // Creates a separator. | |
| 65 views::Separator* CreateSeparator(views::Separator::Orientation orientation) { | |
| 66 views::Separator* separator = | |
| 67 new views::Separator(views::Separator::HORIZONTAL); | |
| 68 separator->SetColor(ash::kBorderDarkColor); | |
| 69 separator->SetBorder( | |
| 70 views::Border::CreateEmptyBorder(kSeparatorInset, 0, kSeparatorInset, 0)); | |
| 71 return separator; | |
| 72 } | |
| 73 | |
| 74 class TitleBarView : public views::View { | |
| 75 public: | |
| 76 TitleBarView() { | |
| 77 auto& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 78 | |
| 79 auto* box_layout = | |
| 80 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0); | |
| 81 SetLayoutManager(box_layout); | |
| 82 | |
| 83 views::Label* text_label = | |
| 84 new views::Label(l10n_util::GetStringUTF16(IDS_ASH_PALETTE_TITLE)); | |
| 85 text_label->SetBorder(views::Border::CreateEmptyBorder( | |
| 86 0, ash::kTrayPopupPaddingHorizontal, 0, 0)); | |
| 87 text_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 88 text_label->SetFontList(rb.GetFontList(ui::ResourceBundle::BoldFont)); | |
| 89 AddChildView(text_label); | |
| 90 box_layout->SetFlexForView(text_label, 1); | |
| 91 | |
| 92 // TODO(jdufault): Use proper icons. | |
| 93 ash::TrayPopupHeaderButton* help_button = new ash::TrayPopupHeaderButton( | |
| 94 nullptr, IDR_AURA_UBER_TRAY_SHUTDOWN, IDR_AURA_UBER_TRAY_SHUTDOWN, | |
| 95 IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER, IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER, | |
| 96 IDS_ASH_STATUS_TRAY_SHUTDOWN); | |
| 97 help_button->SetTooltipText( | |
| 98 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SHUTDOWN)); | |
| 99 AddChildView(help_button); | |
| 100 | |
| 101 AddChildView(CreateSeparator(views::Separator::VERTICAL)); | |
| 102 | |
| 103 // TODO(jdufault): Use proper icons. | |
| 104 ash::TrayPopupHeaderButton* settings_button = | |
| 105 new ash::TrayPopupHeaderButton( | |
| 106 nullptr, IDR_AURA_UBER_TRAY_SHUTDOWN, IDR_AURA_UBER_TRAY_SHUTDOWN, | |
| 107 IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER, | |
| 108 IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER, IDS_ASH_STATUS_TRAY_SHUTDOWN); | |
| 109 settings_button->SetTooltipText( | |
| 110 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SHUTDOWN)); | |
| 111 AddChildView(settings_button); | |
| 112 } | |
| 113 | |
| 114 ~TitleBarView() override {} | |
| 115 | |
| 116 private: | |
| 117 DISALLOW_COPY_AND_ASSIGN(TitleBarView); | |
| 118 }; | |
| 119 | |
| 120 } // namespace | |
| 121 | |
| 122 namespace ash { | |
| 123 | |
| 124 PaletteTray::PaletteTray(WmShelf* wm_shelf) | |
| 125 : TrayBackgroundView(wm_shelf), | |
| 126 palette_tool_manager_(new PaletteToolManager( | |
| 127 wm_shelf->GetWindow(), | |
| 128 base::Bind(&PaletteTray::UpdateTrayIcon, base::Unretained(this)))) { | |
| 129 PaletteTool::RegisterToolInstances(palette_tool_manager_.get()); | |
| 130 | |
| 131 SetContentsBackground(); | |
| 132 | |
| 133 SetLayoutManager(new views::FillLayout()); | |
| 134 icon_ = new views::ImageView(); | |
| 135 UpdateTrayIcon(); | |
| 136 | |
| 137 SetIconBorderForShelfAlignment(); | |
| 138 tray_container()->AddChildView(icon_); | |
| 139 | |
| 140 WmShell::Get()->AddShellObserver(this); | |
| 141 WmShell::Get()->GetSessionStateDelegate()->AddSessionStateObserver(this); | |
| 142 | |
| 143 UpdateIconVisibility(); | |
| 144 } | |
| 145 | |
| 146 PaletteTray::~PaletteTray() { | |
| 147 if (bubble_) | |
| 148 bubble_->bubble_view()->reset_delegate(); | |
| 149 | |
| 150 WmShell::Get()->RemoveShellObserver(this); | |
| 151 WmShell::Get()->GetSessionStateDelegate()->RemoveSessionStateObserver(this); | |
| 152 } | |
| 153 | |
| 154 bool PaletteTray::PerformAction(const ui::Event& event) { | |
| 155 if (bubble_) { | |
| 156 bubble_.reset(); | |
| 157 return true; | |
| 158 } | |
| 159 | |
| 160 return OpenBubble(); | |
| 161 } | |
| 162 | |
| 163 bool PaletteTray::OpenBubble() { | |
| 164 views::TrayBubbleView::InitParams init_params( | |
| 165 views::TrayBubbleView::ANCHOR_TYPE_TRAY, GetAnchorAlignment(), | |
| 166 kPaletteWidth, kPaletteWidth); | |
| 167 init_params.first_item_has_no_margin = true; | |
| 168 init_params.can_activate = true; | |
| 169 init_params.close_on_deactivate = true; | |
| 170 | |
| 171 views::View* anchor = this->tray_container(); | |
|
oshima
2016/07/13 21:14:23
try_container()?
jdufault
2016/07/18 20:44:02
Done.
| |
| 172 DCHECK(anchor); | |
|
oshima
2016/07/13 21:14:23
or just inline tray_container()
jdufault
2016/07/18 20:44:02
Done.
| |
| 173 | |
| 174 // Create view, customize it. | |
| 175 views::TrayBubbleView* bubble_view = | |
| 176 views::TrayBubbleView::Create(anchor, this, &init_params); | |
| 177 bubble_view->set_margins(gfx::Insets(kBubbleViewTopMargin, 0, 0, 0)); | |
| 178 bubble_view->SetArrowPaintType(views::BubbleBorder::PAINT_NONE); | |
| 179 | |
| 180 // Add child views. | |
| 181 bubble_view->AddChildView(new TitleBarView()); | |
| 182 bubble_view->AddChildView(CreateSeparator(views::Separator::HORIZONTAL)); | |
| 183 AddToolsToView(bubble_view); | |
| 184 | |
| 185 // Show the bubble. | |
| 186 bubble_.reset(new ash::TrayBubbleWrapper(this, bubble_view)); | |
| 187 | |
| 188 SetDrawBackgroundAsActive(true); | |
| 189 | |
| 190 return true; | |
| 191 } | |
| 192 | |
| 193 void PaletteTray::AddToolsToView(views::View* host) { | |
| 194 std::vector<PaletteToolView> views = palette_tool_manager_->CreateViews(); | |
| 195 if (!views.size()) | |
| 196 return; | |
|
oshima
2016/07/13 21:14:23
can you add comment?
jdufault
2016/07/18 20:44:02
Done.
| |
| 197 | |
| 198 PaletteGroup group = views[0].group; | |
| 199 for (const PaletteToolView& view : views) { | |
| 200 // If the group changes, add a separator. | |
| 201 if (group != view.group) { | |
| 202 group = view.group; | |
| 203 host->AddChildView(CreateSeparator(views::Separator::HORIZONTAL)); | |
| 204 } | |
| 205 | |
| 206 host->AddChildView(view.view); | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 void PaletteTray::SessionStateChanged( | |
| 211 SessionStateDelegate::SessionState state) { | |
| 212 UpdateIconVisibility(); | |
| 213 } | |
| 214 | |
| 215 void PaletteTray::ClickedOutsideBubble() { | |
| 216 bubble_.reset(); | |
| 217 } | |
| 218 | |
| 219 base::string16 PaletteTray::GetAccessibleNameForTray() { | |
| 220 return l10n_util::GetStringUTF16(IDS_ASH_PALETTE_TITLE); | |
| 221 } | |
| 222 | |
| 223 void PaletteTray::HideBubbleWithView(const views::TrayBubbleView* bubble_view) { | |
| 224 if (bubble_->bubble_view() == bubble_view) | |
| 225 bubble_.reset(); | |
| 226 } | |
| 227 | |
| 228 void PaletteTray::BubbleViewDestroyed() { | |
| 229 palette_tool_manager_->ViewsDestroyed(); | |
| 230 SetDrawBackgroundAsActive(false); | |
| 231 } | |
| 232 | |
| 233 void PaletteTray::OnMouseEnteredView() {} | |
| 234 | |
| 235 void PaletteTray::OnMouseExitedView() {} | |
| 236 | |
| 237 base::string16 PaletteTray::GetAccessibleNameForBubble() { | |
| 238 return GetAccessibleNameForTray(); | |
| 239 } | |
| 240 | |
| 241 gfx::Rect PaletteTray::GetAnchorRect( | |
| 242 views::Widget* anchor_widget, | |
| 243 views::TrayBubbleView::AnchorType anchor_type, | |
| 244 views::TrayBubbleView::AnchorAlignment anchor_alignment) const { | |
| 245 gfx::Rect r = | |
| 246 GetBubbleAnchorRect(anchor_widget, anchor_type, anchor_alignment); | |
| 247 | |
| 248 // Move the palette to the left so the right edge of the palette aligns with | |
| 249 // the right edge of the tray button. | |
| 250 if (IsHorizontalAlignment(shelf_alignment())) | |
| 251 r.Offset(-r.width() + tray_container()->width(), 0); | |
| 252 else | |
| 253 r.Offset(0, -r.height() + tray_container()->height()); | |
| 254 | |
| 255 return r; | |
| 256 } | |
| 257 | |
| 258 void PaletteTray::OnBeforeBubbleWidgetInit( | |
| 259 views::Widget* anchor_widget, | |
| 260 views::Widget* bubble_widget, | |
| 261 views::Widget::InitParams* params) const { | |
| 262 // Place the bubble in the same root window as |anchor_widget|. | |
| 263 WmLookup::Get() | |
| 264 ->GetWindowForWidget(anchor_widget) | |
| 265 ->GetRootWindowController() | |
| 266 ->ConfigureWidgetInitParamsForContainer( | |
| 267 bubble_widget, kShellWindowId_SettingBubbleContainer, params); | |
| 268 } | |
| 269 | |
| 270 void PaletteTray::HideBubble(const views::TrayBubbleView* bubble_view) { | |
| 271 HideBubbleWithView(bubble_view); | |
| 272 } | |
| 273 | |
| 274 void PaletteTray::SetShelfAlignment(ShelfAlignment alignment) { | |
| 275 if (alignment == shelf_alignment()) | |
| 276 return; | |
| 277 | |
| 278 TrayBackgroundView::SetShelfAlignment(alignment); | |
| 279 SetIconBorderForShelfAlignment(); | |
| 280 } | |
| 281 | |
| 282 void PaletteTray::AnchorUpdated() { | |
| 283 if (bubble_) | |
| 284 bubble_->bubble_view()->UpdateBubble(); | |
| 285 } | |
| 286 | |
| 287 void PaletteTray::SetIconBorderForShelfAlignment() { | |
| 288 if (IsHorizontalAlignment(shelf_alignment())) { | |
| 289 icon_->SetBorder(views::Border::CreateEmptyBorder( | |
| 290 kHorizontalShelfVerticalPadding, kHorizontalShelfHorizontalPadding, | |
|
oshima
2016/07/13 21:14:23
nit: Insets(h, v) may be shorter.
jdufault
2016/07/18 20:44:02
Easier to read, at least.
| |
| 291 kHorizontalShelfVerticalPadding, kHorizontalShelfHorizontalPadding)); | |
| 292 } else { | |
| 293 icon_->SetBorder(views::Border::CreateEmptyBorder( | |
| 294 kVerticalShelfVerticalPadding, kVerticalShelfHorizontalPadding, | |
| 295 kVerticalShelfVerticalPadding, kVerticalShelfHorizontalPadding)); | |
| 296 } | |
| 297 } | |
| 298 | |
| 299 void PaletteTray::UpdateTrayIcon() { | |
| 300 gfx::VectorIconId icon = GetTrayIconForTool( | |
| 301 palette_tool_manager_->GetActiveTool(PaletteGroup::MODE)); | |
| 302 | |
| 303 gfx::ImageSkia image_md = CreateVectorIcon(icon, kShelfIconColor); | |
| 304 icon_->SetImage(image_md); | |
| 305 } | |
| 306 | |
| 307 void PaletteTray::UpdateIconVisibility() { | |
| 308 // TODO(jdufault): Properly plumb if the UI should be shown. | |
| 309 return; | |
|
oshima
2016/07/13 21:14:23
debug code?
jdufault
2016/07/18 20:44:02
There's no command line flag added yet that this w
| |
| 310 | |
| 311 SessionStateDelegate* session_state_delegate = | |
| 312 WmShell::Get()->GetSessionStateDelegate(); | |
| 313 | |
| 314 SetVisible(!session_state_delegate->IsScreenLocked() && | |
| 315 session_state_delegate->GetSessionState() == | |
| 316 SessionStateDelegate::SESSION_STATE_ACTIVE && | |
| 317 WmShell::Get()->system_tray_delegate()->GetUserLoginStatus() != | |
| 318 LoginStatus::KIOSK_APP); | |
| 319 } | |
| 320 | |
| 321 } // namespace ash | |
| OLD | NEW |