Skip to content

Commit

Permalink
新增全部策略页并优化搜索功能 (#12)
Browse files Browse the repository at this point in the history
修复 搜索策略时输入过快导致报错的问题
修复 一个策略的默认值错误
修复 进入策略详情页产生绑定错误的问题
修复 文件选择器“所有文件”默认字符串获取失败的问题
优化 模糊搜索功能
新增 全部策略页
调整 禁用导航缓存以缓解内存占用过大的问题
调整 进入策略列表时默认选择已配置页
调整 当策略详情列表为空时显示一个表情
调整 README.md排版并增加多语言版本
调整 语言字符串获取方式
调整 欢迎页排版
调整 部分翻译文本
  • Loading branch information
NXY666 authored Jan 1, 2024
1 parent f382480 commit 92b09bb
Show file tree
Hide file tree
Showing 23 changed files with 685 additions and 92 deletions.
2 changes: 2 additions & 0 deletions Models/Icon/IconMap.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;

// ReSharper disable ClassNeverInstantiated.Global

namespace PolicyManager.Models.Icon;

public class IconMap : Dictionary<string, string>;
4 changes: 4 additions & 0 deletions Models/Policy/PolicyMenu.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Collections.Generic;

// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable PropertyCanBeMadeInitOnly.Global

namespace PolicyManager.Models.Policy;

public class PolicyMenu : List<PolicyMenuItem>;
Expand Down
3 changes: 3 additions & 0 deletions Pages/Policy/Contents/PolicyContent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
using PolicyManager.Utils;

namespace PolicyManager.Pages.Policy.Contents;
Expand All @@ -11,6 +12,8 @@ public abstract class PolicyContent : Page

protected PolicyContent(NotifyPolicyManager policyManager)
{
NavigationCacheMode = NavigationCacheMode.Disabled;

PolicyManager = policyManager;

DataContext = This;
Expand Down
4 changes: 4 additions & 0 deletions Pages/Policy/DetailPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,9 @@
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackPanel Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center"
Visibility="{Binding Path=IsExpanderListItemsEmpty, Converter={StaticResource BooleanVisibilityConverter}}">
<TextBlock Text="¯\_(ツ)_/¯" FontSize="50" HorizontalAlignment="Center" Foreground="Gray" Margin="0,0,0,100" />
</StackPanel>
</Grid>
</Page>
19 changes: 18 additions & 1 deletion Pages/Policy/DetailPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace PolicyManager.Pages.Policy;

public sealed class DetailPageModel : INotifyPropertyChanged
public sealed class DetailPageModel : INotifyPropertyChanged, IDisposable
{
public delegate void SearchPolicyEventHandler(string keyword);

Expand Down Expand Up @@ -42,6 +42,8 @@ public PolicyMenuItem ActivePolicyMenu

public ObservableCollection<ExpanderListItem> ExpanderListItems { get; } = [];

public bool IsExpanderListItemsEmpty => ExpanderListItems.Count == 0;

public SearchPolicyEventHandler SearchPolicyHandler { get; init; }

public event PropertyChangedEventHandler PropertyChanged;
Expand All @@ -55,6 +57,12 @@ internal void OnSearchPolicyEvent(string keyword)
{
SearchPolicyEvent?.Invoke(keyword);
}

public void Dispose()
{
// PolicyPage停止监听SearchPolicyEvent事件
SearchPolicyEvent -= SearchPolicyHandler;
}
}

public class ExpanderListItem
Expand All @@ -72,6 +80,8 @@ public sealed partial class DetailPage
public DetailPage()
{
InitializeComponent();
NavigationCacheMode = NavigationCacheMode.Disabled;
DataContext = null;
}

protected override void OnNavigatedTo(NavigationEventArgs e)
Expand Down Expand Up @@ -168,4 +178,11 @@ private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArg

dataContext.PolicyManager.PolicyLevel = comboBox.SelectedIndex;
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);

_dataContext.Dispose();
}
}
1 change: 1 addition & 0 deletions Pages/Policy/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public sealed partial class SettingsPage
public SettingsPage()
{
InitializeComponent();
NavigationCacheMode = NavigationCacheMode.Disabled;
}

protected override void OnNavigatedTo(NavigationEventArgs e)
Expand Down
73 changes: 61 additions & 12 deletions Pages/PolicyPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CommunityToolkit.WinUI.UI.Controls.TextToolbarSymbols;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
using PolicyManager.Models.Policy;
Expand All @@ -19,6 +18,8 @@ public class PolicyPageModel
public PolicyDetailMap PolicyDetailMap { init; get; }

public PolicyMenu PolicyMenuList { init; get; }

public string LastSearchKeyword { set; get; }
}

