gui/preferences: Make Themes selection a dropdown menu

Note that this doesn't remove the themes dialog functionality, since
it is currently used by the :theme command in the game console.
This commit is contained in:
Iris Morelle 2023-05-23 01:51:16 -04:00
parent 1fb83c3c7a
commit f7c879a171
No known key found for this signature in database
GPG key ID: BB9666228F278524
3 changed files with 62 additions and 7 deletions

View file

@ -375,11 +375,21 @@
border_size = 5
horizontal_alignment = "left"
[button]
id = choose_theme
label = _ "Theme"
[label]
label = _ "Theme:"
definition = "default_small"
[/label]
[/column]
[column]
border = "all"
border_size = 5
horizontal_alignment = "left"
[menu_button]
id = "choose_theme"
tooltip = _ "Change the in-game theme"
[/button]
[/menu_button]
[/column]
[/row]
#enddef

View file

@ -23,6 +23,7 @@
#include "filesystem.hpp"
#include "formatter.hpp"
#include "formula/string_utils.hpp"
#include "game_data.hpp"
#include "gettext.hpp"
#include "hotkey/hotkey_item.hpp"
#include "lexical_cast.hpp"
@ -31,6 +32,8 @@
#include "preferences/game.hpp"
#include "preferences/general.hpp"
#include "preferences/lobby.hpp"
#include "resources.hpp"
#include "theme.hpp"
#include "video.hpp"
// Sub-dialog includes
@ -114,6 +117,7 @@ preferences_dialog::preferences_dialog(const PREFERENCE_VIEW initial_view)
: modal_dialog(window_id())
, adv_preferences_(preferences::get_advanced_preferences())
, resolutions_() // should be populated by set_resolution_list before use
, themes_() // populated by set_theme_list
, last_selected_item_(0)
, accl_speeds_({0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 3, 4, 8, 16})
, visible_hotkeys_()
@ -150,6 +154,23 @@ void preferences_dialog::set_resolution_list(menu_button& res_list)
res_list.set_values(options, current_res);
}
void preferences_dialog::set_theme_list(menu_button& theme_list)
{
themes_ = theme::get_basic_theme_info();
std::vector<config> options;
for(const auto& theme : themes_) {
options.emplace_back("label", theme.name, "tooltip", theme.description);
}
const unsigned current_theme = std::distance(themes_.begin(),
std::find_if(themes_.begin(), themes_.end(), [](const auto& theme) {
return theme.id == preferences::theme();
}));
theme_list.set_values(options, current_theme);
}
widget_data preferences_dialog::get_friends_list_row_data(const acquaintance& entry)
{
widget_data data;
@ -498,9 +519,13 @@ void preferences_dialog::initialize_callbacks()
register_bool("vsync", true, vsync, set_vsync);
/* SELECT THEME */
connect_signal_mouse_left_click(
find_widget<button>(this, "choose_theme", false),
std::bind(&show_theme_dialog));
menu_button& theme_list = find_widget<menu_button>(this, "choose_theme", false);
set_theme_list(theme_list);
connect_signal_notify_modified(theme_list,
std::bind(&preferences_dialog::handle_theme_select, this));
//connect_signal_mouse_left_click(
// find_widget<button>(this, "choose_theme", false),
// std::bind(&show_theme_dialog));
//
// SOUND PANEL
@ -1102,6 +1127,21 @@ void preferences_dialog::handle_res_select()
}
}
void preferences_dialog::handle_theme_select()
{
menu_button& theme_list = find_widget<menu_button>(this, "choose_theme", false);
const auto selection = theme_list.get_value();
const auto& theme = themes_.at(selection);
auto* display = display::get_singleton();
preferences::set_theme(theme.id);
if(display && resources::gamedata && resources::gamedata->get_theme().empty()) {
display->set_theme(theme.id);
}
}
void preferences_dialog::on_page_select()
{
const int selected_row =

View file

@ -22,6 +22,7 @@
#include "hotkey/hotkey_command.hpp"
#include "preferences/advanced.hpp"
#include "preferences/game.hpp"
#include "theme.hpp"
// This file is not named preferences.hpp in order -I conflicts with
// src/preferences.hpp.
@ -31,6 +32,7 @@ namespace hotkey {
}
struct point;
//struct theme_info;
namespace preferences {
enum PREFERENCE_VIEW {
@ -84,6 +86,7 @@ private:
void initialize_callbacks();
void initialize_tabs(listbox& selector);
void set_resolution_list(menu_button& res_list);
void set_theme_list(menu_button& theme_list);
listbox& setup_hotkey_list();
template<bool(*toggle_getter)(), bool(*toggle_setter)(bool), int(*vol_getter)(), void(*vol_setter)(int)>
@ -108,6 +111,7 @@ private:
/** Special callback functions */
void handle_res_select();
void handle_theme_select();
void fullscreen_toggle_callback();
void add_hotkey_callback(listbox& hotkeys);
void remove_hotkey_callback(listbox& hotkeys);
@ -119,6 +123,7 @@ private:
const preferences::advanced_pref_list& adv_preferences_;
std::vector<point> resolutions_;
std::vector<theme_info> themes_;
int last_selected_item_;