ime_mode_indicator_view.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/ime/ime_mode_indicator_view.h"
  5. #include "ash/public/cpp/shell_window_ids.h"
  6. #include "ash/shell.h"
  7. #include "ash/wm/window_util.h"
  8. #include "base/logging.h"
  9. #include "ui/base/metadata/metadata_impl_macros.h"
  10. #include "ui/display/display.h"
  11. #include "ui/display/screen.h"
  12. #include "ui/views/bubble/bubble_frame_view.h"
  13. #include "ui/views/controls/label.h"
  14. #include "ui/views/layout/fill_layout.h"
  15. #include "ui/wm/core/window_animations.h"
  16. namespace ash {
  17. namespace {
  18. // Minimum size of inner contents in pixel.
  19. // 43 is the designed size including the default margin (6 * 2).
  20. const int kMinSize = 31;
  21. // After this duration in msec, the mode inicator will be fading out.
  22. const int kShowingDuration = 500;
  23. class ModeIndicatorFrameView : public views::BubbleFrameView {
  24. public:
  25. METADATA_HEADER(ModeIndicatorFrameView);
  26. explicit ModeIndicatorFrameView()
  27. : views::BubbleFrameView(gfx::Insets(), gfx::Insets()) {}
  28. ModeIndicatorFrameView(const ModeIndicatorFrameView&) = delete;
  29. ModeIndicatorFrameView& operator=(const ModeIndicatorFrameView&) = delete;
  30. ~ModeIndicatorFrameView() override {}
  31. private:
  32. // views::BubbleFrameView overrides:
  33. gfx::Rect GetAvailableScreenBounds(const gfx::Rect& rect) const override {
  34. return display::Screen::GetScreen()
  35. ->GetDisplayNearestPoint(rect.CenterPoint())
  36. .bounds();
  37. }
  38. };
  39. BEGIN_METADATA(ModeIndicatorFrameView, views::BubbleFrameView)
  40. END_METADATA
  41. } // namespace
  42. ImeModeIndicatorView::ImeModeIndicatorView(const gfx::Rect& cursor_bounds,
  43. const std::u16string& label)
  44. : cursor_bounds_(cursor_bounds), label_view_(new views::Label(label)) {
  45. SetButtons(ui::DIALOG_BUTTON_NONE);
  46. SetCanActivate(false);
  47. set_accept_events(false);
  48. set_shadow(views::BubbleBorder::STANDARD_SHADOW);
  49. SetArrow(views::BubbleBorder::TOP_CENTER);
  50. // Ignore this view for accessibility purposes.
  51. SetAccessibleRole(ax::mojom::Role::kNone);
  52. }
  53. ImeModeIndicatorView::~ImeModeIndicatorView() = default;
  54. void ImeModeIndicatorView::ShowAndFadeOut() {
  55. ::wm::SetWindowVisibilityAnimationTransition(GetWidget()->GetNativeView(),
  56. ::wm::ANIMATE_HIDE);
  57. GetWidget()->Show();
  58. timer_.Start(FROM_HERE, base::Milliseconds(kShowingDuration), GetWidget(),
  59. &views::Widget::Close);
  60. }
  61. void ImeModeIndicatorView::OnBeforeBubbleWidgetInit(
  62. views::Widget::InitParams* params,
  63. views::Widget* widget) const {
  64. aura::Window* window = window_util::GetActiveWindow();
  65. if (window) { // Null check for tests.
  66. params->parent = Shell::GetContainer(window->GetRootWindow(),
  67. kShellWindowId_SettingBubbleContainer);
  68. } else {
  69. params->parent = Shell::GetPrimaryRootWindow();
  70. }
  71. }
  72. gfx::Size ImeModeIndicatorView::CalculatePreferredSize() const {
  73. gfx::Size size = label_view_->GetPreferredSize();
  74. size.SetToMax(gfx::Size(kMinSize, kMinSize));
  75. return size;
  76. }
  77. void ImeModeIndicatorView::Init() {
  78. SetLayoutManager(std::make_unique<views::FillLayout>());
  79. AddChildView(label_view_);
  80. SetAnchorRect(cursor_bounds_);
  81. }
  82. std::unique_ptr<views::NonClientFrameView>
  83. ImeModeIndicatorView::CreateNonClientFrameView(views::Widget* widget) {
  84. auto frame = std::make_unique<ModeIndicatorFrameView>();
  85. // arrow adjustment in BubbleDialogDelegateView is unnecessary because arrow
  86. // of this bubble is always center.
  87. auto border = std::make_unique<views::BubbleBorder>(arrow(), GetShadow());
  88. border->SetColor(color());
  89. frame->SetBubbleBorder(std::move(border));
  90. return frame;
  91. }
  92. BEGIN_METADATA(ImeModeIndicatorView, views::BubbleDialogDelegateView)
  93. END_METADATA
  94. } // namespace ash