1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_TextInputListener_h
8 #define mozilla_TextInputListener_h
9 
10 #include "mozilla/WeakPtr.h"
11 #include "nsCycleCollectionParticipant.h"
12 #include "nsIDOMEventListener.h"
13 #include "nsStringFwd.h"
14 #include "nsWeakReference.h"
15 
16 class nsIFrame;
17 class nsTextControlFrame;
18 
19 namespace mozilla {
20 class TextControlElement;
21 class TextControlState;
22 class TextEditor;
23 
24 namespace dom {
25 class Selection;
26 }  // namespace dom
27 
28 class TextInputListener final : public nsIDOMEventListener,
29                                 public nsSupportsWeakReference {
30  public:
31   explicit TextInputListener(TextControlElement* aTextControlElement);
32 
SetFrame(nsIFrame * aTextControlFrame)33   void SetFrame(nsIFrame* aTextControlFrame) { mFrame = aTextControlFrame; }
SettingValue(bool aValue)34   void SettingValue(bool aValue) { mSettingValue = aValue; }
SetValueChanged(bool aSetValueChanged)35   void SetValueChanged(bool aSetValueChanged) {
36     mSetValueChanged = aSetValueChanged;
37   }
38 
39   /**
40    * aFrame is an optional pointer to our frame, if not passed the method will
41    * use mFrame to compute it lazily.
42    */
43   void HandleValueChanged();
44 
45   /**
46    * OnEditActionHandled() is called when the editor handles each edit action.
47    */
48   [[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult
49   OnEditActionHandled(TextEditor& aTextEditor);
50 
51   /**
52    * OnSelectionChange() is called when selection is changed in the editor.
53    */
54   MOZ_CAN_RUN_SCRIPT
55   void OnSelectionChange(dom::Selection& aSelection, int16_t aReason);
56 
57   /**
58    * Start to listen or end listening to selection change in the editor.
59    */
StartToListenToSelectionChange()60   void StartToListenToSelectionChange() { mListeningToSelectionChange = true; }
EndListeningToSelectionChange()61   void EndListeningToSelectionChange() { mListeningToSelectionChange = false; }
62 
63   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
64   NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(TextInputListener,
65                                            nsIDOMEventListener)
66   NS_DECL_NSIDOMEVENTLISTENER
67 
68  protected:
69   virtual ~TextInputListener() = default;
70 
71   nsresult UpdateTextInputCommands(const nsAString& aCommandsToUpdate,
72                                    dom::Selection* aSelection = nullptr,
73                                    int16_t aReason = 0);
74 
75  protected:
76   nsIFrame* mFrame;
77   TextControlElement* const mTxtCtrlElement;
78   WeakPtr<TextControlState> const mTextControlState;
79 
80   bool mSelectionWasCollapsed;
81 
82   /**
83    * Whether we had undo items or not the last time we got EditAction()
84    * notification (when this state changes we update undo and redo menus)
85    */
86   bool mHadUndoItems;
87   /**
88    * Whether we had redo items or not the last time we got EditAction()
89    * notification (when this state changes we update undo and redo menus)
90    */
91   bool mHadRedoItems;
92   /**
93    * Whether we're in the process of a SetValue call, and should therefore
94    * refrain from calling OnValueChanged.
95    */
96   bool mSettingValue;
97   /**
98    * Whether we are in the process of a SetValue call that doesn't want
99    * |SetValueChanged| to be called.
100    */
101   bool mSetValueChanged;
102   /**
103    * Whether we're listening to selection change in the editor.
104    */
105   bool mListeningToSelectionChange;
106 };
107 
108 }  // namespace mozilla
109 
110 #endif  // #ifndef mozilla_TextInputListener_h
111