autofill_popup_view.h 5.2 KB

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