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

ash / system / accessibility / floating_accessibility_controller.cc [blame]

// Copyright 2020 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/floating_accessibility_controller.h"

#include "ash/accessibility/accessibility_controller.h"
#include "ash/bubble/bubble_constants.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/accessibility/floating_menu_utils.h"
#include "ash/system/tray/tray_background_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 "base/check.h"
#include "base/notreached.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/border.h"

namespace ash {

namespace {

constexpr int kFloatingMenuHeight = 64;
constexpr base::TimeDelta kAnimationDuration = base::Milliseconds(150);

// Implements scoped (RAII) activation for a FloatingAccessibilityBubbleView
// object.
class ScopedBubbleViewActivator {
 public:
  ScopedBubbleViewActivator(ash::FloatingAccessibilityBubbleView* bubble_view,
                            std::u16string accessible_name)
      : bubble_view_(bubble_view) {
    DCHECK(bubble_view_);
    bubble_view_->SetCanActivate(true);
    bubble_view_->GetViewAccessibility().SetName(accessible_name);
  }

  ScopedBubbleViewActivator(const ScopedBubbleViewActivator&) = delete;
  ScopedBubbleViewActivator& operator=(const ScopedBubbleViewActivator&) =
      delete;

  ~ScopedBubbleViewActivator() {
    bubble_view_->SetCanActivate(false);
    bubble_view_->GetViewAccessibility().SetName(std::u16string());
  }

 private:
  raw_ptr<ash::FloatingAccessibilityBubbleView> bubble_view_ = nullptr;
};

}  // namespace

FloatingAccessibilityController::FloatingAccessibilityController(
    AccessibilityController* accessibility_controller)
    : accessibility_controller_(accessibility_controller) {
  Shell::Get()->locale_update_controller()->AddObserver(this);
  accessibility_controller_->AddObserver(this);
}
FloatingAccessibilityController::~FloatingAccessibilityController() {
  Shell::Get()->locale_update_controller()->RemoveObserver(this);
  accessibility_controller_->RemoveObserver(this);
  if (bubble_widget_ && !bubble_widget_->IsClosed()) {
    bubble_widget_->CloseNow();
  }
}

void FloatingAccessibilityController::Show(FloatingMenuPosition position) {
  // Kiosk check.
  if (!Shell::Get()->session_controller()->IsRunningInAppMode()) {
    NOTREACHED()
        << "Floating accessibility menu can only be run in a kiosk session.";
  }

  DCHECK(!bubble_view_);

  TrayBubbleView::InitParams init_params;
  init_params.delegate = GetWeakPtr();
  // Our view uses SettingsBubbleContainer since it is activatable and is
  // included in the collision detection logic.
  init_params.parent_window = Shell::GetContainer(
      Shell::GetPrimaryRootWindow(), kShellWindowId_SettingBubbleContainer);
  init_params.anchor_mode = TrayBubbleView::AnchorMode::kRect;
  // 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 detailed 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 detailed view bubble's shadow.
  init_params.insets = gfx::Insets::TLBR(0, kCollisionWindowWorkAreaInsetsDp,
                                         kCollisionWindowWorkAreaInsetsDp,
                                         kCollisionWindowWorkAreaInsetsDp);
  init_params.max_height = kFloatingMenuHeight;
  init_params.translucent = true;
  init_params.close_on_deactivate = false;
  init_params.type = TrayBubbleView::TrayBubbleType::kAccessibilityBubble;
  bubble_view_ = new FloatingAccessibilityBubbleView(init_params);

  menu_view_ = new FloatingAccessibilityView(this);
  menu_view_->SetBorder(views::CreateEmptyBorder(
      gfx::Insets::TLBR(kUnifiedTopShortcutSpacing, 0, 0, 0)));
  bubble_view_->AddChildView(menu_view_.get());
  bubble_view_->SetFocusBehavior(views::View::FocusBehavior::ACCESSIBLE_ONLY);

  bubble_widget_ = views::BubbleDialogDelegateView::CreateBubble(bubble_view_);
  // Keep bubble view deactivated not to steal focus from input by clicks on
  // dictation or on-screen keyboard buttons.
  bubble_view_->SetCanActivate(false);
  TrayBackgroundView::InitializeBubbleAnimations(bubble_widget_);
  bubble_view_->InitializeAndShowBubble();
  UpdateOpacity();

  menu_view_->Initialize();

  SetMenuPosition(position);
}

void FloatingAccessibilityController::SetMenuPosition(
    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_->SetMenuPosition(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();
  }

  gfx::Rect new_bounds = GetOnScreenBoundsForFloatingMenuPosition(
      menu_view_->GetPreferredSize(), new_position);

  gfx::Rect resting_bounds =
      CollisionDetectionUtils::AdjustToFitMovementAreaByGravity(
          display::Screen::GetScreen()->GetDisplayNearestWindow(
              bubble_widget_->GetNativeWindow()),
          new_bounds);
  // 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;
  }

