summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/editing/suggestion/text_suggestion_controller.h
blob: 61702afe3e0a25d67e9696d3c2198936f97934f0 (plain)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_SUGGESTION_TEXT_SUGGESTION_CONTROLLER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_SUGGESTION_TEXT_SUGGESTION_CONTROLLER_H_

#include <utility>
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/blink/public/mojom/input/input_host.mojom-blink.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/document_shutdown_observer.h"
#include "third_party/blink/renderer/core/editing/forward.h"
#include "third_party/blink/renderer/core/editing/markers/document_marker.h"
#include "third_party/blink/renderer/platform/heap/handle.h"

namespace blink {

class Document;
class DocumentMarker;
class LocalFrame;
struct TextSuggestionInfo;

// This class handles functionality related to displaying a menu of text
// suggestions (e.g. from spellcheck), and performing actions relating to those
// suggestions. Android is currently the only platform that has such a menu.
class CORE_EXPORT TextSuggestionController final
    : public GarbageCollected<TextSuggestionController>,
      public DocumentShutdownObserver {
  USING_GARBAGE_COLLECTED_MIXIN(TextSuggestionController);

 public:
  explicit TextSuggestionController(LocalFrame&);

  void DidAttachDocument(Document*);

  bool IsMenuOpen() const;

  void HandlePotentialSuggestionTap(const PositionInFlatTree& caret_position);

  void ApplySpellCheckSuggestion(const String& suggestion);
  void ApplyTextSuggestion(int32_t marker_tag, uint32_t suggestion_index);
  void DeleteActiveSuggestionRange();
  void OnNewWordAddedToDictionary(const String& word);
  void OnSuggestionMenuClosed();
  void SuggestionMenuTimeoutCallback(size_t max_number_of_suggestions);

  void Trace(Visitor*) override;

 private:
  friend class TextSuggestionControllerTest;
  Document& GetDocument() const;
  bool IsAvailable() const;
  LocalFrame& GetFrame() const;

  std::pair<const Node*, const DocumentMarker*> FirstMarkerIntersectingRange(
      const EphemeralRangeInFlatTree&,
      DocumentMarker::MarkerTypes) const;
  std::pair<const Node*, const DocumentMarker*> FirstMarkerTouchingSelection(
      DocumentMarker::MarkerTypes) const;

  void AttemptToDeleteActiveSuggestionRange();
  void CallMojoShowTextSuggestionMenu(
      const Vector<TextSuggestionInfo>& text_suggestion_infos,
      const String& misspelled_word);
  void ShowSpellCheckMenu(
      const std::pair<const Text*, DocumentMarker*>& node_spelling_marker_pair);
  void ShowSuggestionMenu(
      const HeapVector<std::pair<Member<const Text>, Member<DocumentMarker>>>&
          node_suggestion_marker_pairs,
      size_t max_number_of_suggestions);
  void ReplaceActiveSuggestionRange(const String&);
  void ReplaceRangeWithText(const EphemeralRange&, const String& replacement);

  bool is_suggestion_menu_open_;
  const Member<LocalFrame> frame_;
  mojo::Remote<mojom::blink::TextSuggestionHost> text_suggestion_host_;

  DISALLOW_COPY_AND_ASSIGN(TextSuggestionController);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_SUGGESTION_TEXT_SUGGESTION_CONTROLLER_H_