UI.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "pch.h"
  2. #include "UI.h"
  3. namespace winrt::Maple_App::implementation
  4. {
  5. using Windows::UI::Xaml::Controls::ContentDialog;
  6. using Windows::UI::Xaml::Input::StandardUICommand;
  7. using Windows::UI::Xaml::Input::StandardUICommandKind;
  8. using Windows::Foundation::Metadata::ApiInformation;
  9. void UI::NotifyUser(hstring msg, hstring title)
  10. {
  11. static Windows::UI::Core::CoreDispatcher dispatcher{ Windows::UI::Xaml::Window::Current().Dispatcher() };
  12. static std::vector<std::pair<hstring, hstring>> messages{};
  13. dispatcher.RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal,
  14. [msg = std::move(msg), title = std::move(title)]() -> fire_and_forget {
  15. messages.push_back(std::make_pair(msg, title));
  16. static bool isQueueRunning{ false };
  17. if (std::exchange(isQueueRunning, true))
  18. {
  19. co_return;
  20. }
  21. // Display all messages until the queue is drained.
  22. while (messages.size() > 0)
  23. {
  24. auto it = messages.begin();
  25. auto [message, messageTitle] = std::move(*it);
  26. messages.erase(it);
  27. ContentDialog dialog;
  28. if (messageTitle.size() > 0)
  29. {
  30. dialog.Title(box_value(std::move(messageTitle)));
  31. }
  32. if (ApiInformation::IsTypePresent(L"Windows.UI.Xaml.Input.StandardUICommand"))
  33. {
  34. dialog.CloseButtonCommand(StandardUICommand(StandardUICommandKind::Close));
  35. }
  36. else if (ApiInformation::IsPropertyPresent(
  37. L"Windows.UI.Xaml.Controls.ContentDialog", L"CloseButtonText"))
  38. {
  39. dialog.CloseButtonText(L"Close");
  40. }
  41. else
  42. {
  43. dialog.SecondaryButtonText(L"Close");
  44. }
  45. dialog.Content(box_value(message));
  46. co_await dialog.ShowAsync();
  47. }
  48. isQueueRunning = false;
  49. });
  50. }
  51. void UI::NotifyUser(char const* msg, hstring title)
  52. {
  53. NotifyUser(to_hstring(msg), std::move(title));
  54. }
  55. void UI::NotifyException(std::wstring_view context)
  56. {
  57. try
  58. {
  59. throw;
  60. }
  61. catch (hresult_error const& hr)
  62. {
  63. NotifyUser(hr.message(), hstring{ L"Error occurred: " } + context);
  64. }
  65. catch (std::exception const& ex)
  66. {
  67. NotifyUser(ex.what(), hstring{ L"Unexpected exception: " } + context);
  68. }
  69. }
  70. }