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"
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
;
20 class nsITransactionManager
;
21 class nsPIDOMWindowOuter
;
25 class TransactionManager
;
27 class ComposerCommandsUpdater final
: public nsITimerCallback
, public nsINamed
{
29 ComposerCommandsUpdater();
32 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
33 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(ComposerCommandsUpdater
,
37 NS_DECL_NSITIMERCALLBACK
42 void Init(nsPIDOMWindowOuter
& aDOMWindow
);
45 * OnSelectionChange() is called when selection is changed in the editor.
47 void OnSelectionChange() { PrimeUpdateTimer(); }
50 * OnHTMLEditorCreated() is called when `HTMLEditor` is created and
53 MOZ_CAN_RUN_SCRIPT
void OnHTMLEditorCreated() {
54 UpdateOneCommand("obs_documentCreated");
58 * OnBeforeHTMLEditorDestroyed() is called when `HTMLEditor` is being
61 MOZ_CAN_RUN_SCRIPT
void OnBeforeHTMLEditorDestroyed() {
62 // cancel any outstanding update timer
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).
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
)) {
81 UpdateCommandGroup(CommandGroup::Save
);
82 UpdateCommandGroup(CommandGroup::Undo
);
83 mDirtyState
= aNowDirty
;
87 * The following methods are called when aTransactionManager did
88 * `DoTransaction`, `UndoTransaction` or `RedoTransaction` of a transaction
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
);
99 virtual ~ComposerCommandsUpdater();
102 eStateUninitialized
= -1,
107 bool SelectionIsCollapsed();
108 MOZ_CAN_RUN_SCRIPT nsresult
UpdateOneCommand(const char* aCommand
);
109 enum class CommandGroup
{
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
;
126 int8_t mSelectionCollapsed
;
127 bool mFirstDoOfFirstUndo
;
130 } // namespace mozilla
132 #endif // #ifndef mozilla_ComposerCommandsUpdater_h