Skip to content

Commit 0860245

Browse files
committed
code_style: simple window do not using DataContext
Signed-off-by: leo <longshuang@msn.cn>
1 parent 43fed8e commit 0860245

File tree

6 files changed

+54
-65
lines changed

6 files changed

+54
-65
lines changed

src/App.axaml.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,14 @@ private bool TryLaunchAsCoreEditor(IClassicDesktopStyleApplicationLifetime deskt
445445

446446
var file = args[1];
447447
if (!File.Exists(file))
448+
{
448449
desktop.Shutdown(-1);
449-
else
450-
desktop.MainWindow = new Views.StandaloneCommitMessageEditor(file);
450+
return true;
451+
}
451452

453+
var editor = new Views.StandaloneCommitMessageEditor();
454+
editor.SetFile(file);
455+
desktop.MainWindow = editor;
452456
return true;
453457
}
454458

@@ -461,7 +465,9 @@ private bool TryLaunchAsAskpass(IClassicDesktopStyleApplicationLifetime desktop)
461465
var args = desktop.Args;
462466
if (args?.Length > 0)
463467
{
464-
desktop.MainWindow = new Views.Askpass(args[0]);
468+
var askpass = new Views.Askpass();
469+
askpass.TxtDescription.Text = args[0];
470+
desktop.MainWindow = askpass;
465471
return true;
466472
}
467473

src/Views/Askpass.axaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
xmlns:v="using:SourceGit.Views"
66
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
77
x:Class="SourceGit.Views.Askpass"
8-
x:DataType="v:Askpass"
8+
x:Name="ThisControl"
99
Icon="/App.ico"
1010
Title="{DynamicResource Text.Askpass}"
1111
SizeToContent="WidthAndHeight"
1212
CanResize="False"
1313
WindowStartupLocation="CenterScreen">
1414
<Grid RowDefinitions="Auto,*">
1515
<!-- TitleBar -->
16-
<Grid Grid.Row="0" Height="28" IsVisible="{Binding !UseSystemWindowFrame}">
16+
<Grid Grid.Row="0" Height="28" IsVisible="{Binding !#ThisControl.UseSystemWindowFrame}">
1717
<Border Background="{DynamicResource Brush.TitleBar}"
1818
BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border0}"
1919
PointerPressed="BeginMoveWindow"/>
@@ -36,23 +36,23 @@
3636

3737
<StackPanel Grid.Row="1" Margin="0,16" Orientation="Vertical">
3838
<Border Margin="16,0">
39-
<TextBlock Text="{Binding Description}" TextWrapping="Wrap"/>
39+
<TextBlock x:Name="TxtDescription" Text="Enter passphrase:" TextWrapping="Wrap"/>
4040
</Border>
4141

42-
<TextBox Margin="16"
43-
MinWidth="300"
44-
Height="32"
42+
<TextBox x:Name="TxtPassphrase"
43+
Margin="16"
44+
MinWidth="300"
45+
Height="32"
4546
Focusable="True"
46-
Text="{Binding Passphrase, Mode=TwoWay}"
4747
PasswordChar="*"
48-
RevealPassword="{Binding ShowPassword, Mode=OneWay}"
48+
RevealPassword="{Binding #ToggleShowPassword.IsChecked, Mode=OneWay}"
4949
HorizontalAlignment="Stretch"
5050
v:AutoFocusBehaviour.IsEnabled="True">
5151
<TextBox.InnerRightContent>
5252
<ToggleButton Grid.Column="6"
53+
x:Name="ToggleShowPassword"
5354
Classes="toggle_untracked"
54-
Width="26" Height="14"
55-
IsChecked="{Binding ShowPassword, Mode=TwoWay}"/>
55+
Width="26" Height="14"/>
5656
</TextBox.InnerRightContent>
5757
</TextBox>
5858

src/Views/Askpass.axaml.cs

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,12 @@
11
using System;
2-
3-
using Avalonia;
42
using Avalonia.Interactivity;
53

