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

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: Add icons from https://codereview.chromium.org/2143073003/ 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 {
36
37 // Predefined padding for the icon used in this tray. These are to be set to the
38 // border of the icon, depending on the current shelf_alignment()
Evan Stade 2016/07/22 23:04:34 nit: final punctuation and vertical bars around co
jdufault 2016/07/25 19:28:29 Done.
39 const int kHorizontalShelfHorizontalPadding = 8;
40 const int kHorizontalShelfVerticalPadding = 4;
41 const int kVerticalShelfHorizontalPadding = 2;
42 const int kVerticalShelfVerticalPadding = 5;
43
44 // Width of the palette itself.
45 const int kPaletteWidth = 360;
46
47 const int kSeparatorInset = 10;
48
49 const int kBubbleViewTopMargin = 7;
50
51 // Returns true if the command line flag is present that enables the palette.
52 bool IsPaletteEnabled() {
53 // TODO(jdufault): Hookup this function when the flag is in Chrome.
54 return false;
55 }
56
57 // Creates a separator.
58 views::Separator* CreateSeparator(views::Separator::Orientation orientation) {
59 views::Separator* separator =
60 new views::Separator(views::Separator::HORIZONTAL);
61 separator->SetColor(ash::kBorderDarkColor);
62 separator->SetBorder(
63 views::Border::CreateEmptyBorder(kSeparatorInset, 0, kSeparatorInset, 0));
64 return separator;
65 }
66
67 class TitleBarView : public views::View {
68 public:
69 TitleBarView() {
70 auto& rb = ui::ResourceBundle::GetSharedInstance();
71
72 auto* box_layout =
73 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0);
74 SetLayoutManager(box_layout);
75
76 views::Label* text_label =
77 new views::Label(l10n_util::GetStringUTF16(IDS_ASH_PALETTE_TITLE));
78 text_label->SetBorder(views::Border::CreateEmptyBorder(
79 0, ash::kTrayPopupPaddingHorizontal, 0, 0));
80 text_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
81 text_label->SetFontList(rb.GetFontList(ui::ResourceBundle::BoldFont));
82 AddChildView(text_label);
83 box_layout->SetFlexForView(text_label, 1);
84
85 // TODO(jdufault): Use proper icons.
86 ash::TrayPopupHeaderButton* help_button = new ash::TrayPopupHeaderButton(
87 nullptr, IDR_AURA_UBER_TRAY_SHUTDOWN, IDR_AURA_UBER_TRAY_SHUTDOWN,
88 IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER, IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
89 IDS_ASH_STATUS_TRAY_SHUTDOWN);
90 help_button->SetTooltipText(
91 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SHUTDOWN));
92 AddChildView(help_button);
93
94 AddChildView(CreateSeparator(views::Separator::VERTICAL));
95
96 // TODO(jdufault): Use proper icons.
97 ash::TrayPopupHeaderButton* settings_button =
98 new ash::TrayPopupHeaderButton(
99 nullptr, IDR_AURA_UBER_TRAY_SHUTDOWN, IDR_AURA_UBER_TRAY_SHUTDOWN,
100 IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER,
101 IDR_AURA_UBER_TRAY_SHUTDOWN_HOVER, IDS_ASH_STATUS_TRAY_SHUTDOWN);
102 settings_button->SetTooltipText(
103 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SHUTDOWN));
104 AddChildView(settings_button);
105 }
106
107 ~TitleBarView() override {}
108
109 private:
110 DISALLOW_COPY_AND_ASSIGN(TitleBarView);
111 };
112
113 } // namespace
Evan Stade 2016/07/22 23:04:34 I would put this anonymous namespace inside the as
jdufault 2016/07/25 19:28:29 Done.
114
115 namespace ash {
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));
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 base::Optional<gfx::VectorIconId> tray_icon =
Evan Stade 2016/07/22 23:04:34 I don't think base::Optional is necessary here, ty
jdufault 2016/07/25 19:28:29 I've removed the base::Optional bit. The fallback
Evan Stade 2016/07/25 19:57:53 sorry, still don't follow. I tracked down where th
jdufault 2016/07/25 22:17:33 Done.
305 palette_tool_manager_->GetActiveTrayIcon(
306 palette_tool_manager_->GetActiveTool(ash::PaletteGroup::MODE));
307 gfx::VectorIconId icon =
308 tray_icon.value_or(gfx::VectorIconId::PALETTE_TRAY_ICON_DEFAULT);
309
310 icon_->SetImage(CreateVectorIcon(icon, kShelfIconColor));
311 }
312
313 void PaletteTray::UpdateIconVisibility() {
314 if (!IsPaletteEnabled())
315 return;
316
317 SessionStateDelegate* session_state_delegate =
318 WmShell::Get()->GetSessionStateDelegate();
319
320 SetVisible(!session_state_delegate->IsScreenLocked() &&
321 session_state_delegate->GetSessionState() ==
322 SessionStateDelegate::SESSION_STATE_ACTIVE &&
323 WmShell::Get()->system_tray_delegate()->GetUserLoginStatus() !=
324 LoginStatus::KIOSK_APP);
325 }
326
327 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698