App.cpp 5.1 KB

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