EditPage.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "pch.h"
  2. #include "EditPage.h"
  3. #if __has_include("EditPage.g.cpp")
  4. #include "EditPage.g.cpp"
  5. #endif
  6. #include "UI.h"
  7. using namespace std::literals::chrono_literals;
  8. using namespace winrt;
  9. using namespace winrt::Windows::Storage::Streams;
  10. using namespace winrt::Windows::UI::Text;
  11. namespace winrt::Maple_App::implementation
  12. {
  13. EditPage::EditPage()
  14. {
  15. InitializeComponent();
  16. }
  17. fire_and_forget EditPage::OnNavigatedTo(NavigationEventArgs const& e) {
  18. try {
  19. const auto lifetime = get_strong();
  20. const auto& param = e.Parameter().as<Maple_App::ConfigViewModel>();
  21. m_file = param.File();
  22. try {
  23. const auto& text = co_await FileIO::ReadTextAsync(param.File(), UnicodeEncoding::Utf8);
  24. EditBox().Document().SetText(TextSetOptions::None, text);
  25. }
  26. catch (const winrt::hresult_error&) {
  27. EditBox().Document().SetText(TextSetOptions::None, L"Invalid configuration file");
  28. }
  29. const auto weakThis = lifetime->get_weak();
  30. m_saveModifiedContent = [weakThis]() -> IAsyncAction {
  31. if (const auto self{ weakThis.get() }) {
  32. return self->SaveDocument();
  33. }
  34. return {};
  35. };
  36. }
  37. catch (...)
  38. {
  39. UI::NotifyException(L"Failed to load file");
  40. }
  41. }
  42. void EditPage::OnNavigatingFrom(NavigatingCancelEventArgs const&) {
  43. if (m_file == nullptr || !m_file.IsAvailable()) {
  44. return;
  45. }
  46. SaveDocument();
  47. }
  48. void EditPage::EditBox_TextChanging(IInspectable const&, RichEditBoxTextChangingEventArgs const&)
  49. {
  50. if (m_loaded == 2) {
  51. SaveModifiedContent = m_saveModifiedContent;
  52. SaveButton().IsEnabled(true);
  53. }
  54. else {
  55. m_loaded++;
  56. }
  57. }
  58. fire_and_forget EditPage::SaveButton_Click(IInspectable const& sender, RoutedEventArgs const&) {
  59. try {
  60. const auto lifetime = get_strong();
  61. const auto& placementTarget = sender.try_as<FrameworkElement>();
  62. const auto currentValidateRequest = ++validateRequest;
  63. ValidConfigFlyout().Hide();
  64. InvalidConfigFlyout().Hide();
  65. co_await SaveDocument();
  66. if (validateRequest != currentValidateRequest) {
  67. co_return;
  68. }
  69. // Validate
  70. const auto& path = winrt::to_string(m_file.Path());
  71. co_await winrt::resume_background();
  72. const auto result = leaf_test_config(path.data());
  73. co_await winrt::resume_foreground(Dispatcher());
  74. if (validateRequest != currentValidateRequest) {
  75. co_return;
  76. }
  77. switch (result)
  78. {
  79. case LEAF_ERR_OK:
  80. ValidConfigFlyout().ShowAt(placementTarget);
  81. break;
  82. case LEAF_ERR_CONFIG:
  83. InvalidConfigFlyout().ShowAt(placementTarget);
  84. break;
  85. default:
  86. // TODO: handle errors
  87. break;
  88. }
  89. co_await winrt::resume_after(2s);
  90. co_await winrt::resume_foreground(Dispatcher());
  91. if (validateRequest != currentValidateRequest) {
  92. co_return;
  93. }
  94. ValidConfigFlyout().Hide();
  95. InvalidConfigFlyout().Hide();
  96. }
  97. catch (...)
  98. {
  99. UI::NotifyException(L"Saving file");
  100. }
  101. }
  102. void EditPage::HelpButton_Click(IInspectable const&, RoutedEventArgs const&) {
  103. const auto _ = winrt::Windows::System::Launcher::LaunchUriAsync(Uri{ L"https://github.com/eycorsican/leaf/blob/master/README.zh.md" });
  104. }
  105. IAsyncAction EditPage::SaveDocument()
  106. {
  107. try {
  108. const auto lifetime = get_strong();
  109. if (!SaveButton().IsEnabled()) {
  110. co_return;
  111. }
  112. SaveModifiedContent = nullptr;
  113. SaveButton().IsEnabled(false);
  114. hstring content{};
  115. EditBox().Document().GetText(TextGetOptions::NoHidden | TextGetOptions::UseCrlf, content);
  116. const auto data = to_string(content);
  117. co_return co_await FileIO::WriteBytesAsync(
  118. m_file,
  119. std::vector<uint8_t>(data.begin(), data.end()));
  120. }
  121. catch (...)
  122. {
  123. UI::NotifyException(L"Saving document");
  124. }
  125. }
  126. }