forked from kiwibrowser/src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshelf_view.h
546 lines (419 loc) · 20.8 KB
/
shelf_view.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ASH_SHELF_SHELF_VIEW_H_
#define ASH_SHELF_SHELF_VIEW_H_
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "ash/public/cpp/shelf_model_observer.h"
#include "ash/public/interfaces/shelf.mojom.h"
#include "ash/shelf/ink_drop_button_listener.h"
#include "ash/shelf/shelf_button_pressed_metric_tracker.h"
#include "ash/shelf/shelf_tooltip_manager.h"
#include "ash/wm/tablet_mode/tablet_mode_observer.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/app_list/views/app_list_drag_and_drop_host.h"
#include "ui/views/animation/bounds_animator_observer.h"
#include "ui/views/animation/ink_drop_state.h"
#include "ui/views/context_menu_controller.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/menu/menu_types.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/view.h"
#include "ui/views/view_model.h"
namespace ui {
class MenuModel;
}
namespace views {
class BoundsAnimator;
class MenuRunner;
}
namespace ash {
class AppListButton;
class BackButton;
class DragImageView;
class OverflowBubble;
class OverflowButton;
class ScopedRootWindowForNewWindows;
class Shelf;
class ShelfButton;
class ShelfModel;
struct ShelfItem;
class ShelfWidget;
enum ShelfAlignmentUmaEnumValue {
SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM,
SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT,
SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT,
// Must be last entry in enum.
SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT,
};
// ShelfView contains the shelf items visible within an active user session.
// ShelfView and LoginShelfView should never be shown together.
class ASH_EXPORT ShelfView : public views::View,
public ShelfModelObserver,
public InkDropButtonListener,
public views::ContextMenuController,
public views::FocusTraversable,
public views::BoundsAnimatorObserver,
public app_list::ApplicationDragAndDropHost {
public:
ShelfView(ShelfModel* model, Shelf* shelf, ShelfWidget* shelf_widget);
~ShelfView() override;
Shelf* shelf() const { return shelf_; }
ShelfModel* model() const { return model_; }
void Init();
void OnShelfAlignmentChanged();
// Returns the ideal bounds of the specified item, or an empty rect if id
// isn't know. If the item is in an overflow shelf, the overflow icon location
// will be returned.
gfx::Rect GetIdealBoundsOfItemIcon(const ShelfID& id);
// Repositions the icon for the specified item by the midpoint of the window.
void UpdatePanelIconPosition(const ShelfID& id, const gfx::Point& midpoint);
// Returns true if we're showing a menu.
bool IsShowingMenu() const;
// Returns true if we're showing a menu for |view|. |view| could be a
// ShelfButton or the ShelfView.
bool IsShowingMenuForView(views::View* view) const;
// Returns true if overflow bubble is shown.
bool IsShowingOverflowBubble() const;
// Sets owner overflow bubble instance from which this shelf view pops
// out as overflow.
void set_owner_overflow_bubble(OverflowBubble* owner) {
owner_overflow_bubble_ = owner;
}
AppListButton* GetAppListButton() const;
BackButton* GetBackButton() const;
// Returns true if the mouse cursor exits the area for launcher tooltip.
// There are thin gaps between launcher buttons but the tooltip shouldn't hide
// in the gaps, but the tooltip should hide if the mouse moved totally outside
// of the buttons area.
bool ShouldHideTooltip(const gfx::Point& cursor_location) const;
// Returns true if a tooltip should be shown for the shelf item |view|.
bool ShouldShowTooltipForView(const views::View* view) const;
// Returns the title of the shelf item |view|.
base::string16 GetTitleForView(const views::View* view) const;
// Returns rectangle bounding all visible launcher items. Used screen
// coordinate system.
gfx::Rect GetVisibleItemsBoundsInScreen();
// InkDropButtonListener:
void ButtonPressed(views::Button* sender,
const ui::Event& event,
views::InkDrop* ink_drop) override;
// Overridden from FocusTraversable:
views::FocusSearch* GetFocusSearch() override;
FocusTraversable* GetFocusTraversableParent() override;
View* GetFocusTraversableParentView() override;
// Overridden from app_list::ApplicationDragAndDropHost:
void CreateDragIconProxy(const gfx::Point& location_in_screen_coordinates,
const gfx::ImageSkia& icon,
views::View* replaced_view,
const gfx::Vector2d& cursor_offset_from_center,
float scale_factor) override;
void CreateDragIconProxyByLocationWithNoAnimation(
const gfx::Point& origin_in_screen_coordinates,
const gfx::ImageSkia& icon,
views::View* replaced_view,
float scale_factor) override;
void UpdateDragIconProxy(
const gfx::Point& location_in_screen_coordinates) override;
void UpdateDragIconProxyByLocation(
const gfx::Point& origin_in_screen_coordinates) override;
void DestroyDragIconProxy() override;
bool StartDrag(const std::string& app_id,
const gfx::Point& location_in_screen_coordinates) override;
bool Drag(const gfx::Point& location_in_screen_coordinates) override;
void EndDrag(bool cancel) override;
// Returns true if |event| on the shelf item is going to activate the item.
// Used to determine whether a pending ink drop should be shown or not.
bool ShouldEventActivateButton(views::View* view, const ui::Event& event);
// The shelf buttons use the Pointer interface to enable item reordering.
enum Pointer { NONE, DRAG_AND_DROP, MOUSE, TOUCH };
void PointerPressedOnButton(views::View* view,
Pointer pointer,
const ui::LocatedEvent& event);
void PointerDraggedOnButton(views::View* view,
Pointer pointer,
const ui::LocatedEvent& event);
void PointerReleasedOnButton(views::View* view,
Pointer pointer,
bool canceled);
// Updates the background for the shelf items.
void UpdateShelfItemBackground(SkColor color);
// True if the current |drag_view_| is the given |drag_view|.
bool IsDraggedView(const ShelfButton* drag_view) const;
// Returns the list of open windows that correspond to the app represented by
// this shelf view.
const std::vector<aura::Window*> GetOpenWindowsForShelfView(
views::View* view);
// Return the view model for test purposes.
const views::ViewModel* view_model_for_test() const {
return view_model_.get();
}
// Return the main shelf. This will return nullptr if this is not called on
// the overflow shelf.
ShelfView* main_shelf() { return main_shelf_; }
const ShelfButton* drag_view() const { return drag_view_; }
private:
friend class ShelfViewTestAPI;
class FadeOutAnimationDelegate;
class StartFadeAnimationDelegate;
enum RemovableState {
REMOVABLE, // Item can be removed when dragged away.
DRAGGABLE, // Item can be dragged, but will snap always back to origin.
NOT_REMOVABLE, // Item is fixed and can never be removed.
};
// Minimum distance before drag starts.
static const int kMinimumDragDistance;
// Returns true when this ShelfView is used for Overflow Bubble.
// In this mode, it does not show app list, panel and overflow button.
// Note:
// * When Shelf can contain only one item (overflow button) due to very
// small resolution screen, overflow bubble can show app list and panel
// button.
bool is_overflow_mode() const { return overflow_mode_; }
bool dragging() const { return drag_pointer_ != NONE; }
// Sets the bounds of each view to its ideal bounds.
void LayoutToIdealBounds();
// Update all button's visibility in overflow.
void UpdateAllButtonsVisibilityInOverflowMode();
// Calculates the ideal bounds. The bounds of each button corresponding to an
// item in the model is set in |view_model_|.
void CalculateIdealBounds(gfx::Rect* overflow_bounds) const;
// Returns the index of the last view whose max primary axis coordinate is
// less than |max_value|. Returns -1 if nothing fits, or there are no views.
int DetermineLastVisibleIndex(int max_value) const;
// Returns the index of the first panel whose min primary axis coordinate is
// at least |min_value|. Returns the index past the last panel if none fit.
int DetermineFirstVisiblePanelIndex(int min_value) const;
// Animates the bounds of each view to its ideal bounds.
void AnimateToIdealBounds();
// Creates the view used to represent |item|.
views::View* CreateViewForItem(const ShelfItem& item);
// Fades |view| from an opacity of 0 to 1. This is when adding a new item.
void FadeIn(views::View* view);
// Invoked when the pointer has moved enough to trigger a drag. Sets
// internal state in preparation for the drag.
void PrepareForDrag(Pointer pointer, const ui::LocatedEvent& event);
// Invoked when the mouse is dragged. Updates the models as appropriate.
void ContinueDrag(const ui::LocatedEvent& event);
// Ends the drag on the other shelf. (ie if we are on main shelf, ends drag on
// the overflow shelf). Invoked when a shelf item is being dragged from one
// shelf to the other.
void EndDragOnOtherShelf(bool cancel);
// Handles ripping off an item from the shelf. Returns true when the item got
// removed.
bool HandleRipOffDrag(const ui::LocatedEvent& event);
// Finalize the rip off dragging by either |cancel| the action or validating.
void FinalizeRipOffDrag(bool cancel);
// Check if an item can be ripped off or not.
RemovableState RemovableByRipOff(int index) const;
// Returns true if |typea| and |typeb| should be in the same drag range.
bool SameDragType(ShelfItemType typea, ShelfItemType typeb) const;
// Returns the range (in the model) the item at the specified index can be
// dragged to.
std::pair<int, int> GetDragRange(int index);
// If there is a drag operation in progress it's canceled. If |modified_index|
// is valid, the new position of the corresponding item is returned.
int CancelDrag(int modified_index);
// Returns rectangle bounds used for drag insertion.
// Note:
// * When overflow button is visible, returns bounds from first item
// to overflow button.
// * When overflow button is visible and one or more panel items exists,
// returns bounds from first item to last panel item.
// * In the overflow mode, returns only bubble's bounds.
gfx::Rect GetBoundsForDragInsertInScreen();
// Common setup done for all children.
void ConfigureChildView(views::View* view);
// Toggles the overflow menu.
void ToggleOverflowBubble();
// Invoked after the fading out animation for item deletion is ended.
void OnFadeOutAnimationEnded();
// Fade in last visible item.
void StartFadeInLastVisibleItem();
// Updates the visible range of overflow items in |overflow_view|.
void UpdateOverflowRange(ShelfView* overflow_view) const;
// Gets the menu anchor rect for menus. |source| is the view that is
// asking for a menu, |location| is the location of the event,
// |source_type| is the type of event that asked for the menu, and
// |context_menu| is whether the menu is for a context or app menu.
gfx::Rect GetMenuAnchorRect(const views::View* source,
const gfx::Point& location,
ui::MenuSourceType source_type,
bool context_menu) const;
// Gets the menu anchor rect that aligns the menu to the edge of the
// shelf for touch events. |source| is the view that is asking for the
// menu, |location| is the location of the event.
gfx::Rect GetTouchMenuAnchorRect(const views::View* source,
const gfx::Point& location) const;
// Gets the menu anchor position for a menu. |for_item| is true if the menu is
// for an item on the shelf, or false if the menu is for the shelf view
// itself, |context_menu| is whether the menu will be an application menu or
// context menu, and |touch_menu| is whether the menu was initiated by touch.
views::MenuAnchorPosition GetMenuAnchorPosition(bool for_item,
bool context_menu) const;
// Overridden from views::View:
gfx::Size CalculatePreferredSize() const override;
void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
void OnPaint(gfx::Canvas* canvas) override;
FocusTraversable* GetPaneFocusTraversable() override;
void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
void ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) override;
// Overridden from ui::EventHandler:
void OnGestureEvent(ui::GestureEvent* event) override;
bool OnMouseWheel(const ui::MouseWheelEvent& event) override;
// Overridden from ShelfModelObserver:
void ShelfItemAdded(int model_index) override;
void ShelfItemRemoved(int model_index, const ShelfItem& old_item) override;
void ShelfItemChanged(int model_index, const ShelfItem& old_item) override;
void ShelfItemMoved(int start_index, int target_index) override;
void ShelfItemDelegateChanged(const ShelfID& id,
ShelfItemDelegate* old_delegate,
ShelfItemDelegate* delegate) override;
// Handles the result when querying ShelfItemDelegates for context menu items.
// Shows a default shelf context menu with optional extra custom |menu_items|.
void AfterGetContextMenuItems(const ShelfID& shelf_id,
const gfx::Point& point,
views::View* source,
ui::MenuSourceType source_type,
std::vector<mojom::MenuItemPtr> menu_items);
// Handles the result of an item selection, records the |action| taken and
// optionally shows an application menu with the given |menu_items|.
void AfterItemSelected(
const ShelfItem& item,
views::Button* sender,
std::unique_ptr<ui::Event> event,
views::InkDrop* ink_drop,
ShelfAction action,
base::Optional<std::vector<mojom::MenuItemPtr>> menu_items);
// Overridden from views::ContextMenuController:
void ShowContextMenuForView(views::View* source,
const gfx::Point& point,
ui::MenuSourceType source_type) override;
// Show either a context or normal click menu of given |menu_model|.
// If |context_menu| is set, the displayed menu is a context menu and not
// a menu listing one or more running applications.
// The |click_point| is only used for |context_menu|'s.
void ShowMenu(std::unique_ptr<ui::MenuModel> menu_model,
views::View* source,
const gfx::Point& click_point,
bool context_menu,
ui::MenuSourceType source_type);
// Callback for MenuRunner.
void OnMenuClosed(views::View* source);
// Overridden from views::BoundsAnimatorObserver:
void OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) override;
void OnBoundsAnimatorDone(views::BoundsAnimator* animator) override;
// Returns true if the (press down) |event| is a repost event from an event
// which just closed the menu of a shelf item. If it occurs on the same shelf
// item, we should ignore the call.
bool IsRepostEvent(const ui::Event& event);
// Returns true if the given |item| is supposed to be shown to the user.
bool ShouldShowShelfItem(const ShelfItem& item);
// Convenience accessor to model_->items().
const ShelfItem* ShelfItemForView(const views::View* view) const;
// Get the distance from the given |coordinate| to the closest point on this
// launcher/shelf.
int CalculateShelfDistance(const gfx::Point& coordinate) const;
bool CanPrepareForDrag(Pointer pointer, const ui::LocatedEvent& event);
// Updates the back button opacity and focus behavior based on tablet mode.
void UpdateBackButton();
// The model; owned by Launcher.
ShelfModel* model_;
// The shelf controller; owned by RootWindowController.
Shelf* shelf_;
// The shelf widget for this view. For overflow bubbles, this is the widget
// for the shelf, not for the bubble.
ShelfWidget* shelf_widget_;
// Used to manage the set of active launcher buttons. There is a view per
// item in |model_|.
std::unique_ptr<views::ViewModel> view_model_;
// Index of first visible launcher item.
int first_visible_index_ = 0;
// Last index of a launcher button that is visible
// (does not go into overflow).
mutable int last_visible_index_ = -1;
std::unique_ptr<views::BoundsAnimator> bounds_animator_;
OverflowButton* overflow_button_ = nullptr;
std::unique_ptr<OverflowBubble> overflow_bubble_;
OverflowBubble* owner_overflow_bubble_ = nullptr;
ShelfTooltipManager tooltip_;
// Pointer device that initiated the current drag operation. If there is no
// current dragging operation, this is NONE.
Pointer drag_pointer_ = NONE;
// The view being dragged. This is set immediately when the mouse is pressed.
// |dragging_| is set only if the mouse is dragged far enough.
ShelfButton* drag_view_ = nullptr;
// The view showing a context menu. This can be either a ShelfView or
// ShelfButton.
views::View* menu_owner_ = nullptr;
// Position of the mouse down event in |drag_view_|'s coordinates.
gfx::Point drag_origin_;
// Index |drag_view_| was initially at.
int start_drag_index_ = -1;
// Used for the context menu of a particular item.
ShelfID context_menu_id_;
std::unique_ptr<views::FocusSearch> focus_search_;
// Manages the context menu, and the list menu.
std::unique_ptr<ui::MenuModel> menu_model_;
std::unique_ptr<views::MenuRunner> launcher_menu_runner_;
std::unique_ptr<ScopedRootWindowForNewWindows>
scoped_root_window_for_new_windows_;
// True when an item being inserted or removed in the model cancels a drag.
bool cancelling_drag_model_changed_ = false;
// Index of the last hidden launcher item. If there are no hidden items this
// will be equal to last_visible_index_ + 1.
mutable int last_hidden_index_ = 0;
// The timestamp of the event which closed the last menu - or 0.
base::TimeTicks closing_event_time_;
// The timestamp of the event which opened the last context menu on a
// ShelfButton. Used in metrics.
base::TimeTicks shelf_button_context_menu_time_;
// True if a drag and drop operation created/pinned the item in the launcher
// and it needs to be deleted/unpinned again if the operation gets cancelled.
bool drag_and_drop_item_pinned_ = false;
// The ShelfItem currently used for drag and drop; empty if none.
ShelfID drag_and_drop_shelf_id_;
// The original launcher item's size before the dragging operation.
gfx::Size pre_drag_and_drop_size_;
// The image proxy for drag operations when a drag and drop host exists and
// the item can be dragged outside the app grid.
std::unique_ptr<ash::DragImageView> drag_image_;
// The cursor offset to the middle of the dragged item.
gfx::Vector2d drag_image_offset_;
// The view which gets replaced by our drag icon proxy.
views::View* drag_replaced_view_ = nullptr;
// True when the icon was dragged off the shelf.
bool dragged_off_shelf_ = false;
// True when an item is dragged from one shelf to another (eg. overflow).
bool dragged_to_another_shelf_ = false;
// The rip off view when a snap back operation is underway.
views::View* snap_back_from_rip_off_view_ = nullptr;
// True when this ShelfView is used for Overflow Bubble.
bool overflow_mode_ = false;
// Holds a pointer to main ShelfView when a ShelfView is in overflow mode.
ShelfView* main_shelf_ = nullptr;
// True when ripped item from overflow bubble is entered into Shelf.
bool dragged_off_from_overflow_to_shelf_ = false;
// True if the event is a repost event from a event which has just closed the
// menu of the same shelf item.
bool is_repost_event_on_same_item_ = false;
// Record the index for the last pressed shelf item. This variable is used to
// check if a repost event occurs on the same shelf item as previous one. If
// so, the repost event should be ignored.
int last_pressed_index_ = -1;
// Tracks UMA metrics based on shelf button press actions.
ShelfButtonPressedMetricTracker shelf_button_pressed_metric_tracker_;
// Color used to paint the background behind the app list button and back
// button.
SkColor shelf_item_background_color_;
base::WeakPtrFactory<ShelfView> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ShelfView);
};
} // namespace ash
#endif // ASH_SHELF_SHELF_VIEW_H_