|
24 | 24 |
|
25 | 25 | #include "FileWatch.hpp" |
26 | 26 |
|
| 27 | +#include "wintoastlib.h" |
| 28 | +using namespace WinToastLib; |
| 29 | + |
27 | 30 | std::unordered_set< |
28 | 31 | std::shared_ptr<std::function<void(mb_shell::js::menu_info_basic_js)>>> |
29 | 32 | mb_shell::menu_callbacks_js; |
@@ -1265,15 +1268,91 @@ bool win32::is_key_down(std::string key) { |
1265 | 1268 | {"y", 'Y'}, |
1266 | 1269 | {"z", 'Z'}}); |
1267 | 1270 |
|
1268 | | - auto keycode = std::ranges::find_if( |
1269 | | - key_map, [key_lower](const auto &pair) { |
1270 | | - return key_lower == pair.first; |
1271 | | - }); |
| 1271 | + auto keycode = std::ranges::find_if(key_map, [key_lower](const auto &pair) { |
| 1272 | + return key_lower == pair.first; |
| 1273 | + }); |
1272 | 1274 |
|
1273 | 1275 | if (keycode != key_map.end()) { |
1274 | 1276 | return GetAsyncKeyState(keycode->second) & 0x8000; |
1275 | 1277 | } |
1276 | 1278 |
|
1277 | 1279 | return false; |
1278 | 1280 | } |
| 1281 | + |
| 1282 | +struct WinToastEventHandler : public IWinToastHandler { |
| 1283 | + std::function<void(int)> on_activate = [](int) {}; |
| 1284 | + std::function<void(WinToastDismissalReason)> on_dismiss = |
| 1285 | + [](WinToastDismissalReason) {}; |
| 1286 | + void toastActivated() const override {} |
| 1287 | + void toastActivated(int actionIndex) const override { |
| 1288 | + on_activate(actionIndex); |
| 1289 | + } |
| 1290 | + void toastActivated(const char *) const override {} |
| 1291 | + |
| 1292 | + void toastDismissed(WinToastDismissalReason state) const override { |
| 1293 | + on_dismiss(state); |
| 1294 | + } |
| 1295 | + |
| 1296 | + void toastFailed() const override {} |
| 1297 | +} winToastEventHandler; |
| 1298 | + |
| 1299 | +static void wintoast_init() { |
| 1300 | + static bool initialized = false; |
| 1301 | + if (initialized) |
| 1302 | + return; |
| 1303 | + initialized = true; |
| 1304 | + WinToast::instance()->setAppName(L"Breeze"); |
| 1305 | + WinToast::instance()->setAppUserModelId(L"breeze-shell"); |
| 1306 | + WinToast::instance()->initialize(); |
| 1307 | +} |
| 1308 | + |
| 1309 | +void notification::send_basic(std::string message) { |
| 1310 | + wintoast_init(); |
| 1311 | + |
| 1312 | + WinToastTemplate templ(WinToastTemplate::ImageAndText02); |
| 1313 | + templ.setTextField(utf8_to_wstring(message), WinToastTemplate::FirstLine); |
| 1314 | + WinToast::instance()->showToast(templ, &winToastEventHandler); |
| 1315 | +} |
| 1316 | +void notification::send_with_image(std::string message, std::string icon_path) { |
| 1317 | + wintoast_init(); |
| 1318 | + |
| 1319 | + WinToastTemplate templ(WinToastTemplate::ImageAndText02); |
| 1320 | + templ.setTextField(utf8_to_wstring(message), WinToastTemplate::FirstLine); |
| 1321 | + templ.setImagePath(utf8_to_wstring(icon_path)); |
| 1322 | + WinToast::instance()->showToast(templ, &winToastEventHandler); |
| 1323 | +} |
| 1324 | +void notification::send_title_text(std::string title, std::string message, |
| 1325 | + std::string image_path) { |
| 1326 | + wintoast_init(); |
| 1327 | + WinToastTemplate templ(WinToastTemplate::ImageAndText02); |
| 1328 | + templ.setTextField(utf8_to_wstring(title), WinToastTemplate::FirstLine); |
| 1329 | + templ.setTextField(utf8_to_wstring(message), WinToastTemplate::SecondLine); |
| 1330 | + if (!image_path.empty()) |
| 1331 | + templ.setImagePath(utf8_to_wstring(image_path)); |
| 1332 | + WinToast::instance()->showToast(templ, &winToastEventHandler); |
| 1333 | +} |
| 1334 | +void notification::send_with_buttons( |
| 1335 | + std::string title, std::string message, |
| 1336 | + std::vector<std::pair<std::string, std::function<void()>>> buttons) { |
| 1337 | + wintoast_init(); |
| 1338 | + WinToastTemplate templ(WinToastTemplate::Text02); |
| 1339 | + templ.setTextField(utf8_to_wstring(title), WinToastTemplate::FirstLine); |
| 1340 | + templ.setTextField(utf8_to_wstring(message), WinToastTemplate::SecondLine); |
| 1341 | + |
| 1342 | + for (const auto &[button_text, callback] : buttons) { |
| 1343 | + templ.addAction(utf8_to_wstring(button_text)); |
| 1344 | + } |
| 1345 | + |
| 1346 | + auto* handler = new WinToastEventHandler(); |
| 1347 | + handler->on_activate = [buttons, handler](int actionIndex) { |
| 1348 | + if (actionIndex >= 0 && actionIndex < buttons.size()) { |
| 1349 | + buttons[actionIndex].second(); |
| 1350 | + } |
| 1351 | + delete handler; |
| 1352 | + }; |
| 1353 | + handler->on_dismiss = [=](WinToastEventHandler::WinToastDismissalReason) { |
| 1354 | + }; |
| 1355 | + |
| 1356 | + WinToast::instance()->showToast(templ, handler); |
| 1357 | +} |
1279 | 1358 | } // namespace mb_shell::js |
0 commit comments