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