-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreferences.cpp
More file actions
277 lines (222 loc) · 7.84 KB
/
preferences.cpp
File metadata and controls
277 lines (222 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <helpers/foobar2000+atl.h>
#include <pfc/int_types.h>
#include <SDK/cfg_var.h>
#include "resources.h"
#include <helpers/atl-misc.h>
#include <helpers/DarkMode.h>
#include <utility>
#include <spdlog/spdlog.h>
#include "mcp.h"
// Sample preferences interface: two meaningless configuration settings accessible through a preferences page and one accessible through advanced preferences.
// Dark Mode:
// (1) Add fb2k::CDarkModeHooks member.
// (2) Initialize it in our WM_INITDIALOG handler.
// (3) Tell foobar2000 that this prefernces page supports dark mode, by returning correct get_state() flags.
// That's all.
// These GUIDs identify the variables within our component's configuration file.
static constexpr GUID guid_IDC_editEndpoint = {
0xbd5c777, 0x735c, 0x440d, {0x8c, 0x71, 0x49, 0xb6, 0xac, 0xff, 0xce, 0xb8}
};
static constexpr GUID guid_IDC_checkEnable = {
0x482465a5, 0x7ab6, 0x42ee, {0xb8, 0x1a, 0x53, 0xe4, 0x6f, 0x56, 0xa2, 0xb6}
};
// defaults
static constexpr char cfg_editEndpoint[] = "localhost:9910";
static constexpr bool cfg_checkEnable = true;
namespace foo_ai
{
cfg_var_modern::cfg_string IDC_editEndpoint(guid_IDC_editEndpoint, cfg_editEndpoint);
cfg_var_modern::cfg_bool IDC_checkEnable(guid_IDC_checkEnable, cfg_checkEnable);
void get_endpoint(pfc::string_base& out)
{
IDC_editEndpoint.get(out);
}
bool is_server_enabled()
{
return IDC_checkEnable.get();
}
void restart_mcp_server()
{
if (!is_server_enabled())
{
mcp_manager::instance().stop();
return;
}
pfc::string8 endpoint;
get_endpoint(endpoint);
std::string ep(endpoint.c_str());
std::string host = "localhost";
int port = 9910;
auto colon = ep.find(':');
if (colon != std::string::npos)
{
host = ep.substr(0, colon);
try
{
port = std::stoi(ep.substr(colon + 1));
} catch (const std::exception&)
{
spdlog::error("Invalid port number in endpoint configuration: '{}'", ep.substr(colon + 1));
}
}
else if (!ep.empty())
{
host = ep;
}
if (port < 1 || port > 65535)
{
spdlog::error("Port number out of range in endpoint configuration: {}", port);
port = 9910;
}
mcp_manager::instance().start(host, port);
}
}
using namespace foo_ai;
#ifdef _WIN32
class CMyPreferences : public CDialogImpl<CMyPreferences>, public preferences_page_instance
{
public:
//Constructor - invoked by preferences_page_impl helpers - don't do Create() in here, preferences_page_impl does this for us
explicit CMyPreferences(preferences_page_callback::ptr callback) : m_bMsgHandled(0), m_callback(std::move(callback))
{
}
//Note that we don't bother doing anything regarding destruction of our class.
//The host ensures that our dialog is destroyed first, then the last reference to our preferences_page_instance object is released, causing our object to be deleted.
//dialog resource ID
enum { IDD = IDD_PREFERENCES };
// preferences_page_instance methods (not all of them - get_wnd() is supplied by preferences_page_impl helpers)
t_uint32 get_state();
void apply();
void reset();
//WTL message map
BEGIN_MSG_MAP_EX(CMyPreferences)
MSG_WM_INITDIALOG(OnInitDialog)
COMMAND_HANDLER_EX(IDC_EDIT_ENDPOINT, EN_CHANGE, OnEditChange)
COMMAND_HANDLER_EX(IDC_CHECK_ENABLE, BN_CLICKED, OnCheckboxChange)
END_MSG_MAP()
private:
BOOL OnInitDialog(CWindow, LPARAM);
void OnEditChange(UINT, int, CWindow);
void OnCheckboxChange(UINT, int, CWindow);
bool HasChanged() const;
void OnChanged();
pfc::string8 UpdateSSEUrl();
const preferences_page_callback::ptr m_callback;
// Dark mode hooks object, must be a member of dialog class.
fb2k::CDarkModeHooks m_dark;
};
BOOL CMyPreferences::OnInitDialog(CWindow, LPARAM)
{
// Enable dark mode
// One call does it all, applies all relevant hacks automatically
m_dark.AddDialogWithControls(*this);
auto string = pfc::string_formatter();
IDC_editEndpoint.get(string);
uSetDlgItemText(*this, IDC_EDIT_ENDPOINT, string);
// Set checkbox state
uButton_SetCheck(*this, IDC_CHECK_ENABLE, IDC_checkEnable.get());
// Update SSE URL display
UpdateSSEUrl();
return FALSE;
}
void CMyPreferences::OnEditChange(UINT, int, CWindow)
{
// not much to do here
OnChanged();
}
void CMyPreferences::OnCheckboxChange(UINT, int, CWindow)
{
OnChanged();
}
t_uint32 CMyPreferences::get_state()
{
// IMPORTANT: Always return dark_mode_supported - tell foobar2000 that this preferences page is dark mode compliant.
t_uint32 state = preferences_state::resettable | preferences_state::dark_mode_supported;
if (HasChanged()) state |= preferences_state::changed;
return state;
}
void CMyPreferences::reset()
{
uSetDlgItemText(*this, IDC_EDIT_ENDPOINT, cfg_editEndpoint);
uButton_SetCheck(*this, IDC_CHECK_ENABLE, cfg_checkEnable);
OnChanged();
}
void CMyPreferences::apply()
{
const auto endpoint = UpdateSSEUrl();
IDC_editEndpoint = endpoint;
// Save checkbox state
IDC_checkEnable = uButton_GetCheck(*this, IDC_CHECK_ENABLE) != 0;
restart_mcp_server();
uSetDlgItemText(*this, IDC_EDIT_ENDPOINT, endpoint);
OnChanged();
//our dialog content has not changed but the flags have - our currently shown values now match the settings so the apply button can be disabled
}
bool CMyPreferences::HasChanged() const
{
pfc::string8 endpoint;
uGetDlgItemText(*this, IDC_EDIT_ENDPOINT, endpoint);
bool checkEnabled = uButton_GetCheck(*this, IDC_CHECK_ENABLE) != 0;
//returns whether our dialog content is different from the current configuration (whether the apply button should be enabled or not)
return endpoint != IDC_editEndpoint || checkEnabled != IDC_checkEnable.get();
}
void CMyPreferences::OnChanged()
{
//tell the host that our state has changed to enable/disable the apply button appropriately.
m_callback->on_state_changed();
}
pfc::string8 CMyPreferences::UpdateSSEUrl()
{
pfc::string8 endpoint;
uGetDlgItemText(*this, IDC_EDIT_ENDPOINT, endpoint);
std::string ep(endpoint.c_str());
std::string host = "localhost";
int port = 9910;
auto colon = ep.find(':');
if (colon != std::string::npos)
{
host = ep.substr(0, colon);
try
{
port = std::stoi(ep.substr(colon + 1));
} catch (const std::exception&)
{
spdlog::error("Invalid port number in endpoint configuration: '{}'", ep.substr(colon + 1));
}
}
else if (!ep.empty())
{
host = ep;
}
if (port < 1 || port > 65535)
{
spdlog::error("Port number out of range in endpoint configuration: {}", port);
port = 9910;
}
std::string sseUrl = "http://" + host + ":" + std::to_string(port) + "/sse";
uSetDlgItemText(*this, IDC_TEXT_SSE_URL, sseUrl.c_str());
return (host + ":" + std::to_string(port)).c_str();
}
class preferences_page_myimpl : public preferences_page_impl<CMyPreferences>
{
// preferences_page_impl<> helper deals with instantiation of our dialog; inherits from preferences_page_v3.
public:
virtual ~preferences_page_myimpl() = default;
const char* get_name() override;
GUID get_guid() override;
GUID get_parent_guid() override;
};
const char* preferences_page_myimpl::get_name()
{
return "AI";
}
GUID preferences_page_myimpl::get_guid()
{
return GUID{0xb90d1b13, 0xcb44, 0x4dbb, {0xb3, 0xce, 0x60, 0x98, 0xae, 0x54, 0x7a, 0xe4}};
}
GUID preferences_page_myimpl::get_parent_guid()
{
return guid_tools;
}
static preferences_page_factory_t<preferences_page_myimpl> g_preferences_page_myimpl_factory;
#endif // _WIN32