App.cpp 4.7 KB

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