1 // Copyright 2013 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/shelf/shelf_window_watcher_item_delegate.h"
6 
7 #include <utility>
8 
9 #include "ash/public/cpp/shelf_model.h"
10 #include "ash/public/cpp/window_properties.h"
11 #include "ash/shelf/shelf_context_menu_model.h"
12 #include "ash/shelf/shelf_controller.h"
13 #include "ash/shell.h"
14 #include "ash/wm/window_state.h"
15 #include "ash/wm/window_util.h"
16 #include "components/strings/grit/components_strings.h"
17 #include "ui/aura/client/aura_constants.h"
18 #include "ui/aura/window.h"
19 #include "ui/base/models/image_model.h"
20 #include "ui/events/types/event_type.h"
21 #include "ui/views/vector_icons.h"
22 #include "ui/wm/core/window_animations.h"
23 
24 namespace ash {
25 
26 namespace {
27 
28 // Close command id; avoids colliding with ShelfContextMenuModel command ids.
29 const int kCloseCommandId = ShelfContextMenuModel::MENU_ASH_END + 1;
30 
31 }  // namespace
32 
ShelfWindowWatcherItemDelegate(const ShelfID & id,aura::Window * window)33 ShelfWindowWatcherItemDelegate::ShelfWindowWatcherItemDelegate(
34     const ShelfID& id,
35     aura::Window* window)
36     : ShelfItemDelegate(id), window_(window) {
37   DCHECK(!id.IsNull());
38   DCHECK(window_);
39 }
40 
41 ShelfWindowWatcherItemDelegate::~ShelfWindowWatcherItemDelegate() = default;
42 
ItemSelected(std::unique_ptr<ui::Event> event,int64_t display_id,ShelfLaunchSource source,ItemSelectedCallback callback,const ItemFilterPredicate & filter_predicate)43 void ShelfWindowWatcherItemDelegate::ItemSelected(
44     std::unique_ptr<ui::Event> event,
45     int64_t display_id,
46     ShelfLaunchSource source,
47     ItemSelectedCallback callback,
48     const ItemFilterPredicate& filter_predicate) {
49   if (wm::IsActiveWindow(window_)) {
50     if (event && event->type() == ui::ET_KEY_RELEASED) {
51       ::wm::AnimateWindow(window_, ::wm::WINDOW_ANIMATION_TYPE_BOUNCE);
52       std::move(callback).Run(SHELF_ACTION_NONE, {});
53       return;
54     }
55     window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
56     std::move(callback).Run(SHELF_ACTION_WINDOW_MINIMIZED, {});
57     return;
58   }
59   wm::ActivateWindow(window_);
60   std::move(callback).Run(SHELF_ACTION_WINDOW_ACTIVATED, {});
61 }
62 
GetContextMenu(int64_t display_id,GetContextMenuCallback callback)63 void ShelfWindowWatcherItemDelegate::GetContextMenu(
64     int64_t display_id,
65     GetContextMenuCallback callback) {
66   auto menu = std::make_unique<ShelfContextMenuModel>(this, display_id);
67   // Show a default context menu with just an extra close item.
68   menu->AddItemWithStringIdAndIcon(
69       kCloseCommandId, IDS_CLOSE,
70       ui::ImageModel::FromVectorIcon(views::kCloseIcon));
71   std::move(callback).Run(std::move(menu));
72 }
73 
ExecuteCommand(bool from_context_menu,int64_t command_id,int32_t event_flags,int64_t display_id)74 void ShelfWindowWatcherItemDelegate::ExecuteCommand(bool from_context_menu,
75                                                     int64_t command_id,
76                                                     int32_t event_flags,
77                                                     int64_t display_id) {
78   DCHECK_EQ(command_id, kCloseCommandId) << "Unknown ShelfItemDelegate command";
79   Close();
80 }
81 
Close()82 void ShelfWindowWatcherItemDelegate::Close() {
83   window_util::CloseWidgetForWindow(window_);
84 }
85 
86 }  // namespace ash
87