public sealed partial class PolicyPage
Expand All @@ -28,6 +29,7 @@ public sealed partial class PolicyPage
public PolicyPage()
{
InitializeComponent();
NavigationCacheMode = NavigationCacheMode.Disabled;
}

protected override void OnNavigatedTo(NavigationEventArgs e)
Expand All @@ -46,6 +48,18 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
PolicyMenuList = ResourceUtil.GetEmbeddedJson<PolicyMenu>($"StaticModels.Policy.{policyType}.{{LangCode}}.PolicyMenuList.json")
};

// All
PolicyNavigationView.MenuItems.Add(
new NavigationViewItem
{
Content = ResourceUtil.GetString("PolicyPage/AllNavigationItem/Content"),
Icon = IconUtil.GetIconByName("ViewAll"),
Tag = "All"
}
);

PolicyNavigationView.MenuItems.Add(new NavigationViewItemSeparator());

foreach (
var navigationViewItem
in _dataContext.PolicyMenuList.Select(
Expand All @@ -61,6 +75,9 @@ in _dataContext.PolicyMenuList.Select(
PolicyNavigationView.MenuItems.Add(navigationViewItem);
}

// Configured
PolicyNavigationView.SelectedItem = PolicyNavigationView.FooterMenuItems[0];

DataContext = _dataContext;
}

Expand All @@ -72,41 +89,68 @@ private void DetailNavigate(PolicyMenuItem policyMenuItem)
SearchPolicyHandler = SearchPolicy,
PolicyType = _dataContext.PolicyType
};
DetailFrame.Navigate(typeof(DetailPage), detailPageModel);
BaseNavigate(typeof(DetailPage), policyMenuItem.Identifier, detailPageModel);
}

private void AllNavigate()
{
var allPolicyMenuItem = new PolicyMenuItem
{
Name = ResourceUtil.GetString("PolicyPage/AllNavigate/AllPolicyName"),
Icon = "ViewAll",
Identifier = "special:all",
Items = _dataContext.PolicyDetailMap.Select(keyValuePair => keyValuePair.Key).ToList()
};
DetailNavigate(allPolicyMenuItem);
}

private void SettingsNavigate()
{
var settingsPageModel = new SettingsPageModel
{
PolicyType = _dataContext.PolicyType,
PolicyRegistryPath = _dataContext.PolicyRegistryPath
};
DetailFrame.Navigate(typeof(SettingsPage), settingsPageModel);
BaseNavigate(typeof(SettingsPage), "special:settings", settingsPageModel);
}

private void BaseNavigate(Type pageType, string identifier, object parameter = null)
{
DetailFrame.Tag = identifier;
DetailFrame.Navigate(pageType, parameter);
}

