App.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "pch.h"
  2. #include <winsock2.h>
  3. #include <chrono>
  4. #include "App.h"
  5. #include "MainPage.h"
  6. #include "EditPage.h"
  7. #include "MonacoEditPage.h"
  8. #include "UI.h"
  9. using namespace winrt;
  10. using namespace Windows::ApplicationModel;
  11. using namespace Windows::ApplicationModel::Activation;
  12. using namespace Windows::Foundation;
  13. using namespace Windows::UI::Xaml;
  14. using namespace Windows::UI::Xaml::Controls;
  15. using namespace Windows::UI::Xaml::Navigation;
  16. using namespace Maple_App;
  17. using namespace Maple_App::implementation;
  18. extern "C" void* lwip_strerr(uint8_t) {
  19. return const_cast<char*>("");
  20. }
  21. /// <summary>
  22. /// Initializes the singleton application object. This is the first line of authored code
  23. /// executed, and as such is the logical equivalent of main() or WinMain().
  24. /// </summary>
  25. App::App()
  26. {
  27. WSADATA wsaData;
  28. if (FAILED(WSAStartup(0x202, &wsaData))) {
  29. // ???
  30. }
  31. InitializeComponent();
  32. Suspending({ this, &App::OnSuspending });
  33. UnhandledException([this](IInspectable const&, UnhandledExceptionEventArgs const& e)
  34. {
  35. auto errorMessage = e.Message();
  36. #if defined _DEBUG && !defined DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
  37. if (IsDebuggerPresent())
  38. {
  39. __debugbreak();
  40. }
  41. #endif
  42. UI::NotifyUser(std::move(errorMessage), L"Unexpected error");
  43. e.Handled(true);
  44. });
  45. }
  46. /// <summary>
  47. /// Invoked when the application is launched normally by the end user. Other entry points
  48. /// will be used such as when the application is launched to open a specific file.
  49. /// </summary>
  50. /// <param name="e">Details about the launch request and process.</param>
  51. void App::OnLaunched(LaunchActivatedEventArgs const& e)
  52. {
  53. Frame rootFrame{ nullptr };
  54. auto content = Window::Current().Content();
  55. if (content)
  56. {
  57. rootFrame = content.try_as<Frame>();
  58. }
  59. // Do not repeat app initialization when the Window already has content,
  60. // just ensure that the window is active
  61. if (rootFrame == nullptr)
  62. {
  63. // Create a Frame to act as the navigation context and associate it with
  64. // a SuspensionManager key
  65. rootFrame = Frame();
  66. rootFrame.NavigationFailed({ this, &App::OnNavigationFailed });
  67. if (e.PreviousExecutionState() == ApplicationExecutionState::Terminated)
  68. {
  69. // Restore the saved session state only when appropriate, scheduling the
  70. // final launch steps after the restore is complete
  71. }
  72. if (e.PrelaunchActivated() == false)
  73. {
  74. if (rootFrame.Content() == nullptr)
  75. {
  76. // When the navigation stack isn't restored navigate to the first page,
  77. // configuring the new page by passing required information as a navigation
  78. // parameter
  79. rootFrame.Navigate(xaml_typename<Maple_App::MainPage>(), box_value(e.Arguments()));
  80. }
  81. // Place the frame in the current Window
  82. Window::Current().Content(rootFrame);
  83. // Ensure the current window is active
  84. Window::Current().Activate();
  85. }
  86. }
  87. else
  88. {
  89. if (e.PrelaunchActivated() == false)
  90. {
  91. if (rootFrame.Content() == nullptr)
  92. {
  93. // When the navigation stack isn't restored navigate to the first page,
  94. // configuring the new page by passing required information as a navigation
  95. // parameter
  96. rootFrame.Navigate(xaml_typename<Maple_App::MainPage>(), box_value(e.Arguments()));
  97. }
  98. // Ensure the current window is active
  99. Window::Current().Activate();
  100. }
  101. }
  102. }
  103. /// <summary>
  104. /// Invoked when application execution is being suspended. Application state is saved
  105. /// without knowing whether the application will be terminated or resumed with the contents
  106. /// of memory still intact.
  107. /// </summary>
  108. /// <param name="sender">The source of the suspend request.</param>
  109. /// <param name="e">Details about the suspend request.</param>
  110. fire_and_forget App::OnSuspending([[maybe_unused]] IInspectable const& sender, [[maybe_unused]] SuspendingEventArgs const& e)
  111. {
  112. using namespace std::literals::chrono_literals;
  113. // Save application state and stop any background activity
  114. const auto saveModifiedContent = std::move(EditPage::SaveModifiedContent);
  115. const auto saveMonacoModifiedContent = std::move(MonacoEditPage::SaveModifiedContent);
  116. if (saveModifiedContent == nullptr && saveMonacoModifiedContent == nullptr) {
  117. co_return;
  118. }
  119. const auto& def = e.SuspendingOperation().GetDeferral();
  120. if (saveModifiedContent != nullptr) {
  121. co_await saveModifiedContent();
  122. }
  123. if (saveMonacoModifiedContent != nullptr)
  124. {
  125. co_await saveMonacoModifiedContent();
  126. }
  127. def.Complete();
  128. }
  129. /// <summary>
  130. /// Invoked when Navigation to a certain page fails
  131. /// </summary>
  132. /// <param name="sender">The Frame which failed navigation</param>
  133. /// <param name="e">Details about the navigation failure</param>
  134. void App::OnNavigationFailed(IInspectable const&, NavigationFailedEventArgs const& e)
  135. {
  136. throw hresult_error(E_FAIL, hstring(L"Failed to load Page ") + e.SourcePageType().Name);
  137. }