Implement settings proxy pane

This commit is contained in:
Yusyuriv 2024-05-16 00:53:12 +06:00
parent 74825fc27a
commit f7b2d9df3b
No known key found for this signature in database
GPG key ID: A91C52E6F73148E0
3 changed files with 133 additions and 179 deletions

View file

@ -0,0 +1,62 @@
using System.Net;
using System.Windows;
using CommunityToolkit.Mvvm.Input;
using Flow.Launcher.Core;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
namespace Flow.Launcher.SettingPages.ViewModels;
public partial class SettingsPaneProxyViewModel : BaseModel
{
private readonly Updater _updater;
public Settings Settings { get; }
public SettingsPaneProxyViewModel(Settings settings, Updater updater)
{
_updater = updater;
Settings = settings;
}
[RelayCommand]
private void OnTestProxyClicked()
{
var message = TestProxy();
MessageBox.Show(InternationalizationManager.Instance.GetTranslation(message));
}
private string TestProxy()
{
if (string.IsNullOrEmpty(Settings.Proxy.Server)) return "serverCantBeEmpty";
if (Settings.Proxy.Port <= 0) return "portCantBeEmpty";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_updater.GitHubRepository);
if (string.IsNullOrEmpty(Settings.Proxy.UserName) || string.IsNullOrEmpty(Settings.Proxy.Password))
{
request.Proxy = new WebProxy(Settings.Proxy.Server, Settings.Proxy.Port);
}
else
{
request.Proxy = new WebProxy(Settings.Proxy.Server, Settings.Proxy.Port)
{
Credentials = new NetworkCredential(Settings.Proxy.UserName, Settings.Proxy.Password)
};
}
try
{
var response = (HttpWebResponse)request.GetResponse();
return response.StatusCode switch
{
HttpStatusCode.OK => "proxyIsCorrect",
_ => "proxyConnectFailed"
};
}
catch
{
return "proxyConnectFailed";
}
}
}

View file

