1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/ash/shelf/app_window_shelf_item_controller.h"
#include <algorithm>
#include <iterator>
#include <utility>
#include "ash/public/cpp/shelf_types.h"
#include "ash/public/cpp/window_properties.h"
#include "ash/wm/window_animations.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/ui/ash/shelf/app_window_base.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller.h"
#include "chrome/browser/ui/ash/shelf/shelf_context_menu.h"
#include "chrome/browser/ui/ash/shelf/shelf_controller_helper.h"
#include "chromeos/ui/wm/desks/desks_helper.h"
#include "components/app_constants/constants.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/wm/core/window_util.h"
namespace {
// Activates |app_window|. If |allow_minimize| is true and the system allows it,
// the the window will get minimized instead.
// Returns the action performed. Should be one of SHELF_ACTION_NONE,
// SHELF_ACTION_WINDOW_ACTIVATED, or SHELF_ACTION_WINDOW_MINIMIZED.
ash::ShelfAction ShowAndActivateOrMinimize(ui::BaseWindow* app_window,
bool allow_minimize) {
// Either show or minimize windows when shown from the shelf.
return ChromeShelfController::instance()->ActivateWindowOrMinimizeIfActive(
app_window, allow_minimize);
}
// Activate the given |window_to_show|, or - if already selected - advance to
// the next window of similar type using the given |windows| list.
// Returns the action performed. Should be one of SHELF_ACTION_NONE,
// SHELF_ACTION_WINDOW_ACTIVATED, or SHELF_ACTION_WINDOW_MINIMIZED.
ash::ShelfAction ActivateOrAdvanceToNextAppWindow(
AppWindowBase* window_to_show,
const AppWindowShelfItemController::WindowList& windows) {
DCHECK(window_to_show);
auto i = std::ranges::find(windows, window_to_show);
if (i != windows.end()) {
if (++i != windows.end()) {
window_to_show = *i;
} else {
window_to_show = windows.front();
}
}
if (window_to_show->IsActive()) {
// Coming here, only a single window is active. For keyboard activations
// the window gets animated.
ash::BounceWindow(window_to_show->GetNativeWindow());
} else {
return ShowAndActivateOrMinimize(window_to_show, windows.size() == 1);
}
return ash::SHELF_ACTION_NONE;
}
} // namespace
AppWindowShelfItemController::AppWindowShelfItemController(
const ash::ShelfID& shelf_id)
: ash::ShelfItemDelegate(shelf_id) {}
AppWindowShelfItemController::~AppWindowShelfItemController() {
WindowList windows(windows_);
for (AppWindowBase* window : hidden_windows_) {
windows.push_back(window);
}
for (AppWindowBase* window : windows) {
window->SetController(nullptr);
}
}
void AppWindowShelfItemController::AddWindow(AppWindowBase* app_window) {
aura::Window* window = app_window->GetNativeWindow();
if (window && !observed_windows_.IsObservingSource(window)) {
observed_windows_.AddObservation(window);
}
if (window && window->GetProperty(ash::kHideInShelfKey)) {
hidden_windows_.push_front(app_window);
} else {
windows_.push_front(app_window);
}
UpdateShelfItemIcon();
}
AppWindowShelfItemController::WindowList::iterator
AppWindowShelfItemController::GetFromNativeWindow(aura::Window* window,
WindowList& list) {
return std::ranges::find(list, window, &AppWindowBase::GetNativeWindow);
}
void AppWindowShelfItemController::RemoveWindow(AppWindowBase* app_window) {
DCHECK(app_window);
aura::Window* window = app_window->GetNativeWindow();
if (window && observed_windows_.IsObservingSource(window)) {
observed_windows_.RemoveObservation(window);
}
if (app_window == last_active_window_) {
last_active_window_ = nullptr;
}
auto iter = std::ranges::find(windows_, app_window);
if (iter != windows_.end()) {
windows_.erase(iter);
} else {
iter = std::ranges::find(hidden_windows_, app_window);
if (iter == hidden_windows_.end()) {
return;
}
hidden_windows_.erase(iter);
}
UpdateShelfItemIcon();
}
AppWindowBase* AppWindowShelfItemController::GetAppWindow(aura::Window* window,
bool include_hidden) {
auto iter = GetFromNativeWindow(window, windows_);
if (iter != windows_.end()) {
return *iter;
}
if (include_hidden) {
iter = GetFromNativeWindow(window, hidden_windows_);
if (iter != hidden_windows_.end()) {
return *iter;
}
}
return nullptr;
}
void AppWindowShelfItemController::SetActiveWindow(aura::Window* window) {
// If the window is hidden, do not set it as last_active_window
AppWindowBase* app_window = GetAppWindow(window, false);
if (app_window) {
last_active_window_ = app_window;
}
UpdateShelfItemIcon();
}
AppWindowShelfItemController*
AppWindowShelfItemController::AsAppWindowShelfItemController() {
return this;
}
void AppWindowShelfItemController::ItemSelected(
std::unique_ptr<ui::Event> event,
int64_t display_id,
ash::ShelfLaunchSource source,
ItemSelectedCallback callback,
const ItemFilterPredicate& filter_predicate) {
WindowList filtered_windows;
for (AppWindowBase* window : windows_) {
if (filter_predicate.is_null() ||
filter_predicate.Run(window->GetNativeWindow())) {
filtered_windows.push_back(window);
}
}
if (filtered_windows.empty()) {
std::move(callback).Run(ash::SHELF_ACTION_NONE, {});
return;
}
auto* last_active = last_active_window_.get();
if (last_active && !filter_predicate.is_null() &&
!filter_predicate.Run(last_active->GetNativeWindow())) {
last_active = nullptr;
}
AppWindowBase* window_to_show =
last_active ? last_active : filtered_windows.front().get();
// If the event was triggered by a keystroke, we try to advance to the next
// item if the window we are trying to activate is already active.
ash::ShelfAction action = ash::SHELF_ACTION_NONE;
if (filtered_windows.size() >= 1 && window_to_show->IsActive() && event &&
event->type() == ui::EventType::kKeyReleased) {
action = ActivateOrAdvanceToNextAppWindow(window_to_show, filtered_windows);
} else if (filtered_windows.size() <= 1 || source != ash::LAUNCH_FROM_SHELF) {
action = ShowAndActivateOrMinimize(
window_to_show, /*allow_minimize=*/filtered_windows.size() == 1);
} else {
// Do nothing if multiple windows are available when launching from shelf -
// the shelf will show a context menu with available windows.
action = ash::SHELF_ACTION_NONE;
}
std::move(callback).Run(
action,
GetAppMenuItems(event ? event->flags() : ui::EF_NONE, filter_predicate));
}
ash::ShelfItemDelegate::AppMenuItems
AppWindowShelfItemController::GetAppMenuItems(
int event_flags,
const ItemFilterPredicate& filter_predicate) {
AppMenuItems items;
std::u16string app_title = ShelfControllerHelper::GetAppTitle(
ChromeShelfController::instance()->profile(), app_id());
int command_id = -1;
for (const AppWindowBase* it : windows()) {
++command_id;
aura::Window* window = it->GetNativeWindow();
// Can window be null?
if (!filter_predicate.is_null() && !filter_predicate.Run(window)) {
continue;
}
auto title = (window && !window->GetTitle().empty()) ? window->GetTitle()
: app_title;
gfx::ImageSkia image;
if (window) {
// Prefer the smaller window icon because that fits better inside a menu.
const gfx::ImageSkia* icon =
window->GetProperty(aura::client::kWindowIconKey);
if (!icon || icon->isNull()) {
// Fall back to the larger app icon.
icon = window->GetProperty(aura::client::kAppIconKey);
}
if (icon && !icon->isNull()) {
image = *icon;
}
}
items.push_back({command_id, title, image});
}
return items;
}
void AppWindowShelfItemController::GetContextMenu(
int64_t display_id,
GetContextMenuCallback callback) {
ChromeShelfController* controller = ChromeShelfController::instance();
const ash::ShelfItem* item = controller->GetItem(shelf_id());
context_menu_ = ShelfContextMenu::Create(controller, item, display_id);
context_menu_->GetMenuModel(std::move(callback));
}
void AppWindowShelfItemController::Close() {
for (AppWindowBase* window : windows_) {
window->Close();
}
for (AppWindowBase* window : hidden_windows_) {
window->Close();
}
}
void AppWindowShelfItemController::ActivateIndexedApp(size_t index) {
if (index >= windows_.size()) {
return;
}
auto it = windows_.begin();
std::advance(it, index);
ShowAndActivateOrMinimize(*it, /*allow_minimize=*/windows_.size() == 1);
}
void AppWindowShelfItemController::OnWindowPropertyChanged(aura::Window* window,
const void* key,
intptr_t old) {
if (key == aura::client::kDrawAttentionKey) {
ash::ShelfItemStatus status;
// Active windows don't draw attention because the user is looking at them.
if (window->GetProperty(aura::client::kDrawAttentionKey) &&
!wm::IsActiveWindow(window)) {
status = ash::STATUS_ATTENTION;
} else {
status = ash::STATUS_RUNNING;
}
ChromeShelfController::instance()->SetItemStatus(shelf_id(), status);
} else if (key == aura::client::kAppIconKey) {
UpdateShelfItemIcon();
} else if (key == ash::kHideInShelfKey) {
UpdateWindowInLists(window);
}
}
AppWindowBase* AppWindowShelfItemController::GetLastActiveWindow() {
if (last_active_window_) {
return last_active_window_;
}
if (windows_.empty()) {
return nullptr;
}
return windows_.front();
}
void AppWindowShelfItemController::UpdateShelfItemIcon() {
// Set the shelf item icon from the kAppIconKey property of the current
// (or most recently) active window. If there is no valid icon, ask
// ChromeShelfController to update the icon.
const gfx::ImageSkia* app_icon = nullptr;
AppWindowBase* last_active_window = GetLastActiveWindow();
if (last_active_window && last_active_window->GetNativeWindow()) {
app_icon = last_active_window->GetNativeWindow()->GetProperty(
aura::client::kAppIconKey);
}
// TODO(khmel): Remove using image_set_by_controller
if (app_icon && !app_icon->isNull() &&
ChromeShelfController::instance()->GetItem(shelf_id())) {
set_image_set_by_controller(true);
ChromeShelfController::instance()->SetItemImage(shelf_id(), *app_icon);
} else if (image_set_by_controller()) {
set_image_set_by_controller(false);
ChromeShelfController::instance()->UpdateItemImage(shelf_id().app_id);
}
}
void AppWindowShelfItemController::UpdateWindowInLists(aura::Window* window) {
if (window->GetProperty(ash::kHideInShelfKey)) {
// Hide Window:
auto it = GetFromNativeWindow(window, windows_);
if (it != windows_.end()) {
hidden_windows_.push_front(*it);
windows_.erase(it);
UpdateShelfItemIcon();
}
} else {
// Unhide window:
auto it = GetFromNativeWindow(window, hidden_windows_);
if (it != hidden_windows_.end()) {
windows_.push_front(*it);
hidden_windows_.erase(it);
UpdateShelfItemIcon();
}
}
}
void AppWindowShelfItemController::ExecuteCommand(bool from_context_menu,
int64_t command_id,
int32_t event_flags,
int64_t display_id) {
DCHECK(!from_context_menu);
ActivateIndexedApp(command_id);
}
|