Bläddra i källkod

Add config validation

bdbai 4 år sedan
förälder
incheckning
b994cdcd79

+ 4 - 0
Maple.App/App.cpp

@@ -15,6 +15,10 @@ using namespace Windows::UI::Xaml::Navigation;
 using namespace Maple_App;
 using namespace Maple_App::implementation;
 
+extern "C" void* lwip_strerr(uint8_t) {
+    return const_cast<char*>("");
+}
+
 /// <summary>
 /// Initializes the singleton application object.  This is the first line of authored code
 /// executed, and as such is the logical equivalent of main() or WinMain().

+ 40 - 4
Maple.App/EditPage.cpp

@@ -4,6 +4,7 @@
 #include "EditPage.g.cpp"
 #endif
 
+using namespace std::literals::chrono_literals;
 using namespace winrt;
 using namespace winrt::Windows::Storage::Streams;
 using namespace winrt::Windows::UI::Text;
@@ -52,14 +53,51 @@ namespace winrt::Maple_App::implementation
             m_loaded++;
         }
     }
-    void EditPage::SaveButton_Click(IInspectable const&, RoutedEventArgs const&) {
-        SaveDocument();
+    fire_and_forget EditPage::SaveButton_Click(IInspectable const& sender, RoutedEventArgs const&) {
+        const auto lifetime = get_strong();
+        const auto& placementTarget = sender.try_as<FrameworkElement>();
+        const auto currentValidateRequest = ++validateRequest;
+        ValidConfigFlyout().Hide();
+        InvalidConfigFlyout().Hide();
+        co_await SaveDocument();
+        if (validateRequest != currentValidateRequest) {
+            co_return;
+        }
+
+        // Validate
+        const auto& path = winrt::to_string(m_file.Path());
+        co_await winrt::resume_background();
+        const auto result = leaf_test_config(path.data());
+        co_await winrt::resume_foreground(Dispatcher());
+        if (validateRequest != currentValidateRequest) {
+            co_return;
+        }
+        switch (result)
+        {
+        case LEAF_ERR_OK:
+            ValidConfigFlyout().ShowAt(placementTarget);
+            break;
+        case LEAF_ERR_CONFIG:
+            InvalidConfigFlyout().ShowAt(placementTarget);
+            break;
+        default:
+            // TODO: handle errors
+            break;
+        }
+        co_await winrt::resume_after(2s);
+        co_await winrt::resume_foreground(Dispatcher());
+        if (validateRequest != currentValidateRequest) {
+            co_return;
+        }
+        ValidConfigFlyout().Hide();
+        InvalidConfigFlyout().Hide();
     }
     void EditPage::HelpButton_Click(IInspectable const&, RoutedEventArgs const&) {
         const auto _ = winrt::Windows::System::Launcher::LaunchUriAsync(Uri{ L"https://github.com/eycorsican/leaf/blob/master/README.zh.md" });
     }
     IAsyncAction EditPage::SaveDocument()
     {
+        const auto lifetime = get_strong();
         if (!SaveButton().IsEnabled()) {
             co_return;
         }
@@ -74,5 +112,3 @@ namespace winrt::Maple_App::implementation
     }
 }
 
-
-

+ 4 - 1
Maple.App/EditPage.h

@@ -5,6 +5,7 @@
 #include <winrt/Windows.System.h>
 
 #include "EditPage.g.h"
+#include "leaf.h"
 
 using namespace winrt::Windows::Foundation;
 using namespace winrt::Windows::Storage;
@@ -22,8 +23,9 @@ namespace winrt::Maple_App::implementation
         void OnNavigatingFrom(NavigatingCancelEventArgs const& e);
 
         void EditBox_TextChanging(IInspectable const& sender, RichEditBoxTextChangingEventArgs const& e);
-        void SaveButton_Click(IInspectable const& sender, RoutedEventArgs const& e);
+        fire_and_forget SaveButton_Click(IInspectable const& sender, RoutedEventArgs const& e);
         void HelpButton_Click(IInspectable const& sender, RoutedEventArgs const& e);
+        void ValidateButton_Click(IInspectable const& sender, RoutedEventArgs const& e);
 
         inline static std::function<IAsyncAction()> SaveModifiedContent{ nullptr };
 
@@ -31,6 +33,7 @@ namespace winrt::Maple_App::implementation
         StorageFile m_file{ nullptr };
         uint8_t m_loaded{}; // TextChanging will be triggered twice before actual user inputs
         std::function<IAsyncAction()> m_saveModifiedContent{ nullptr };
+        uint8_t validateRequest{};
 
         IAsyncAction SaveDocument();
     };

+ 20 - 0
Maple.App/EditPage.xaml

@@ -10,6 +10,26 @@
     mc:Ignorable="d"
     NavigationCacheMode="Disabled">
 
