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

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