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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/accessibility/autoclick_menu_bubble_controller.h"
#include <string>
#include "ash/bubble/bubble_constants.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/shelf/shelf.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/accessibility/autoclick_scroll_bubble_controller.h"
#include "ash/system/accessibility/floating_menu_utils.h"
#include "ash/system/tray/tray_background_view.h"
#include "ash/system/tray/tray_bubble_view.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/wm/collision_detection/collision_detection_utils.h"
#include "ash/wm/work_area_insets.h"
#include "ash/wm/workspace/workspace_layout_manager.h"
#include "ash/wm/workspace_controller.h"
#include "base/debug/crash_logging.h"
#include "base/debug/dump_without_crashing.h"
#include "base/strings/string_number_conversions.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/display/display.h"
#include "ui/display/manager/display_manager.h"
#include "ui/events/event_utils.h"
#include "ui/views/border.h"
#include "ui/views/bubble/bubble_border.h"
namespace ash {
namespace {
// Autoclick menu constants.
const int kAutoclickMenuWidth = 369;
const int kAutoclickMenuHeight = 64;
} // namespace
AutoclickMenuBubbleController::AutoclickMenuBubbleController() {
Shell::Get()->locale_update_controller()->AddObserver(this);
}
AutoclickMenuBubbleController::~AutoclickMenuBubbleController() {
if (bubble_widget_ && !bubble_widget_->IsClosed())
bubble_widget_->CloseNow();
Shell::Get()->locale_update_controller()->RemoveObserver(this);
scroll_bubble_controller_.reset();
}
void AutoclickMenuBubbleController::SetEventType(AutoclickEventType type) {
if (menu_view_)
menu_view_->UpdateEventType(type);
if (type == AutoclickEventType::kScroll) {
// If the type is scroll, show the scroll bubble using the
// scroll_bubble_controller_.
if (!scroll_bubble_controller_) {
scroll_bubble_controller_ =
std::make_unique<AutoclickScrollBubbleController>();
}
gfx::Rect anchor_rect = bubble_view_->GetBoundsInScreen();
anchor_rect.Inset(-kCollisionWindowWorkAreaInsetsDp);
scroll_bubble_controller_->ShowBubble(
anchor_rect, GetAnchorAlignmentForFloatingMenuPosition(position_));
} else if (scroll_bubble_controller_) {
scroll_bubble_controller_ = nullptr;
// Update the bubble menu's position in case it had moved out of the way
// for the scroll bubble.
SetPosition(position_);
}
}
void AutoclickMenuBubbleController::SetPosition(
FloatingMenuPosition new_position) {
if (!menu_view_ || !bubble_view_ || !bubble_widget_)
return;
// Update the menu view's UX if the position has changed, or if it's not the
// default position (because that can change with language direction).
if (position_ != new_position ||
new_position == FloatingMenuPosition::kSystemDefault) {
menu_view_->UpdatePosition(new_position);
}
position_ = new_position;
// If this is the default system position, pick the position based on the
// language direction.
if (new_position == FloatingMenuPosition::kSystemDefault)
new_position = DefaultSystemFloatingMenuPosition();
// TODO(katie): Support multiple displays: draw the menu on whichever display
// the cursor is on.
gfx::Rect new_bounds = GetOnScreenBoundsForFloatingMenuPosition(
gfx::Size(kAutoclickMenuWidth, kAutoclickMenuHeight), new_position);
// TODO(396681078): Confirm the preventive fix, clean up logging, and swap to
// a valid display if needed.
const display::Display target_display =
display::Screen::GetScreen()->GetDisplayNearestWindow(
bubble_widget_->GetNativeWindow());
if (!target_display.is_valid() ||
!Shell::GetRootWindowControllerWithDisplayId(target_display.id())) {
SCOPED_CRASH_KEY_NUMBER("AMBC", "target_display", target_display.id());
if (display::DisplayManager* display_manager =
Shell::Get()->display_manager()) {
std::string key;
for (size_t i = 0; i < display_manager->GetNumDisplays(); i++) {
const display::Display& current_display =
display_manager->GetDisplayAt(i);
key += (i ? "_" : "") + base::NumberToString(i);
key += "_" + base::NumberToString(current_display.id());
key += "_" + base::NumberToString(current_display.is_valid());
key += "_" + base::NumberToString(
!!Shell::GetRootWindowControllerWithDisplayId(
current_display.id()));
}
key += "_" + base::NumberToString(display_manager->IsInUnifiedMode());
SCOPED_CRASH_KEY_STRING256("AMBC", "display_info", key);
}
base::debug::DumpWithoutCrashing();
return;
}
// Update the preferred bounds based on other system windows.
gfx::Rect resting_bounds = CollisionDetectionUtils::GetRestingPosition(
target_display, new_bounds,
CollisionDetectionUtils::RelativePriority::kAutomaticClicksMenu);
// Un-inset the bounds to get the widget's bounds, which includes the drop
// shadow.
resting_bounds.Inset(gfx::Insets::TLBR(0, -kCollisionWindowWorkAreaInsetsDp,
-kCollisionWindowWorkAreaInsetsDp,
-kCollisionWindowWorkAreaInsetsDp));
if (bubble_widget_->GetWindowBoundsInScreen() == resting_bounds)
return;
if (animate_) {
ui::ScopedLayerAnimationSettings settings(
bubble_widget_->GetLayer()->GetAnimator());
settings.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
settings.SetTransitionDuration(base::Milliseconds(kAnimationDurationMs));
settings.SetTweenType(gfx::Tween::EASE_OUT);
}
bubble_widget_->SetBounds(resting_bounds);
if (!scroll_bubble_controller_)
return;
// Position the scroll bubble with respect to the menu.
scroll_bubble_controller_->UpdateAnchorRect(
resting_bounds, GetAnchorAlignmentForFloatingMenuPosition(new_position));
}
void AutoclickMenuBubbleController::SetScrollPosition(
gfx::Rect scroll_bounds_in_dips,
const gfx::Point& scroll_point_in_dips) {
if (!scroll_bubble_controller_)
return;
scroll_bubble_controller_->SetScrollPosition(scroll_bounds_in_dips,
scroll_point_in_dips);
}
void AutoclickMenuBubbleController::ShowBubble(AutoclickEventType type,
FloatingMenuPosition position) {
// Ignore if bubble widget already exists.
if (bubble_widget_)
return;
DCHECK(!bubble_view_);
TrayBubbleView::InitParams init_params;
init_params.delegate = GetWeakPtr();
// Anchor within the overlay container.
init_params.parent_window =
Shell::GetContainer(Shell::GetPrimaryRootWindow(),
kShellWindowId_AccessibilityBubbleContainer);
init_params.anchor_mode = TrayBubbleView::AnchorMode::kRect;
init_params.is_anchored_to_status_area = false;
init_params.close_on_deactivate = false;
// The widget's shadow is drawn below and on the sides of the view, with a
// width of kCollisionWindowWorkAreaInsetsDp. Set the top inset to 0 to ensure
// the scroll view is drawn at kCollisionWindowWorkAreaInsetsDp above the
// bubble menu when the position is at the bottom of the screen. The space
// between the bubbles belongs to the scroll view bubble's shadow.
init_params.insets = gfx::Insets::TLBR(0, kCollisionWindowWorkAreaInsetsDp,
kCollisionWindowWorkAreaInsetsDp,
kCollisionWindowWorkAreaInsetsDp);
init_params.preferred_width = kAutoclickMenuWidth;
init_params.translucent = true;
init_params.type = TrayBubbleView::TrayBubbleType::kAccessibilityBubble;
bubble_view_ = new TrayBubbleView(init_params);
menu_view_ = new AutoclickMenuView(type, position);
menu_view_->SetBorder(views::CreateEmptyBorder(
gfx::Insets::TLBR(kUnifiedTopShortcutSpacing, 0, 0, 0)));
bubble_view_->AddChildViewRaw(menu_view_.get());
bubble_widget_ = views::BubbleDialogDelegateView::CreateBubble(bubble_view_);
TrayBackgroundView::InitializeBubbleAnimations(bubble_widget_);
CollisionDetectionUtils::MarkWindowPriorityForCollisionDetection(
bubble_widget_->GetNativeWindow(),
CollisionDetectionUtils::RelativePriority::kAutomaticClicksMenu);
bubble_view_->InitializeAndShowBubble();
SetPosition(position);
}
void AutoclickMenuBubbleController::CloseBubble() {
if (!bubble_widget_ || bubble_widget_->IsClosed())
return;
bubble_widget_->Close();
}
void AutoclickMenuBubbleController::SetBubbleVisibility(bool is_visible) {
if (!bubble_widget_)
return;
if (is_visible)
bubble_widget_->Show();
else
bubble_widget_->Hide();
if (!scroll_bubble_controller_)
return;
scroll_bubble_controller_->SetBubbleVisibility(is_visible);
}
void AutoclickMenuBubbleController::ClickOnBubble(gfx::Point location_in_dips,
int mouse_event_flags) {
if (!bubble_widget_)
return;
// Change the event location bounds to be relative to the menu bubble.
location_in_dips -= bubble_view_->GetBoundsInScreen().OffsetFromOrigin();
// Generate synthesized mouse events for the click.
const ui::MouseEvent press_event(
ui::EventType::kMousePressed, location_in_dips, location_in_dips,
ui::EventTimeForNow(), mouse_event_flags | ui::EF_LEFT_MOUSE_BUTTON,
ui::EF_LEFT_MOUSE_BUTTON);
const ui::MouseEvent release_event(
ui::EventType::kMouseReleased, location_in_dips, location_in_dips,
ui::EventTimeForNow(), mouse_event_flags | ui::EF_LEFT_MOUSE_BUTTON,
ui::EF_LEFT_MOUSE_BUTTON);
// Send the press/release events to the widget's root view for processing.
bubble_widget_->GetRootView()->OnMousePressed(press_event);
bubble_widget_->GetRootView()->OnMouseReleased(release_event);
}
void AutoclickMenuBubbleController::ClickOnScrollBubble(
gfx::Point location_in_dips,
int mouse_event_flags) {
if (!scroll_bubble_controller_)
return;
scroll_bubble_controller_->ClickOnBubble(location_in_dips, mouse_event_flags);
}
bool AutoclickMenuBubbleController::ContainsPointInScreen(
const gfx::Point& point) {
return bubble_view_ && bubble_view_->GetBoundsInScreen().Contains(point);
}
bool AutoclickMenuBubbleController::ScrollBubbleContainsPointInScreen(
const gfx::Point& point) {
return scroll_bubble_controller_ &&
scroll_bubble_controller_->ContainsPointInScreen(point);
}
void AutoclickMenuBubbleController::BubbleViewDestroyed() {
bubble_view_ = nullptr;
bubble_widget_ = nullptr;
menu_view_ = nullptr;
}
std::u16string AutoclickMenuBubbleController::GetAccessibleNameForBubble() {
return l10n_util::GetStringUTF16(IDS_ASH_AUTOCLICK_MENU);
}
void AutoclickMenuBubbleController::HideBubble(
const TrayBubbleView* bubble_view) {
// This function is currently not unused for bubbles of type
// `kAccessibilityBubble`, so can leave this empty.
}
void AutoclickMenuBubbleController::OnLocaleChanged() {
// Layout update is needed when language changes between LTR and RTL, if the
// position is the system default.
if (position_ == FloatingMenuPosition::kSystemDefault)
SetPosition(position_);
}
} // namespace ash
|