From 0860245674b7f3e42277eaf1e9aa438d4a2853de Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 7 Mar 2025 15:44:50 +0800 Subject: [PATCH] code_style: simple window do not using DataContext Signed-off-by: leo --- src/App.axaml.cs | 12 +++++-- src/Views/Askpass.axaml | 20 +++++------ src/Views/Askpass.axaml.cs | 34 ++----------------- src/Views/ChangeViewModeSwitcher.axaml | 14 ++++---- src/Views/ChangeViewModeSwitcher.axaml.cs | 19 +++++++++-- .../StandaloneCommitMessageEditor.axaml.cs | 20 +++++------ 6 files changed, 54 insertions(+), 65 deletions(-) diff --git a/src/App.axaml.cs b/src/App.axaml.cs index 95b396de..25e32323 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -445,10 +445,14 @@ namespace SourceGit var file = args[1]; if (!File.Exists(file)) + { desktop.Shutdown(-1); - else - desktop.MainWindow = new Views.StandaloneCommitMessageEditor(file); + return true; + } + var editor = new Views.StandaloneCommitMessageEditor(); + editor.SetFile(file); + desktop.MainWindow = editor; return true; } @@ -461,7 +465,9 @@ namespace SourceGit var args = desktop.Args; if (args?.Length > 0) { - desktop.MainWindow = new Views.Askpass(args[0]); + var askpass = new Views.Askpass(); + askpass.TxtDescription.Text = args[0]; + desktop.MainWindow = askpass; return true; } diff --git a/src/Views/Askpass.axaml b/src/Views/Askpass.axaml index f2308805..811f9c3c 100644 --- a/src/Views/Askpass.axaml +++ b/src/Views/Askpass.axaml @@ -5,7 +5,7 @@ xmlns:v="using:SourceGit.Views" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="SourceGit.Views.Askpass" - x:DataType="v:Askpass" + x:Name="ThisControl" Icon="/App.ico" Title="{DynamicResource Text.Askpass}" SizeToContent="WidthAndHeight" @@ -13,7 +13,7 @@ WindowStartupLocation="CenterScreen"> - + @@ -36,23 +36,23 @@ - + - + Width="26" Height="14"/> diff --git a/src/Views/Askpass.axaml.cs b/src/Views/Askpass.axaml.cs index 673fd8ef..bbe26e98 100644 --- a/src/Views/Askpass.axaml.cs +++ b/src/Views/Askpass.axaml.cs @@ -1,43 +1,12 @@ using System; - -using Avalonia; using Avalonia.Interactivity; namespace SourceGit.Views { public partial class Askpass : ChromelessWindow { - public static readonly StyledProperty ShowPasswordProperty = - AvaloniaProperty.Register(nameof(ShowPassword)); - - public bool ShowPassword - { - get => GetValue(ShowPasswordProperty); - set => SetValue(ShowPasswordProperty, value); - } - - public string Description - { - get; - private set; - } = string.Empty; - - public string Passphrase - { - get; - set; - } = string.Empty; - public Askpass() { - DataContext = this; - InitializeComponent(); - } - - public Askpass(string description) - { - Description = description; - DataContext = this; InitializeComponent(); } @@ -49,7 +18,8 @@ namespace SourceGit.Views private void EnterPassword(object _1, RoutedEventArgs _2) { - Console.Out.Write($"{Passphrase}\n"); + var passphrase = TxtPassphrase.Text ?? string.Empty; + Console.Out.Write($"{passphrase}\n"); App.Quit(0); } } diff --git a/src/Views/ChangeViewModeSwitcher.axaml b/src/Views/ChangeViewModeSwitcher.axaml index 4ded60c7..4825c2e2 100644 --- a/src/Views/ChangeViewModeSwitcher.axaml +++ b/src/Views/ChangeViewModeSwitcher.axaml @@ -6,21 +6,21 @@ xmlns:v="using:SourceGit.Views" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="SourceGit.Views.ChangeViewModeSwitcher" - x:DataType="v:ChangeViewModeSwitcher"> + x:Name="ThisControl"> diff --git a/src/Views/ChangeViewModeSwitcher.axaml.cs b/src/Views/ChangeViewModeSwitcher.axaml.cs index 0cb2c4a9..ed306619 100644 --- a/src/Views/ChangeViewModeSwitcher.axaml.cs +++ b/src/Views/ChangeViewModeSwitcher.axaml.cs @@ -1,5 +1,6 @@ using Avalonia; using Avalonia.Controls; +using Avalonia.Interactivity; namespace SourceGit.Views { @@ -16,13 +17,25 @@ namespace SourceGit.Views public ChangeViewModeSwitcher() { - DataContext = this; InitializeComponent(); } - public void SwitchMode(object param) + private void SwitchToList(object sender, RoutedEventArgs e) { - ViewMode = (Models.ChangeViewMode)param; + ViewMode = Models.ChangeViewMode.List; + e.Handled = true; + } + + private void SwitchToGrid(object sender, RoutedEventArgs e) + { + ViewMode = Models.ChangeViewMode.Grid; + e.Handled = true; + } + + private void SwitchToTree(object sender, RoutedEventArgs e) + { + ViewMode = Models.ChangeViewMode.Tree; + e.Handled = true; } } } diff --git a/src/Views/StandaloneCommitMessageEditor.axaml.cs b/src/Views/StandaloneCommitMessageEditor.axaml.cs index 6833fb33..8a49ee74 100644 --- a/src/Views/StandaloneCommitMessageEditor.axaml.cs +++ b/src/Views/StandaloneCommitMessageEditor.axaml.cs @@ -9,18 +9,14 @@ namespace SourceGit.Views { public StandaloneCommitMessageEditor() { - _file = string.Empty; - DataContext = this; InitializeComponent(); } - public StandaloneCommitMessageEditor(string file) + public void SetFile(string file) { _file = file; - DataContext = this; - InitializeComponent(); - var content = File.ReadAllText(file).ReplaceLineEndings("\n"); + var content = File.ReadAllText(file).ReplaceLineEndings("\n").Trim(); var firstLineEnd = content.IndexOf('\n'); if (firstLineEnd == -1) { @@ -29,7 +25,7 @@ namespace SourceGit.Views else { Editor.SubjectEditor.Text = content.Substring(0, firstLineEnd); - Editor.DescriptionEditor.Text = content.Substring(firstLineEnd + 1); + Editor.DescriptionEditor.Text = content.Substring(firstLineEnd + 1).Trim(); } } @@ -41,12 +37,16 @@ namespace SourceGit.Views private void SaveAndClose(object _1, RoutedEventArgs _2) { - File.WriteAllText(_file, Editor.Text); - _exitCode = 0; + if (!string.IsNullOrEmpty(_file)) + { + File.WriteAllText(_file, Editor.Text); + _exitCode = 0; + } + Close(); } - private readonly string _file; + private string _file = string.Empty; private int _exitCode = -1; } }