James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 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/shelf/shelf_controller.h" |
| 6 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 7 | #include "ash/public/cpp/config.h" |
| 8 | #include "ash/public/cpp/remote_shelf_item_delegate.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 9 | #include "ash/root_window_controller.h" |
jamescook | 788b4fc | 2017-05-18 16:16:06 | [diff] [blame] | 10 | #include "ash/session/session_controller.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 11 | #include "ash/shelf/app_list_shelf_item_delegate.h" |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 12 | #include "ash/shelf/shelf.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 13 | #include "ash/shell.h" |
msw | 109806d | 2017-06-02 20:11:57 | [diff] [blame] | 14 | #include "ash/strings/grit/ash_strings.h" |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 15 | #include "base/auto_reset.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
msw | 109806d | 2017-06-02 20:11:57 | [diff] [blame] | 17 | #include "ui/base/l10n/l10n_util.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 18 | #include "ui/base/models/simple_menu_model.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 19 | #include "ui/display/display.h" |
| 20 | #include "ui/display/screen.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 21 | |
| 22 | namespace ash { |
| 23 | |
| 24 | namespace { |
| 25 | |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 26 | // Returns the Shelf instance for the display with the given |display_id|. |
| 27 | Shelf* GetShelfForDisplay(int64_t display_id) { |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 28 | // The controller may be null for invalid ids or for displays being removed. |
| 29 | RootWindowController* root_window_controller = |
| 30 | Shell::GetRootWindowControllerWithDisplayId(display_id); |
jamescook | aa9f49f | 2017-05-30 20:48:45 | [diff] [blame] | 31 | return root_window_controller ? root_window_controller->shelf() : nullptr; |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | } // namespace |
| 35 | |
| 36 | ShelfController::ShelfController() { |
msw | 109806d | 2017-06-02 20:11:57 | [diff] [blame] | 37 | // Set the delegate and title string for the app list item. |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 38 | model_.SetShelfItemDelegate(ShelfID(kAppListId), |
| 39 | base::MakeUnique<AppListShelfItemDelegate>()); |
msw | 109806d | 2017-06-02 20:11:57 | [diff] [blame] | 40 | DCHECK_EQ(0, model_.ItemIndexByID(ShelfID(kAppListId))); |
| 41 | ShelfItem item = model_.items()[0]; |
| 42 | item.title = l10n_util::GetStringUTF16(IDS_ASH_SHELF_APP_LIST_LAUNCHER_TITLE); |
| 43 | model_.Set(0, item); |
| 44 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 45 | model_.AddObserver(this); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 46 | } |
| 47 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 48 | ShelfController::~ShelfController() { |
| 49 | model_.RemoveObserver(this); |
| 50 | } |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 51 | |
| 52 | void ShelfController::BindRequest(mojom::ShelfControllerRequest request) { |
| 53 | bindings_.AddBinding(this, std::move(request)); |
| 54 | } |
| 55 | |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 56 | void ShelfController::NotifyShelfInitialized(Shelf* shelf) { |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 57 | // Notify observers, Chrome will set alignment and auto-hide from prefs. |
varkha | 6b016b0f | 2017-05-26 16:14:18 | [diff] [blame] | 58 | display::Display display = |
| 59 | display::Screen::GetScreen()->GetDisplayNearestWindow(shelf->GetWindow()); |
| 60 | int64_t display_id = display.id(); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 61 | observers_.ForAllPtrs([display_id](mojom::ShelfObserver* observer) { |
jamescook | 788b4fc | 2017-05-18 16:16:06 | [diff] [blame] | 62 | observer->OnShelfInitialized(display_id); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 63 | }); |
| 64 | } |
| 65 | |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 66 | void ShelfController::NotifyShelfAlignmentChanged(Shelf* shelf) { |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 67 | ShelfAlignment alignment = shelf->alignment(); |
varkha | 6b016b0f | 2017-05-26 16:14:18 | [diff] [blame] | 68 | display::Display display = |
| 69 | display::Screen::GetScreen()->GetDisplayNearestWindow(shelf->GetWindow()); |
| 70 | int64_t display_id = display.id(); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 71 | observers_.ForAllPtrs( |
| 72 | [alignment, display_id](mojom::ShelfObserver* observer) { |
| 73 | observer->OnAlignmentChanged(alignment, display_id); |
| 74 | }); |
| 75 | } |
| 76 | |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 77 | void ShelfController::NotifyShelfAutoHideBehaviorChanged(Shelf* shelf) { |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 78 | ShelfAutoHideBehavior behavior = shelf->auto_hide_behavior(); |
varkha | 6b016b0f | 2017-05-26 16:14:18 | [diff] [blame] | 79 | display::Display display = |
| 80 | display::Screen::GetScreen()->GetDisplayNearestWindow(shelf->GetWindow()); |
| 81 | int64_t display_id = display.id(); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 82 | observers_.ForAllPtrs([behavior, display_id](mojom::ShelfObserver* observer) { |
| 83 | observer->OnAutoHideBehaviorChanged(behavior, display_id); |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | void ShelfController::AddObserver( |
| 88 | mojom::ShelfObserverAssociatedPtrInfo observer) { |
| 89 | mojom::ShelfObserverAssociatedPtr observer_ptr; |
| 90 | observer_ptr.Bind(std::move(observer)); |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 91 | |
| 92 | if (Shell::GetAshConfig() == Config::MASH) { |
| 93 | // Mash synchronizes two ShelfModel instances, owned by Ash and Chrome. |
| 94 | // Notify Chrome of existing ShelfModel items created by Ash. |
| 95 | for (int i = 0; i < model_.item_count(); ++i) { |
| 96 | const ShelfItem& item = model_.items()[i]; |
| 97 | observer_ptr->OnShelfItemAdded(i, item); |
| 98 | ShelfItemDelegate* delegate = model_.GetShelfItemDelegate(item.id); |
Michael Wasserman | 78b6f3e | 2017-07-20 19:51:20 | [diff] [blame] | 99 | if (delegate) { |
| 100 | observer_ptr->OnShelfItemDelegateChanged( |
| 101 | item.id, delegate->CreateInterfacePtrAndBind()); |
| 102 | } |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 106 | observers_.AddPtr(std::move(observer_ptr)); |
| 107 | } |
| 108 | |
| 109 | void ShelfController::SetAlignment(ShelfAlignment alignment, |
| 110 | int64_t display_id) { |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 111 | Shelf* shelf = GetShelfForDisplay(display_id); |
jamescook | 788b4fc | 2017-05-18 16:16:06 | [diff] [blame] | 112 | // TODO(jamescook): The session state check should not be necessary, but |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 113 | // otherwise this wrongly tries to set the alignment on a secondary display |
jamescook | 788b4fc | 2017-05-18 16:16:06 | [diff] [blame] | 114 | // during login before the ShelfLockingManager is created. |
| 115 | if (shelf && Shell::Get()->session_controller()->IsActiveUserSessionStarted()) |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 116 | shelf->SetAlignment(alignment); |
| 117 | } |
| 118 | |
| 119 | void ShelfController::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide, |
| 120 | int64_t display_id) { |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 121 | Shelf* shelf = GetShelfForDisplay(display_id); |
jamescook | 788b4fc | 2017-05-18 16:16:06 | [diff] [blame] | 122 | // TODO(jamescook): The session state check should not be necessary, but |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 123 | // otherwise this wrongly tries to set auto-hide state on a secondary display |
jamescook | 788b4fc | 2017-05-18 16:16:06 | [diff] [blame] | 124 | // during login. |
| 125 | if (shelf && Shell::Get()->session_controller()->IsActiveUserSessionStarted()) |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 126 | shelf->SetAutoHideBehavior(auto_hide); |
| 127 | } |
| 128 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 129 | void ShelfController::AddShelfItem(int32_t index, const ShelfItem& item) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 130 | DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << " Unexpected model sync"; |
| 131 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 132 | index = index < 0 ? model_.item_count() : index; |
Julien Brianceau | 293917a8 | 2017-08-01 17:32:59 | [diff] [blame^] | 133 | DCHECK_GT(index, 0) << " Items can not precede the AppList"; |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 134 | DCHECK_LE(index, model_.item_count()) << " Index out of bounds"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 135 | index = std::min(std::max(index, 1), model_.item_count()); |
| 136 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 137 | model_.AddAt(index, item); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 138 | } |
| 139 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 140 | void ShelfController::RemoveShelfItem(const ShelfID& id) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 141 | DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << " Unexpected model sync"; |
| 142 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 143 | const int index = model_.ItemIndexByID(id); |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 144 | DCHECK_GE(index, 0) << " No item found with the id: " << id; |
| 145 | DCHECK_NE(index, 0) << " The AppList shelf item cannot be removed"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 146 | if (index <= 0) |
| 147 | return; |
| 148 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 149 | model_.RemoveItemAt(index); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 150 | } |
| 151 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 152 | void ShelfController::MoveShelfItem(const ShelfID& id, int32_t index) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 153 | DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << " Unexpected model sync"; |
| 154 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 155 | const int current_index = model_.ItemIndexByID(id); |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 156 | DCHECK_GE(current_index, 0) << " No item found with the id: " << id; |
| 157 | DCHECK_NE(current_index, 0) << " The AppList shelf item cannot be moved"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 158 | if (current_index <= 0) |
| 159 | return; |
Julien Brianceau | 293917a8 | 2017-08-01 17:32:59 | [diff] [blame^] | 160 | DCHECK_GT(index, 0) << " Items can not precede the AppList"; |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 161 | DCHECK_LT(index, model_.item_count()) << " Index out of bounds"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 162 | index = std::min(std::max(index, 1), model_.item_count() - 1); |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 163 | DCHECK_NE(current_index, index) << " The item is already at the given index"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 164 | if (current_index == index) |
| 165 | return; |
| 166 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 167 | model_.Move(current_index, index); |
| 168 | } |
| 169 | |
| 170 | void ShelfController::UpdateShelfItem(const ShelfItem& item) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 171 | DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << " Unexpected model sync"; |
| 172 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 173 | const int index = model_.ItemIndexByID(item.id); |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 174 | DCHECK_GE(index, 0) << " No item found with the id: " << item.id; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 175 | if (index < 0) |
| 176 | return; |
| 177 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 178 | model_.Set(index, item); |
| 179 | } |
| 180 | |
| 181 | void ShelfController::SetShelfItemDelegate( |
| 182 | const ShelfID& id, |
| 183 | mojom::ShelfItemDelegatePtr delegate) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 184 | DCHECK_EQ(Shell::GetAshConfig(), Config::MASH) << " Unexpected model sync"; |
| 185 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 186 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 187 | if (delegate.is_bound()) |
| 188 | model_.SetShelfItemDelegate( |
| 189 | id, base::MakeUnique<RemoteShelfItemDelegate>(id, std::move(delegate))); |
| 190 | else |
| 191 | model_.SetShelfItemDelegate(id, nullptr); |
| 192 | } |
| 193 | |
| 194 | void ShelfController::ShelfItemAdded(int index) { |
| 195 | if (applying_remote_shelf_model_changes_ || |
| 196 | Shell::GetAshConfig() != Config::MASH) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | const ShelfItem& item = model_.items()[index]; |
| 201 | observers_.ForAllPtrs([index, item](mojom::ShelfObserver* observer) { |
| 202 | observer->OnShelfItemAdded(index, item); |
| 203 | }); |
| 204 | } |
| 205 | |
| 206 | void ShelfController::ShelfItemRemoved(int index, const ShelfItem& old_item) { |
| 207 | if (applying_remote_shelf_model_changes_ || |
| 208 | Shell::GetAshConfig() != Config::MASH) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | observers_.ForAllPtrs([old_item](mojom::ShelfObserver* observer) { |
| 213 | observer->OnShelfItemRemoved(old_item.id); |
| 214 | }); |
| 215 | } |
| 216 | |
| 217 | void ShelfController::ShelfItemMoved(int start_index, int target_index) { |
| 218 | if (applying_remote_shelf_model_changes_ || |
| 219 | Shell::GetAshConfig() != Config::MASH) { |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | const ShelfItem& item = model_.items()[target_index]; |
| 224 | observers_.ForAllPtrs([item, target_index](mojom::ShelfObserver* observer) { |
| 225 | observer->OnShelfItemMoved(item.id, target_index); |
| 226 | }); |
| 227 | } |
| 228 | |
| 229 | void ShelfController::ShelfItemChanged(int index, const ShelfItem& old_item) { |
| 230 | if (applying_remote_shelf_model_changes_ || |
| 231 | Shell::GetAshConfig() != Config::MASH) { |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | const ShelfItem& item = model_.items()[index]; |
| 236 | observers_.ForAllPtrs([item](mojom::ShelfObserver* observer) { |
| 237 | observer->OnShelfItemUpdated(item); |
| 238 | }); |
| 239 | } |
| 240 | |
| 241 | void ShelfController::ShelfItemDelegateChanged(const ShelfID& id, |
| 242 | ShelfItemDelegate* delegate) { |
| 243 | if (applying_remote_shelf_model_changes_ || |
| 244 | Shell::GetAshConfig() != Config::MASH) { |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | observers_.ForAllPtrs([id, delegate](mojom::ShelfObserver* observer) { |
| 249 | observer->OnShelfItemDelegateChanged( |
| 250 | id, delegate ? delegate->CreateInterfacePtrAndBind() |
| 251 | : mojom::ShelfItemDelegatePtr()); |
| 252 | }); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | } // namespace ash |