The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
quit_confirmation.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2015 - 2025
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 <vector>
18#include <string>
19
20#include <functional>
21
22/**
23 * Implements a quit confirmation dialog.
24 *
25 * Any object of this type will prevent the game from quitting immediately.
26 * Instead, a confirmation dialog will pop up when attempting to close.
27 */
29{
30public:
31 explicit quit_confirmation(const std::function<bool()>& prompt = &quit_confirmation::default_prompt)
32 : prompt_(prompt) { blockers_.push_back(this); }
33
34 ~quit_confirmation() { blockers_.pop_back(); }
35
36 /**
37 * Shows the quit confirmation if needed.
38 *
39 * @throws video::quit If the user chooses to quit or no prompt was
40 * displayed.
41 */
42 static bool quit();
43 static void quit_to_title();
44 static void quit_to_desktop();
45
46 static bool show_prompt(const std::string& message);
47 static bool default_prompt();
48
49private:
50 // noncopyable
53
54 static inline std::vector<quit_confirmation*> blockers_ {};
55 static inline bool open_ = false;
56
57 std::function<bool()> prompt_;
58};
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()