| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chrome/browser/ui/ash/launcher/browser_shortcut_launcher_item_controll
er.h" | 5 #include "chrome/browser/ui/ash/launcher/browser_shortcut_launcher_item_controll
er.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "ash/common/shelf/shelf_delegate.h" | 10 #include "ash/common/shelf/shelf_delegate.h" |
| 11 #include "ash/common/shelf/shelf_model.h" | 11 #include "ash/common/shelf/shelf_model.h" |
| 12 #include "ash/common/wm_shell.h" | 12 #include "ash/common/wm_shell.h" |
| 13 #include "ash/common/wm_window.h" | 13 #include "ash/common/wm_window.h" |
| 14 #include "ash/public/cpp/shelf_application_menu_item.h" | |
| 15 #include "ash/resources/grit/ash_resources.h" | 14 #include "ash/resources/grit/ash_resources.h" |
| 16 #include "ash/wm/window_properties.h" | 15 #include "ash/wm/window_properties.h" |
| 17 #include "ash/wm/window_util.h" | 16 #include "ash/wm/window_util.h" |
| 18 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 19 #include "chrome/browser/chrome_notification_types.h" | 18 #include "chrome/browser/chrome_notification_types.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" | 20 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" |
| 22 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller_util.h" | 21 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller_util.h" |
| 23 #include "chrome/browser/ui/ash/launcher/launcher_context_menu.h" | 22 #include "chrome/browser/ui/ash/launcher/launcher_context_menu.h" |
| 24 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | 23 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 // content which might change and as such change the application type. | 175 // content which might change and as such change the application type. |
| 177 if (!browser || !IsBrowserFromActiveUser(browser) || | 176 if (!browser || !IsBrowserFromActiveUser(browser) || |
| 178 IsSettingsBrowser(browser)) | 177 IsSettingsBrowser(browser)) |
| 179 return; | 178 return; |
| 180 | 179 |
| 181 browser->window()->GetNativeWindow()->SetProperty( | 180 browser->window()->GetNativeWindow()->SetProperty( |
| 182 ash::kShelfIDKey, | 181 ash::kShelfIDKey, |
| 183 launcher_controller()->GetShelfIDForWebContents(web_contents)); | 182 launcher_controller()->GetShelfIDForWebContents(web_contents)); |
| 184 } | 183 } |
| 185 | 184 |
| 186 ash::ShelfAction BrowserShortcutLauncherItemController::ItemSelected( | 185 void BrowserShortcutLauncherItemController::ItemSelected( |
| 187 ui::EventType event_type, | 186 std::unique_ptr<ui::Event> event, |
| 188 int event_flags, | |
| 189 int64_t display_id, | 187 int64_t display_id, |
| 190 ash::ShelfLaunchSource source) { | 188 ash::ShelfLaunchSource source, |
| 191 if (event_flags & ui::EF_CONTROL_DOWN) { | 189 const ItemSelectedCallback& callback) { |
| 190 if (event && (event->flags() & ui::EF_CONTROL_DOWN)) { |
| 192 chrome::NewEmptyWindow(launcher_controller()->profile()); | 191 chrome::NewEmptyWindow(launcher_controller()->profile()); |
| 193 return ash::SHELF_ACTION_NEW_WINDOW_CREATED; | 192 callback.Run(ash::SHELF_ACTION_NEW_WINDOW_CREATED, base::nullopt); |
| 193 return; |
| 194 } | 194 } |
| 195 | 195 |
| 196 MenuItemList items = GetAppMenuItems(event ? event->flags() : ui::EF_NONE); |
| 197 |
| 196 // In case of a keyboard event, we were called by a hotkey. In that case we | 198 // In case of a keyboard event, we were called by a hotkey. In that case we |
| 197 // activate the next item in line if an item of our list is already active. | 199 // activate the next item in line if an item of our list is already active. |
| 198 if (event_type == ui::ET_KEY_RELEASED) | 200 if (event && event->type() == ui::ET_KEY_RELEASED) { |
| 199 return ActivateOrAdvanceToNextBrowser(); | 201 callback.Run(ActivateOrAdvanceToNextBrowser(), std::move(items)); |
| 202 return; |
| 203 } |
| 200 | 204 |
| 201 Browser* last_browser = | 205 Browser* last_browser = |
| 202 chrome::FindTabbedBrowser(launcher_controller()->profile(), true); | 206 chrome::FindTabbedBrowser(launcher_controller()->profile(), true); |
| 203 | 207 |
| 204 if (!last_browser) { | 208 if (!last_browser) { |
| 205 chrome::NewEmptyWindow(launcher_controller()->profile()); | 209 chrome::NewEmptyWindow(launcher_controller()->profile()); |
| 206 return ash::SHELF_ACTION_NEW_WINDOW_CREATED; | 210 callback.Run(ash::SHELF_ACTION_NEW_WINDOW_CREATED, base::nullopt); |
| 211 return; |
| 207 } | 212 } |
| 208 | 213 |
| 209 return launcher_controller()->ActivateWindowOrMinimizeIfActive( | 214 ash::ShelfAction action = |
| 210 last_browser->window(), GetAppMenuItems(0).size() == 1); | 215 launcher_controller()->ActivateWindowOrMinimizeIfActive( |
| 216 last_browser->window(), items.size() == 1); |
| 217 callback.Run(action, std::move(items)); |
| 211 } | 218 } |
| 212 | 219 |
| 213 ash::ShelfAppMenuItemList | 220 MenuItemList BrowserShortcutLauncherItemController::GetAppMenuItems( |
| 214 BrowserShortcutLauncherItemController::GetAppMenuItems(int event_flags) { | 221 int event_flags) { |
| 215 browser_menu_items_.clear(); | 222 browser_menu_items_.clear(); |
| 216 registrar_.RemoveAll(); | 223 registrar_.RemoveAll(); |
| 217 | 224 |
| 218 ash::ShelfAppMenuItemList items; | 225 MenuItemList items; |
| 219 bool found_tabbed_browser = false; | 226 bool found_tabbed_browser = false; |
| 220 for (auto* browser : GetListOfActiveBrowsers()) { | 227 for (auto* browser : GetListOfActiveBrowsers()) { |
| 221 if (browser_menu_items_.size() >= kMaxItems) | 228 if (browser_menu_items_.size() >= kMaxItems) |
| 222 break; | 229 break; |
| 223 TabStripModel* tab_strip = browser->tab_strip_model(); | 230 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 224 const int tab_index = tab_strip->active_index(); | 231 const int tab_index = tab_strip->active_index(); |
| 225 if (tab_index < 0 || tab_index >= kMaxItems) | 232 if (tab_index < 0 || tab_index >= kMaxItems) |
| 226 continue; | 233 continue; |
| 227 if (browser->is_type_tabbed()) | 234 if (browser->is_type_tabbed()) |
| 228 found_tabbed_browser = true; | 235 found_tabbed_browser = true; |
| 229 if (!(event_flags & ui::EF_SHIFT_DOWN)) { | 236 if (!(event_flags & ui::EF_SHIFT_DOWN)) { |
| 230 content::WebContents* tab = tab_strip->GetWebContentsAt(tab_index); | 237 content::WebContents* tab = tab_strip->GetWebContentsAt(tab_index); |
| 231 gfx::Image icon = GetBrowserListIcon(tab); | 238 ash::mojom::MenuItemPtr item(ash::mojom::MenuItem::New()); |
| 232 base::string16 title = GetBrowserListTitle(tab); | 239 item->command_id = GetCommandId(browser_menu_items_.size(), kNoTab); |
| 233 items.push_back(base::MakeUnique<ash::ShelfApplicationMenuItem>( | 240 item->label = GetBrowserListTitle(tab); |
| 234 GetCommandId(browser_menu_items_.size(), kNoTab), title, &icon)); | 241 item->image = *GetBrowserListIcon(tab).ToSkBitmap(); |
| 242 items.push_back(std::move(item)); |
| 235 } else { | 243 } else { |
| 236 for (uint16_t i = 0; i < tab_strip->count() && i < kMaxItems; ++i) { | 244 for (uint16_t i = 0; i < tab_strip->count() && i < kMaxItems; ++i) { |
| 237 content::WebContents* tab = tab_strip->GetWebContentsAt(i); | 245 content::WebContents* tab = tab_strip->GetWebContentsAt(i); |
| 238 gfx::Image icon = launcher_controller()->GetAppListIcon(tab); | 246 ash::mojom::MenuItemPtr item(ash::mojom::MenuItem::New()); |
| 239 base::string16 title = launcher_controller()->GetAppListTitle(tab); | 247 item->command_id = GetCommandId(browser_menu_items_.size(), i); |
| 240 items.push_back(base::MakeUnique<ash::ShelfApplicationMenuItem>( | 248 item->label = launcher_controller()->GetAppListTitle(tab); |
| 241 GetCommandId(browser_menu_items_.size(), i), title, &icon)); | 249 item->image = *launcher_controller()->GetAppListIcon(tab).ToSkBitmap(); |
| 250 items.push_back(std::move(item)); |
| 242 } | 251 } |
| 243 } | 252 } |
| 244 browser_menu_items_.push_back(browser); | 253 browser_menu_items_.push_back(browser); |
| 245 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, | 254 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, |
| 246 content::Source<Browser>(browser)); | 255 content::Source<Browser>(browser)); |
| 247 } | 256 } |
| 248 // If only windowed applications are open, we return an empty list to | 257 // If only windowed applications are open, we return an empty list to |
| 249 // enforce the creation of a new browser. | 258 // enforce the creation of a new browser. |
| 250 if (!found_tabbed_browser) { | 259 if (!found_tabbed_browser) { |
| 251 items.clear(); | 260 items.clear(); |
| 252 browser_menu_items_.clear(); | 261 browser_menu_items_.clear(); |
| 253 registrar_.RemoveAll(); | 262 registrar_.RemoveAll(); |
| 254 } | 263 } |
| 255 return items; | 264 return items; |
| 256 } | 265 } |
| 257 | 266 |
| 258 void BrowserShortcutLauncherItemController::ExecuteCommand(uint32_t command_id, | 267 void BrowserShortcutLauncherItemController::ExecuteCommand( |
| 259 int event_flags) { | 268 uint32_t command_id, |
| 269 int32_t event_flags) { |
| 260 const uint16_t browser_index = GetBrowserIndex(command_id); | 270 const uint16_t browser_index = GetBrowserIndex(command_id); |
| 261 // Check that the index is valid and the browser has not been closed. | 271 // Check that the index is valid and the browser has not been closed. |
| 262 if (browser_index < browser_menu_items_.size() && | 272 if (browser_index < browser_menu_items_.size() && |
| 263 browser_menu_items_[browser_index]) { | 273 browser_menu_items_[browser_index]) { |
| 264 Browser* browser = browser_menu_items_[browser_index]; | 274 Browser* browser = browser_menu_items_[browser_index]; |
| 265 TabStripModel* tab_strip = browser->tab_strip_model(); | 275 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 266 const uint16_t tab_index = GetWebContentsIndex(command_id); | 276 const uint16_t tab_index = GetWebContentsIndex(command_id); |
| 267 if (event_flags & (ui::EF_SHIFT_DOWN | ui::EF_MIDDLE_MOUSE_BUTTON)) { | 277 if (event_flags & (ui::EF_SHIFT_DOWN | ui::EF_MIDDLE_MOUSE_BUTTON)) { |
| 268 if (tab_index == kNoTab) { | 278 if (tab_index == kNoTab) { |
| 269 tab_strip->CloseAllTabs(); | 279 tab_strip->CloseAllTabs(); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 Browser* browser = content::Source<Browser>(source).ptr(); | 402 Browser* browser = content::Source<Browser>(source).ptr(); |
| 393 DCHECK(browser); | 403 DCHECK(browser); |
| 394 BrowserList::BrowserVector::iterator item = std::find( | 404 BrowserList::BrowserVector::iterator item = std::find( |
| 395 browser_menu_items_.begin(), browser_menu_items_.end(), browser); | 405 browser_menu_items_.begin(), browser_menu_items_.end(), browser); |
| 396 DCHECK(item != browser_menu_items_.end()); | 406 DCHECK(item != browser_menu_items_.end()); |
| 397 // Clear the entry for the closed browser and leave other indices intact. | 407 // Clear the entry for the closed browser and leave other indices intact. |
| 398 *item = nullptr; | 408 *item = nullptr; |
| 399 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING, | 409 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING, |
| 400 content::Source<Browser>(browser)); | 410 content::Source<Browser>(browser)); |
| 401 } | 411 } |
| OLD | NEW |