Skip to content

Commit e680e79

Browse files
committed
Set adjustable button width in settings, use readonly commands
1 parent 6dd9e37 commit e680e79

File tree

6 files changed

+96
-72
lines changed

6 files changed

+96
-72
lines changed

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@
554554
<DependentUpon>SearchView.xaml</DependentUpon>
555555
</Compile>
556556
<Compile Include="UI\FolderBrowser.cs" />
557+
<Compile Include="UI\GeneralConverters\EqualWidthConverter.cs" />
557558
<Compile Include="UI\IMessageBox.cs" />
558559
<Compile Include="UI\ParserErrors\ParseErrorListItem.cs" />
559560
<Compile Include="UI\Refactorings\EncapsulateFieldDialog.cs">
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Linq;
4+
using System.Windows.Data;
5+
6+
namespace Rubberduck.UI.GeneralConverters
7+
{
8+
public class EqualWidthConverter : IMultiValueConverter
9+
{
10+
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
11+
{
12+
return Math.Abs(values.Cast<double>().Max()) < .1 ? -1 : values.Cast<double>().Max();
13+
}
14+
15+
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
16+
{
17+
return new object[] { };
18+
}
19+
}
20+
}

RetailCoder.VBE/UI/Settings/SettingsControl.xaml

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:settings="clr-namespace:Rubberduck.UI.Settings"
77
xmlns:converters="clr-namespace:Rubberduck.UI.Settings.Converters"
8+
xmlns:generalConverters="clr-namespace:Rubberduck.UI.GeneralConverters"
89
mc:Ignorable="d"
910
d:DesignHeight="300" d:DesignWidth="600"
1011
d:DataContext="{d:DesignInstance {x:Type settings:SettingsControlViewModel}, IsDesignTimeCreatable=False}">
1112
<UserControl.Resources>
13+
<generalConverters:EqualWidthConverter x:Key="EqualWidthConverter" />
14+
1215
<LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" EndPoint="0,1" StartPoint="0,0">
1316
<GradientStop Color="#FFD9F4FF" Offset="0"/>
1417
<GradientStop Color="#FF9BDDFB" Offset="1"/>
@@ -95,24 +98,48 @@
9598
Background="{x:Static SystemColors.ControlDarkBrush}">
9699
<StackPanel Orientation="Horizontal"
97100
HorizontalAlignment="Right">
98-
<Button Content="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=Settings_ResetSettings}"
99-
MinWidth="75"
101+
<Button Name="ResetButton"
102+
Content="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=Settings_ResetSettings}"
100103
Height="20"
101104
HorizontalAlignment="Right"
102105
Margin="20,0"
103-
Command="{Binding ResetButtonCommand}" />
104-
<Button Content="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=OK}"
105-
MinWidth="75"
106+
Padding="10,0"
107+
Command="{Binding ResetButtonCommand}">
108+
<Button.MinWidth>
109+
<MultiBinding Converter="{StaticResource EqualWidthConverter}">
110+
<Binding ElementName="OkButton" Path="ActualWidth" />
111+
<Binding ElementName="CancelButton" Path="ActualWidth" />
112+
</MultiBinding>
113+
</Button.MinWidth>
114+
</Button>
115+
<Button Name="OkButton"
116+
Content="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=OK}"
106117
Height="20"
107118
HorizontalAlignment="Left"
108119
Margin="5,0"
109-
Command="{Binding OKButtonCommand}" />
110-
<Button Content="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=CancelButtonText}"
111-
MinWidth="75"
120+
Padding="10,0"
121+
Command="{Binding OKButtonCommand}">
122+
<Button.MinWidth>
123+
<MultiBinding Converter="{StaticResource EqualWidthConverter}">
124+
<Binding ElementName="CancelButton" Path="ActualWidth" />
125+
<Binding ElementName="ResetButton" Path="ActualWidth" />
126+
</MultiBinding>
127+
</Button.MinWidth>
128+
</Button>
129+
<Button Name="CancelButton"
130+
Content="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=CancelButtonText}"
112131
Height="20"
113132
HorizontalAlignment="Right"
114-
Margin="5,0,10,0"
115-
Command="{Binding CancelButtonCommand}" />
133+
Margin="5,0"
134+
Padding="10,0"
135+
Command="{Binding CancelButtonCommand}">
136+
<Button.MinWidth>
137+
<MultiBinding Converter="{StaticResource EqualWidthConverter}">
138+
<Binding ElementName="OkButton" Path="ActualWidth" />
139+
<Binding ElementName="ResetButton" Path="ActualWidth" />
140+
</MultiBinding>
141+
</Button.MinWidth>
142+
</Button>
116143
</StackPanel>
117144
</DockPanel>
118145
</Border>

RetailCoder.VBE/UI/Settings/SettingsControlViewModel.cs

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public SettingsControlViewModel(IGeneralConfigService configService,
3030
};
3131

3232
SelectedSettingsView = SettingsViews.First(v => v.View == activeView);
33+
34+
_okButtonCommand = new DelegateCommand(_ => SaveAndCloseWindow());
35+
_cancelButtonCommand = new DelegateCommand(_ => CloseWindow());
36+
_resetButtonCommand = new DelegateCommand(_ => ResetSettings());
3337
}
3438

