[ColorPicker][Settings]Fix duplicate name crash and cleanup (#28713)

This commit is contained in:
Davide Giacometti 2023-09-22 15:57:48 +02:00 committed by GitHub
parent 8d5aa9fe6c
commit b24ae12c5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 18 deletions

View File

@ -162,7 +162,7 @@
x:Uid="ColorFormatDialog"
IsPrimaryButtonEnabled="{Binding IsValid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
PrimaryButtonStyle="{ThemeResource AccentButtonStyle}"
SecondaryButtonClick="ColorFormatDialog_CancelButtonClick">
Closed="ColorFormatDialog_Closed">
<ContentDialog.DataContext>
<models:ColorFormatModel />
</ContentDialog.DataContext>

View File

@ -112,7 +112,6 @@ namespace Microsoft.PowerToys.Settings.UI.Views
ColorFormatModel colorFormat = ColorFormatDialog.DataContext as ColorFormatModel;
string oldName = ((KeyValuePair<string, string>)ColorFormatDialog.Tag).Key;
ViewModel.UpdateColorFormat(oldName, colorFormat);
ColorFormatDialog.Hide();
}
private async void NewFormatClick(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
@ -127,19 +126,6 @@ namespace Microsoft.PowerToys.Settings.UI.Views
await ColorFormatDialog.ShowAsync();
}
private void ColorFormatDialog_CancelButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
if (ColorFormatDialog.Tag is KeyValuePair<string, string>)
{
ColorFormatModel modifiedColorFormat = ColorFormatDialog.DataContext as ColorFormatModel;
KeyValuePair<string, string> oldProperties = (KeyValuePair<string, string>)ColorFormatDialog.Tag;
modifiedColorFormat.Name = oldProperties.Key;
modifiedColorFormat.Format = oldProperties.Value;
}
ColorFormatDialog.Hide();
}
private async void EditButton_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
SettingsCard btn = sender as SettingsCard;
@ -162,5 +148,16 @@ namespace Microsoft.PowerToys.Settings.UI.Views
{
ViewModel.RefreshEnabledState();
}
private void ColorFormatDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args)
{
if (args.Result != ContentDialogResult.Primary && ColorFormatDialog.Tag is KeyValuePair<string, string>)
{
ColorFormatModel modifiedColorFormat = ColorFormatDialog.DataContext as ColorFormatModel;
KeyValuePair<string, string> oldProperties = (KeyValuePair<string, string>)ColorFormatDialog.Tag;
modifiedColorFormat.Name = oldProperties.Key;
modifiedColorFormat.Format = oldProperties.Value;
}
}
}
}

View File

@ -300,13 +300,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
UpdateColorFormats();
UpdateColorFormatPreview();
ScheduleSavingOfSettings();
}
private void ColorFormat_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
UpdateColorFormats();
ScheduleSavingOfSettings();
// Remaining properties are handled by the collection and by the dialog
if (e.PropertyName == nameof(ColorFormatModel.IsShown))
{
UpdateColorFormats();
ScheduleSavingOfSettings();
}
}
private void ScheduleSavingOfSettings()
@ -437,6 +440,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
SelectedColorRepresentationValue = colorFormat.Name; // name might be changed by the user
}
UpdateColorFormats();
UpdateColorFormatPreview();
}