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 "nsITransactionListener.h" // for nsITransactionListener
16 #include "nscore.h" // for NS_IMETHOD, nsresult, etc
18 class nsCommandManager
;
21 class nsITransactionManager
;
22 class nsPIDOMWindowOuter
;
26 class ComposerCommandsUpdater final
: public nsITransactionListener
,
27 public nsITimerCallback
,
30 ComposerCommandsUpdater();
33 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
34 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(ComposerCommandsUpdater
,
35 nsITransactionListener
)
38 NS_DECL_NSITIMERCALLBACK
43 // nsITransactionListener
44 NS_DECL_NSITRANSACTIONLISTENER
46 void Init(nsPIDOMWindowOuter
& aDOMWindow
);
49 * OnSelectionChange() is called when selection is changed in the editor.
51 void OnSelectionChange() { PrimeUpdateTimer(); }
54 * OnHTMLEditorCreated() is called when `HTMLEditor` is created and
57 MOZ_CAN_RUN_SCRIPT
void OnHTMLEditorCreated() {
58 UpdateOneCommand("obs_documentCreated");
62 * OnBeforeHTMLEditorDestroyed() is called when `HTMLEditor` is being
65 MOZ_CAN_RUN_SCRIPT
void OnBeforeHTMLEditorDestroyed() {
66 // cancel any outstanding update timer
68 mUpdateTimer
->Cancel();
69 mUpdateTimer
= nullptr;
72 // We can't notify the command manager of this right now; it is too late in
73 // some cases and the window is already partially destructed (e.g. JS
74 // objects may be gone).
78 * OnHTMLEditorDirtyStateChanged() is called when dirty state of `HTMLEditor`
79 * is changed form or to "dirty".
81 MOZ_CAN_RUN_SCRIPT
void OnHTMLEditorDirtyStateChanged(bool aNowDirty
) {
82 if (mDirtyState
== static_cast<int8_t>(aNowDirty
)) {
85 UpdateCommandGroup(CommandGroup::Save
);
86 UpdateCommandGroup(CommandGroup::Undo
);
87 mDirtyState
= aNowDirty
;
91 virtual ~ComposerCommandsUpdater();
94 eStateUninitialized
= -1,
99 bool SelectionIsCollapsed();
100 MOZ_CAN_RUN_SCRIPT nsresult
UpdateOneCommand(const char* aCommand
);
101 enum class CommandGroup
{
106 MOZ_CAN_RUN_SCRIPT
void UpdateCommandGroup(CommandGroup aCommandGroup
);
108 nsCommandManager
* GetCommandManager();
110 nsresult
PrimeUpdateTimer();
111 void TimerCallback();
113 nsCOMPtr
<nsITimer
> mUpdateTimer
;
114 nsCOMPtr
<nsPIDOMWindowOuter
> mDOMWindow
;
115 nsCOMPtr
<nsIDocShell
> mDocShell
;
118 int8_t mSelectionCollapsed
;
119 bool mFirstDoOfFirstUndo
;
122 } // namespace mozilla
124 #endif // #ifndef mozilla_ComposerCommandsUpdater_h