bdbai преди 4 години
родител
ревизия
95dd100f6d
променени са 6 файла, в които са добавени 51 реда и са изтрити 15 реда
  1. 19 13
      Maple.App/MainPage.cpp
  2. 24 0
      Maple.App/MainPage.h
  3. 5 1
      Maple.App/MainPage.xaml
  4. 1 0
      Maple.App/Maple.App.vcxproj
  5. 1 0
      Maple.App/pch.h
  6. 1 1
      leaf

+ 19 - 13
Maple.App/MainPage.cpp

@@ -3,10 +3,13 @@
 #include "MainPage.g.cpp"
 #include <filesystem>
 #include <winrt/Windows.Networking.Vpn.h>
+#include <winrt/Windows.UI.ViewManagement.h>
 #include "Model\Netif.h"
 
 namespace winrt::Maple_App::implementation
 {
+    using namespace winrt::Windows::UI::ViewManagement;
+
     std::string getNormalizedExtentionFromPath(const winrt::hstring& path) {
         auto ext = std::filesystem::path(std::wstring_view(path)).extension().string();
         std::transform(ext.begin(), ext.end(), ext.begin(), [](const auto ch) {
@@ -238,6 +241,20 @@ namespace winrt::Maple_App::implementation
         RequestRenameItem(item);
     }
 
+    fire_and_forget MainPage::ConfigImportMenuItem_Click(IInspectable const& sender, RoutedEventArgs const& e)
+    {
+        const auto lifetime = get_strong();
+        bool unsnapped = ((ApplicationView::Value() != ApplicationViewState::Snapped) || ApplicationView::TryUnsnap());
+        if (!unsnapped)
+        {
+            co_await NotifyUser(L"Cannot unsnap the app.");
+            co_return;
+        }
+        ImportFilePicker().FileTypeFilter().ReplaceAll({ L".conf", L".json", L".mmdb", L".dat" });
+        const auto& files = co_await ImportFilePicker().PickMultipleFilesAsync();
+        co_await ImportFiles(files);
+    }
+
     fire_and_forget MainPage::ConfigDuplicateMenuItem_Click(IInspectable const& sender, RoutedEventArgs const&)
     {
         const auto lifetime = get_strong();
@@ -340,19 +357,7 @@ namespace winrt::Maple_App::implementation
         }
 
         const auto& items = co_await dataView.GetStorageItemsAsync();
-        const auto& targetDir = co_await InitializeConfigFolder();
-        for (const auto& item : items) {
-            const auto& file = item.try_as<IStorageFile>();
-            if (file == nullptr) {
-                continue;
-            }
-            auto ext = getNormalizedExtentionFromPath(file.Path());
-            if (ext != ".json" && ext != ".conf" && ext != ".mmdb" && ext != ".dat") {
-                continue;
-            }
-            const auto& newFile = co_await file.CopyAsync(targetDir, file.Name(), NameCollisionOption::GenerateUniqueName);
-            ConfigItems().Append(co_await ConfigViewModel::FromFile(newFile, false));
-        }
+        co_await ImportFiles(items);
     }
 
     void MainPage::WindowWidth_CurrentStateChanged(IInspectable const&, VisualStateChangedEventArgs const& e)
@@ -390,3 +395,4 @@ namespace winrt::Maple_App::implementation
         }
     }
 }
+

+ 24 - 0
Maple.App/MainPage.h

@@ -15,8 +15,12 @@ using namespace Windows::UI::Xaml::Input;
 
 namespace winrt::Maple_App::implementation
 {
+    template<typename T>
+    concept ConvertableToIStorageItem = std::convertible_to<T, IStorageItem>;
+
     static const hstring CONFIG_PATH_SETTING_KEY = L"CONFIG_PATH";
     static const hstring NETIF_SETTING_KEY = L"NETIF";
+    std::string getNormalizedExtentionFromPath(const winrt::hstring& path);
     struct MainPage : MainPageT<MainPage>
     {
         MainPage();
@@ -31,6 +35,7 @@ namespace winrt::Maple_App::implementation
         void RenameDialogPrimaryButton_Click(IInspectable const& sender, ContentDialogButtonClickEventArgs const& e);
         void RenameDialogText_KeyDown(IInspectable const& sender, KeyRoutedEventArgs const& e);
         fire_and_forget ConfigCreateMenuItem_Click(IInspectable const& sender, RoutedEventArgs const& e);
+        fire_and_forget ConfigImportMenuItem_Click(IInspectable const& sender, RoutedEventArgs const& e);
         fire_and_forget ConfigDuplicateMenuItem_Click(IInspectable const& sender, RoutedEventArgs const& e);
         void MainPivot_PivotItemLoaded(Pivot const& sender, PivotItemEventArgs const& args);
         void NetifCombobox_SelectionChanged(IInspectable const& sender, SelectionChangedEventArgs const& e);
@@ -72,6 +77,25 @@ namespace winrt::Maple_App::implementation
         void SetAsDefault(const Maple_App::ConfigViewModel& item);
         fire_and_forget ConfirmRename();
         IAsyncAction LoadConfigs();
+
+        template<ConvertableToIStorageItem T>
+        IAsyncAction ImportFiles(const IVectorView<T>& items) {
+            const auto lifetime = get_strong();
+            const auto& targetDir = co_await InitializeConfigFolder();
+            // TODO: concurrency
+            for (const auto& item : items) {
+                const auto& file = item.try_as<IStorageFile>();
+                if (file == nullptr) {
+                    continue;
+                }
+                auto ext = getNormalizedExtentionFromPath(file.Path());
+                if (ext != ".json" && ext != ".conf" && ext != ".mmdb" && ext != ".dat") {
+                    continue;
+                }
+                const auto& newFile = co_await file.CopyAsync(targetDir, file.Name(), NameCollisionOption::GenerateUniqueName);
+                ConfigItems().Append(co_await ConfigViewModel::FromFile(newFile, false));
+            }
+        }
     };
 }
 