3539
private ObservableCollection<SettingsView> _settingsViews;
@@ -75,73 +79,51 @@ private void SaveConfig()
7579
_configService.SaveConfiguration(_config, _config.UserSettings.GeneralSettings.Language.Code != oldLangCode);
7680
}
7781

78-
public event EventHandler OnOKButtonClicked;
79-
public event EventHandler OnCancelButtonClicked;
80-
81-
#region Commands
82-
83-
private ICommand _okButtonCommand;
84-
public ICommand OKButtonCommand
82+
private void CloseWindow()
8583
{
86-
get
84+
var handler = OnWindowClosed;
85+
if (handler != null)
8786
{
88-
if (_okButtonCommand != null)
89-
{
90-
return _okButtonCommand;
91-
}
92-
return _okButtonCommand = new DelegateCommand(_ =>
93-
{
94-
SaveConfig();
95-
96-
var handler = OnOKButtonClicked;
97-
if (handler != null)
98-
{
99-
handler(this, EventArgs.Empty);
100-
}
101-
});
87+
handler(this, EventArgs.Empty);
10288
}
10389
}
10490

105-
private ICommand _cancelButtonCommand;
106-
public ICommand CancelButtonCommand
91+
private void SaveAndCloseWindow()
10792
{
108-
get
93+
SaveConfig();
94+
CloseWindow();
95+
}
96+
97+
private void ResetSettings()
98+
{
99+
var defaultConfig = _configService.GetDefaultConfiguration();
100+
foreach (var vm in SettingsViews.Select(v => v.Control.ViewModel))
109101
{
110-
if (_cancelButtonCommand != null)
111-
{
112-
return _cancelButtonCommand;
113-
}
114-
return _cancelButtonCommand = new DelegateCommand(_ =>
115-
{
116-
var handler = OnCancelButtonClicked;
117-
if (handler != null)
118-
{
119-
handler(this, EventArgs.Empty);
120-
}
121-
});
102+
vm.SetToDefaults(defaultConfig);
122103
}
123104
}
124105

125-
private ICommand _resetButtonCommand;
126-
public ICommand ResetButtonCommand
106+
public event EventHandler OnWindowClosed;
107+
108+
private readonly ICommand _okButtonCommand;
109+
public ICommand OKButtonCommand
127110
{
128111
get
129112
{
130-
if (_resetButtonCommand != null)
131-
{
132-
return _resetButtonCommand;
133-
}
134-
return _resetButtonCommand = new DelegateCommand(_ =>
135-
{
136-
var defaultConfig = _configService.GetDefaultConfiguration();
137-
foreach (var vm in SettingsViews.Select(v => v.Control.ViewModel))
138-
{
139-
vm.SetToDefaults(defaultConfig);
140-
}
141-
});
113+
return _okButtonCommand;
142114
}
143115
}
144116

145-
#endregion
117+
private readonly ICommand _cancelButtonCommand;
118+
public ICommand CancelButtonCommand
119+
{
120+
get { return _cancelButtonCommand; }
121+
}
122+
123+
private readonly ICommand _resetButtonCommand;
124+
public ICommand ResetButtonCommand
125+
{
126+
get { return _resetButtonCommand; }
127+
}
146128
}
147129
}

RetailCoder.VBE/UI/Settings/SettingsForm.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,10 @@ public SettingsForm(IGeneralConfigService configService, SettingsViews activeVie
4343
},
4444
activeView);
4545

46-
ViewModel.OnOKButtonClicked += ViewModel_OnOKButtonClicked;
47-
ViewModel.OnCancelButtonClicked += ViewModel_OnCancelButtonClicked;
46+
ViewModel.OnWindowClosed += ViewModel_OnWindowClosed;
4847
}
4948

50-
void ViewModel_OnOKButtonClicked(object sender, System.EventArgs e)
51-
{
52-
Close();
53-
}
54-
55-
void ViewModel_OnCancelButtonClicked(object sender, System.EventArgs e)
49+
void ViewModel_OnWindowClosed(object sender, System.EventArgs e)
5650
{
5751
Close();
5852
}

RubberduckTests/Settings/SettingsControlTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public void OKButtonFiresEvent()
400400
var eventIsFired = false;
401401
var viewModel = GetDefaultViewModel(GetConfigLoader(GetDefaultConfig()));
402402

403-
viewModel.OnOKButtonClicked += (sender, args) => { eventIsFired = true; };
403+
viewModel.OnWindowClosed += (sender, args) => { eventIsFired = true; };
404404

405405
viewModel.OKButtonCommand.Execute(null);
406406

@@ -413,7 +413,7 @@ public void CancelButtonFiresEvent()
413413
var eventIsFired = false;
414414
var viewModel = GetDefaultViewModel(GetConfigLoader(GetDefaultConfig()));
415415

416-
viewModel.OnCancelButtonClicked += (sender, args) => { eventIsFired = true; };
416+
viewModel.OnWindowClosed += (sender, args) => { eventIsFired = true; };
417417

418418
viewModel.CancelButtonCommand.Execute(null);
419419

0 commit comments

Comments
 (0)