64
namespace SourceGit.Views
75
{
86
public partial class Askpass : ChromelessWindow
97
{
10-
public static readonly StyledProperty<bool> ShowPasswordProperty =
11-
AvaloniaProperty.Register<Askpass, bool>(nameof(ShowPassword));
12-
13-
public bool ShowPassword
14-
{
15-
get => GetValue(ShowPasswordProperty);
16-
set => SetValue(ShowPasswordProperty, value);
17-
}
18-
19-
public string Description
20-
{
21-
get;
22-
private set;
23-
} = string.Empty;
24-
25-
public string Passphrase
26-
{
27-
get;
28-
set;
29-
} = string.Empty;
30-
318
public Askpass()
329
{
33-
DataContext = this;
34-
InitializeComponent();
35-
}
36-
37-
public Askpass(string description)
38-
{
39-
Description = description;
40-
DataContext = this;
4110
InitializeComponent();
4211
}
4312

@@ -49,7 +18,8 @@ private void CloseWindow(object _1, RoutedEventArgs _2)
4918

5019
private void EnterPassword(object _1, RoutedEventArgs _2)
5120
{
52-
Console.Out.Write($"{Passphrase}\n");
21+
var passphrase = TxtPassphrase.Text ?? string.Empty;
22+
Console.Out.Write($"{passphrase}\n");
5323
App.Quit(0);
5424
}
5525
}

src/Views/ChangeViewModeSwitcher.axaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
xmlns:v="using:SourceGit.Views"
77
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
88
x:Class="SourceGit.Views.ChangeViewModeSwitcher"
9-
x:DataType="v:ChangeViewModeSwitcher">
9+
x:Name="ThisControl">
1010
<Button Classes="icon_button" ToolTip.Tip="{DynamicResource Text.ChangeDisplayMode}">
1111
<Button.Flyout>
1212
<MenuFlyout Placement="BottomEdgeAlignedLeft">
13-
<MenuItem Header="{DynamicResource Text.ChangeDisplayMode.List}" Command="{Binding SwitchMode}" CommandParameter="{x:Static m:ChangeViewMode.List}">
13+
<MenuItem Header="{DynamicResource Text.ChangeDisplayMode.List}" Click="SwitchToList">
1414
<MenuItem.Icon>
1515
<Path Width="12" Height="12" Data="{StaticResource Icons.List}"/>
1616
</MenuItem.Icon>
1717
</MenuItem>
18-
<MenuItem Header="{DynamicResource Text.ChangeDisplayMode.Grid}" Command="{Binding SwitchMode}" CommandParameter="{x:Static m:ChangeViewMode.Grid}">
18+
<MenuItem Header="{DynamicResource Text.ChangeDisplayMode.Grid}" Click="SwitchToGrid">
1919
<MenuItem.Icon>
2020
<Path Width="12" Height="12" Data="{StaticResource Icons.Grid}"/>
2121
</MenuItem.Icon>
2222
</MenuItem>
23-
<MenuItem Header="{DynamicResource Text.ChangeDisplayMode.Tree}" Command="{Binding SwitchMode}" CommandParameter="{x:Static m:ChangeViewMode.Tree}">
23+
<MenuItem Header="{DynamicResource Text.ChangeDisplayMode.Tree}" Click="SwitchToTree">
2424
<MenuItem.Icon>
2525
<Path Width="12" Height="12" Data="{StaticResource Icons.Tree}"/>
2626
</MenuItem.Icon>
@@ -31,13 +31,13 @@
3131
<Grid Width="14" Height="14" HorizontalAlignment="Center" VerticalAlignment="Center">
3232
<Path Width="14" Height="14"
3333
Data="{StaticResource Icons.List}"
34-
IsVisible="{Binding ViewMode, Converter={x:Static ObjectConverters.Equal}, ConverterParameter={x:Static m:ChangeViewMode.List}}"/>
34+
IsVisible="{Binding #ThisControl.ViewMode, Converter={x:Static ObjectConverters.Equal}, ConverterParameter={x:Static m:ChangeViewMode.List}}"/>
3535
<Path Width="14" Height="14"
3636
Data="{StaticResource Icons.Grid}"
37-
IsVisible="{Binding ViewMode, Converter={x:Static ObjectConverters.Equal}, ConverterParameter={x:Static m:ChangeViewMode.Grid}}"/>
37+
IsVisible="{Binding #ThisControl.ViewMode, Converter={x:Static ObjectConverters.Equal}, ConverterParameter={x:Static m:ChangeViewMode.Grid}}"/>
3838
<Path Width="14" Height="14"
3939
Data="{StaticResource Icons.Tree}"
40-
IsVisible="{Binding ViewMode, Converter={x:Static ObjectConverters.Equal}, ConverterParameter={x:Static m:ChangeViewMode.Tree}}"/>
40+
IsVisible="{Binding #ThisControl.ViewMode, Converter={x:Static ObjectConverters.Equal}, ConverterParameter={x:Static m:ChangeViewMode.Tree}}"/>
4141
</Grid>
4242
</Button>
4343
</UserControl>

src/Views/ChangeViewModeSwitcher.axaml.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Avalonia;
22
using Avalonia.Controls;
3+
using Avalonia.Interactivity;
34

45
namespace SourceGit.Views
56
{
@@ -16,13 +17,25 @@ public Models.ChangeViewMode ViewMode
1617

1718
public ChangeViewModeSwitcher()
1819
{
19-
DataContext = this;
2020
InitializeComponent();
2121
}
2222

23-
public void SwitchMode(object param)
23+
private void SwitchToList(object sender, RoutedEventArgs e)
2424
{
25-
ViewMode = (Models.ChangeViewMode)param;
25+
ViewMode = Models.ChangeViewMode.List;
26+
e.Handled = true;
27+
}
28+
29+
private void SwitchToGrid(object sender, RoutedEventArgs e)
30+
{
31+
ViewMode = Models.ChangeViewMode.Grid;
32+
e.Handled = true;
33+
}
34+
35+
private void SwitchToTree(object sender, RoutedEventArgs e)
36+
{
37+
ViewMode = Models.ChangeViewMode.Tree;
38+
e.Handled = true;
2639
}
2740
}
2841
}

