OLD | NEW |
(Empty) | |
| 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 "chrome/browser/ui/ash/launcher/extension_app_window_launcher_item_cont
roller.h" |
| 6 |
| 7 #include "ash/wm/window_state.h" |
| 8 #include "ash/wm/window_util.h" |
| 9 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item.h" |
| 10 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item_v2app.h" |
| 11 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" |
| 12 #include "chrome/browser/ui/ash/launcher/launcher_application_menu_item_model.h" |
| 13 #include "chrome/browser/ui/ash/launcher/launcher_context_menu.h" |
| 14 #include "chrome/browser/ui/ash/launcher/launcher_item_controller.h" |
| 15 #include "components/favicon/content/content_favicon_driver.h" |
| 16 #include "content/public/browser/web_contents.h" |
| 17 #include "extensions/browser/app_window/app_window.h" |
| 18 #include "extensions/browser/app_window/native_app_window.h" |
| 19 #include "skia/ext/image_operations.h" |
| 20 #include "ui/aura/client/aura_constants.h" |
| 21 #include "ui/aura/window.h" |
| 22 #include "ui/events/event.h" |
| 23 #include "ui/gfx/image/image_skia.h" |
| 24 #include "ui/wm/core/window_animations.h" |
| 25 |
| 26 namespace { |
| 27 |
| 28 // Size of the icon in the shelf launcher in display-independent pixels. |
| 29 const int kAppListIconSize = 24; |
| 30 |
| 31 // This will return a slightly smaller icon than the app icon to be used in |
| 32 // the application list menu. |
| 33 gfx::Image GetAppListIcon(extensions::AppWindow* app_window) { |
| 34 // TODO(skuhne): We instead might want to use LoadImages in |
| 35 // AppWindow::UpdateExtensionAppIcon() to let the extension give us |
| 36 // pre-defined icons in the launcher and the launcher list sizes. Since there |
| 37 // is no mock yet, doing this now seems a bit premature and we scale for the |
| 38 // time being. |
| 39 if (app_window->app_icon().IsEmpty()) |
| 40 return gfx::Image(); |
| 41 |
| 42 SkBitmap bmp = skia::ImageOperations::Resize( |
| 43 *app_window->app_icon().ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, |
| 44 kAppListIconSize, kAppListIconSize); |
| 45 return gfx::Image(gfx::ImageSkia::CreateFrom1xBitmap(bmp)); |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 ExtensionAppWindowLauncherItemController:: |
| 51 ExtensionAppWindowLauncherItemController( |
| 52 Type type, |
| 53 const std::string& app_shelf_id, |
| 54 const std::string& app_id, |
| 55 ChromeLauncherController* controller) |
| 56 : AppWindowLauncherItemController(type, app_shelf_id, app_id, controller) {} |
| 57 |
| 58 ExtensionAppWindowLauncherItemController:: |
| 59 ~ExtensionAppWindowLauncherItemController() {} |
| 60 |
| 61 void ExtensionAppWindowLauncherItemController::AddAppWindow( |
| 62 extensions::AppWindow* app_window) { |
| 63 if (app_window->window_type_is_panel() && type() != TYPE_APP_PANEL) |
| 64 LOG(ERROR) << "AppWindow of type Panel added to non-panel launcher item"; |
| 65 DCHECK(window_to_app_window_.find(app_window->GetBaseWindow()) == |
| 66 window_to_app_window_.end()); |
| 67 AddWindow(app_window->GetBaseWindow()); |
| 68 window_to_app_window_[app_window->GetBaseWindow()] = app_window; |
| 69 } |
| 70 |
| 71 void ExtensionAppWindowLauncherItemController::OnWindowRemoved( |
| 72 ui::BaseWindow* window) { |
| 73 WindowToAppWindow::iterator it = window_to_app_window_.find(window); |
| 74 if (it == window_to_app_window_.end()) { |
| 75 NOTREACHED(); |
| 76 return; |
| 77 } |
| 78 |
| 79 window_to_app_window_.erase(it); |
| 80 } |
| 81 |
| 82 ChromeLauncherAppMenuItems |
| 83 ExtensionAppWindowLauncherItemController::GetApplicationList(int event_flags) { |
| 84 ChromeLauncherAppMenuItems items = |
| 85 AppWindowLauncherItemController::GetApplicationList(event_flags); |
| 86 int index = 0; |
| 87 for (const auto* window : windows()) { |
| 88 extensions::AppWindow* app_window = window_to_app_window_[window]; |
| 89 DCHECK(app_window); |
| 90 |
| 91 // If the app's web contents provides a favicon, use it. Otherwise, use a |
| 92 // scaled down app icon. |
| 93 favicon::FaviconDriver* favicon_driver = |
| 94 favicon::ContentFaviconDriver::FromWebContents( |
| 95 app_window->web_contents()); |
| 96 gfx::Image result = favicon_driver->GetFavicon(); |
| 97 if (result.IsEmpty()) |
| 98 result = GetAppListIcon(app_window); |
| 99 |
| 100 items.push_back(new ChromeLauncherAppMenuItemV2App( |
| 101 app_window->GetTitle(), |
| 102 &result, // Will be copied |
| 103 app_id(), launcher_controller(), index, |
| 104 index == 0 /* has_leading_separator */)); |
| 105 ++index; |
| 106 } |
| 107 return items; |
| 108 } |
| 109 |
| 110 ash::ShelfItemDelegate::PerformedAction |
| 111 ExtensionAppWindowLauncherItemController::ItemSelected(const ui::Event& event) { |
| 112 if (windows().empty()) |
| 113 return kNoAction; |
| 114 |
| 115 if (type() == TYPE_APP_PANEL) { |
| 116 DCHECK_EQ(windows().size(), 1u); |
| 117 ui::BaseWindow* panel = windows().front(); |
| 118 aura::Window* panel_window = panel->GetNativeWindow(); |
| 119 // If the panel is attached on another display, move it to the current |
| 120 // display and activate it. |
| 121 if (ash::wm::GetWindowState(panel_window)->panel_attached() && |
| 122 ash::wm::MoveWindowToEventRoot(panel_window, event)) { |
| 123 if (!panel->IsActive()) |
| 124 return ShowAndActivateOrMinimize(panel); |
| 125 } else { |
| 126 return ShowAndActivateOrMinimize(panel); |
| 127 } |
| 128 } else { |
| 129 return AppWindowLauncherItemController::ItemSelected(event); |
| 130 } |
| 131 return kNoAction; |
| 132 } |
| 133 |
| 134 base::string16 ExtensionAppWindowLauncherItemController::GetTitle() { |
| 135 // For panels return the title of the contents if set. |
| 136 // Otherwise return the title of the app. |
| 137 if (type() == TYPE_APP_PANEL && !windows().empty()) { |
| 138 extensions::AppWindow* app_window = |
| 139 window_to_app_window_[windows().front()]; |
| 140 DCHECK(app_window); |
| 141 if (app_window->web_contents()) { |
| 142 base::string16 title = app_window->web_contents()->GetTitle(); |
| 143 if (!title.empty()) |
| 144 return title; |
| 145 } |
| 146 } |
| 147 return AppWindowLauncherItemController::GetTitle(); |
| 148 } |
| 149 |
| 150 ash::ShelfMenuModel* |
| 151 ExtensionAppWindowLauncherItemController::CreateApplicationMenu( |
| 152 int event_flags) { |
| 153 return new LauncherApplicationMenuItemModel(GetApplicationList(event_flags)); |
| 154 } |
| 155 |
| 156 bool ExtensionAppWindowLauncherItemController::IsDraggable() { |
| 157 if (type() == TYPE_APP_PANEL) |
| 158 return true; |
| 159 return AppWindowLauncherItemController::IsDraggable(); |
| 160 } |
| 161 |
| 162 bool ExtensionAppWindowLauncherItemController::ShouldShowTooltip() { |
| 163 if (type() == TYPE_APP_PANEL && IsVisible()) |
| 164 return false; |
| 165 return AppWindowLauncherItemController::ShouldShowTooltip(); |
| 166 } |
OLD | NEW |