The Battle for Wesnoth  1.17.21+dev
quit_confirmation.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2015 - 2023
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include <cassert>
18 #include <vector>
19 #include <string>
20 
21 #include <functional>
22 
23 /**
24  * Implements a quit confirmation dialog.
25  *
26  * Any object of this type will prevent the game from quitting immediately.
27  * Instead, a confirmation dialog will pop up when attempting to close.
28  */
30 {
31 public:
32  explicit quit_confirmation(const std::function<bool()>& prompt = &quit_confirmation::default_prompt)
33  : prompt_(prompt) { blockers_.push_back(this); }
34 
35  ~quit_confirmation() { blockers_.pop_back(); }
36 
37  /**
38  * Shows the quit confirmation if needed.
39  *
40  * @throws video::quit If the user chooses to quit or no prompt was
41  * displayed.
42  */
43  static bool quit();
44  static void quit_to_title();
45  static void quit_to_desktop();
46 
47  static bool show_prompt(const std::string& message);
48  static bool default_prompt();
49 
50 private:
51  // noncopyable
54 
55  static inline std::vector<quit_confirmation*> blockers_ {};
56  static inline bool open_ = false;
57 
58  std::function<bool()> prompt_;
59 };
Implements a quit confirmation dialog.
static bool default_prompt()
static bool show_prompt(const std::string &message)
quit_confirmation(const std::function< bool()> &prompt=&quit_confirmation::default_prompt)
static std::vector< quit_confirmation * > blockers_
static bool quit()
Shows the quit confirmation if needed.
quit_confirmation(const quit_confirmation &)=delete
static void quit_to_desktop()
const quit_confirmation & operator=(const quit_confirmation &)=delete
std::function< bool()> prompt_
static void quit_to_title()