Backed out changeset f85447f6f56d (bug 1891145) for causing mochitest failures @...
[gecko.git] / editor / composer / ComposerCommandsUpdater.h
blob6cf0886c5720b5103e5332148d9b0ff975f81636
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */
7 #ifndef mozilla_ComposerCommandsUpdater_h
8 #define mozilla_ComposerCommandsUpdater_h
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
17 class nsCommandManager;
18 class nsIDocShell;
19 class nsITransaction;
20 class nsITransactionManager;
21 class nsPIDOMWindowOuter;
23 namespace mozilla {
25 class TransactionManager;
27 class ComposerCommandsUpdater final : public nsITimerCallback, public nsINamed {
28 public:
29 ComposerCommandsUpdater();
31 // nsISupports
32 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
33 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(ComposerCommandsUpdater,
34 nsITimerCallback)
36 // nsITimerCallback
37 NS_DECL_NSITIMERCALLBACK
39 // nsINamed
40 NS_DECL_NSINAMED
42 void Init(nsPIDOMWindowOuter& aDOMWindow);
44 /**
45 * OnSelectionChange() is called when selection is changed in the editor.
47 void OnSelectionChange() { PrimeUpdateTimer(); }
49 /**
50 * OnHTMLEditorCreated() is called when `HTMLEditor` is created and
51 * initialized.
53 MOZ_CAN_RUN_SCRIPT void OnHTMLEditorCreated() {
54 UpdateOneCommand("obs_documentCreated");
57 /**
58 * OnBeforeHTMLEditorDestroyed() is called when `HTMLEditor` is being
59 * destroyed.
61 MOZ_CAN_RUN_SCRIPT void OnBeforeHTMLEditorDestroyed() {
62 // cancel any outstanding update timer
63 if (mUpdateTimer) {
64 mUpdateTimer->Cancel();
65 mUpdateTimer = nullptr;
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).
73 /**
74 * OnHTMLEditorDirtyStateChanged() is called when dirty state of `HTMLEditor`
75 * is changed form or to "dirty".
77 MOZ_CAN_RUN_SCRIPT void OnHTMLEditorDirtyStateChanged(bool aNowDirty) {
78 if (mDirtyState == static_cast<int8_t>(aNowDirty)) {
79 return;
81 UpdateCommandGroup(CommandGroup::Save);
82 UpdateCommandGroup(CommandGroup::Undo);
83 mDirtyState = aNowDirty;
86 /**
87 * The following methods are called when aTransactionManager did
88 * `DoTransaction`, `UndoTransaction` or `RedoTransaction` of a transaction
89 * instance.
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);
98 protected:
99 virtual ~ComposerCommandsUpdater();
101 enum {
102 eStateUninitialized = -1,
103 eStateOff = 0,
104 eStateOn = 1,
107 bool SelectionIsCollapsed();
108 MOZ_CAN_RUN_SCRIPT nsresult UpdateOneCommand(const char* aCommand);
109 enum class CommandGroup {
110 Save,
111 Style,
112 Undo,
114 MOZ_CAN_RUN_SCRIPT void UpdateCommandGroup(CommandGroup aCommandGroup);
116 nsCommandManager* GetCommandManager();
118 nsresult PrimeUpdateTimer();
119 void TimerCallback();
121 nsCOMPtr<nsITimer> mUpdateTimer;
122 nsCOMPtr<nsPIDOMWindowOuter> mDOMWindow;
123 nsCOMPtr<nsIDocShell> mDocShell;
125 int8_t mDirtyState;
126 int8_t mSelectionCollapsed;
127 bool mFirstDoOfFirstUndo;
130 } // namespace mozilla
132 #endif // #ifndef mozilla_ComposerCommandsUpdater_h