Skip to content

Commit

Permalink
Use Avalonia 11 generated view codes (#1479)
Browse files Browse the repository at this point in the history
Avalonia 11 automatically generates some view codes.

- Properties for controls with `x:Name` or `Name`.
- `InitializeComponent()` method to initialize such properties.
- DevTools for debug builds.

This change uses such generated codes in views and reduces manual
efforts. The names of the controls are changed to start with underscore,
so that the generated property names align with the naming style in this
project.
  • Loading branch information
mjcheetham authored Dec 5, 2023
2 parents df96311 + 2e16a72 commit 387fa93
Show file tree
Hide file tree
Showing 22 changed files with 44 additions and 176 deletions.
8 changes: 4 additions & 4 deletions src/shared/Atlassian.Bitbucket/UI/Views/CredentialsView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
Margin="0,10,0,0"/>
</StackPanel>

<TabControl x:Name="authModesTabControl"
<TabControl x:Name="_authModesTabControl"
VerticalContentAlignment="Center"
AutoScrollToSelectedItem="True"
Width="288"
Expand All @@ -48,7 +48,7 @@
<TextBlock Text="Browser" FontSize="12" />
</TabItem.Header>
<StackPanel Margin="0,15">
<Button x:Name="oauthLoginButton" IsDefault="True"
<Button x:Name="_oauthLoginButton" IsDefault="True"
HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
Command="{Binding OAuthCommand}"
Classes="accent" Height="40" Padding="15,5"
Expand All @@ -61,11 +61,11 @@
<TextBlock Text="Password/Token" FontSize="12" />
</TabItem.Header>
<StackPanel Margin="0,15">
<TextBox x:Name="userNameTextBox" Margin="0,0,0,10"
<TextBox x:Name="_userNameTextBox" Margin="0,0,0,10"
HorizontalAlignment="Stretch"
Watermark="Email or username"
Text="{Binding UserName}" />
<TextBox x:Name="passwordTextBox" Margin="0,0,0,10"
<TextBox x:Name="_passwordTextBox" Margin="0,0,0,10"
HorizontalAlignment="Stretch"
Watermark="Password or token" PasswordChar=""
Text="{Binding Password}" />
Expand Down
19 changes: 2 additions & 17 deletions src/shared/Atlassian.Bitbucket/UI/Views/CredentialsView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,11 @@ namespace Atlassian.Bitbucket.UI.Views
{
public partial class CredentialsView : UserControl, IFocusable
{
private TabControl _tabControl;
private Button _oauthLoginButton;
private TextBox _userNameTextBox;
private TextBox _passwordTextBox;

public CredentialsView()
{
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);

_tabControl = this.FindControl<TabControl>("authModesTabControl");
_oauthLoginButton = this.FindControl<Button>("oauthLoginButton");
_userNameTextBox = this.FindControl<TextBox>("userNameTextBox");
_passwordTextBox = this.FindControl<TextBox>("passwordTextBox");
}

public void SetFocus()
{
if (!(DataContext is CredentialsViewModel vm))
Expand All @@ -37,12 +22,12 @@ public void SetFocus()

if (vm.ShowOAuth)
{
_tabControl.SelectedIndex = 0;
_authModesTabControl.SelectedIndex = 0;
_oauthLoginButton.Focus();
}
else if (vm.ShowBasic)
{
_tabControl.SelectedIndex = 1;
_authModesTabControl.SelectedIndex = 1;
if (string.IsNullOrWhiteSpace(vm.UserName))
{
// Workaround: https://github.com/git-ecosystem/git-credential-manager/issues/1293
Expand Down
8 changes: 0 additions & 8 deletions src/shared/Core/UI/Controls/AboutWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ public partial class AboutWindow : Window
public AboutWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}

private void ProjectButton_Click(object sender, RoutedEventArgs e)
Expand Down
2 changes: 1 addition & 1 deletion src/shared/Core/UI/Controls/DialogWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
Content="Show Custom Chrome" Margin="5,0"/>
</StackPanel>

<ContentControl x:Name="contentHolder" Margin="30,25,30,30"/>
<ContentControl x:Name="_contentHolder" Margin="30,25,30,30"/>
</DockPanel>
</Border>

Expand Down
11 changes: 0 additions & 11 deletions src/shared/Core/UI/Controls/DialogWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace GitCredentialManager.UI.Controls
public partial class DialogWindow : Window
{
private readonly Control _view;
private ContentControl _contentHolder;

public DialogWindow() : this(null)
{
Expand All @@ -22,20 +21,10 @@ public DialogWindow() : this(null)
public DialogWindow(Control view)
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
_view = view;
_contentHolder.Content = _view;
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);

_contentHolder = this.FindControl<ContentControl>("contentHolder");
}

protected override void OnDataContextChanged(EventArgs e)
{
if (DataContext is WindowViewModel vm)
Expand Down
5 changes: 0 additions & 5 deletions src/shared/Core/UI/Controls/ProgressWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ public ProgressWindow()
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}

public static IntPtr ShowAndGetHandle(CancellationToken ct)
{
var tsc = new TaskCompletionSource<IntPtr>();
Expand Down
4 changes: 2 additions & 2 deletions src/shared/Core/UI/Views/CredentialsView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
</StackPanel>

<StackPanel Margin="20,0">
<TextBox x:Name="userNameTextBox"
<TextBox x:Name="_userNameTextBox"
Watermark="Username" Margin="0,0,0,10"
Text="{Binding UserName}"/>
<TextBox x:Name="passwordTextBox"
<TextBox x:Name="_passwordTextBox"
Watermark="Password" Margin="0,0,0,20"
PasswordChar=""
Text="{Binding Password}"/>
Expand Down
11 changes: 0 additions & 11 deletions src/shared/Core/UI/Views/CredentialsView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,11 @@ namespace GitCredentialManager.UI.Views
{
public partial class CredentialsView : UserControl, IFocusable
{
private TextBox _userNameTextBox;
private TextBox _passwordTextBox;

public CredentialsView()
{
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);

_userNameTextBox = this.FindControl<TextBox>("userNameTextBox");
_passwordTextBox = this.FindControl<TextBox>("passwordTextBox");
}

public void SetFocus()
{
if (!(DataContext is CredentialsViewModel vm))
Expand Down
5 changes: 0 additions & 5 deletions src/shared/Core/UI/Views/DefaultAccountView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,5 @@ public DefaultAccountView()
{
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}
5 changes: 0 additions & 5 deletions src/shared/Core/UI/Views/DeviceCodeView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,5 @@ public DeviceCodeView()
{
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}
5 changes: 0 additions & 5 deletions src/shared/Core/UI/Views/OAuthView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,5 @@ public OAuthView()
{
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,5 @@ public HorizontalShadowDivider()
{
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}
12 changes: 6 additions & 6 deletions src/shared/GitHub/UI/Controls/SixDigitInput.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
</Style>
</UserControl.Styles>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="one"/>
<TextBox x:Name="two"/>
<TextBox x:Name="three"/>
<TextBox x:Name="four"/>
<TextBox x:Name="five"/>
<TextBox x:Name="six" Margin="0"/>
<TextBox x:Name="_one"/>
<TextBox x:Name="_two"/>
<TextBox x:Name="_three"/>
<TextBox x:Name="_four"/>
<TextBox x:Name="_five"/>
<TextBox x:Name="_six" Margin="0"/>
</StackPanel>
</UserControl>
17 changes: 6 additions & 11 deletions src/shared/GitHub/UI/Controls/SixDigitInput.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,15 @@ public partial class SixDigitInput : UserControl, IFocusable
public SixDigitInput()
{
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);

_textBoxes = new[]
{
this.FindControl<TextBox>("one"),
this.FindControl<TextBox>("two"),
this.FindControl<TextBox>("three"),
this.FindControl<TextBox>("four"),
this.FindControl<TextBox>("five"),
this.FindControl<TextBox>("six"),
_one,
_two,
_three,
_four,
_five,
_six,
};

foreach (TextBox textBox in _textBoxes)
Expand Down
12 changes: 6 additions & 6 deletions src/shared/GitHub/UI/Views/CredentialsView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
Classes="hyperlink"/>
</WrapPanel>

<TabControl x:Name="authModesTabControl"
<TabControl x:Name="_authModesTabControl"
VerticalContentAlignment="Center"
AutoScrollToSelectedItem="True">
<TabControl.Styles>
Expand All @@ -62,15 +62,15 @@
<TextBlock Text="{Binding OAuthModeTitle}" FontSize="12" />
</TabItem.Header>
<StackPanel Margin="0,10">
<Button x:Name="signInBrowserButton"
<Button x:Name="_signInBrowserButton"
Content="Sign in with your browser"
IsDefault="True"
Command="{Binding SignInBrowserCommand}"
IsVisible="{Binding ShowBrowserLogin}"
HorizontalAlignment="Center"
Margin="0,0,0,10"
Classes="accent"/>
<Button x:Name="signInDeviceButton"
<Button x:Name="_signInDeviceButton"
Content="Sign in with a code"
Command="{Binding SignInDeviceCommand}"
IsVisible="{Binding ShowDeviceLogin}"
Expand All @@ -85,7 +85,7 @@
<TextBlock Text="Token" FontSize="12" />
</TabItem.Header>
<StackPanel Margin="0,10">
<TextBox x:Name="tokenTextBox"
<TextBox x:Name="_tokenTextBox"
Watermark="Personal access token" Margin="0,0,0,10"
PasswordChar=""
Text="{Binding Token}"/>
Expand All @@ -103,10 +103,10 @@
<TextBlock Text="Password" FontSize="12" />
</TabItem.Header>
<StackPanel Margin="0,10">
<TextBox x:Name="userNameTextBox"
<TextBox x:Name="_userNameTextBox"
Watermark="Username or email" Margin="0,0,0,10"
Text="{Binding UserName}"/>
<TextBox x:Name="passwordTextBox"
<TextBox x:Name="_passwordTextBox"
Watermark="Password" Margin="0,0,0,10"
PasswordChar=""
Text="{Binding Password}"/>
Expand Down
31 changes: 6 additions & 25 deletions src/shared/GitHub/UI/Views/CredentialsView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,11 @@ namespace GitHub.UI.Views
{
public partial class CredentialsView : UserControl, IFocusable
{
private TabControl _tabControl;
private Button _browserButton;
private Button _deviceButton;
private TextBox _tokenTextBox;
private TextBox _userNameTextBox;
private TextBox _passwordTextBox;

public CredentialsView()
{
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);

_tabControl = this.FindControl<TabControl>("authModesTabControl");
_browserButton = this.FindControl<Button>("signInBrowserButton");
_deviceButton = this.FindControl<Button>("signInDeviceButton");
_tokenTextBox = this.FindControl<TextBox>("tokenTextBox");
_userNameTextBox = this.FindControl<TextBox>("userNameTextBox");
_passwordTextBox = this.FindControl<TextBox>("passwordTextBox");
}

public void SetFocus()
{
if (!(DataContext is CredentialsViewModel vm))
Expand All @@ -43,25 +24,25 @@ public void SetFocus()
// and focus on the button/text box
if (vm.ShowBrowserLogin)
{
_tabControl.SelectedIndex = 0;
_browserButton.Focus();
_authModesTabControl.SelectedIndex = 0;
_signInBrowserButton.Focus();
}
else if (vm.ShowDeviceLogin)
{
_tabControl.SelectedIndex = 0;
_deviceButton.Focus();
_authModesTabControl.SelectedIndex = 0;
_signInDeviceButton.Focus();
}
else if (vm.ShowTokenLogin)
{
_tabControl.SelectedIndex = 1;
_authModesTabControl.SelectedIndex = 1;
// Workaround: https://github.com/git-ecosystem/git-credential-manager/issues/1293
if (!PlatformUtils.IsMacOS())
_tokenTextBox.Focus();

}
else if (vm.ShowBasicLogin)
{
_tabControl.SelectedIndex = 2;
_authModesTabControl.SelectedIndex = 2;
if (string.IsNullOrWhiteSpace(vm.UserName))
{
// Workaround: https://github.com/git-ecosystem/git-credential-manager/issues/1293
Expand Down
5 changes: 0 additions & 5 deletions src/shared/GitHub/UI/Views/DeviceCodeView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,5 @@ public DeviceCodeView()
{
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}
5 changes: 0 additions & 5 deletions src/shared/GitHub/UI/Views/SelectAccountView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ public SelectAccountView()
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}

private void ListBox_OnDoubleTapped(object sender, TappedEventArgs e)
{
if (DataContext is SelectAccountViewModel { SelectedAccount: not null } vm)
Expand Down
Loading

0 comments on commit 387fa93

Please sign in to comment.