  ui::ScopedLayerAnimationSettings settings(
      bubble_widget_->GetLayer()->GetAnimator());
  settings.SetPreemptionStrategy(
      ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
  settings.SetTransitionDuration(kAnimationDuration);
  settings.SetTweenType(gfx::Tween::EASE_OUT);
  bubble_widget_->SetBounds(resting_bounds);

  if (detailed_menu_controller_) {
    detailed_menu_controller_->UpdateAnchorRect(
        resting_bounds, GetAnchorAlignmentForFloatingMenuPosition(position_));
  }
}

void FloatingAccessibilityController::FocusOnMenu() {
  // Temporarily activate floating accessibility bubble view when processing
  // a keyboard shortcut to allow getting focus.
  ScopedBubbleViewActivator activator(bubble_view_,
                                      GetAccessibleNameForBubble());

  bubble_view_->GetFocusManager()->ClearFocus();
  bubble_view_->GetFocusManager()->AdvanceFocus(false /* reverse */);
}

void FloatingAccessibilityController::OnDetailedMenuEnabled(bool enabled) {
  if (enabled) {
    detailed_menu_controller_ =
        std::make_unique<FloatingAccessibilityDetailedController>(this);
    gfx::Rect anchor_rect = bubble_view_->GetBoundsInScreen();
    anchor_rect.Inset(gfx::Insets::TLBR(0, -kCollisionWindowWorkAreaInsetsDp,
                                        -kCollisionWindowWorkAreaInsetsDp,
                                        -kCollisionWindowWorkAreaInsetsDp));
    detailed_menu_controller_->Show(
        anchor_rect, GetAnchorAlignmentForFloatingMenuPosition(position_));
    menu_view_->SetDetailedViewShown(true);
  } else {
    detailed_menu_controller_.reset();
    // We may need to update the autoclick bounds.
    Shell::Get()
        ->accessibility_controller()
        ->UpdateAutoclickMenuBoundsIfNeeded();
    bubble_view_->GetFocusManager()->ClearFocus();
  }
}

void FloatingAccessibilityController::OnLayoutChanged() {
  if (on_layout_change_) {
    on_layout_change_.Run();
  }
  SetMenuPosition(position_);
}

void FloatingAccessibilityController::OnFocused() {
  UpdateOpacity();
}

void FloatingAccessibilityController::OnBlurred() {
  UpdateOpacity();
}

void FloatingAccessibilityController::OnDetailedMenuClosed() {
  detailed_menu_controller_.reset();

  if (!menu_view_) {
    return;
  }
  menu_view_->SetDetailedViewShown(false);
  if (bubble_widget_->IsActive()) {
    menu_view_->FocusOnDetailedViewButton();
  }
}

views::Widget* FloatingAccessibilityController::GetBubbleWidget() {
  return bubble_widget_;
}

void FloatingAccessibilityController::BubbleViewDestroyed() {
  bubble_view_ = nullptr;
  bubble_widget_ = nullptr;
  menu_view_ = nullptr;
}

std::u16string FloatingAccessibilityController::GetAccessibleNameForBubble() {
  return l10n_util::GetStringUTF16(IDS_ASH_FLOATING_ACCESSIBILITY_MAIN_MENU);
}

void FloatingAccessibilityController::HideBubble(
    const TrayBubbleView* bubble_view) {}

void FloatingAccessibilityController::OnMouseEnteredView() {
  UpdateOpacity();
}

void FloatingAccessibilityController::OnMouseExitedView() {
  UpdateOpacity();
}

void FloatingAccessibilityController::OnLocaleChanged() {
  // Layout update is needed when language changes between LTR and RTL, if the
  // position is the system default.
  if (position_ == FloatingMenuPosition::kSystemDefault) {
    SetMenuPosition(position_);
  }
}

void FloatingAccessibilityController::OnAccessibilityStatusChanged() {
  // Some features may change the available screen area(docked magnifier), we
  // will update the location of the menu in such cases.
  SetMenuPosition(position_);
  if (detailed_menu_controller_) {
    detailed_menu_controller_->OnAccessibilityStatusChanged();
  }
}

void FloatingAccessibilityController::OnDisplayMetricsChanged(
    const display::Display& display,
    uint32_t changed_metrics) {
  SetMenuPosition(position_);
}

void FloatingAccessibilityController::UpdateOpacity() {
  const bool focus_in_children =
      bubble_view_->Contains(bubble_view_->GetFocusManager()->GetFocusedView());
  const bool is_opaque = bubble_view_->IsMouseHovered() || focus_in_children;
  bubble_view_->layer()->SetOpacity(is_opaque ? 1.0f : 0.65f);
}

}  // namespace ash