shelf_controller.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #include "ash/shelf/shelf_controller.h"
  5. #include <memory>
  6. #include "ash/constants/ash_pref_names.h"
  7. #include "ash/public/cpp/message_center/arc_notification_constants.h"
  8. #include "ash/public/cpp/shelf_item_delegate.h"
  9. #include "ash/public/cpp/shelf_prefs.h"
  10. #include "ash/root_window_controller.h"
  11. #include "ash/session/session_controller_impl.h"
  12. #include "ash/shelf/launcher_nudge_controller.h"
  13. #include "ash/shelf/shelf.h"
  14. #include "ash/shell.h"
  15. #include "ash/strings/grit/ash_strings.h"
  16. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  17. #include "base/bind.h"
  18. #include "chromeos/strings/grit/chromeos_strings.h"
  19. #include "components/pref_registry/pref_registry_syncable.h"
  20. #include "components/prefs/pref_change_registrar.h"
  21. #include "components/prefs/pref_registry_simple.h"
  22. #include "components/prefs/pref_service.h"
  23. #include "components/services/app_service/public/cpp/app_registry_cache_wrapper.h"
  24. #include "ui/base/l10n/l10n_util.h"
  25. #include "ui/display/display.h"
  26. #include "ui/display/screen.h"
  27. namespace ash {
  28. namespace {
  29. // Returns the Shelf instance for the display with the given |display_id|.
  30. Shelf* GetShelfForDisplay(int64_t display_id) {
  31. // The controller may be null for invalid ids or for displays being removed.
  32. RootWindowController* root_window_controller =
  33. Shell::GetRootWindowControllerWithDisplayId(display_id);
  34. return root_window_controller ? root_window_controller->shelf() : nullptr;
  35. }
  36. // Set each Shelf's auto-hide behavior from the per-display pref.
  37. void SetShelfAutoHideFromPrefs() {
  38. // TODO(jamescook): The session state check should not be necessary, but
  39. // otherwise this wrongly tries to set the alignment on a secondary display
  40. // during login before the ShelfLockingManager is created.
  41. SessionControllerImpl* session_controller =
  42. Shell::Get()->session_controller();
  43. PrefService* prefs = session_controller->GetLastActiveUserPrefService();
  44. if (!prefs || !session_controller->IsActiveUserSessionStarted())
  45. return;
  46. for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) {
  47. auto value = GetShelfAutoHideBehaviorPref(prefs, display.id());
  48. // Don't show the shelf in app mode.
  49. if (session_controller->IsRunningInAppMode())
  50. value = ShelfAutoHideBehavior::kAlwaysHidden;
  51. if (Shelf* shelf = GetShelfForDisplay(display.id()))
  52. shelf->SetAutoHideBehavior(value);
  53. }
  54. }
  55. // Set each Shelf's alignment from the per-display pref.
  56. void SetShelfAlignmentFromPrefs() {
  57. // TODO(jamescook): The session state check should not be necessary, but
  58. // otherwise this wrongly tries to set the alignment on a secondary display
  59. // during login before the ShelfLockingManager is created.
  60. SessionControllerImpl* session_controller =
  61. Shell::Get()->session_controller();
  62. PrefService* prefs = session_controller->GetLastActiveUserPrefService();
  63. if (!prefs || !session_controller->IsActiveUserSessionStarted())
  64. return;
  65. for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) {
  66. if (Shelf* shelf = GetShelfForDisplay(display.id()))
  67. shelf->SetAlignment(GetShelfAlignmentPref(prefs, display.id()));
  68. }
  69. }
  70. void UpdateShelfVisibility() {
  71. for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) {
  72. if (Shelf* shelf = GetShelfForDisplay(display.id()))
  73. shelf->UpdateVisibilityState();
  74. }
  75. }
  76. // Set each Shelf's auto-hide behavior and alignment from the per-display prefs.
  77. void SetShelfBehaviorsFromPrefs() {
  78. SetShelfAutoHideFromPrefs();
  79. // The shelf should always be bottom-aligned in tablet mode; alignment is
  80. // assigned from prefs when tablet mode is exited.
  81. if (Shell::Get()->tablet_mode_controller()->InTabletMode()) {
  82. return;
  83. }
  84. SetShelfAlignmentFromPrefs();
  85. }
  86. } // namespace
  87. ShelfController::ShelfController() {
  88. ShelfModel::SetInstance(&model_);
  89. Shell::Get()->session_controller()->AddObserver(this);
  90. Shell::Get()->tablet_mode_controller()->AddObserver(this);
  91. Shell::Get()->window_tree_host_manager()->AddObserver(this);
  92. model_.AddObserver(this);
  93. }
  94. ShelfController::~ShelfController() {
  95. model_.DestroyItemDelegates();
  96. }
  97. void ShelfController::Init() {
  98. launcher_nudge_controller_ = std::make_unique<LauncherNudgeController>();
  99. }
  100. void ShelfController::Shutdown() {
  101. model_.RemoveObserver(this);
  102. Shell::Get()->window_tree_host_manager()->RemoveObserver(this);
  103. Shell::Get()->tablet_mode_controller()->RemoveObserver(this);
  104. Shell::Get()->session_controller()->RemoveObserver(this);
  105. }
  106. // static
  107. void ShelfController::RegisterProfilePrefs(PrefRegistrySimple* registry) {
  108. // These prefs are public for ChromeShelfController's OnIsSyncingChanged.
  109. // See the pref names definitions for explanations of the synced, local, and
  110. // per-display behaviors.
  111. registry->RegisterStringPref(
  112. prefs::kShelfAutoHideBehavior, kShelfAutoHideBehaviorNever,
  113. user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PREF);
  114. registry->RegisterStringPref(prefs::kShelfAutoHideBehaviorLocal,
  115. std::string());
  116. registry->RegisterStringPref(
  117. prefs::kShelfAlignment, kShelfAlignmentBottom,
  118. user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PREF);
  119. registry->RegisterStringPref(prefs::kShelfAlignmentLocal, std::string());
  120. registry->RegisterDictionaryPref(prefs::kShelfPreferences);
  121. LauncherNudgeController::RegisterProfilePrefs(registry);
  122. }
  123. void ShelfController::OnActiveUserSessionChanged(const AccountId& account_id) {
  124. if (model_.in_shelf_party())
  125. model_.ToggleShelfParty();
  126. }
  127. void ShelfController::OnSessionStateChanged(
  128. session_manager::SessionState state) {
  129. if (model_.in_shelf_party())
  130. model_.ToggleShelfParty();
  131. }
  132. void ShelfController::OnActiveUserPrefServiceChanged(
  133. PrefService* pref_service) {
  134. SetShelfBehaviorsFromPrefs();
  135. pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
  136. pref_change_registrar_->Init(pref_service);
  137. pref_change_registrar_->Add(prefs::kShelfAlignmentLocal,
  138. base::BindRepeating(&SetShelfAlignmentFromPrefs));
  139. pref_change_registrar_->Add(prefs::kShelfAutoHideBehaviorLocal,
  140. base::BindRepeating(&SetShelfAutoHideFromPrefs));
  141. pref_change_registrar_->Add(prefs::kShelfPreferences,
  142. base::BindRepeating(&SetShelfBehaviorsFromPrefs));
  143. pref_change_registrar_->Add(
  144. prefs::kAppNotificationBadgingEnabled,
  145. base::BindRepeating(&ShelfController::UpdateAppNotificationBadging,
  146. base::Unretained(this)));
  147. // Observe AppRegistryCache for the current active account to get
  148. // notification updates.
  149. AccountId account_id =
  150. Shell::Get()->session_controller()->GetActiveAccountId();
  151. cache_ = apps::AppRegistryCacheWrapper::Get().GetAppRegistryCache(account_id);
  152. Observe(cache_);
  153. // Resetting the recorded pref forces the next call to
  154. // UpdateAppNotificationBadging() to update notification badging for every
  155. // app item.
  156. notification_badging_pref_enabled_.reset();
  157. // Update the notification badge indicator for all apps. This will also
  158. // ensure that apps have the correct notification badge value for the
  159. // multiprofile case when switching between users.
  160. UpdateAppNotificationBadging();
  161. }
  162. void ShelfController::OnTabletModeStarted() {
  163. // Do nothing when running in app mode.
  164. if (Shell::Get()->session_controller()->IsRunningInAppMode())
  165. return;
  166. // Force the shelf to be visible and to be bottom aligned in tablet mode; the
  167. // prefs are restored on exit.
  168. for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) {
  169. if (Shelf* shelf = GetShelfForDisplay(display.id())) {
  170. // Only animate into tablet mode if the shelf alignment will not change.
  171. if (shelf->IsHorizontalAlignment())
  172. shelf->set_is_tablet_mode_animation_running(true);
  173. shelf->SetAlignment(ShelfAlignment::kBottom);
  174. }
  175. }
  176. }
  177. void ShelfController::OnTabletModeEnded() {
  178. // Do nothing when running in app mode.
  179. if (Shell::Get()->session_controller()->IsRunningInAppMode())
  180. return;
  181. SetShelfBehaviorsFromPrefs();
  182. // Only animate out of tablet mode if the shelf alignment will not change.
  183. for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) {
  184. if (Shelf* shelf = GetShelfForDisplay(display.id())) {
  185. if (shelf->IsHorizontalAlignment())
  186. shelf->set_is_tablet_mode_animation_running(true);
  187. }
  188. }
  189. }
  190. void ShelfController::OnDisplayConfigurationChanged() {
  191. // Update the alignment and auto-hide state from prefs, because a display may
  192. // have been added, or the display ids for existing shelf instances may have
  193. // changed. See https://crbug.com/748291
  194. SetShelfBehaviorsFromPrefs();
  195. // Update shelf visibility to adapt to display changes. For instance shelf
  196. // should be hidden on secondary display during inactive session states.
  197. UpdateShelfVisibility();
  198. }
  199. void ShelfController::OnAppUpdate(const apps::AppUpdate& update) {
  200. if (update.HasBadgeChanged() &&
  201. notification_badging_pref_enabled_.value_or(false)) {
  202. bool has_badge = update.HasBadge().value_or(false);
  203. model_.UpdateItemNotification(update.AppId(), has_badge);
  204. }
  205. }
  206. void ShelfController::OnAppRegistryCacheWillBeDestroyed(
  207. apps::AppRegistryCache* cache) {
  208. Observe(nullptr);
  209. }
  210. void ShelfController::ShelfItemAdded(int index) {
  211. if (!cache_ || !notification_badging_pref_enabled_.value_or(false))
  212. return;
  213. auto app_id = model_.items()[index].id.app_id;
  214. // Update the notification badge indicator for the newly added shelf item.
  215. cache_->ForOneApp(app_id, [this](const apps::AppUpdate& update) {
  216. bool has_badge = update.HasBadge().value_or(false);
  217. model_.UpdateItemNotification(update.AppId(), has_badge);
  218. });
  219. }
  220. void ShelfController::UpdateAppNotificationBadging() {
  221. bool new_badging_enabled = pref_change_registrar_
  222. ? pref_change_registrar_->prefs()->GetBoolean(
  223. prefs::kAppNotificationBadgingEnabled)
  224. : false;
  225. if (notification_badging_pref_enabled_.has_value() &&
  226. notification_badging_pref_enabled_.value() == new_badging_enabled) {
  227. return;
  228. }
  229. notification_badging_pref_enabled_ = new_badging_enabled;
  230. if (cache_) {
  231. cache_->ForEachApp([this](const apps::AppUpdate& update) {
  232. // Set the app notification badge hidden when the pref is disabled.
  233. bool has_badge = notification_badging_pref_enabled_.value()
  234. ? update.HasBadge().value_or(false)
  235. : false;
  236. model_.UpdateItemNotification(update.AppId(), has_badge);
  237. });
  238. }
  239. }
  240. } // namespace ash