+    <Page.Resources>
+        <ResourceDictionary>
+            <Flyout x:Name="ValidConfigFlyout">
+                <StackPanel Orientation="Horizontal">
+                    <SymbolIcon Symbol="Accept" VerticalAlignment="Center" Margin="0, 0, 8, 0"/>
+                    <TextBlock TextWrapping="Wrap">No error</TextBlock>
+                </StackPanel>
+            </Flyout>
+            <Flyout x:Name="InvalidConfigFlyout">
+                <StackPanel Orientation="Horizontal">
+                    <SymbolIcon Symbol="Important" VerticalAlignment="Center" Margin="0, 0, 8, 0"/>
+                    <TextBlock TextWrapping="Wrap">
+                        <Run>There is an error(s) in your configuration.</Run>
+                        <LineBreak/>
+                        <Run>Please check carefully and correct them.</Run>
+                    </TextBlock>
+                </StackPanel>
+            </Flyout>
+        </ResourceDictionary>
+    </Page.Resources>
     <Grid>
         <Grid.RowDefinitions>
             <RowDefinition Height="Auto"/>

+ 26 - 0
Maple.App/Maple.App.vcxproj

@@ -89,6 +89,30 @@
     <AppxBundlePlatforms>x64</AppxBundlePlatforms>
     <HoursBetweenUpdateChecks>0</HoursBetweenUpdateChecks>
   </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+    <LibraryPath>..\leaf\target\x86_64-uwp-windows-msvc\debug;$(LibraryPath)</LibraryPath>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+    <LibraryPath>..\leaf\target\x86_64-uwp-windows-msvc\release;$(LibraryPath)</LibraryPath>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <LibraryPath>..\leaf\target\i686-uwp-windows-msvc\release;$(LibraryPath)</LibraryPath>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <LibraryPath>..\leaf\target\i686-uwp-windows-msvc\debug;$(LibraryPath)</LibraryPath>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">
+    <LibraryPath>..\leaf\target\thumbv7a-uwp-windows-msvc\debug;$(LibraryPath)</LibraryPath>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">
+    <LibraryPath>..\leaf\target\thumbv7a-uwp-windows-msvc\release;$(LibraryPath)</LibraryPath>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
+    <LibraryPath>..\leaf\target\aarch64-uwp-windows-msvc\release;$(LibraryPath)</LibraryPath>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
+    <LibraryPath>..\leaf\target\aarch64-uwp-windows-msvc\debug;$(LibraryPath)</LibraryPath>
+  </PropertyGroup>
   <ItemDefinitionGroup>
     <ClCompile>
       <PrecompiledHeader>Use</PrecompiledHeader>
@@ -105,6 +129,7 @@
     </ClCompile>
     <Link>
       <GenerateWindowsMetadata>false</GenerateWindowsMetadata>
+      <AdditionalDependencies>leaf.lib;WindowsApp.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
@@ -134,6 +159,7 @@
       <DependentUpon>EditPage.xaml</DependentUpon>
       <SubType>Code</SubType>
     </ClInclude>
+    <ClInclude Include="leaf.h" />
     <ClInclude Include="MmdbPage.h">
       <DependentUpon>MmdbPage.xaml</DependentUpon>
       <SubType>Code</SubType>

+ 1 - 0
Maple.App/Maple.App.vcxproj.filters

@@ -9,6 +9,7 @@
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="pch.h" />
+    <ClInclude Include="leaf.h" />
   </ItemGroup>
   <ItemGroup>
     <Image Include="Assets\LockScreenLogo.scale-200.png" />

+ 9 - 0
Maple.App/leaf.h

@@ -0,0 +1,9 @@
+#pragma once
+
+#include <cstdint>
+
+extern "C" {
+    const int32_t LEAF_ERR_OK = 0;
+    const int32_t LEAF_ERR_CONFIG = 2;
+    int32_t leaf_test_config(const char* config_path);
+}

+ 1 - 16
Maple.Task/Maple.Task.vcxproj

@@ -123,24 +123,13 @@
       <SubSystem>Console</SubSystem>
       <GenerateWindowsMetadata>false</GenerateWindowsMetadata>
       <ModuleDefinitionFile>Maple_Task.def</ModuleDefinitionFile>
+      <AdditionalDependencies>leaf.lib;WindowsApp.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
     <ClCompile>
       <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
     </ClCompile>
-    <Link>
-      <AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">leaf.lib;WindowsApp.lib;%(AdditionalDependencies)</AdditionalDependencies>
-    </Link>
-    <Link>
-      <AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">leaf.lib;WindowsApp.lib;%(AdditionalDependencies)</AdditionalDependencies>
-    </Link>
-    <Link>
-      <AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">leaf.lib;WindowsApp.lib;%(AdditionalDependencies)</AdditionalDependencies>
-    </Link>
-    <Link>
-      <AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">leaf.lib;WindowsApp.lib;%(AdditionalDependencies)</AdditionalDependencies>
-    </Link>
     <Link>
       <IgnoreSpecificDefaultLibraries Condition="'$(Configuration)'=='Debug'">MSVCRT</IgnoreSpecificDefaultLibraries>
     </Link>
@@ -152,10 +141,6 @@
     <Link>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
-      <AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">leaf.lib;WindowsApp.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">leaf.lib;WindowsApp.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">leaf.lib;WindowsApp.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Release|x64'">leaf.lib;WindowsApp.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>