Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: ash/common/system/chromeos/palette/palette_tray.cc

Issue 2148573002: Add palette tray to ash. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@stylus-tool-structure
Patch Set: Address comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 const int kSeparatorInset = 10;
Evan Stade 2016/07/25 23:32:06 nit: imo, this kind of constant which is only used
jdufault 2016/07/25 23:44:39 Moved to inside of CreateSeparator.
49
50 const int kBubbleViewTopMargin = 7;
Evan Stade 2016/07/25 23:32:06 this should probably be shared with something. It'
jdufault 2016/07/25 23:44:39 Done.
51
52 // Returns true if the command line flag is present that enables the palette.
53 bool IsPaletteEnabled() {
54 // TODO(jdufault): Hookup this function when the flag is in Chrome.
55 return false;
56 }
57
58 // Creates a separator.
59 views::Separator* CreateSeparator(views::Separator::Orientation orientation) {
60 views::Separator* separator =
61 new views::Separator(views::Separator::HORIZONTAL);
62 separator->SetColor(ash::kBorderDarkColor);
63 separator->SetBorder(
64 views::Border::CreateEmptyBorder(kSeparatorInset, 0, kSeparatorInset, 0));
65 return separator;
66 }
67
68 // Creates the title-bar view.
69 views::View* CreateTitleView() {
70 auto& rb = ui::ResourceBundle::GetSharedInstance();
71
72 auto* root = new views::View();
73 auto* box_layout =
74 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0);
75 root->SetLayoutManager(box_layout);
76 root->SetBorder(views::Border::CreateEmptyBorder(
77 kBubbleViewTopMargin, ash::kTrayPopupPaddingHorizontal, 0, 0));
78
79 views::Label* text_label =
80 new views::Label(l10n_util::GetStringUTF16(IDS_ASH_PALETTE_TITLE));
81 text_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
82 text_label->SetFontList(rb.GetFontList(ui::ResourceBundle::BoldFont));
83 root->AddChildView(text_label);
84 box_layout->SetFlexForView(text_label, 1);
85
86 // TODO(jdufault): Use proper icons.
87 ash::TrayPopupHeaderButton* help_button = new ash::TrayPopupHeaderButton(
88 nullptr, IDR_AURA_UBER_TRAY_SHUTDOWN, IDR_AURA_UBER_TRAY_SHUTDOWN,
89 IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER, IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
90 IDS_ASH_STATUS_TRAY_SHUTDOWN);
91 help_button->SetTooltipText(
92 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SHUTDOWN));
93 root->AddChildView(help_button);
94
95 root->AddChildView(CreateSeparator(views::Separator::VERTICAL));
96
97 // TODO(jdufault): Use proper icons.
98 ash::TrayPopupHeaderButton* settings_button = new ash::TrayPopupHeaderButton(
99 nullptr, IDR_AURA_UBER_TRAY_SHUTDOWN, IDR_AURA_UBER_TRAY_SHUTDOWN,
100 IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER, IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
101 IDS_ASH_STATUS_TRAY_SHUTDOWN);
102 settings_button->SetTooltipText(
103 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SHUTDOWN));
104 root->AddChildView(settings_button);
105
106 return root;
107 }
108
109 } // namespace
110
111 PaletteTray::PaletteTray(WmShelf* wm_shelf)
112 : TrayBackgroundView(wm_shelf),
113 palette_tool_manager_(new PaletteToolManager(this)) {
114 PaletteTool::RegisterToolInstances(palette_tool_manager_.get());
115
116 SetContentsBackground();
117
118 SetLayoutManager(new views::FillLayout());
119 icon_ = new views::ImageView();
120 UpdateTrayIcon();
121
122 SetIconBorderForShelfAlignment();
123 tray_container()->AddChildView(icon_);
124
125 WmShell::Get()->AddShellObserver(this);
126 WmShell::Get()->GetSessionStateDelegate()->AddSessionStateObserver(this);
127
128 UpdateIconVisibility();
129 }
130
131 PaletteTray::~PaletteTray() {
132 if (bubble_)
133 bubble_->bubble_view()->reset_delegate();
134
135 WmShell::Get()->RemoveShellObserver(this);
136 WmShell::Get()->GetSessionStateDelegate()->RemoveSessionStateObserver(this);
137 }
138
139 bool PaletteTray::PerformAction(const ui::Event& event) {
140 if (bubble_) {
141 bubble_.reset();
142 return true;
143 }
144
145 return OpenBubble();
146 }
147
148 bool PaletteTray::OpenBubble() {
149 views::TrayBubbleView::InitParams init_params(
150 views::TrayBubbleView::ANCHOR_TYPE_TRAY, GetAnchorAlignment(),
151 kPaletteWidth, kPaletteWidth);
152 init_params.first_item_has_no_margin = true;
153 init_params.can_activate = true;
154 init_params.close_on_deactivate = true;
155
156 DCHECK(tray_container());
157
158 // Create view, customize it.
159 views::TrayBubbleView* bubble_view =
160 views::TrayBubbleView::Create(tray_container(), this, &init_params);
161 bubble_view->SetArrowPaintType(views::BubbleBorder::PAINT_NONE);
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
OLDNEW
« no previous file with comments | « ash/common/system/chromeos/palette/palette_tray.h ('k') | ash/common/system/status_area_widget.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698