| 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, |
| 193 std::vector<ash::mojom::MenuItemPtr>()); |
| 194 return; |
| 194 } | 195 } |
| 195 | 196 |
| 197 std::vector<ash::mojom::MenuItemPtr> items = |
| 198 GetAppMenuItems(event ? event->flags() : ui::EF_NONE); |
| 199 |
| 196 // In case of a keyboard event, we were called by a hotkey. In that case we | 200 // 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. | 201 // activate the next item in line if an item of our list is already active. |
| 198 if (event_type == ui::ET_KEY_RELEASED) | 202 if (event && event->type() == ui::ET_KEY_RELEASED) { |
| 199 return ActivateOrAdvanceToNextBrowser(); | 203 callback.Run(ActivateOrAdvanceToNextBrowser(), std::move(items)); |
| 204 return; |
| 205 } |
| 200 | 206 |
| 201 Browser* last_browser = | 207 Browser* last_browser = |
| 202 chrome::FindTabbedBrowser(launcher_controller()->profile(), true); | 208 chrome::FindTabbedBrowser(launcher_controller()->profile(), true); |
| 203 | 209 |
| 204 if (!last_browser) { | 210 if (!last_browser) { |
| 205 chrome::NewEmptyWindow(launcher_controller()->profile()); | 211 chrome::NewEmptyWindow(launcher_controller()->profile()); |
| 206 return ash::SHELF_ACTION_NEW_WINDOW_CREATED; | 212 callback.Run(ash::SHELF_ACTION_NEW_WINDOW_CREATED, |
| 213 std::vector<ash::mojom::MenuItemPtr>()); |
| 214 return; |
| 207 } | 215 } |
| 208 | 216 |
| 209 return launcher_controller()->ActivateWindowOrMinimizeIfActive( | 217 ash::ShelfAction action = |
| 210 last_browser->window(), GetAppMenuItems(0).size() == 1); | 218 launcher_controller()->ActivateWindowOrMinimizeIfActive( |
| 219 last_browser->window(), items.size() == 1); |
| 220 callback.Run(action, std::move(items)); |
| 211 } | 221 } |
| 212 | 222 |
| 213 ash::ShelfAppMenuItemList | 223 std::vector<ash::mojom::MenuItemPtr> |
| 214 BrowserShortcutLauncherItemController::GetAppMenuItems(int event_flags) { | 224 BrowserShortcutLauncherItemController::GetAppMenuItems(int event_flags) { |
| 215 browser_menu_items_.clear(); | 225 browser_menu_items_.clear(); |
| 216 registrar_.RemoveAll(); | 226 registrar_.RemoveAll(); |
| 217 | 227 |
| 218 ash::ShelfAppMenuItemList items; | 228 std::vector<ash::mojom::MenuItemPtr> items; |
| 219 bool found_tabbed_browser = false; | 229 bool found_tabbed_browser = false; |
| 220 for (auto* browser : GetListOfActiveBrowsers()) { | 230 for (auto* browser : GetListOfActiveBrowsers()) { |
| 221 if (browser_menu_items_.size() >= kMaxItems) | 231 if (browser_menu_items_.size() >= kMaxItems) |
| 222 break; | 232 break; |
| 223 TabStripModel* tab_strip = browser->tab_strip_model(); | 233 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 224 const int tab_index = tab_strip->active_index(); | 234 const int tab_index = tab_strip->active_index(); |
| 225 if (tab_index < 0 || tab_index >= kMaxItems) | 235 if (tab_index < 0 || tab_index >= kMaxItems) |
| 226 continue; | 236 continue; |
| 227 if (browser->is_type_tabbed()) | 237 if (browser->is_type_tabbed()) |
| 228 found_tabbed_browser = true; | 238 found_tabbed_browser = true; |
| 229 if (!(event_flags & ui::EF_SHIFT_DOWN)) { | 239 if (!(event_flags & ui::EF_SHIFT_DOWN)) { |
| 230 content::WebContents* tab = tab_strip->GetWebContentsAt(tab_index); | 240 content::WebContents* tab = tab_strip->GetWebContentsAt(tab_index); |
| 231 gfx::Image icon = GetBrowserListIcon(tab); | 241 ash::mojom::MenuItemPtr item(ash::mojom::MenuItem::New()); |
| 232 base::string16 title = GetBrowserListTitle(tab); | 242 item->command_id = GetCommandId(browser_menu_items_.size(), kNoTab); |
| 233 items.push_back(base::MakeUnique<ash::ShelfApplicationMenuItem>( | 243 item->label = GetBrowserListTitle(tab); |
| 234 GetCommandId(browser_menu_items_.size(), kNoTab), title, &icon)); | 244 item->image = *GetBrowserListIcon(tab).ToSkBitmap(); |
| 245 items.push_back(std::move(item)); |
| 235 } else { | 246 } else { |
| 236 for (uint16_t i = 0; i < tab_strip->count() && i < kMaxItems; ++i) { | 247 for (uint16_t i = 0; i < tab_strip->count() && i < kMaxItems; ++i) { |
| 237 content::WebContents* tab = tab_strip->GetWebContentsAt(i); | 248 content::WebContents* tab = tab_strip->GetWebContentsAt(i); |
| 238 gfx::Image icon = launcher_controller()->GetAppListIcon(tab); | 249 ash::mojom::MenuItemPtr item(ash::mojom::MenuItem::New()); |
| 239 base::string16 title = launcher_controller()->GetAppListTitle(tab); | 250 item->command_id = GetCommandId(browser_menu_items_.size(), i); |
| 240 items.push_back(base::MakeUnique<ash::ShelfApplicationMenuItem>( | 251 item->label = launcher_controller()->GetAppListTitle(tab); |
| 241 GetCommandId(browser_menu_items_.size(), i), title, &icon)); | 252 item->image = *launcher_controller()->GetAppListIcon(tab).ToSkBitmap(); |
| 253 items.push_back(std::move(item)); |
| 242 } | 254 } |
| 243 } | 255 } |
| 244 browser_menu_items_.push_back(browser); | 256 browser_menu_items_.push_back(browser); |
| 245 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, | 257 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, |
| 246 content::Source<Browser>(browser)); | 258 content::Source<Browser>(browser)); |
| 247 } | 259 } |
| 248 // If only windowed applications are open, we return an empty list to | 260 // If only windowed applications are open, we return an empty list to |
| 249 // enforce the creation of a new browser. | 261 // enforce the creation of a new browser. |
| 250 if (!found_tabbed_browser) { | 262 if (!found_tabbed_browser) { |
| 251 items.clear(); | 263 items.clear(); |
| 252 browser_menu_items_.clear(); | 264 browser_menu_items_.clear(); |
| 253 registrar_.RemoveAll(); | 265 registrar_.RemoveAll(); |
| 254 } | 266 } |
| 255 return items; | 267 return items; |
| 256 } | 268 } |
| 257 | 269 |
| 258 void BrowserShortcutLauncherItemController::ExecuteCommand(uint32_t command_id, | 270 void BrowserShortcutLauncherItemController::ExecuteCommand( |
| 259 int event_flags) { | 271 uint32_t command_id, |
| 272 int32_t event_flags) { |
| 260 const uint16_t browser_index = GetBrowserIndex(command_id); | 273 const uint16_t browser_index = GetBrowserIndex(command_id); |
| 261 // Check that the index is valid and the browser has not been closed. | 274 // Check that the index is valid and the browser has not been closed. |
| 262 if (browser_index < browser_menu_items_.size() && | 275 if (browser_index < browser_menu_items_.size() && |
| 263 browser_menu_items_[browser_index]) { | 276 browser_menu_items_[browser_index]) { |
| 264 Browser* browser = browser_menu_items_[browser_index]; | 277 Browser* browser = browser_menu_items_[browser_index]; |
| 265 TabStripModel* tab_strip = browser->tab_strip_model(); | 278 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 266 const uint16_t tab_index = GetWebContentsIndex(command_id); | 279 const uint16_t tab_index = GetWebContentsIndex(command_id); |
| 267 if (event_flags & (ui::EF_SHIFT_DOWN | ui::EF_MIDDLE_MOUSE_BUTTON)) { | 280 if (event_flags & (ui::EF_SHIFT_DOWN | ui::EF_MIDDLE_MOUSE_BUTTON)) { |
| 268 if (tab_index == kNoTab) { | 281 if (tab_index == kNoTab) { |
| 269 tab_strip->CloseAllTabs(); | 282 tab_strip->CloseAllTabs(); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 Browser* browser = content::Source<Browser>(source).ptr(); | 405 Browser* browser = content::Source<Browser>(source).ptr(); |
| 393 DCHECK(browser); | 406 DCHECK(browser); |
| 394 BrowserList::BrowserVector::iterator item = std::find( | 407 BrowserList::BrowserVector::iterator item = std::find( |
| 395 browser_menu_items_.begin(), browser_menu_items_.end(), browser); | 408 browser_menu_items_.begin(), browser_menu_items_.end(), browser); |
| 396 DCHECK(item != browser_menu_items_.end()); | 409 DCHECK(item != browser_menu_items_.end()); |
| 397 // Clear the entry for the closed browser and leave other indices intact. | 410 // Clear the entry for the closed browser and leave other indices intact. |
| 398 *item = nullptr; | 411 *item = nullptr; |
| 399 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING, | 412 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING, |
| 400 content::Source<Browser>(browser)); | 413 content::Source<Browser>(browser)); |
| 401 } | 414 } |
| OLD | NEW |