private void SearchPolicy(string rawKeyword)
{
rawKeyword = rawKeyword.Trim();

// 如果是空的,就不搜索
if (rawKeyword == string.Empty)
{
return;
}

if (AutoSuggestBox.Text != rawKeyword)
if (AutoSuggestBox.Text.Trim() != rawKeyword)
{
AutoSuggestBox.Text = rawKeyword;
}

// 分割 去重 移除空白
var splitKeyword = rawKeyword.Split(" ").Distinct().Where(keyword => keyword != string.Empty).ToList();

// 如果和上次搜索的一样,就不搜索
var parsedKeyword = splitKeyword.Aggregate((a, b) => $"{a} {b}");
if (parsedKeyword == _dataContext.LastSearchKeyword && ReferenceEquals(DetailFrame.Tag, "special:searchresult")) return;
_dataContext.LastSearchKeyword = parsedKeyword;

var policyMenuItem = new PolicyMenuItem
{
Name = ResourceUtil.GetString("PolicyPage/SearchPolicy/SearchResultName"),
Name = string.Format(ResourceUtil.GetString("PolicyPage/SearchPolicy/SearchResultName"), parsedKeyword),
Icon = "Search",
Identifier = "special:searchresult",
Items = []
};

var splitKeyword = rawKeyword.ToLower().Split(" ");

List<string> perfectResult = [], betterResult = [], normalResult = [], shitResult = [];

foreach (var key in _dataContext.PolicyDetailMap.Keys)
Expand Down Expand Up @@ -203,6 +247,9 @@ private void PolicyNavigationView_SelectionChanged(NavigationView sender, Naviga
{
switch (tag)
{
case "All":
AllNavigate();
break;
case "Settings":
SettingsNavigate();
break;
Expand All @@ -221,17 +268,19 @@ private void AutoSuggestBox_OnTextChanged(AutoSuggestBox sender, AutoSuggestBoxT
{
if (args.Reason == AutoSuggestionBoxTextChangeReason.SuggestionChosen) return;

if (sender.Text == string.Empty)
var searchText = sender.Text.Trim();

if (searchText == string.Empty)
{
sender.ItemsSource = new List();
sender.ItemsSource = null;
return;
}

var splitText = sender.Text.ToLower().Split(" ");
var splitSearchText = searchText.Split(" ");

var suggestList = (
from key in _dataContext.PolicyDetailMap.Keys
let found = splitText.All(keyword => key.Contains(keyword, StringComparison.OrdinalIgnoreCase))
let found = splitSearchText.All(keyword => key.Contains(keyword, StringComparison.OrdinalIgnoreCase))
where found
select _dataContext.PolicyDetailMap[key].Name
).ToList();
Expand Down
30 changes: 16 additions & 14 deletions Pages/WelcomePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center"
Margin="0,0,0,40">
<Image Source="/Assets/icon.ico" Width="40" Margin="0,0,20,0" />
<TextBlock x:Uid="WelcomePage/TitleTextBlock" Foreground="#004700" FontWeight="bold" FontSize="24"
VerticalAlignment="Center" CharacterSpacing="1" />
<Border Background="#D2FFE1" Width="90" Height="90" CornerRadius="100" Margin="0,0,0,20">
<Image Source="/Assets/icon.ico" Width="50" VerticalAlignment="Center" />
</Border>
<StackPanel Grid.Row="1" Margin="0,0,0,30" HorizontalAlignment="Center">
<TextBlock x:Uid="WelcomePage/TitleTextBlock" Foreground="#444746" FontSize="24"
FontWeight="Bold" CharacterSpacing="2" Margin=" 0,0,5,0" />
</StackPanel>
<Grid Row="1" ColumnSpacing="20">
<Grid Row="2" ColumnSpacing="20">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
Expand All @@ -33,7 +35,7 @@
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ImageIcon Source="/Assets/msedge.ico" Width="40" />
<TextBlock Grid.Row="1" Text="Edge" FontSize="16" VerticalAlignment="Center" />
<TextBlock Grid.Row="1" Text="Edge" FontSize="16" HorizontalAlignment="Center" />
</Grid>
</Button.Content>
</Button>
Expand All @@ -45,7 +47,7 @@
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ImageIcon Source="/Assets/msedgeupdate.ico" Width="40" />
<TextBlock Grid.Row="1" Text="Edge Update" FontSize="16" VerticalAlignment="Center" />
<TextBlock Grid.Row="1" Text="Edge Update" FontSize="16" HorizontalAlignment="Center" />
</Grid>
</Button.Content>
</Button>
Expand All @@ -57,29 +59,29 @@
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ImageIcon Source="/Assets/msedgewebview2.ico" Width="40" />
<TextBlock Grid.Row="1" Text="Edge Webview 2" FontSize="16" VerticalAlignment="Center" />
<TextBlock Grid.Row="1" Text="Edge Webview 2" FontSize="16" HorizontalAlignment="Center" />
</Grid>
</Button.Content>
</Button>
</Grid>
<Grid Row="2" Margin="0, 20, 0, 0" ColumnSpacing="20">
<Grid Row="3" Margin="0, 20, 0, 0" ColumnSpacing="20">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" HorizontalAlignment="Stretch" Click="GithubButton_OnClick">
<Button.Content>
<StackPanel Orientation="Horizontal" Spacing="5">
<ImageIcon Source="/Assets/github.svg" Height="15" />
<TextBlock x:Uid="WelcomePage/GithubButtonText" />
<ImageIcon Source="/Assets/github.svg" Height="25" Margin="2,0" />
<TextBlock x:Uid="WelcomePage/GithubButtonText" VerticalAlignment="Center" />
</StackPanel>
</Button.Content>
</Button>
<Button Grid.Column="1" HorizontalAlignment="Stretch" Click="FeedbackButton_OnClick">
<Button.Content>
<StackPanel Orientation="Horizontal" Spacing="5">
<FontIcon Glyph="&#xED15;" FontSize="13" />
<TextBlock x:Uid="WelcomePage/FeedbackButtonText" />
<FontIcon Glyph="&#xED15;" FontSize="21" Margin="4,2" />
<TextBlock x:Uid="WelcomePage/FeedbackButtonText" VerticalAlignment="Center" />
</StackPanel>
</Button.Content>
</Button>
Expand Down
2 changes: 0 additions & 2 deletions PolicyManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
<TrimMode>partial</TrimMode>
<ApplicationIcon>Assets\icon.ico</ApplicationIcon>
<AssemblyName>EdgePolicyManager</AssemblyName>
<FileVersion>0.0.0</FileVersion>
<AssemblyVersion>0.0.0</AssemblyVersion>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
</PropertyGroup>
<ItemGroup>
Expand Down
Loading

0 comments on commit 92b09bb

Please sign in to comment.