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

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