TextInputListener.h (3280B)
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 33 void SetFrame(nsIFrame* aTextControlFrame) { mFrame = aTextControlFrame; } 34 void SettingValue(bool aValue) { mSettingValue = aValue; } 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(TextEditor&); 44 45 /** 46 * OnEditActionHandled() is called when the editor handles each edit action. 47 */ 48 [[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult OnEditActionHandled(TextEditor&); 49 50 /** 51 * OnSelectionChange() is called when selection is changed in the editor. 52 */ 53 MOZ_CAN_RUN_SCRIPT 54 void OnSelectionChange(dom::Selection& aSelection, int16_t aReason); 55 56 /** 57 * Start to listen or end listening to selection change in the editor. 58 */ 59 void StartToListenToSelectionChange() { mListeningToSelectionChange = true; } 60 void EndListeningToSelectionChange() { mListeningToSelectionChange = false; } 61 62 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 63 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(TextInputListener, 64 nsIDOMEventListener) 65 NS_DECL_NSIDOMEVENTLISTENER 66 67 protected: 68 virtual ~TextInputListener() = default; 69 70 nsresult UpdateTextInputCommands(const nsAString& aCommandsToUpdate); 71 72 protected: 73 nsIFrame* mFrame; 74 TextControlElement* const mTxtCtrlElement; 75 WeakPtr<TextControlState> const mTextControlState; 76 77 bool mSelectionWasCollapsed; 78 79 /** 80 * Whether we had undo items or not the last time we got EditAction() 81 * notification (when this state changes we update undo and redo menus) 82 */ 83 bool mHadUndoItems; 84 /** 85 * Whether we had redo items or not the last time we got EditAction() 86 * notification (when this state changes we update undo and redo menus) 87 */ 88 bool mHadRedoItems; 89 /** 90 * Whether we're in the process of a SetValue call, and should therefore 91 * refrain from calling OnValueChanged. 92 */ 93 bool mSettingValue; 94 /** 95 * Whether we are in the process of a SetValue call that doesn't want 96 * |SetValueChanged| to be called. 97 */ 98 bool mSetValueChanged; 99 /** 100 * Whether we're listening to selection change in the editor. 101 */ 102 bool mListeningToSelectionChange; 103 }; 104 105 } // namespace mozilla 106 107 #endif // #ifndef mozilla_TextInputListener_h