Skip to content

Commit d43ce17

Browse files
committed
enhance: check new branch name in Push to a NEW branch dialog
Signed-off-by: leo <longshuang@msn.cn>
1 parent b68825a commit d43ce17

4 files changed

Lines changed: 57 additions & 13 deletions

File tree

src/ViewModels/PushToNewBranch.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using System.Diagnostics.CodeAnalysis;
3+
using CommunityToolkit.Mvvm.ComponentModel;
4+
5+
namespace SourceGit.ViewModels
6+
{
7+
public class PushToNewBranch : ObservableValidator
8+
{
9+
public string Remote
10+
{
11+
get;
12+
}
13+
14+
[Required(ErrorMessage = "Branch name is required!")]
15+
[CustomValidation(typeof(PushToNewBranch), nameof(ValidateBranchName))]
16+
public string BranchName
17+
{
18+
get => _branchName;
19+
set => SetProperty(ref _branchName, value, true);
20+
}
21+
22+
public PushToNewBranch(string remote)
23+
{
24+
Remote = remote;
25+
}
26+
27+
public static ValidationResult ValidateBranchName(string name, ValidationContext ctx)
28+
{
29+
if (!Models.RefName.IsValidBranchName(name))
30+
return new ValidationResult("Bad branch name format!");
31+
32+
return ValidationResult.Success;
33+
}
34+
35+
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode")]
36+
public bool Check()
37+
{
38+
ValidateAllProperties();
39+
return !HasErrors;
40+
}
41+
42+
private string _branchName = string.Empty;
43+
}
44+
}

src/Views/Push.axaml.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ private async void OnPushToNewBranch(object sender, RoutedEventArgs e)
2020
if (launcher == null)
2121
return;
2222

23-
var dialog = new PushToNewBranch();
24-
dialog.SetRemote(push.SelectedRemote.Name);
23+
var dialog = new PushToNewBranch()
24+
{
25+
DataContext = new ViewModels.PushToNewBranch(push.SelectedRemote.Name)
26+
};
2527

2628
var name = await dialog.ShowDialog<string>(launcher);
2729
if (!string.IsNullOrEmpty(name))

src/Views/PushToNewBranch.axaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:v="using:SourceGit.Views"
6+
xmlns:vm="using:SourceGit.ViewModels"
67
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
78
x:Class="SourceGit.Views.PushToNewBranch"
89
x:Name="ThisControl"
10+
x:DataType="vm:PushToNewBranch"
911
Icon="/App.ico"
1012
Title="{DynamicResource Text.PushToNewBranch}"
11-
SizeToContent="WidthAndHeight"
13+
Width="400" SizeToContent="Height"
1214
CanResize="False"
1315
WindowStartupLocation="CenterOwner">
1416
<Grid RowDefinitions="Auto,*">
@@ -40,13 +42,14 @@
4042
</Border>
4143

4244
<v:BranchOrTagNameTextBox x:Name="TxtName"
43-
MinWidth="300" Height="32"
45+
Height="32"
46+
Text="{Binding BranchName, Mode=TwoWay}"
4447
Margin="16,8,16,0"
4548
CornerRadius="3"
4649
Focusable="True">
4750
<TextBox.InnerLeftContent>
4851
<Border Height="22" CornerRadius="10" Margin="4,0,0,0" Background="Green">
49-
<TextBlock x:Name="TxtPrefix" Text="REMOTE" Foreground="White" Margin="8,0"/>
52+
<TextBlock x:Name="TxtPrefix" Text="{Binding Remote, Mode=OneWay}" Foreground="White" Margin="8,0"/>
5053
</Border>
5154
</TextBox.InnerLeftContent>
5255
</v:BranchOrTagNameTextBox>
@@ -63,7 +66,6 @@
6366
Content="{DynamicResource Text.Cancel}"
6467
Click="OnCancel"/>
6568
</StackPanel>
66-
67-
</StackPanel>
69+
</StackPanel>
6870
</Grid>
6971
</v:ChromelessWindow>

src/Views/PushToNewBranch.axaml.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@ protected override void OnLoaded(RoutedEventArgs e)
1717
TxtName.Focus(NavigationMethod.Directional);
1818
}
1919

20-
public void SetRemote(string remote)
21-
{
22-
TxtPrefix.Text = remote;
23-
}
24-
2520
private void OnSure(object _1, RoutedEventArgs _2)
2621
{
27-
Close(TxtName.Text);
22+
if (DataContext is ViewModels.PushToNewBranch { HasErrors: false } vm && vm.Check())
23+
Close(vm.BranchName);
2824
}
2925

3026
private void OnCancel(object _1, RoutedEventArgs _2)

0 commit comments

Comments
 (0)