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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
// Copyright 2021 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/annotator/annotation_tray.h"
#include "ash/annotator/annotator_controller.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/tray_background_view_catalog.h"
#include "ash/projector/projector_controller_impl.h"
#include "ash/projector/ui/projector_color_button.h"
#include "ash/public/cpp/annotator/annotator_tool.h"
#include "ash/public/cpp/shelf_config.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/shelf/shelf.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_provider.h"
#include "ash/system/tray/hover_highlight_view.h"
#include "ash/system/tray/tray_bubble_wrapper.h"
#include "ash/system/tray/tray_container.h"
#include "ash/system/tray/tray_popup_utils.h"
#include "ash/system/tray/tray_utils.h"
#include "components/prefs/pref_service.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/compositor/layer.h"
#include "ui/events/event.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/separator.h"
#include "ui/views/layout/box_layout.h"
namespace ash {
namespace {
// Margins between the title view and the edges around it (dp).
constexpr int kPaddingBetweenBottomAndLastTrayItem = 4;
// Width of the bubble itself (dp).
constexpr int kBubbleWidth = 196;
// Insets for the views (dp).
constexpr auto kPenViewPadding = gfx::Insets::TLBR(4, 16, 0, 16);
// Spacing between buttons (dp).
constexpr int kButtonsPadding = 12;
// Size of menu rows.
constexpr int kMenuRowHeight = 48;
// Color selection buttons.
constexpr int kColorButtonColorViewSize = 16;
constexpr int kColorButtonViewRadius = 28;
constexpr SkColor kPenColors[] = {
kAnnotatorMagentaPenColor, kAnnotatorBluePenColor, kAnnotatorYellowPenColor,
kAnnotatorRedPenColor};
bool IsAnnotatorEnabled() {
auto* controller = Shell::Get()->annotator_controller();
// `controller` may not be available yet as the `AnnotationTray`
// is created before it.
return controller && controller->is_annotator_enabled();
}
AnnotatorToolType GetCurrentTool() {
return IsAnnotatorEnabled() ?
AnnotatorToolType::kMarker : AnnotatorToolType::kToolNone;
}
const gfx::VectorIcon& GetIconForTool(AnnotatorToolType tool, SkColor color) {
if(tool == AnnotatorToolType::kToolNone){
return kPaletteTrayIconProjectorIcon;
}
if(tool == AnnotatorToolType::kMarker){
switch (color) {
case kAnnotatorMagentaPenColor:
return kPaletteTrayIconProjectorMagentaIcon;
case kAnnotatorBluePenColor:
return kPaletteTrayIconProjectorBlueIcon;
case kAnnotatorRedPenColor:
return kPaletteTrayIconProjectorRedIcon;
case kAnnotatorYellowPenColor:
return kPaletteTrayIconProjectorYellowIcon;
}
}
NOTREACHED();
}
} // namespace
AnnotationTray::AnnotationTray(Shelf* shelf)
: TrayBackgroundView(shelf,
TrayBackgroundViewCatalogName::kProjectorAnnotation),
image_view_(
tray_container()->AddChildView(std::make_unique<views::ImageView>())),
pen_view_(nullptr) {
SetCallback(base::BindRepeating(&AnnotationTray::OnTrayButtonPressed,
base::Unretained(this)));
// Right click should show the bubble. In tablet mode, long press is
// synonymous with right click, gesture long press must be intercepted via
// `OnGestureEvent()` override, as `views::Button` forces long press to show a
// contextual menu.
SetTriggerableEventFlags(ui::EF_LEFT_MOUSE_BUTTON |
ui::EF_RIGHT_MOUSE_BUTTON);
image_view_->SetTooltipText(GetTooltip());
image_view_->SetHorizontalAlignment(views::ImageView::Alignment::kCenter);
image_view_->SetVerticalAlignment(views::ImageView::Alignment::kCenter);
image_view_->SetPreferredSize(gfx::Size(kTrayItemSize, kTrayItemSize));
ResetTray();
UpdateAccessibleName(IsAnnotatorEnabled());
session_observer_.Observe(Shell::Get()->session_controller());
}
AnnotationTray::~AnnotationTray() = default;
void AnnotationTray::OnGestureEvent(ui::GestureEvent* event) {
// Long Press typically is used to show a contextual menu, but because in
// tablet mode tapping the pod is used to toggle a feature, long press is the
// only available way to show the bubble.
// TODO(crbug.com/40242435): Put this where we handle other button
// activations, once the `views::Button` code allows it.
if (event->details().type() != ui::EventType::kGestureLongPress) {
TrayBackgroundView::OnGestureEvent(event);
return;
}
ShowBubble();
}
void AnnotationTray::ClickedOutsideBubble(
const ui::LocatedEvent& event) {
CloseBubble();
}
void AnnotationTray::UpdateTrayItemColor(bool is_active) {
SetIconImage(is_active);
}
void AnnotationTray::HandleLocaleChange() {}
void AnnotationTray::HideBubbleWithView(
const TrayBubbleView* bubble_view) {
if (bubble_->bubble_view() == bubble_view)
CloseBubble();
}
void AnnotationTray::CloseBubbleInternal() {
pen_view_ = nullptr;
bubble_.reset();
// Annotator can be enabled after closing the bubble so set the activity state
// according to it.
SetIsActive(IsAnnotatorEnabled());
shelf()->UpdateAutoHideState();
}
void AnnotationTray::ShowBubble() {
if (bubble_)
return;
DCHECK(tray_container());
TrayBubbleView::InitParams init_params = CreateInitParamsForTrayBubble(this);
init_params.preferred_width = kBubbleWidth;
// Create and customize bubble view.
auto bubble_view = std::make_unique<TrayBubbleView>(init_params);
bubble_view->SetBorder(views::CreateEmptyBorder(
gfx::Insets::TLBR(0, 0, kPaddingBetweenBottomAndLastTrayItem, 0)));
// Add drawing tools
{
auto* marker_view_container =
bubble_view->AddChildView(std::make_unique<views::View>());
auto box_layout = std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kHorizontal, kPenViewPadding,
kButtonsPadding);
box_layout->set_cross_axis_alignment(
views::BoxLayout::CrossAxisAlignment::kCenter);
box_layout->set_minimum_cross_axis_size(kMenuRowHeight);
marker_view_container->SetLayoutManager(std::move(box_layout));
for (SkColor color : kPenColors) {
auto* color_button = marker_view_container->AddChildView(
std::make_unique<ProjectorColorButton>(
base::BindRepeating(&AnnotationTray::OnPenColorPressed,
base::Unretained(this), color),
color, kColorButtonColorViewSize, kColorButtonViewRadius,
l10n_util::GetStringUTF16(GetAccessibleNameForColor(color))));
color_button->SetToggled(current_pen_color_ == color);
}
}
// Show the bubble.
bubble_ = std::make_unique<TrayBubbleWrapper>(this);
bubble_->ShowBubble(std::move(bubble_view));
SetIsActive(true);
}
TrayBubbleView* AnnotationTray::GetBubbleView() {
return bubble_ ? bubble_->bubble_view() : nullptr;
}
views::Widget* AnnotationTray::GetBubbleWidget() const {
return bubble_ ? bubble_->GetBubbleWidget() : nullptr;
}
void AnnotationTray::OnThemeChanged() {
TrayBackgroundView::OnThemeChanged();
UpdateIcon();
}
void AnnotationTray::HideBubble(const TrayBubbleView* bubble_view) {
CloseBubble();
}
void AnnotationTray::OnActiveUserPrefServiceChanged(
PrefService* pref_service) {
const uint64_t color =
pref_service->GetUint64(prefs::kProjectorAnnotatorLastUsedMarkerColor);
current_pen_color_ = !color ? kAnnotatorDefaultPenColor : color;
}
void AnnotationTray::HideAnnotationTray() {
SetVisiblePreferred(false);
UpdateIcon();
PrefService* pref_service =
Shell::Get()->session_controller()->GetActivePrefService();
pref_service->SetUint64(prefs::kProjectorAnnotatorLastUsedMarkerColor,
current_pen_color_);
ResetTray();
}
void AnnotationTray::OnTrayButtonPressed(const ui::Event& event) {
// NOTE: Long press not supported via the `views::Button` callback, it
// is handled via OnGestureEvent override.
if (event.IsMouseEvent() && event.AsMouseEvent()->IsRightMouseButton()) {
ShowBubble();
return;
}
ToggleAnnotator();
}
void AnnotationTray::SetTrayEnabled(bool enabled) {
SetEnabled(enabled);
if (enabled)
return;
// For disabled state, set icon color to kIconColorPrimary with 30% opacity.
SkColor disabled_icon_color =
SkColorSetA(AshColorProvider::Get()->GetContentLayerColor(
AshColorProvider::ContentLayerType::kIconColorPrimary),
0x4D);
image_view_->SetImage(ui::ImageModel::FromVectorIcon(
kPaletteTrayIconProjectorIcon, disabled_icon_color));
image_view_->SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_STATUS_AREA_PROJECTOR_ANNOTATION_TRAY_UNAVAILABLE));
}
void AnnotationTray::ToggleAnnotator() {
if (GetCurrentTool() == AnnotatorToolType::kToolNone) {
EnableAnnotatorWithPenColor();
} else {
DeactivateActiveTool();
}
if (bubble_) {
CloseBubble();
}
UpdateIcon();
}
void AnnotationTray::UpdateAccessibleName(bool is_annotator_enabled) {
std::u16string enabled_state = l10n_util::GetStringUTF16(
is_annotator_enabled
? IDS_ASH_STATUS_AREA_PROJECTOR_ANNOTATION_TRAY_ON_STATE
: IDS_ASH_STATUS_AREA_PROJECTOR_ANNOTATION_TRAY_OFF_STATE);
GetViewAccessibility().SetName(l10n_util::GetStringFUTF16(
IDS_ASH_STATUS_AREA_PROJECTOR_ANNOTATION_TRAY_ACCESSIBLE_TITLE,
enabled_state));
}
void AnnotationTray::EnableAnnotatorWithPenColor() {
auto* controller = Shell::Get()->annotator_controller();
DCHECK(controller);
AnnotatorTool tool;
tool.color = current_pen_color_;
controller->SetAnnotatorTool(tool);
controller->EnableAnnotatorTool();
}
void AnnotationTray::DeactivateActiveTool() {
auto* controller = Shell::Get()->annotator_controller();
DCHECK(controller);
controller->ResetTools();
}
void AnnotationTray::UpdateIcon() {
bool annotator_toggled = false;
if (is_active() != IsAnnotatorEnabled()) {
SetIsActive(IsAnnotatorEnabled());
annotator_toggled = true;
}
// Only sets the image if the annotator was not toggled, since
// `UpdateTrayItemColor()` will be called in `SetIsActive()` to set the image
// only when active state changes.
if (!annotator_toggled) {
SetIconImage(is_active());
}
image_view_->SetTooltipText(GetTooltip());
}
void AnnotationTray::OnPenColorPressed(SkColor color) {
current_pen_color_ = color;
EnableAnnotatorWithPenColor();
CloseBubble();
UpdateIcon();
}
int AnnotationTray::GetAccessibleNameForColor(SkColor color) {
switch (color) {
case kAnnotatorRedPenColor:
return IDS_RED_COLOR_BUTTON;
case kAnnotatorBluePenColor:
return IDS_BLUE_COLOR_BUTTON;
case kAnnotatorYellowPenColor:
return IDS_YELLOW_COLOR_BUTTON;
case kAnnotatorMagentaPenColor:
return IDS_MAGENTA_COLOR_BUTTON;
}
NOTREACHED();
}
void AnnotationTray::ResetTray() {
// Disable the tray icon. It is enabled once the ink canvas is initialized.
SetEnabled(false);
}
std::u16string AnnotationTray::GetTooltip() {
std::u16string enabled_state = l10n_util::GetStringUTF16(
GetCurrentTool() == AnnotatorToolType::kToolNone
? IDS_ASH_STATUS_AREA_PROJECTOR_ANNOTATION_TRAY_OFF_STATE
: IDS_ASH_STATUS_AREA_PROJECTOR_ANNOTATION_TRAY_ON_STATE);
return l10n_util::GetStringFUTF16(
IDS_ASH_STATUS_AREA_PROJECTOR_ANNOTATION_TRAY_TOOLTIP, enabled_state);
}
void AnnotationTray::SetIconImage(bool is_active) {
image_view_->SetImage(ui::ImageModel::FromVectorIcon(
GetIconForTool(GetCurrentTool(), current_pen_color_),
is_active ? cros_tokens::kCrosSysSystemOnPrimaryContainer
: cros_tokens::kCrosSysOnSurface));
}
BEGIN_METADATA(AnnotationTray)
END_METADATA
} // namespace ash
|