| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/shell_window_launcher_item_controller.h
" | 5 #include "chrome/browser/ui/ash/launcher/shell_window_launcher_item_controller.h
" |
| 6 | 6 |
| 7 #include "ash/launcher/launcher_util.h" | 7 #include "ash/launcher/launcher_util.h" |
| 8 #include "ash/wm/window_util.h" | 8 #include "ash/wm/window_util.h" |
| 9 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item.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" | 10 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item_v2app.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 } // namespace | 35 } // namespace |
| 36 | 36 |
| 37 ShellWindowLauncherItemController::ShellWindowLauncherItemController( | 37 ShellWindowLauncherItemController::ShellWindowLauncherItemController( |
| 38 Type type, | 38 Type type, |
| 39 const std::string& app_launcher_id, | 39 const std::string& app_launcher_id, |
| 40 const std::string& app_id, | 40 const std::string& app_id, |
| 41 ChromeLauncherController* controller) | 41 ChromeLauncherController* controller) |
| 42 : LauncherItemController(type, app_id, controller), | 42 : LauncherItemController(type, app_id, controller), |
| 43 last_active_shell_window_(NULL), |
| 43 app_launcher_id_(app_launcher_id), | 44 app_launcher_id_(app_launcher_id), |
| 44 ALLOW_THIS_IN_INITIALIZER_LIST(observed_windows_(this)) { | 45 ALLOW_THIS_IN_INITIALIZER_LIST(observed_windows_(this)) { |
| 45 } | 46 } |
| 46 | 47 |
| 47 ShellWindowLauncherItemController::~ShellWindowLauncherItemController() { | 48 ShellWindowLauncherItemController::~ShellWindowLauncherItemController() { |
| 48 } | 49 } |
| 49 | 50 |
| 50 void ShellWindowLauncherItemController::AddShellWindow( | 51 void ShellWindowLauncherItemController::AddShellWindow( |
| 51 ShellWindow* shell_window, | 52 ShellWindow* shell_window, |
| 52 ash::LauncherItemStatus status) { | 53 ash::LauncherItemStatus status) { |
| 53 if (shell_window->window_type_is_panel() && type() != TYPE_APP_PANEL) { | 54 if (shell_window->window_type_is_panel() && type() != TYPE_APP_PANEL) |
| 54 LOG(ERROR) << "ShellWindow of type Panel added to non-panel launcher item"; | 55 LOG(ERROR) << "ShellWindow of type Panel added to non-panel launcher item"; |
| 55 } | 56 shell_windows_.push_front(shell_window); |
| 56 if (status == ash::STATUS_ACTIVE) | |
| 57 shell_windows_.push_front(shell_window); | |
| 58 else | |
| 59 shell_windows_.push_back(shell_window); | |
| 60 observed_windows_.Add(shell_window->GetNativeWindow()); | 57 observed_windows_.Add(shell_window->GetNativeWindow()); |
| 61 } | 58 } |
| 62 | 59 |
| 63 void ShellWindowLauncherItemController::RemoveShellWindowForWindow( | 60 void ShellWindowLauncherItemController::RemoveShellWindowForWindow( |
| 64 aura::Window* window) { | 61 aura::Window* window) { |
| 65 ShellWindowList::iterator iter = | 62 ShellWindowList::iterator iter = |
| 66 std::find_if(shell_windows_.begin(), shell_windows_.end(), | 63 std::find_if(shell_windows_.begin(), shell_windows_.end(), |
| 67 ShellWindowHasWindow(window)); | 64 ShellWindowHasWindow(window)); |
| 65 if (iter != shell_windows_.end()) { |
| 66 if (*iter == last_active_shell_window_) |
| 67 last_active_shell_window_ = NULL; |
| 68 shell_windows_.erase(iter); |
| 69 } |
| 70 observed_windows_.Remove(window); |
| 71 } |
| 72 |
| 73 void ShellWindowLauncherItemController::SetActiveWindow(aura::Window* window) { |
| 74 ShellWindowList::iterator iter = |
| 75 std::find_if(shell_windows_.begin(), shell_windows_.end(), |
| 76 ShellWindowHasWindow(window)); |
| 68 if (iter != shell_windows_.end()) | 77 if (iter != shell_windows_.end()) |
| 69 shell_windows_.erase(iter); | 78 last_active_shell_window_ = *iter; |
| 70 observed_windows_.Remove(window); | |
| 71 } | |
| 72 | |
| 73 void ShellWindowLauncherItemController::SetActiveWindow( | |
| 74 aura::Window* window) { | |
| 75 ShellWindowList::iterator iter = | |
| 76 std::find_if(shell_windows_.begin(), shell_windows_.end(), | |
| 77 ShellWindowHasWindow(window)); | |
| 78 if (iter != shell_windows_.end()) { | |
| 79 ShellWindow* shell_window = *iter; | |
| 80 shell_windows_.erase(iter); | |
| 81 shell_windows_.push_front(shell_window); | |
| 82 } | |
| 83 } | 79 } |
| 84 | 80 |
| 85 string16 ShellWindowLauncherItemController::GetTitle() { | 81 string16 ShellWindowLauncherItemController::GetTitle() { |
| 86 // For panels return the title of the contents if set. | 82 // For panels return the title of the contents if set. |
| 87 // Otherwise return the title of the app. | 83 // Otherwise return the title of the app. |
| 88 if (type() == TYPE_APP_PANEL && !shell_windows_.empty()) { | 84 if (type() == TYPE_APP_PANEL && !shell_windows_.empty()) { |
| 89 ShellWindow* shell_window = shell_windows_.front(); | 85 ShellWindow* shell_window = shell_windows_.front(); |
| 90 if (shell_window->web_contents()) { | 86 if (shell_window->web_contents()) { |
| 91 string16 title = shell_window->web_contents()->GetTitle(); | 87 string16 title = shell_window->web_contents()->GetTitle(); |
| 92 if (!title.empty()) | 88 if (!title.empty()) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 118 return false; | 114 return false; |
| 119 } | 115 } |
| 120 | 116 |
| 121 void ShellWindowLauncherItemController::Launch( | 117 void ShellWindowLauncherItemController::Launch( |
| 122 int event_flags) { | 118 int event_flags) { |
| 123 launcher_controller()->LaunchApp(app_id(), ui::EF_NONE); | 119 launcher_controller()->LaunchApp(app_id(), ui::EF_NONE); |
| 124 } | 120 } |
| 125 | 121 |
| 126 void ShellWindowLauncherItemController::Activate() { | 122 void ShellWindowLauncherItemController::Activate() { |
| 127 DCHECK(!shell_windows_.empty()); | 123 DCHECK(!shell_windows_.empty()); |
| 128 shell_windows_.front()->GetBaseWindow()->Activate(); | 124 ShellWindow* window_to_activate = last_active_shell_window_ ? |
| 125 last_active_shell_window_ : shell_windows_.back(); |
| 126 window_to_activate->GetBaseWindow()->Activate(); |
| 129 } | 127 } |
| 130 | 128 |
| 131 void ShellWindowLauncherItemController::Close() { | 129 void ShellWindowLauncherItemController::Close() { |
| 132 // Note: Closing windows may affect the contents of shell_windows_. | 130 // Note: Closing windows may affect the contents of shell_windows_. |
| 133 ShellWindowList windows_to_close = shell_windows_; | 131 ShellWindowList windows_to_close = shell_windows_; |
| 134 for (ShellWindowList::iterator iter = windows_to_close.begin(); | 132 for (ShellWindowList::iterator iter = windows_to_close.begin(); |
| 135 iter != windows_to_close.end(); ++iter) { | 133 iter != windows_to_close.end(); ++iter) { |
| 136 (*iter)->GetBaseWindow()->Close(); | 134 (*iter)->GetBaseWindow()->Close(); |
| 137 } | 135 } |
| 138 } | 136 } |
| 139 | 137 |
| 140 // Behavior for app windows: | |
| 141 // * One window: Toggle minimization when clicked. | |
| 142 // * Multiple windows: | |
| 143 // ** If the first window is not active, activate it. | |
| 144 // ** Otherwise activate the next window. | |
| 145 void ShellWindowLauncherItemController::Clicked(const ui::Event& event) { | 138 void ShellWindowLauncherItemController::Clicked(const ui::Event& event) { |
| 146 if (shell_windows_.empty()) | 139 if (shell_windows_.empty()) |
| 147 return; | 140 return; |
| 148 ShellWindow* first_window = shell_windows_.front(); | 141 if (launcher_controller()->GetPerAppInterface() || |
| 149 if (shell_windows_.size() == 1) { | 142 shell_windows_.size() == 1) { |
| 150 ash::launcher::MoveToEventRootIfPanel(first_window->GetNativeWindow(), | 143 ShellWindow* window_to_show = last_active_shell_window_ ? |
| 151 event); | 144 last_active_shell_window_ : shell_windows_.front(); |
| 152 // If the window moves, it becomes inactive first then | 145 RestoreOrShow(window_to_show); |
| 153 // gets activated in |RestoreOrShow| below. | |
| 154 if (first_window->GetBaseWindow()->IsActive()) | |
| 155 first_window->GetBaseWindow()->Minimize(); | |
| 156 else | |
| 157 RestoreOrShow(first_window); | |
| 158 } else { | 146 } else { |
| 159 if (!first_window->GetBaseWindow()->IsActive()) { | 147 // TODO(stevenjb): Deprecate |
| 160 RestoreOrShow(first_window); | 148 if (!last_active_shell_window_ || |
| 161 } else { | 149 last_active_shell_window_->GetBaseWindow()->IsActive()) { |
| 162 shell_windows_.pop_front(); | 150 // Restore all windows since there is no other way to restore them. |
| 163 shell_windows_.push_back(first_window); | 151 for (ShellWindowList::iterator iter = shell_windows_.begin(); |
| 164 RestoreOrShow(shell_windows_.front()); | 152 iter != shell_windows_.end(); ++iter) { |
| 153 ShellWindow* shell_window = *iter; |
| 154 if (shell_window->GetBaseWindow()->IsMinimized()) |
| 155 shell_window->GetBaseWindow()->Restore(); |
| 156 } |
| 165 } | 157 } |
| 158 if (last_active_shell_window_) |
| 159 RestoreOrShow(last_active_shell_window_); |
| 166 } | 160 } |
| 167 } | 161 } |
| 168 | 162 |
| 169 void ShellWindowLauncherItemController::ActivateIndexedApp( | 163 void ShellWindowLauncherItemController::ActivateIndexedApp(size_t index) { |
| 170 size_t index) { | 164 if (index >= shell_windows_.size()) |
| 171 if (index < shell_windows_.size()) { | 165 return; |
| 172 ShellWindowList::iterator it = shell_windows_.begin(); | 166 ShellWindowList::iterator it = shell_windows_.begin(); |
| 173 std::advance(it, index); | 167 std::advance(it, index); |
| 174 RestoreOrShow(*it); | 168 RestoreOrShow(*it); |
| 175 } | |
| 176 } | |
| 177 | |
| 178 string16 ShellWindowLauncherItemController::GetTitleOfIndexedApp( | |
| 179 size_t index) { | |
| 180 if (index < shell_windows_.size()) { | |
| 181 ShellWindowList::iterator it = shell_windows_.begin(); | |
| 182 std::advance(it, index); | |
| 183 return (*it)->GetTitle(); | |
| 184 } | |
| 185 return string16(); | |
| 186 } | |
| 187 | |
| 188 gfx::Image* | |
| 189 ShellWindowLauncherItemController::GetIconOfIndexedApp(size_t index) { | |
| 190 if (index < shell_windows_.size()) { | |
| 191 ShellWindowList::iterator it = shell_windows_.begin(); | |
| 192 std::advance(it, index); | |
| 193 return (*it)->GetAppListIcon(); | |
| 194 } | |
| 195 return new gfx::Image(); | |
| 196 } | 169 } |
| 197 | 170 |
| 198 ChromeLauncherAppMenuItems | 171 ChromeLauncherAppMenuItems |
| 199 ShellWindowLauncherItemController::GetApplicationList() { | 172 ShellWindowLauncherItemController::GetApplicationList() { |
| 200 ChromeLauncherAppMenuItems items; | 173 ChromeLauncherAppMenuItems items; |
| 201 if (!launcher_controller()->GetPerAppInterface()) { | 174 items.push_back(new ChromeLauncherAppMenuItem(GetTitle(), NULL, false)); |
| 202 items.push_back(new ChromeLauncherAppMenuItem(GetTitle(), NULL, false)); | 175 int index = 0; |
| 203 for (size_t i = 0; i < shell_window_count(); i++) { | 176 for (ShellWindowList::iterator iter = shell_windows_.begin(); |
| 204 gfx::Image* image = GetIconOfIndexedApp(i); | 177 iter != shell_windows_.end(); ++iter) { |
| 205 items.push_back(new ChromeLauncherAppMenuItemV2App( | 178 ShellWindow* shell_window = *iter; |
| 206 GetTitleOfIndexedApp(i), | 179 scoped_ptr<gfx::Image> image(shell_window->GetAppListIcon()); |
| 207 image, | 180 items.push_back(new ChromeLauncherAppMenuItemV2App( |
| 208 app_id(), | 181 shell_window->GetTitle(), |
| 209 launcher_controller()->GetPerAppInterface(), | 182 image.get(), // Will be copied |
| 210 i, | 183 app_id(), |
| 211 i == 0)); | 184 launcher_controller()->GetPerAppInterface(), |
| 212 delete image; | 185 index, |
| 213 } | 186 index == 0 /* has_leading_separator */)); |
| 187 ++index; |
| 214 } | 188 } |
| 215 return items.Pass(); | 189 return items.Pass(); |
| 216 } | 190 } |
| 217 | 191 |
| 218 void ShellWindowLauncherItemController::OnWindowPropertyChanged( | 192 void ShellWindowLauncherItemController::OnWindowPropertyChanged( |
| 219 aura::Window* window, | 193 aura::Window* window, |
| 220 const void* key, | 194 const void* key, |
| 221 intptr_t old) { | 195 intptr_t old) { |
| 222 if (key == aura::client::kDrawAttentionKey) { | 196 if (key == aura::client::kDrawAttentionKey) { |
| 223 ash::LauncherItemStatus status; | 197 ash::LauncherItemStatus status; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 234 | 208 |
| 235 void ShellWindowLauncherItemController::RestoreOrShow( | 209 void ShellWindowLauncherItemController::RestoreOrShow( |
| 236 ShellWindow* shell_window) { | 210 ShellWindow* shell_window) { |
| 237 if (shell_window->GetBaseWindow()->IsMinimized()) | 211 if (shell_window->GetBaseWindow()->IsMinimized()) |
| 238 shell_window->GetBaseWindow()->Restore(); | 212 shell_window->GetBaseWindow()->Restore(); |
| 239 else | 213 else |
| 240 shell_window->GetBaseWindow()->Show(); | 214 shell_window->GetBaseWindow()->Show(); |
| 241 // Always activate windows when shown from the launcher. | 215 // Always activate windows when shown from the launcher. |
| 242 shell_window->GetBaseWindow()->Activate(); | 216 shell_window->GetBaseWindow()->Activate(); |
| 243 } | 217 } |
| OLD | NEW |