ComposerCommandsUpdater.h (3702B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * 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_ComposerCommandsUpdater_h 8 #define mozilla_ComposerCommandsUpdater_h 9 10 #include "nsCOMPtr.h" // for already_AddRefed, nsCOMPtr 11 #include "nsCycleCollectionParticipant.h" 12 #include "nsINamed.h" 13 #include "nsISupportsImpl.h" // for NS_DECL_ISUPPORTS 14 #include "nsITimer.h" // for NS_DECL_NSITIMERCALLBACK, etc 15 #include "nscore.h" // for NS_IMETHOD, nsresult, etc 16 17 class nsCommandManager; 18 class nsIDocShell; 19 class nsITransaction; 20 class nsITransactionManager; 21 class nsPIDOMWindowOuter; 22 23 namespace mozilla { 24 25 class TransactionManager; 26 27 class ComposerCommandsUpdater final : public nsITimerCallback, public nsINamed { 28 public: 29 ComposerCommandsUpdater(); 30 31 // nsISupports 32 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 33 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(ComposerCommandsUpdater, 34 nsITimerCallback) 35 36 // nsITimerCallback 37 NS_DECL_NSITIMERCALLBACK 38 39 // nsINamed 40 NS_DECL_NSINAMED 41 42 void Init(nsPIDOMWindowOuter& aDOMWindow); 43 44 /** 45 * OnSelectionChange() is called when selection is changed in the editor. 46 */ 47 void OnSelectionChange() { PrimeUpdateTimer(); } 48 49 /** 50 * OnHTMLEditorCreated() is called when `HTMLEditor` is created and 51 * initialized. 52 */ 53 MOZ_CAN_RUN_SCRIPT void OnHTMLEditorCreated() { 54 UpdateOneCommand("obs_documentCreated"); 55 } 56 57 /** 58 * OnBeforeHTMLEditorDestroyed() is called when `HTMLEditor` is being 59 * destroyed. 60 */ 61 MOZ_CAN_RUN_SCRIPT void OnBeforeHTMLEditorDestroyed() { 62 // cancel any outstanding update timer 63 if (mUpdateTimer) { 64 mUpdateTimer->Cancel(); 65 mUpdateTimer = nullptr; 66 } 67 68 // We can't notify the command manager of this right now; it is too late in 69 // some cases and the window is already partially destructed (e.g. JS 70 // objects may be gone). 71 } 72 73 /** 74 * OnHTMLEditorDirtyStateChanged() is called when dirty state of `HTMLEditor` 75 * is changed form or to "dirty". 76 */ 77 MOZ_CAN_RUN_SCRIPT void OnHTMLEditorDirtyStateChanged(bool aNowDirty) { 78 if (mDirtyState == static_cast<int8_t>(aNowDirty)) { 79 return; 80 } 81 UpdateCommandGroup(CommandGroup::Save); 82 UpdateCommandGroup(CommandGroup::Undo); 83 mDirtyState = aNowDirty; 84 } 85 86 /** 87 * The following methods are called when aTransactionManager did 88 * `DoTransaction`, `UndoTransaction` or `RedoTransaction` of a transaction 89 * instance. 90 */ 91 MOZ_CAN_RUN_SCRIPT void DidDoTransaction( 92 TransactionManager& aTransactionManager); 93 MOZ_CAN_RUN_SCRIPT void DidUndoTransaction( 94 TransactionManager& aTransactionManager); 95 MOZ_CAN_RUN_SCRIPT void DidRedoTransaction( 96 TransactionManager& aTransactionManager); 97 98 protected: 99 virtual ~ComposerCommandsUpdater(); 100 101 enum { 102 eStateUninitialized = -1, 103 eStateOff = 0, 104 eStateOn = 1, 105 }; 106 107 bool SelectionIsCollapsed(); 108 MOZ_CAN_RUN_SCRIPT nsresult UpdateOneCommand(const char* aCommand); 109 enum class CommandGroup { 110 Save, 111 Style, 112 Undo, 113 }; 114 MOZ_CAN_RUN_SCRIPT void UpdateCommandGroup(CommandGroup aCommandGroup); 115 116 nsCommandManager* GetCommandManager(); 117 118 nsresult PrimeUpdateTimer(); 119 void TimerCallback(); 120 121 nsCOMPtr<nsITimer> mUpdateTimer; 122 nsCOMPtr<nsPIDOMWindowOuter> mDOMWindow; 123 nsCOMPtr<nsIDocShell> mDocShell; 124 125 int8_t mDirtyState; 126 int8_t mSelectionCollapsed; 127 bool mFirstDoOfFirstUndo; 128 }; 129 130 } // namespace mozilla 131 132 #endif // #ifndef mozilla_ComposerCommandsUpdater_h