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