autofill_popup_view.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) 2012 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. #ifndef ATOM_BROWSER_UI_VIEWS_AUTOFILL_POPUP_VIEW_H_
  5. #define ATOM_BROWSER_UI_VIEWS_AUTOFILL_POPUP_VIEW_H_
  6. #include "atom/browser/ui/autofill_popup.h"
  7. #if defined(ENABLE_OSR)
  8. #include "atom/browser/osr/osr_view_proxy.h"
  9. #endif
  10. #include "base/optional.h"
  11. #include "content/public/browser/native_web_keyboard_event.h"
  12. #include "content/public/browser/render_widget_host.h"
  13. #include "ui/accessibility/ax_node_data.h"
  14. #include "ui/views/drag_controller.h"
  15. #include "ui/views/focus/widget_focus_manager.h"
  16. #include "ui/views/widget/widget_delegate.h"
  17. #include "ui/views/widget/widget_observer.h"
  18. namespace atom {
  19. const int kPopupBorderThickness = 1;
  20. const int kSmallerFontSizeDelta = -1;
  21. const int kEndPadding = 8;
  22. const int kNamePadding = 15;
  23. const int kRowHeight = 24;
  24. class AutofillPopup;
  25. // Child view only for triggering accessibility events. Rendering is handled
  26. // by |AutofillPopupViewViews|.
  27. class AutofillPopupChildView : public views::View {
  28. public:
  29. explicit AutofillPopupChildView(const base::string16& suggestion)
  30. : suggestion_(suggestion) {
  31. SetFocusBehavior(FocusBehavior::ALWAYS);
  32. }
  33. private:
  34. ~AutofillPopupChildView() override {}
  35. // views::Views implementation
  36. void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
  37. base::string16 suggestion_;
  38. DISALLOW_COPY_AND_ASSIGN(AutofillPopupChildView);
  39. };
  40. class AutofillPopupView : public views::WidgetDelegateView,
  41. public views::WidgetFocusChangeListener,
  42. public views::WidgetObserver,
  43. public views::DragController {
  44. public:
  45. explicit AutofillPopupView(AutofillPopup* popup,
  46. views::Widget* parent_widget);
  47. ~AutofillPopupView() override;
  48. void Show();
  49. void Hide();
  50. void OnSuggestionsChanged();
  51. int GetSelectedLine() { return selected_line_.value_or(-1); }
  52. void WriteDragDataForView(views::View*,
  53. const gfx::Point&,
  54. ui::OSExchangeData*) override;
  55. int GetDragOperationsForView(views::View*, const gfx::Point&) override;
  56. bool CanStartDragForView(views::View*,
  57. const gfx::Point&,
  58. const gfx::Point&) override;
  59. private:
  60. friend class AutofillPopup;
  61. void OnSelectedRowChanged(base::Optional<int> previous_row_selection,
  62. base::Optional<int> current_row_selection);
  63. // Draw the given autofill entry in |entry_rect|.
  64. void DrawAutofillEntry(gfx::Canvas* canvas,
  65. int index,
  66. const gfx::Rect& entry_rect);
  67. // Creates child views based on the suggestions given by |controller_|. These
  68. // child views are used for accessibility events only. We need child views to
  69. // populate the correct |AXNodeData| when user selects a suggestion.
  70. void CreateChildViews();
  71. void DoUpdateBoundsAndRedrawPopup();
  72. // views::Views implementation.
  73. void OnPaint(gfx::Canvas* canvas) override;
  74. void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
  75. void OnMouseCaptureLost() override;
  76. bool OnMouseDragged(const ui::MouseEvent& event) override;
  77. void OnMouseExited(const ui::MouseEvent& event) override;
  78. void OnMouseMoved(const ui::MouseEvent& event) override;
  79. bool OnMousePressed(const ui::MouseEvent& event) override;
  80. void OnMouseReleased(const ui::MouseEvent& event) override;
  81. void OnGestureEvent(ui::GestureEvent* event) override;
  82. bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
  83. bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event);
  84. // views::WidgetFocusChangeListener implementation.
  85. void OnNativeFocusChanged(gfx::NativeView focused_now) override;
  86. // views::WidgetObserver implementation.
  87. void OnWidgetBoundsChanged(views::Widget* widget,
  88. const gfx::Rect& new_bounds) override;
  89. void AcceptSuggestion(int index);
  90. bool AcceptSelectedLine();
  91. void AcceptSelection(const gfx::Point& point);
  92. void SetSelectedLine(base::Optional<int> selected_line);
  93. void SetSelection(const gfx::Point& point);
  94. void SelectNextLine();
  95. void SelectPreviousLine();
  96. void ClearSelection();
  97. // Stop observing the widget.
  98. void RemoveObserver();
  99. // Controller for this popup. Weak reference.
  100. AutofillPopup* popup_;
  101. // The widget of the window that triggered this popup. Weak reference.
  102. views::Widget* parent_widget_;
  103. // The time when the popup was shown.
  104. base::Time show_time_;
  105. // The index of the currently selected line
  106. base::Optional<int> selected_line_;
  107. #if defined(ENABLE_OSR)
  108. std::unique_ptr<OffscreenViewProxy> view_proxy_;
  109. #endif
  110. // The registered keypress callback, responsible for switching lines on
  111. // key presses
  112. content::RenderWidgetHost::KeyPressEventCallback keypress_callback_;
  113. base::WeakPtrFactory<AutofillPopupView> weak_ptr_factory_;
  114. };
  115. } // namespace atom
  116. #endif // ATOM_BROWSER_UI_VIEWS_AUTOFILL_POPUP_VIEW_H_