src/Views/StandaloneCommitMessageEditor.axaml.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,14 @@ public partial class StandaloneCommitMessageEditor : ChromelessWindow
99
{
1010
public StandaloneCommitMessageEditor()
1111
{
12-
_file = string.Empty;
13-
DataContext = this;
1412
InitializeComponent();
1513
}
1614

17-
public StandaloneCommitMessageEditor(string file)
15+
public void SetFile(string file)
1816
{
1917
_file = file;
20-
DataContext = this;
21-
InitializeComponent();
2218

23-
var content = File.ReadAllText(file).ReplaceLineEndings("\n");
19+
var content = File.ReadAllText(file).ReplaceLineEndings("\n").Trim();
2420
var firstLineEnd = content.IndexOf('\n');
2521
if (firstLineEnd == -1)
2622
{
@@ -29,7 +25,7 @@ public StandaloneCommitMessageEditor(string file)
2925
else
3026
{
3127
Editor.SubjectEditor.Text = content.Substring(0, firstLineEnd);
32-
Editor.DescriptionEditor.Text = content.Substring(firstLineEnd + 1);
28+
Editor.DescriptionEditor.Text = content.Substring(firstLineEnd + 1).Trim();
3329
}
3430
}
3531

@@ -41,12 +37,16 @@ protected override void OnClosed(EventArgs e)
4137

4238
private void SaveAndClose(object _1, RoutedEventArgs _2)
4339
{
44-
File.WriteAllText(_file, Editor.Text);
45-
_exitCode = 0;
40+
if (!string.IsNullOrEmpty(_file))
41+
{
42+
File.WriteAllText(_file, Editor.Text);
43+
_exitCode = 0;
44+
}
45+
4646
Close();
4747
}
4848

49-
private readonly string _file;
49+
private string _file = string.Empty;
5050
private int _exitCode = -1;
5151
}
5252
}

0 commit comments

Comments
 (0)