AutoSelectionRestorer.h (1580B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef mozilla_AutoSelectionRestorer_h__ 7 #define mozilla_AutoSelectionRestorer_h__ 8 9 #include "EditorBase.h" 10 11 namespace mozilla { 12 13 /** 14 * Stack based helper class for saving/restoring selection. Note that this 15 * assumes that the nodes involved are still around afterwords! 16 */ 17 class AutoSelectionRestorer final { 18 public: 19 AutoSelectionRestorer() = delete; 20 explicit AutoSelectionRestorer(const AutoSelectionRestorer& aOther) = delete; 21 AutoSelectionRestorer(AutoSelectionRestorer&& aOther) = delete; 22 23 /** 24 * Constructor responsible for remembering all state needed to restore 25 * aSelection. 26 * XXX This constructor and the destructor should be marked as 27 * `MOZ_CAN_RUN_SCRIPT`, but it's impossible due to this may be used 28 * with `Maybe`. 29 */ 30 MOZ_CAN_RUN_SCRIPT_BOUNDARY explicit AutoSelectionRestorer( 31 EditorBase* aEditor); 32 33 /** 34 * Destructor restores mSelection to its former state 35 */ 36 MOZ_CAN_RUN_SCRIPT_BOUNDARY ~AutoSelectionRestorer(); 37 38 /** 39 * Abort() cancels to restore the selection. 40 */ 41 void Abort(); 42 43 bool MaybeRestoreSelectionLater() const { return !!mEditor; } 44 45 protected: 46 // The lifetime must be guaranteed by the creator of this instance. 47 MOZ_KNOWN_LIVE EditorBase* mEditor = nullptr; 48 }; 49 } // namespace mozilla 50 51 #endif