@ -1,181 +1,77 @@
<Page <ui:Page
x:Class="Flow.Launcher.SettingPages.Views.SettingsPaneProxy" x:Class="Flow.Launcher.SettingPages.Views.SettingsPaneProxy"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Flow.Launcher.SettingPages.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:ui="http://schemas.modernwpf.com/2019" xmlns:ui="http://schemas.modernwpf.com/2019"
xmlns:cc="clr-namespace:Flow.Launcher.Resources.Controls"
xmlns:viewModels="clr-namespace:Flow.Launcher.SettingPages.ViewModels"
Title="Proxy" Title="Proxy"
d:DataContext="{d:DesignInstance viewModels:SettingsPaneProxyViewModel}"
d:DesignHeight="450" d:DesignHeight="450"
d:DesignWidth="800" d:DesignWidth="800"
mc:Ignorable="d"> mc:Ignorable="d">
<Page.Resources> <Page.Resources>
<ResourceDictionary> <ResourceDictionary Source="pack://application:,,,/Resources/SettingWindowStyle.xaml" />
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Resources/SettingWindowStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
<CollectionViewSource x:Key="SortedFonts" Source="{Binding Source={x:Static Fonts.SystemFontFamilies}}" />
</ResourceDictionary>
</Page.Resources> </Page.Resources>
<ScrollViewer <ScrollViewer
Margin="0,0,0,0" Margin="0 0 0 0"
Padding="5,0,24,0" Padding="5 0 24 0"
FontSize="14" CanContentScroll="True"
ScrollViewer.CanContentScroll="True"
VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.ScrollUnit="Pixel"> VirtualizingStackPanel.ScrollUnit="Pixel">
<Border>
<StackPanel> <StackPanel>
<Border Margin="0,18,0,0"> <TextBlock
<TextBlock Margin="0 23 0 10"
Grid.Row="0" FontSize="30"
Margin="0,5,0,0" Style="{StaticResource PageTitle}"
FontSize="30" Text="{DynamicResource proxy}"
Style="{StaticResource PageTitle}" TextAlignment="left" />
Text="{DynamicResource proxy}"
TextAlignment="left" />
</Border>
<cc:CardGroup>
<cc:Card Title="{DynamicResource enableProxy}">
<ui:ToggleSwitch IsOn="{Binding Settings.Proxy.Enabled}" />
</cc:Card>
<Border <cc:Card Title="{DynamicResource server}">
Margin="0,10,0,0" <TextBox
Padding="0" Width="300"
CornerRadius="5" IsEnabled="{Binding Settings.Proxy.Enabled}"
Style="{DynamicResource SettingGroupBox}"> Text="{Binding Settings.Proxy.Server}" />
<StackPanel Orientation="Vertical"> </cc:Card>
<Border
Margin="0" <cc:Card Title="{DynamicResource port}">
BorderThickness="0" <TextBox
Style="{DynamicResource SettingGroupBox}"> Width="100"
<ItemsControl Style="{StaticResource SettingGrid}"> IsEnabled="{Binding Settings.Proxy.Enabled}"
<TextBlock Text="{Binding Settings.Proxy.Port, TargetNullValue={x:Static sys:String.Empty}}" />
Grid.Column="1" </cc:Card>
VerticalAlignment="Center"
Style="{DynamicResource SettingTitleLabel}" <cc:Card Title="{DynamicResource userName}">
Text="{DynamicResource enableProxy}" /> <TextBox
<ui:ToggleSwitch Width="200"
Grid.Column="2" IsEnabled="{Binding Settings.Proxy.Enabled}"
Margin="0,0,18,0" Text="{Binding Settings.Proxy.UserName}" />
IsOn="{Binding Settings.Proxy.Enabled}" /> </cc:Card>
</ItemsControl>
</Border> <cc:Card Title="{DynamicResource password}">
<Separator <TextBox
Width="Auto" Width="200"
BorderThickness="1" IsEnabled="{Binding Settings.Proxy.Enabled}"
Style="{StaticResource SettingSeparatorStyle}" /> Text="{Binding Settings.Proxy.Password}" />
<Border </cc:Card>
Margin="0" </cc:CardGroup>
BorderThickness="0"
Style="{DynamicResource SettingGroupBox}"> <cc:Card Title="{DynamicResource testProxy}" Margin="0 8 0 0">
<ItemsControl Style="{StaticResource SettingGrid}"> <Button
<TextBlock Width="150"
Grid.Column="1" Content="{DynamicResource testProxy}"
Margin="22,0,0,0" IsEnabled="{Binding Settings.Proxy.Enabled}"
VerticalAlignment="Center" Command="{Binding TestProxyClickedCommand}"/>
Style="{DynamicResource SettingTitleLabel}" </cc:Card>
Text="{DynamicResource server}" /> </StackPanel>
<TextBox
Grid.Column="2"
Width="300"
Margin="0,0,16,0"
Padding="5"
IsEnabled="{Binding Settings.Proxy.Enabled}"
Text="{Binding Settings.Proxy.Server}" />
</ItemsControl>
</Border>
<Separator
Width="Auto"
BorderThickness="1"
Style="{StaticResource SettingSeparatorStyle}" />
<Border
Margin="0"
BorderThickness="0"
Style="{DynamicResource SettingGroupBox}">
<ItemsControl Style="{StaticResource SettingGrid}">
<TextBlock
Grid.Column="1"
Margin="22,0,0,0"
VerticalAlignment="Center"
Style="{DynamicResource SettingTitleLabel}"
Text="{DynamicResource port}" />
<TextBox
Grid.Column="2"
Width="100"
Margin="0,0,16,0"
IsEnabled="{Binding Settings.Proxy.Enabled}"
Text="{Binding Settings.Proxy.Port, TargetNullValue={x:Static sys:String.Empty}}" />
</ItemsControl>
</Border>
<Separator
Width="Auto"
BorderThickness="1"
Style="{StaticResource SettingSeparatorStyle}" />
<Border
Margin="0"
BorderThickness="0"
Style="{DynamicResource SettingGroupBox}">
<ItemsControl Style="{StaticResource SettingGrid}">
<TextBlock
Grid.Column="1"
Margin="22,0,0,0"
VerticalAlignment="Center"
Style="{DynamicResource SettingTitleLabel}"
Text="{DynamicResource userName}" />
<TextBox
Grid.Column="2"
Width="200"
Margin="0,0,16,0"
Padding="5"
IsEnabled="{Binding Settings.Proxy.Enabled}"
Text="{Binding Settings.Proxy.UserName}" />
</ItemsControl>
</Border>
<Separator
Width="Auto"
BorderThickness="1"
Style="{StaticResource SettingSeparatorStyle}" />
<Border
Margin="0"
BorderThickness="0"
Style="{DynamicResource SettingGroupBox}">
<ItemsControl Style="{StaticResource SettingGrid}">
<TextBlock
Grid.Column="1"
Margin="22,0,0,0"
VerticalAlignment="Center"
Style="{DynamicResource SettingTitleLabel}"
Text="{DynamicResource password}" />
<TextBox
Grid.Column="2"
Width="200"
Margin="0,0,16,0"
Padding="5"
IsEnabled="{Binding Settings.Proxy.Enabled}"
Text="{Binding Settings.Proxy.Password}" />
</ItemsControl>
</Border>
</StackPanel>
</Border>
<Border Margin="0,12,0,0" Style="{DynamicResource SettingGroupBox}">
<ItemsControl Style="{StaticResource SettingGrid}">
<TextBlock
Grid.Column="1"
VerticalAlignment="Center"
Style="{DynamicResource SettingTitleLabel}"
Text="{DynamicResource testProxy}" />
<Button
Grid.Column="2"
Width="150"
Margin="0,0,16,0"
HorizontalAlignment="Right"
Content="{DynamicResource testProxy}"
IsEnabled="{Binding Settings.Proxy.Enabled}" />
</ItemsControl>
</Border>
</StackPanel>
</Border>
</ScrollViewer> </ScrollViewer>
</Page> </ui:Page>

View file

@ -1,28 +1,24 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
using System.Windows.Shapes; using Flow.Launcher.SettingPages.ViewModels;
namespace Flow.Launcher.SettingPages.Views namespace Flow.Launcher.SettingPages.Views;
public partial class SettingsPaneProxy
{ {
/// <summary> private SettingsPaneProxyViewModel _viewModel = null!;
/// Proxy.xaml에 대한 상호 작용 논리
/// </summary> protected override void OnNavigatedTo(NavigationEventArgs e)
public partial class SettingsPaneProxy : Page
{ {
public SettingsPaneProxy() if (!IsInitialized)
{ {
if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings, Updater: { } updater })
throw new ArgumentException($"Settings are required for {nameof(SettingsPaneProxy)}.");
_viewModel = new SettingsPaneProxyViewModel(settings, updater);
DataContext = _viewModel;
InitializeComponent(); InitializeComponent();
} }
base.OnNavigatedTo(e);
} }
} }