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 | |
Mitsuru Oshima | 04b54d0 | 2017-10-09 14:22:45 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 9 | #include "ash/public/cpp/ash_pref_names.h" |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 10 | #include "ash/public/cpp/config.h" |
| 11 | #include "ash/public/cpp/remote_shelf_item_delegate.h" |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 12 | #include "ash/public/cpp/shelf_prefs.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 13 | #include "ash/root_window_controller.h" |
jamescook | 788b4fc | 2017-05-18 16:16:06 | [diff] [blame] | 14 | #include "ash/session/session_controller.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 15 | #include "ash/shelf/app_list_shelf_item_delegate.h" |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 16 | #include "ash/shelf/shelf.h" |
Alex Newcomer | a3bcc9b | 2018-03-02 18:06:08 | [diff] [blame] | 17 | #include "ash/shelf/shelf_constants.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 18 | #include "ash/shell.h" |
msw | 109806d | 2017-06-02 20:11:57 | [diff] [blame] | 19 | #include "ash/strings/grit/ash_strings.h" |
Qiang Xu | b7127da6 | 2017-08-29 01:44:57 | [diff] [blame] | 20 | #include "ash/wm/tablet_mode/tablet_mode_controller.h" |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 21 | #include "base/auto_reset.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 22 | #include "base/strings/utf_string_conversions.h" |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 23 | #include "components/pref_registry/pref_registry_syncable.h" |
| 24 | #include "components/prefs/pref_change_registrar.h" |
| 25 | #include "components/prefs/pref_registry_simple.h" |
| 26 | #include "components/prefs/pref_service.h" |
msw | 109806d | 2017-06-02 20:11:57 | [diff] [blame] | 27 | #include "ui/base/l10n/l10n_util.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 28 | #include "ui/base/models/simple_menu_model.h" |
Alex Newcomer | 509bf67 | 2018-02-08 00:03:30 | [diff] [blame] | 29 | #include "ui/base/ui_base_features.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 30 | #include "ui/display/display.h" |
| 31 | #include "ui/display/screen.h" |
Alex Newcomer | d929ca16 | 2018-01-18 23:24:47 | [diff] [blame] | 32 | #include "ui/message_center/message_center.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 33 | |
| 34 | namespace ash { |
| 35 | |
| 36 | namespace { |
| 37 | |
James Cook | 840177e | 2017-05-25 02:20:01 | [diff] [blame] | 38 | // Returns the Shelf instance for the display with the given |display_id|. |
| 39 | Shelf* GetShelfForDisplay(int64_t display_id) { |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 40 | // The controller may be null for invalid ids or for displays being removed. |
| 41 | RootWindowController* root_window_controller = |
| 42 | Shell::GetRootWindowControllerWithDisplayId(display_id); |
jamescook | aa9f49f | 2017-05-30 20:48:45 | [diff] [blame] | 43 | return root_window_controller ? root_window_controller->shelf() : nullptr; |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 44 | } |
| 45 | |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 46 | // Set each Shelf's auto-hide behavior from the per-display pref. |
| 47 | void SetShelfAutoHideFromPrefs() { |
| 48 | // TODO(jamescook): The session state check should not be necessary, but |
| 49 | // otherwise this wrongly tries to set the alignment on a secondary display |
| 50 | // during login before the ShelfLockingManager is created. |
| 51 | SessionController* session_controller = Shell::Get()->session_controller(); |
Sam McNally | f4dab61 | 2017-08-16 03:06:33 | [diff] [blame] | 52 | PrefService* prefs = session_controller->GetLastActiveUserPrefService(); |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 53 | if (!prefs || !session_controller->IsActiveUserSessionStarted()) |
| 54 | return; |
| 55 | |
| 56 | for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { |
| 57 | auto value = GetShelfAutoHideBehaviorPref(prefs, display.id()); |
| 58 | // Don't show the shelf in app mode. |
| 59 | if (session_controller->IsRunningInAppMode()) |
| 60 | value = SHELF_AUTO_HIDE_ALWAYS_HIDDEN; |
| 61 | Shelf* shelf = GetShelfForDisplay(display.id()); |
| 62 | if (shelf) |
| 63 | shelf->SetAutoHideBehavior(value); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Set each Shelf's alignment from the per-display pref. |
| 68 | void SetShelfAlignmentFromPrefs() { |
| 69 | // TODO(jamescook): The session state check should not be necessary, but |
| 70 | // otherwise this wrongly tries to set the alignment on a secondary display |
| 71 | // during login before the ShelfLockingManager is created. |
| 72 | SessionController* session_controller = Shell::Get()->session_controller(); |
Sam McNally | f4dab61 | 2017-08-16 03:06:33 | [diff] [blame] | 73 | PrefService* prefs = session_controller->GetLastActiveUserPrefService(); |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 74 | if (!prefs || !session_controller->IsActiveUserSessionStarted()) |
| 75 | return; |
| 76 | |
| 77 | for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { |
| 78 | auto value = GetShelfAlignmentPref(prefs, display.id()); |
| 79 | Shelf* shelf = GetShelfForDisplay(display.id()); |
| 80 | if (shelf) |
| 81 | shelf->SetAlignment(value); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Set each Shelf's auto-hide behavior and alignment from the per-display prefs. |
| 86 | void SetShelfBehaviorsFromPrefs() { |
Sammie Quon | a9c65a4 | 2017-11-29 21:18:42 | [diff] [blame] | 87 | // The shelf should always be bottom-aligned and not hidden in tablet mode; |
| 88 | // alignment and auto-hide are assigned from prefs when tablet mode is exited. |
James Cook | d2495fe | 2018-03-19 19:53:10 | [diff] [blame^] | 89 | if (Shell::Get() |
| 90 | ->tablet_mode_controller() |
| 91 | ->IsTabletModeWindowManagerEnabled()) { |
Sammie Quon | a9c65a4 | 2017-11-29 21:18:42 | [diff] [blame] | 92 | return; |
| 93 | } |
| 94 | |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 95 | SetShelfAutoHideFromPrefs(); |
| 96 | SetShelfAlignmentFromPrefs(); |
| 97 | } |
| 98 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 99 | } // namespace |
| 100 | |
Alex Newcomer | d929ca16 | 2018-01-18 23:24:47 | [diff] [blame] | 101 | ShelfController::ShelfController() |
| 102 | : is_touchable_app_context_menu_enabled_( |
Alex Newcomer | 509bf67 | 2018-02-08 00:03:30 | [diff] [blame] | 103 | features::IsTouchableAppContextMenuEnabled()), |
Alex Newcomer | d929ca16 | 2018-01-18 23:24:47 | [diff] [blame] | 104 | message_center_observer_(this) { |
Sammie Quon | 9b911f2f | 2017-12-15 02:53:15 | [diff] [blame] | 105 | // Set the delegate and title string for the back button. |
| 106 | model_.SetShelfItemDelegate(ShelfID(kBackButtonId), nullptr); |
| 107 | DCHECK_EQ(0, model_.ItemIndexByID(ShelfID(kBackButtonId))); |
| 108 | ShelfItem back_item = model_.items()[0]; |
| 109 | back_item.title = l10n_util::GetStringUTF16(IDS_ASH_SHELF_BACK_BUTTON_TITLE); |
| 110 | model_.Set(0, back_item); |
| 111 | |
msw | 109806d | 2017-06-02 20:11:57 | [diff] [blame] | 112 | // Set the delegate and title string for the app list item. |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 113 | model_.SetShelfItemDelegate(ShelfID(kAppListId), |
Mitsuru Oshima | 04b54d0 | 2017-10-09 14:22:45 | [diff] [blame] | 114 | std::make_unique<AppListShelfItemDelegate>()); |
Sammie Quon | 9b911f2f | 2017-12-15 02:53:15 | [diff] [blame] | 115 | DCHECK_EQ(1, model_.ItemIndexByID(ShelfID(kAppListId))); |
| 116 | ShelfItem launcher_item = model_.items()[1]; |
| 117 | launcher_item.title = |
| 118 | l10n_util::GetStringUTF16(IDS_ASH_SHELF_APP_LIST_LAUNCHER_TITLE); |
| 119 | model_.Set(1, launcher_item); |
msw | 109806d | 2017-06-02 20:11:57 | [diff] [blame] | 120 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 121 | model_.AddObserver(this); |
Sam McNally | f4dab61 | 2017-08-16 03:06:33 | [diff] [blame] | 122 | Shell::Get()->session_controller()->AddObserver(this); |
Qiang Xu | b7127da6 | 2017-08-29 01:44:57 | [diff] [blame] | 123 | Shell::Get()->tablet_mode_controller()->AddObserver(this); |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 124 | Shell::Get()->window_tree_host_manager()->AddObserver(this); |
Alex Newcomer | d929ca16 | 2018-01-18 23:24:47 | [diff] [blame] | 125 | if (is_touchable_app_context_menu_enabled_) |
| 126 | message_center_observer_.Add(message_center::MessageCenter::Get()); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 127 | } |
| 128 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 129 | ShelfController::~ShelfController() { |
| 130 | model_.RemoveObserver(this); |
Mike Wasserman | 4cc9f40 | 2017-09-18 20:59:27 | [diff] [blame] | 131 | model_.DestroyItemDelegates(); |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 132 | } |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 133 | |
James Cook | d2495fe | 2018-03-19 19:53:10 | [diff] [blame^] | 134 | void ShelfController::Shutdown() { |
| 135 | Shell::Get()->window_tree_host_manager()->RemoveObserver(this); |
| 136 | Shell::Get()->tablet_mode_controller()->RemoveObserver(this); |
| 137 | Shell::Get()->session_controller()->RemoveObserver(this); |
| 138 | } |
| 139 | |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 140 | // static |
| 141 | void ShelfController::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
Mike Wasserman | 7e440443 | 2017-10-16 16:59:11 | [diff] [blame] | 142 | // These prefs are public for ChromeLauncherController's OnIsSyncingChanged |
| 143 | // and ShelfBoundsChangesProbablyWithUser. See the pref names definitions for |
| 144 | // explanations of the synced, local, and per-display behaviors. |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 145 | registry->RegisterStringPref( |
| 146 | prefs::kShelfAutoHideBehavior, kShelfAutoHideBehaviorNever, |
| 147 | user_prefs::PrefRegistrySyncable::SYNCABLE_PREF | PrefRegistry::PUBLIC); |
| 148 | registry->RegisterStringPref(prefs::kShelfAutoHideBehaviorLocal, |
| 149 | std::string(), PrefRegistry::PUBLIC); |
| 150 | registry->RegisterStringPref( |
| 151 | prefs::kShelfAlignment, kShelfAlignmentBottom, |
| 152 | user_prefs::PrefRegistrySyncable::SYNCABLE_PREF | PrefRegistry::PUBLIC); |
| 153 | registry->RegisterStringPref(prefs::kShelfAlignmentLocal, std::string(), |
| 154 | PrefRegistry::PUBLIC); |
| 155 | registry->RegisterDictionaryPref(prefs::kShelfPreferences, |
| 156 | PrefRegistry::PUBLIC); |
| 157 | } |
| 158 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 159 | void ShelfController::BindRequest(mojom::ShelfControllerRequest request) { |
| 160 | bindings_.AddBinding(this, std::move(request)); |
| 161 | } |
| 162 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 163 | void ShelfController::AddObserver( |
| 164 | mojom::ShelfObserverAssociatedPtrInfo observer) { |
| 165 | mojom::ShelfObserverAssociatedPtr observer_ptr; |
| 166 | observer_ptr.Bind(std::move(observer)); |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 167 | |
Mike Wasserman | 5909b56 | 2018-02-20 18:00:46 | [diff] [blame] | 168 | // Synchronize two ShelfModel instances, one each owned by Ash and Chrome. |
| 169 | // Notify Chrome of existing ShelfModel items and delegates created by Ash. |
| 170 | for (int i = 0; i < model_.item_count(); ++i) { |
| 171 | ShelfItem item = model_.items()[i]; |
| 172 | ShelfItemDelegate* delegate = model_.GetShelfItemDelegate(item.id); |
| 173 | // Notify observers of the delegate before the items themselves; Chrome |
| 174 | // creates default delegates if none exist, breaking ShelfWindowWatcher. |
| 175 | if (delegate) { |
| 176 | observer_ptr->OnShelfItemDelegateChanged( |
| 177 | item.id, delegate->CreateInterfacePtrAndBind()); |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 178 | } |
Mike Wasserman | 5909b56 | 2018-02-20 18:00:46 | [diff] [blame] | 179 | // Pass null images to avoid transport costs; clients don't use images. |
| 180 | item.image = gfx::ImageSkia(); |
| 181 | observer_ptr->OnShelfItemAdded(i, item); |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 182 | } |
| 183 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 184 | observers_.AddPtr(std::move(observer_ptr)); |
| 185 | } |
| 186 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 187 | void ShelfController::AddShelfItem(int32_t index, const ShelfItem& item) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 188 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 189 | index = index < 0 ? model_.item_count() : index; |
Julien Brianceau | 293917a8 | 2017-08-01 17:32:59 | [diff] [blame] | 190 | DCHECK_GT(index, 0) << " Items can not precede the AppList"; |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 191 | DCHECK_LE(index, model_.item_count()) << " Index out of bounds"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 192 | index = std::min(std::max(index, 1), model_.item_count()); |
| 193 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 194 | model_.AddAt(index, item); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 195 | } |
| 196 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 197 | void ShelfController::RemoveShelfItem(const ShelfID& id) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 198 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 199 | const int index = model_.ItemIndexByID(id); |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 200 | DCHECK_GE(index, 0) << " No item found with the id: " << id; |
| 201 | DCHECK_NE(index, 0) << " The AppList shelf item cannot be removed"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 202 | if (index <= 0) |
| 203 | return; |
| 204 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 205 | model_.RemoveItemAt(index); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 206 | } |
| 207 | |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 208 | void ShelfController::MoveShelfItem(const ShelfID& id, int32_t index) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 209 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 210 | const int current_index = model_.ItemIndexByID(id); |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 211 | DCHECK_GE(current_index, 0) << " No item found with the id: " << id; |
| 212 | DCHECK_NE(current_index, 0) << " The AppList shelf item cannot be moved"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 213 | if (current_index <= 0) |
| 214 | return; |
Julien Brianceau | 293917a8 | 2017-08-01 17:32:59 | [diff] [blame] | 215 | DCHECK_GT(index, 0) << " Items can not precede the AppList"; |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 216 | DCHECK_LT(index, model_.item_count()) << " Index out of bounds"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 217 | index = std::min(std::max(index, 1), model_.item_count() - 1); |
Mike Wasserman | f014509 | 2017-09-29 01:30:54 | [diff] [blame] | 218 | if (current_index == index) { |
| 219 | DVLOG(1) << "The item is already at the given index (" << index << "). " |
| 220 | << "This happens when syncing a ShelfModel weight reordering."; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 221 | return; |
Mike Wasserman | f014509 | 2017-09-29 01:30:54 | [diff] [blame] | 222 | } |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 223 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 224 | model_.Move(current_index, index); |
| 225 | } |
| 226 | |
| 227 | void ShelfController::UpdateShelfItem(const ShelfItem& item) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 228 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 229 | const int index = model_.ItemIndexByID(item.id); |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 230 | DCHECK_GE(index, 0) << " No item found with the id: " << item.id; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 231 | if (index < 0) |
| 232 | return; |
| 233 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
Mike Wasserman | b4ed7eab | 2017-10-10 07:08:25 | [diff] [blame] | 234 | |
| 235 | // Keep any existing image if the item was sent without one for efficiency. |
| 236 | ash::ShelfItem new_item = item; |
| 237 | if (item.image.isNull()) |
| 238 | new_item.image = model_.items()[index].image; |
| 239 | model_.Set(index, new_item); |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void ShelfController::SetShelfItemDelegate( |
| 243 | const ShelfID& id, |
| 244 | mojom::ShelfItemDelegatePtr delegate) { |
msw | 70ac45f | 2017-06-05 02:02:45 | [diff] [blame] | 245 | DCHECK(!applying_remote_shelf_model_changes_) << " Unexpected model change"; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 246 | base::AutoReset<bool> reset(&applying_remote_shelf_model_changes_, true); |
| 247 | if (delegate.is_bound()) |
| 248 | model_.SetShelfItemDelegate( |
Mitsuru Oshima | 04b54d0 | 2017-10-09 14:22:45 | [diff] [blame] | 249 | id, std::make_unique<RemoteShelfItemDelegate>(id, std::move(delegate))); |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 250 | else |
| 251 | model_.SetShelfItemDelegate(id, nullptr); |
| 252 | } |
| 253 | |
| 254 | void ShelfController::ShelfItemAdded(int index) { |
Mike Wasserman | 5909b56 | 2018-02-20 18:00:46 | [diff] [blame] | 255 | if (applying_remote_shelf_model_changes_) |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 256 | return; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 257 | |
Mike Wasserman | b4ed7eab | 2017-10-10 07:08:25 | [diff] [blame] | 258 | // Pass null images to avoid transport costs; clients don't use images. |
| 259 | ShelfItem item = model_.items()[index]; |
| 260 | item.image = gfx::ImageSkia(); |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 261 | observers_.ForAllPtrs([index, item](mojom::ShelfObserver* observer) { |
| 262 | observer->OnShelfItemAdded(index, item); |
| 263 | }); |
| 264 | } |
| 265 | |
| 266 | void ShelfController::ShelfItemRemoved(int index, const ShelfItem& old_item) { |
Mike Wasserman | 5909b56 | 2018-02-20 18:00:46 | [diff] [blame] | 267 | if (applying_remote_shelf_model_changes_) |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 268 | return; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 269 | |
| 270 | observers_.ForAllPtrs([old_item](mojom::ShelfObserver* observer) { |
| 271 | observer->OnShelfItemRemoved(old_item.id); |
| 272 | }); |
| 273 | } |
| 274 | |
| 275 | void ShelfController::ShelfItemMoved(int start_index, int target_index) { |
Mike Wasserman | 5909b56 | 2018-02-20 18:00:46 | [diff] [blame] | 276 | if (applying_remote_shelf_model_changes_) |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 277 | return; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 278 | |
| 279 | const ShelfItem& item = model_.items()[target_index]; |
| 280 | observers_.ForAllPtrs([item, target_index](mojom::ShelfObserver* observer) { |
| 281 | observer->OnShelfItemMoved(item.id, target_index); |
| 282 | }); |
| 283 | } |
| 284 | |
| 285 | void ShelfController::ShelfItemChanged(int index, const ShelfItem& old_item) { |
Mike Wasserman | 5909b56 | 2018-02-20 18:00:46 | [diff] [blame] | 286 | if (applying_remote_shelf_model_changes_) |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 287 | return; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 288 | |
Mike Wasserman | b4ed7eab | 2017-10-10 07:08:25 | [diff] [blame] | 289 | // Pass null images to avoid transport costs; clients don't use images. |
| 290 | ShelfItem item = model_.items()[index]; |
| 291 | item.image = gfx::ImageSkia(); |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 292 | observers_.ForAllPtrs([item](mojom::ShelfObserver* observer) { |
| 293 | observer->OnShelfItemUpdated(item); |
| 294 | }); |
| 295 | } |
| 296 | |
| 297 | void ShelfController::ShelfItemDelegateChanged(const ShelfID& id, |
khmel | 1240ee8 | 2017-10-05 23:48:18 | [diff] [blame] | 298 | ShelfItemDelegate* old_delegate, |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 299 | ShelfItemDelegate* delegate) { |
Mike Wasserman | 5909b56 | 2018-02-20 18:00:46 | [diff] [blame] | 300 | if (applying_remote_shelf_model_changes_) |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 301 | return; |
msw | 19b30c2c | 2017-06-01 03:21:40 | [diff] [blame] | 302 | |
| 303 | observers_.ForAllPtrs([id, delegate](mojom::ShelfObserver* observer) { |
| 304 | observer->OnShelfItemDelegateChanged( |
| 305 | id, delegate ? delegate->CreateInterfacePtrAndBind() |
| 306 | : mojom::ShelfItemDelegatePtr()); |
| 307 | }); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 308 | } |
| 309 | |
Mike Wasserman | 0f1bd68 | 2017-10-11 18:20:58 | [diff] [blame] | 310 | void ShelfController::FlushForTesting() { |
| 311 | bindings_.FlushForTesting(); |
| 312 | } |
| 313 | |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 314 | void ShelfController::OnActiveUserPrefServiceChanged( |
| 315 | PrefService* pref_service) { |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 316 | SetShelfBehaviorsFromPrefs(); |
Mitsuru Oshima | 04b54d0 | 2017-10-09 14:22:45 | [diff] [blame] | 317 | pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>(); |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 318 | pref_change_registrar_->Init(pref_service); |
| 319 | pref_change_registrar_->Add(prefs::kShelfAlignmentLocal, |
Qiang Xu | dadd3ad6 | 2018-03-07 17:22:45 | [diff] [blame] | 320 | base::BindRepeating(&SetShelfAlignmentFromPrefs)); |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 321 | pref_change_registrar_->Add(prefs::kShelfAutoHideBehaviorLocal, |
Qiang Xu | dadd3ad6 | 2018-03-07 17:22:45 | [diff] [blame] | 322 | base::BindRepeating(&SetShelfAutoHideFromPrefs)); |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 323 | pref_change_registrar_->Add(prefs::kShelfPreferences, |
Qiang Xu | dadd3ad6 | 2018-03-07 17:22:45 | [diff] [blame] | 324 | base::BindRepeating(&SetShelfBehaviorsFromPrefs)); |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 325 | } |
| 326 | |
Qiang Xu | b7127da6 | 2017-08-29 01:44:57 | [diff] [blame] | 327 | void ShelfController::OnTabletModeStarted() { |
Sammie Quon | 40cdcc1a | 2017-09-21 22:25:49 | [diff] [blame] | 328 | // Force the shelf to be visible and to be bottom aligned in tablet mode; the |
| 329 | // prefs are restored on exit. |
Qiang Xu | b7127da6 | 2017-08-29 01:44:57 | [diff] [blame] | 330 | for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { |
| 331 | Shelf* shelf = GetShelfForDisplay(display.id()); |
Sammie Quon | 40cdcc1a | 2017-09-21 22:25:49 | [diff] [blame] | 332 | if (shelf) { |
Sammie Quon | a87fc32 | 2017-11-10 17:43:37 | [diff] [blame] | 333 | // Only animate into tablet mode if the shelf alignment will not change. |
| 334 | if (shelf->IsHorizontalAlignment()) |
| 335 | shelf->set_is_tablet_mode_animation_running(true); |
Qiang Xu | b7127da6 | 2017-08-29 01:44:57 | [diff] [blame] | 336 | shelf->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_NEVER); |
Sammie Quon | 40cdcc1a | 2017-09-21 22:25:49 | [diff] [blame] | 337 | shelf->SetAlignment(SHELF_ALIGNMENT_BOTTOM); |
| 338 | } |
Qiang Xu | b7127da6 | 2017-08-29 01:44:57 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| 342 | void ShelfController::OnTabletModeEnded() { |
Sammie Quon | 40cdcc1a | 2017-09-21 22:25:49 | [diff] [blame] | 343 | SetShelfBehaviorsFromPrefs(); |
Sammie Quon | a87fc32 | 2017-11-10 17:43:37 | [diff] [blame] | 344 | // Only animate out of tablet mode if the shelf alignment will not change. |
| 345 | for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { |
| 346 | Shelf* shelf = GetShelfForDisplay(display.id()); |
| 347 | if (shelf && shelf->IsHorizontalAlignment()) |
| 348 | shelf->set_is_tablet_mode_animation_running(true); |
| 349 | } |
Qiang Xu | b7127da6 | 2017-08-29 01:44:57 | [diff] [blame] | 350 | } |
| 351 | |
Michael Wasserman | 39e4fe4 | 2017-08-08 23:49:42 | [diff] [blame] | 352 | void ShelfController::OnDisplayConfigurationChanged() { |
| 353 | // Set/init the shelf behaviors from preferences, in case a display was added. |
| 354 | SetShelfBehaviorsFromPrefs(); |
| 355 | } |
| 356 | |
| 357 | void ShelfController::OnWindowTreeHostReusedForDisplay( |
| 358 | AshWindowTreeHost* window_tree_host, |
| 359 | const display::Display& display) { |
| 360 | // See comment in OnWindowTreeHostsSwappedDisplays(). |
| 361 | SetShelfBehaviorsFromPrefs(); |
| 362 | } |
| 363 | |
| 364 | void ShelfController::OnWindowTreeHostsSwappedDisplays( |
| 365 | AshWindowTreeHost* host1, |
| 366 | AshWindowTreeHost* host2) { |
| 367 | // The display ids for existing shelf instances may have changed, so update |
| 368 | // the alignment and auto-hide state from prefs. See http://crbug.com/748291 |
| 369 | SetShelfBehaviorsFromPrefs(); |
| 370 | } |
| 371 | |
Alex Newcomer | d929ca16 | 2018-01-18 23:24:47 | [diff] [blame] | 372 | void ShelfController::OnNotificationAdded(const std::string& notification_id) { |
| 373 | if (!is_touchable_app_context_menu_enabled_) |
| 374 | return; |
| 375 | |
| 376 | message_center::Notification* notification = |
| 377 | message_center::MessageCenter::Get()->FindVisibleNotificationById( |
| 378 | notification_id); |
Alex Newcomer | 9d92fcb9 | 2018-01-24 18:37:53 | [diff] [blame] | 379 | |
Alex Newcomer | a3bcc9b | 2018-03-02 18:06:08 | [diff] [blame] | 380 | if (!notification) |
| 381 | return; |
| 382 | |
| 383 | // Skip this if the notification shouldn't badge an app. |
| 384 | if (notification->notifier_id().type != |
| 385 | message_center::NotifierId::APPLICATION && |
| 386 | notification->notifier_id().type != |
| 387 | message_center::NotifierId::ARC_APPLICATION) { |
Alex Newcomer | d929ca16 | 2018-01-18 23:24:47 | [diff] [blame] | 388 | return; |
Alex Newcomer | 509bf67 | 2018-02-08 00:03:30 | [diff] [blame] | 389 | } |
Alex Newcomer | d929ca16 | 2018-01-18 23:24:47 | [diff] [blame] | 390 | |
Alex Newcomer | a3bcc9b | 2018-03-02 18:06:08 | [diff] [blame] | 391 | // Skip this if the notification doesn't have a valid app id. |
| 392 | if (notification->notifier_id().id == ash::kDefaultArcNotifierId) |
| 393 | return; |
| 394 | |
Alex Newcomer | d929ca16 | 2018-01-18 23:24:47 | [diff] [blame] | 395 | model_.AddNotificationRecord(notification->notifier_id().id, notification_id); |
| 396 | } |
| 397 | |
| 398 | void ShelfController::OnNotificationRemoved(const std::string& notification_id, |
| 399 | bool by_user) { |
| 400 | if (!is_touchable_app_context_menu_enabled_) |
| 401 | return; |
| 402 | |
| 403 | model_.RemoveNotificationRecord(notification_id); |
| 404 | } |
| 405 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 406 | } // namespace ash |