Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/common/shelf/shelf_controller.h" | 5 #include "ash/common/shelf/shelf_controller.h" |
| 6 | 6 |
| 7 #include "ash/common/shelf/shelf_item_delegate.h" | 7 #include "ash/common/shelf/shelf_item_delegate.h" |
| 8 #include "ash/common/shelf/shelf_menu_model.h" | 8 #include "ash/common/shelf/shelf_menu_model.h" |
| 9 #include "ash/common/shelf/wm_shelf.h" | 9 #include "ash/common/shelf/wm_shelf.h" |
| 10 #include "ash/common/shell_delegate.h" | |
| 10 #include "ash/common/wm_lookup.h" | 11 #include "ash/common/wm_lookup.h" |
| 11 #include "ash/common/wm_root_window_controller.h" | 12 #include "ash/common/wm_root_window_controller.h" |
| 12 #include "ash/common/wm_shell.h" | 13 #include "ash/common/wm_shell.h" |
| 13 #include "ash/common/wm_window.h" | 14 #include "ash/common/wm_window.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "services/preferences/public/cpp/pref_observer_store.h" | |
| 17 #include "services/preferences/public/interfaces/preferences.mojom.h" | |
| 18 #include "services/service_manager/public/cpp/connector.h" | |
| 15 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "ui/display/display.h" | 20 #include "ui/display/display.h" |
| 17 #include "ui/display/screen.h" | 21 #include "ui/display/screen.h" |
| 18 #include "ui/gfx/image/image_skia.h" | 22 #include "ui/gfx/image/image_skia.h" |
| 19 #include "ui/resources/grit/ui_resources.h" | 23 #include "ui/resources/grit/ui_resources.h" |
| 20 | 24 |
| 21 namespace ash { | 25 namespace ash { |
| 22 | 26 |
| 23 namespace { | 27 namespace { |
| 24 | 28 |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 // The controller may be null for invalid ids or for displays being removed. | 161 // The controller may be null for invalid ids or for displays being removed. |
| 158 WmRootWindowController* root_window_controller = | 162 WmRootWindowController* root_window_controller = |
| 159 WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id); | 163 WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id); |
| 160 return root_window_controller ? root_window_controller->GetShelf() : nullptr; | 164 return root_window_controller ? root_window_controller->GetShelf() : nullptr; |
| 161 } | 165 } |
| 162 | 166 |
| 163 } // namespace | 167 } // namespace |
| 164 | 168 |
| 165 ShelfController::ShelfController() {} | 169 ShelfController::ShelfController() {} |
| 166 | 170 |
| 167 ShelfController::~ShelfController() {} | 171 ShelfController::~ShelfController() { |
| 172 if (store_) | |
| 173 store_->RemoveObserver(this); | |
| 174 } | |
| 168 | 175 |
| 169 void ShelfController::BindRequest(mojom::ShelfControllerRequest request) { | 176 void ShelfController::BindRequest(mojom::ShelfControllerRequest request) { |
| 170 bindings_.AddBinding(this, std::move(request)); | 177 bindings_.AddBinding(this, std::move(request)); |
| 178 | |
| 179 // Sample of connecting to the PreferencesManager | |
| 180 WmShell* shell = WmShell::Get(); | |
| 181 service_manager::Connector* connector = | |
| 182 shell->delegate()->GetShellConnector(); | |
| 183 if (!connector) | |
| 184 return; | |
| 185 prefs::mojom::PreferencesManagerPtr pref_manager_ptr; | |
| 186 connector->ConnectToInterface("service:content_browser", &pref_manager_ptr); | |
| 187 store_ = new PrefObserverStore(std::move(pref_manager_ptr)); | |
| 188 std::set<std::string> keys; | |
| 189 // chrome::common::pref_names::kShelfAutoHideBehavior | |
| 190 const std::string key("auto_hide_behavior"); | |
| 191 keys.insert(key); | |
|
James Cook
2016/11/15 20:27:17
Drive-by: I wonder if we should have a single pref
jonross
2016/11/15 22:49:44
We could make a single exit point from Ash. To red
| |
| 192 store_->AddObserver(this); | |
| 193 store_->Init(keys); | |
| 171 } | 194 } |
| 172 | 195 |
| 173 void ShelfController::NotifyShelfCreated(WmShelf* shelf) { | 196 void ShelfController::NotifyShelfCreated(WmShelf* shelf) { |
| 174 // Notify observers, Chrome will set alignment and auto-hide from prefs. | 197 // Notify observers, Chrome will set alignment and auto-hide from prefs. |
| 175 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); | 198 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); |
| 176 observers_.ForAllPtrs([display_id](mojom::ShelfObserver* observer) { | 199 observers_.ForAllPtrs([display_id](mojom::ShelfObserver* observer) { |
| 177 observer->OnShelfCreated(display_id); | 200 observer->OnShelfCreated(display_id); |
| 178 }); | 201 }); |
| 179 } | 202 } |
| 180 | 203 |
| 181 void ShelfController::NotifyShelfAlignmentChanged(WmShelf* shelf) { | 204 void ShelfController::NotifyShelfAlignmentChanged(WmShelf* shelf) { |
| 182 ShelfAlignment alignment = shelf->alignment(); | 205 ShelfAlignment alignment = shelf->alignment(); |
| 183 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); | 206 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); |
| 184 observers_.ForAllPtrs( | 207 observers_.ForAllPtrs( |
| 185 [alignment, display_id](mojom::ShelfObserver* observer) { | 208 [alignment, display_id](mojom::ShelfObserver* observer) { |
| 186 observer->OnAlignmentChanged(alignment, display_id); | 209 observer->OnAlignmentChanged(alignment, display_id); |
| 187 }); | 210 }); |
| 188 } | 211 } |
| 189 | 212 |
| 190 void ShelfController::NotifyShelfAutoHideBehaviorChanged(WmShelf* shelf) { | 213 void ShelfController::NotifyShelfAutoHideBehaviorChanged(WmShelf* shelf) { |
| 191 ShelfAutoHideBehavior behavior = shelf->auto_hide_behavior(); | 214 ShelfAutoHideBehavior behavior = shelf->auto_hide_behavior(); |
| 192 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); | 215 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); |
| 193 observers_.ForAllPtrs([behavior, display_id](mojom::ShelfObserver* observer) { | 216 observers_.ForAllPtrs([behavior, display_id](mojom::ShelfObserver* observer) { |
| 194 observer->OnAutoHideBehaviorChanged(behavior, display_id); | 217 observer->OnAutoHideBehaviorChanged(behavior, display_id); |
| 195 }); | 218 }); |
| 219 | |
| 220 // Using Preferences to set the value. the chrome ShelfObserver can seprately | |
| 221 // listen to preference changes to make any according UI state changes. | |
| 222 std::string behaviour_pref; | |
| 223 switch (behavior) { | |
| 224 case SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS: | |
| 225 behaviour_pref = "Always"; | |
| 226 break; | |
| 227 case SHELF_AUTO_HIDE_BEHAVIOR_NEVER: | |
| 228 behaviour_pref = "Never"; | |
| 229 break; | |
| 230 default: | |
| 231 behaviour_pref = "ooops"; | |
| 232 } | |
| 233 std::unique_ptr<base::Value> value(new base::StringValue(behaviour_pref)); | |
| 234 if (store_) | |
| 235 store_->SetValue("auto_hide_behavior", std::move(value), 0); | |
| 196 } | 236 } |
| 197 | 237 |
| 198 void ShelfController::AddObserver( | 238 void ShelfController::AddObserver( |
| 199 mojom::ShelfObserverAssociatedPtrInfo observer) { | 239 mojom::ShelfObserverAssociatedPtrInfo observer) { |
| 200 mojom::ShelfObserverAssociatedPtr observer_ptr; | 240 mojom::ShelfObserverAssociatedPtr observer_ptr; |
| 201 observer_ptr.Bind(std::move(observer)); | 241 observer_ptr.Bind(std::move(observer)); |
| 202 observers_.AddPtr(std::move(observer_ptr)); | 242 observers_.AddPtr(std::move(observer_ptr)); |
| 203 } | 243 } |
| 204 | 244 |
| 205 void ShelfController::SetAlignment(ShelfAlignment alignment, | 245 void ShelfController::SetAlignment(ShelfAlignment alignment, |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 if (!app_id_to_shelf_id_.count(app_id)) | 314 if (!app_id_to_shelf_id_.count(app_id)) |
| 275 return; | 315 return; |
| 276 ShelfID shelf_id = app_id_to_shelf_id_[app_id]; | 316 ShelfID shelf_id = app_id_to_shelf_id_[app_id]; |
| 277 int index = model_.ItemIndexByID(shelf_id); | 317 int index = model_.ItemIndexByID(shelf_id); |
| 278 DCHECK_GE(index, 0); | 318 DCHECK_GE(index, 0); |
| 279 ShelfItem item = *model_.ItemByID(shelf_id); | 319 ShelfItem item = *model_.ItemByID(shelf_id); |
| 280 item.image = GetShelfIconFromBitmap(image); | 320 item.image = GetShelfIconFromBitmap(image); |
| 281 model_.Set(index, item); | 321 model_.Set(index, item); |
| 282 } | 322 } |
| 283 | 323 |
| 324 void ShelfController::OnPrefValueChanged(const std::string& key) { | |
| 325 const base::Value* value = nullptr; | |
| 326 store_->GetValue("auto_hide_behavior", &value); | |
| 327 if (!value) | |
| 328 return; | |
| 329 std::string actual_value; | |
| 330 value->GetAsString(&actual_value); | |
| 331 LOG(ERROR) << "JR OnPrefValueChanged " << key << " : " << actual_value | |
| 332 << "\n"; | |
| 333 | |
| 334 // Call a method that needs the pref here | |
| 335 } | |
| 336 | |
| 337 void ShelfController::OnInitializationCompleted(bool succeeded) { | |
| 338 LOG(ERROR) << "JR OnInitializationCompleted\n"; | |
| 339 if (!succeeded) | |
| 340 return; | |
| 341 const base::Value* value = nullptr; | |
| 342 store_->GetValue("auto_hide_behavior", &value); | |
| 343 if (!value) | |
| 344 return; | |
| 345 std::string actual_value; | |
| 346 value->GetAsString(&actual_value); | |
| 347 LOG(ERROR) << "\tJR auto_hide_behavior: " << actual_value << "\n"; | |
| 348 | |
| 349 // Call a method that needs the pref here | |
| 350 } | |
| 351 | |
| 284 } // namespace ash | 352 } // namespace ash |
| OLD | NEW |