+ 5 - 1
Maple.App/MainPage.xaml

@@ -5,6 +5,7 @@
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:maple_app="using:Maple_App"
+    xmlns:pickers="using:Windows.Storage.Pickers"
     xmlns:Windows10FallCreatorsUpdate="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract, 5)"
     xmlns:Windows10version1809="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract, 7)"
     mc:Ignorable="d"
@@ -13,6 +14,7 @@
 
     <Page.Resources>
         <maple_app:DateTimeConverter x:Key="DateTimeConverter"/>
+        <pickers:FileOpenPicker x:Key="ImportFilePicker" x:Name="ImportFilePicker"/>
     </Page.Resources>
 
     <SplitView x:Name="MainSplitView" DisplayMode="Overlay" PaneClosing="MainSplitView_PaneClosing">
@@ -43,7 +45,7 @@
                             <ListView.Resources>
                                 <MenuFlyout x:Key="ItemContextFlyout">
                                     <MenuFlyoutItem Icon="Favorite" Text="Set as Default" Click="ConfigSetAsDefaultMenuItem_Click"/>
-                                    <MenuFlyoutItem Icon="Copy"  Text="Duplicate" Click="ConfigDuplicateMenuItem_Click"/>
+                                    <MenuFlyoutItem Icon="Copy" Text="Duplicate" Click="ConfigDuplicateMenuItem_Click"/>
                                     <MenuFlyoutItem Icon="Rename" Text="Rename" Click="ConfigRenameMenuItem_Click">
                                         <Windows10FallCreatorsUpdate:MenuFlyoutItem.KeyboardAccelerators>
                                             <Windows10FallCreatorsUpdate:KeyboardAccelerator Key="F2" ScopeOwner="{x:Bind ConfigListView}"/>
@@ -98,6 +100,8 @@
                                     <MenuFlyout Placement="Top">
                                         <MenuFlyoutItem Text="Conf" Click="ConfigCreateMenuItem_Click"/>
                                         <MenuFlyoutItem Text="JSON" Click="ConfigCreateMenuItem_Click"/>
+                                        <MenuFlyoutSeparator/>
+                                        <MenuFlyoutItem Text="Import…" Click="ConfigImportMenuItem_Click"/>
                                     </MenuFlyout>
                                 </AppBarButton.Flyout>
                             </AppBarButton>

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

@@ -101,6 +101,7 @@
       <DisableSpecificWarnings>
       </DisableSpecificWarnings>
       <PreprocessorDefinitions>WIN32_LEAN_AND_MEAN;WINRT_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <LanguageStandard>stdcpplatest</LanguageStandard>
     </ClCompile>
     <Link>
       <GenerateWindowsMetadata>false</GenerateWindowsMetadata>

+ 1 - 0
Maple.App/pch.h

@@ -10,6 +10,7 @@
 #include <winrt/Windows.Foundation.Metadata.h>
 #include <winrt/Windows.Storage.h>
 #include <winrt/Windows.Storage.FileProperties.h>
+#include <winrt/Windows.Storage.Pickers.h>
 #include <winrt/Windows.UI.Core.h>
 #include <winrt/Windows.UI.Text.h>
 #include <winrt/Windows.UI.Xaml.h>

+ 1 - 1
leaf

@@ -1 +1 @@
-Subproject commit b27543c8278a497465f50d7208b2fdc9332570b5
+Subproject commit 785cc88c5913a17a523d96d3ad50bc94b5420b9c