Index: ash/common/shelf/shelf_controller.cc |
diff --git a/ash/mus/shelf_delegate_mus.cc b/ash/common/shelf/shelf_controller.cc |
similarity index 66% |
copy from ash/mus/shelf_delegate_mus.cc |
copy to ash/common/shelf/shelf_controller.cc |
index 7c1e4d9ba8e300c0e03a690a71ccfc42e34995f6..6b228104cf3faa629afc444cbc1f6844c9122961 100644 |
--- a/ash/mus/shelf_delegate_mus.cc |
+++ b/ash/common/shelf/shelf_controller.cc |
@@ -2,14 +2,10 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "ash/mus/shelf_delegate_mus.h" |
- |
-#include <memory> |
+#include "ash/common/shelf/shelf_controller.h" |
#include "ash/common/shelf/shelf_item_delegate.h" |
#include "ash/common/shelf/shelf_menu_model.h" |
-#include "ash/common/shelf/shelf_model.h" |
-#include "ash/common/shelf/shelf_widget.h" |
#include "ash/common/shelf/wm_shelf.h" |
#include "ash/common/wm_lookup.h" |
#include "ash/common/wm_root_window_controller.h" |
@@ -26,8 +22,8 @@ namespace ash { |
namespace { |
-// A ShelfItemDelegate used for pinned items. |
-// TODO(mash): Support open user windows, etc. |
+// A ShelfItemDelegate used for pinned items in mash. |
+// TODO(mash): Support open windows, cooperate with ShelfWindowWatcher. |
class ShelfItemDelegateMus : public ShelfItemDelegate { |
public: |
ShelfItemDelegateMus() {} |
@@ -135,13 +131,14 @@ class ShelfItemDelegateMus : public ShelfItemDelegate { |
DISALLOW_COPY_AND_ASSIGN(ShelfItemDelegateMus); |
}; |
+// Returns the ShelfItemDelegateMus instance for the given |shelf_id|. |
ShelfItemDelegateMus* GetShelfItemDelegate(ShelfID shelf_id) { |
return static_cast<ShelfItemDelegateMus*>( |
WmShell::Get()->shelf_model()->GetShelfItemDelegate(shelf_id)); |
} |
-// Returns an icon image from an SkBitmap, or the default shelf icon |
-// image if the bitmap is empty. Assumes the bitmap is a 1x icon. |
+// Returns an icon image from an SkBitmap, or the default shelf icon image if |
+// the bitmap is empty. Assumes the bitmap is a 1x icon. |
// TODO(jamescook): Support other scale factors. |
gfx::ImageSkia GetShelfIconFromBitmap(const SkBitmap& bitmap) { |
gfx::ImageSkia icon_image; |
@@ -155,16 +152,25 @@ gfx::ImageSkia GetShelfIconFromBitmap(const SkBitmap& bitmap) { |
return icon_image; |
} |
+// Returns the WmShelf instance for the display with the given |display_id|. |
+WmShelf* GetShelfForDisplay(int64_t display_id) { |
+ // The controller may be null for invalid ids or for displays being removed. |
+ WmRootWindowController* root_window_controller = |
+ WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id); |
+ return root_window_controller ? root_window_controller->GetShelf() : nullptr; |
+} |
+ |
} // namespace |
-ShelfDelegateMus::ShelfDelegateMus(ShelfModel* model) : model_(model) {} |
+ShelfController::ShelfController() {} |
-ShelfDelegateMus::~ShelfDelegateMus() {} |
+ShelfController::~ShelfController() {} |
-/////////////////////////////////////////////////////////////////////////////// |
-// ShelfDelegate: |
+void ShelfController::BindRequest(mojom::ShelfControllerRequest request) { |
+ bindings_.AddBinding(this, std::move(request)); |
+} |
-void ShelfDelegateMus::OnShelfCreated(WmShelf* shelf) { |
+void ShelfController::NotifyShelfCreated(WmShelf* shelf) { |
// Notify observers, Chrome will set alignment and auto-hide from prefs. |
int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); |
observers_.ForAllPtrs([display_id](mojom::ShelfObserver* observer) { |
@@ -172,9 +178,7 @@ void ShelfDelegateMus::OnShelfCreated(WmShelf* shelf) { |
}); |
} |
-void ShelfDelegateMus::OnShelfDestroyed(WmShelf* shelf) {} |
- |
-void ShelfDelegateMus::OnShelfAlignmentChanged(WmShelf* shelf) { |
+void ShelfController::NotifyShelfAlignmentChanged(WmShelf* shelf) { |
ShelfAlignment alignment = shelf->alignment(); |
int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); |
observers_.ForAllPtrs( |
@@ -183,7 +187,7 @@ void ShelfDelegateMus::OnShelfAlignmentChanged(WmShelf* shelf) { |
}); |
} |
-void ShelfDelegateMus::OnShelfAutoHideBehaviorChanged(WmShelf* shelf) { |
+void ShelfController::NotifyShelfAutoHideBehaviorChanged(WmShelf* shelf) { |
ShelfAutoHideBehavior behavior = shelf->auto_hide_behavior(); |
int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); |
observers_.ForAllPtrs([behavior, display_id](mojom::ShelfObserver* observer) { |
@@ -191,82 +195,37 @@ void ShelfDelegateMus::OnShelfAutoHideBehaviorChanged(WmShelf* shelf) { |
}); |
} |
-void ShelfDelegateMus::OnShelfAutoHideStateChanged(WmShelf* shelf) {} |
- |
-void ShelfDelegateMus::OnShelfVisibilityStateChanged(WmShelf* shelf) {} |
sky
2016/10/07 16:10:17
How come OnShelfAutoHideStateChanged and OnShelfVi
msw
2016/10/07 22:45:57
All implementations are empty / no-op, ditto for O
|
- |
-ShelfID ShelfDelegateMus::GetShelfIDForAppID(const std::string& app_id) { |
- if (app_id_to_shelf_id_.count(app_id)) |
- return app_id_to_shelf_id_[app_id]; |
- return 0; |
-} |
- |
-ShelfID ShelfDelegateMus::GetShelfIDForAppIDAndLaunchID( |
- const std::string& app_id, |
- const std::string& launch_id) { |
- return ShelfDelegateMus::GetShelfIDForAppID(app_id); |
-} |
- |
-bool ShelfDelegateMus::HasShelfIDToAppIDMapping(ShelfID id) const { |
- return shelf_id_to_app_id_.count(id) != 0; |
-} |
- |
-const std::string& ShelfDelegateMus::GetAppIDForShelfID(ShelfID id) { |
- if (shelf_id_to_app_id_.count(id)) |
- return shelf_id_to_app_id_[id]; |
- return base::EmptyString(); |
-} |
- |
-void ShelfDelegateMus::PinAppWithID(const std::string& app_id) { |
- NOTIMPLEMENTED(); |
-} |
- |
-bool ShelfDelegateMus::IsAppPinned(const std::string& app_id) { |
- NOTIMPLEMENTED(); |
- return false; |
-} |
- |
-void ShelfDelegateMus::UnpinAppWithID(const std::string& app_id) { |
- NOTIMPLEMENTED(); |
-} |
- |
-/////////////////////////////////////////////////////////////////////////////// |
-// mojom::ShelfController: |
- |
-void ShelfDelegateMus::AddObserver( |
+void ShelfController::AddObserver( |
mojom::ShelfObserverAssociatedPtrInfo observer) { |
mojom::ShelfObserverAssociatedPtr observer_ptr; |
observer_ptr.Bind(std::move(observer)); |
- // Notify the observer of the current state. |
- for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { |
- WmWindow* root = WmShell::Get()->GetRootWindowForDisplayId(display.id()); |
- WmShelf* shelf = root->GetRootWindowController()->GetShelf(); |
- observer_ptr->OnAlignmentChanged(shelf->alignment(), display.id()); |
- observer_ptr->OnAutoHideBehaviorChanged(shelf->auto_hide_behavior(), |
- display.id()); |
- } |
observers_.AddPtr(std::move(observer_ptr)); |
} |
-void ShelfDelegateMus::SetAlignment(ShelfAlignment alignment, |
- int64_t display_id) { |
- WmRootWindowController* root_window_controller = |
- WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id); |
- // The controller may be null for invalid ids or for displays being removed. |
- if (root_window_controller && root_window_controller->HasShelf()) |
- root_window_controller->GetShelf()->SetAlignment(alignment); |
+void ShelfController::SetAlignment(ShelfAlignment alignment, |
+ int64_t display_id) { |
+ if (!ash::WmShelf::ShelfAlignmentAllowed()) |
+ return; |
+ |
+ WmShelf* shelf = GetShelfForDisplay(display_id); |
+ // TODO(jamescook): The initialization check should not be necessary, but |
+ // otherwise this wrongly tries to set the alignment on a secondary display |
+ // during login before the ShelfLockingManager and ShelfView are created. |
+ if (shelf && shelf->IsShelfInitialized()) |
+ shelf->SetAlignment(alignment); |
} |
-void ShelfDelegateMus::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide, |
- int64_t display_id) { |
- WmRootWindowController* root_window_controller = |
- WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id); |
- // The controller may be null for invalid ids or for displays being removed. |
- if (root_window_controller && root_window_controller->HasShelf()) |
- root_window_controller->GetShelf()->SetAutoHideBehavior(auto_hide); |
+void ShelfController::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide, |
+ int64_t display_id) { |
+ WmShelf* shelf = GetShelfForDisplay(display_id); |
+ // TODO(jamescook): The initialization check should not be necessary, but |
+ // otherwise this wrongly tries to set auto-hide state on a secondary display |
+ // during login before the ShelfView is created. |
+ if (shelf && shelf->IsShelfInitialized()) |
+ shelf->SetAutoHideBehavior(auto_hide); |
} |
-void ShelfDelegateMus::PinItem( |
+void ShelfController::PinItem( |
mojom::ShelfItemPtr item, |
mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { |
if (app_id_to_shelf_id_.count(item->app_id)) { |
@@ -277,7 +236,7 @@ void ShelfDelegateMus::PinItem( |
return; |
} |
- ShelfID shelf_id = model_->next_id(); |
+ ShelfID shelf_id = model_.next_id(); |
app_id_to_shelf_id_.insert(std::make_pair(item->app_id, shelf_id)); |
shelf_id_to_app_id_.insert(std::make_pair(shelf_id, item->app_id)); |
@@ -285,40 +244,41 @@ void ShelfDelegateMus::PinItem( |
shelf_item.type = TYPE_APP_SHORTCUT; |
shelf_item.status = STATUS_CLOSED; |
shelf_item.image = GetShelfIconFromBitmap(item->image); |
- model_->Add(shelf_item); |
+ model_.Add(shelf_item); |
std::unique_ptr<ShelfItemDelegateMus> item_delegate( |
new ShelfItemDelegateMus()); |
item_delegate->SetDelegate(std::move(delegate)); |
item_delegate->set_pinned(true); |
item_delegate->set_title(base::UTF8ToUTF16(item->app_title)); |
- model_->SetShelfItemDelegate(shelf_id, std::move(item_delegate)); |
+ model_.SetShelfItemDelegate(shelf_id, std::move(item_delegate)); |
} |
-void ShelfDelegateMus::UnpinItem(const std::string& app_id) { |
+void ShelfController::UnpinItem(const std::string& app_id) { |
if (!app_id_to_shelf_id_.count(app_id)) |
return; |
+ |
ShelfID shelf_id = app_id_to_shelf_id_[app_id]; |
ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id); |
DCHECK(item_delegate->pinned()); |
item_delegate->set_pinned(false); |
if (item_delegate->window_id_to_title().empty()) { |
- model_->RemoveItemAt(model_->ItemIndexByID(shelf_id)); |
+ model_.RemoveItemAt(model_.ItemIndexByID(shelf_id)); |
app_id_to_shelf_id_.erase(app_id); |
shelf_id_to_app_id_.erase(shelf_id); |
} |
} |
-void ShelfDelegateMus::SetItemImage(const std::string& app_id, |
- const SkBitmap& image) { |
+void ShelfController::SetItemImage(const std::string& app_id, |
+ const SkBitmap& image) { |
if (!app_id_to_shelf_id_.count(app_id)) |
return; |
ShelfID shelf_id = app_id_to_shelf_id_[app_id]; |
- int index = model_->ItemIndexByID(shelf_id); |
+ int index = model_.ItemIndexByID(shelf_id); |
DCHECK_GE(index, 0); |
- ShelfItem item = *model_->ItemByID(shelf_id); |
+ ShelfItem item = *model_.ItemByID(shelf_id); |
item.image = GetShelfIconFromBitmap(image); |
- model_->Set(index, item); |
+ model_.Set(index, item); |
} |
} // namespace ash |