The Battle for Wesnoth  1.17.12+dev
quit_confirmation.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2015 - 2022
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
52  quit_confirmation(const quit_confirmation&) = delete;
53  const quit_confirmation& operator=(const quit_confirmation&) = delete;
54 
55  static inline std::vector<quit_confirmation*> blockers_ {};
56  static inline bool open_ = false;
57 
58  std::function<bool()> prompt_;
59 };
static void quit_to_title()
static bool show_prompt(const std::string &message)
static std::vector< quit_confirmation * > blockers_
Implements a quit confirmation dialog.
quit_confirmation(const std::function< bool()> &prompt=&quit_confirmation::default_prompt)
const quit_confirmation & operator=(const quit_confirmation &)=delete
static bool quit()
Shows the quit confirmation if needed.
static void quit_to_desktop()
static bool default_prompt()
std::function< bool()> prompt_