Skip to content

Commit 287422b

Browse files
authored
Clean up the Converters implementation (#16820)
I thought the Converters.idl file had a really neat ordering, and I felt like the .cpp implementation fell short of this. This PR reorders the functions in the implementation to match the IDL. It also gets rid of some unnecessary math (int vs. float, clamping) and removes another use of `std::stringstream` (= bad STL class).
1 parent a494548 commit 287422b

File tree

7 files changed

+64
-68
lines changed

7 files changed

+64
-68
lines changed

src/cascadia/TerminalSettingsEditor/Appearances.idl

-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ namespace Microsoft.Terminal.Settings.Editor
122122
Windows.Foundation.Collections.IObservableVector<Microsoft.Terminal.Settings.Editor.EnumEntry> FontWeightList { get; };
123123

124124
IInspectable CurrentFontFace { get; };
125-
Windows.UI.Xaml.Controls.Slider BIOpacitySlider { get; };
126125

127126
IInspectable CurrentIntenseTextStyle;
128127
Windows.Foundation.Collections.IObservableVector<Microsoft.Terminal.Settings.Editor.EnumEntry> IntenseTextStyleList { get; };

src/cascadia/TerminalSettingsEditor/Appearances.xaml

+2-3
Original file line numberDiff line numberDiff line change
@@ -739,13 +739,12 @@
739739
<ColumnDefinition Width="*" />
740740
<ColumnDefinition Width="Auto" />
741741
</Grid.ColumnDefinitions>
742-
<Slider x:Name="BIOpacitySlider"
743-
x:Uid="Profile_BackgroundImageOpacitySlider"
742+
<Slider x:Uid="Profile_BackgroundImageOpacitySlider"
744743
Grid.Column="0"
745744
Value="{x:Bind mtu:Converters.PercentageToPercentageValue(Appearance.BackgroundImageOpacity), BindBack=Appearance.SetBackgroundImageOpacityFromPercentageValue, Mode=TwoWay}" />
746745
<TextBlock Grid.Column="1"
747746
Style="{StaticResource SliderValueLabelStyle}"
748-
Text="{x:Bind mtu:Converters.AppendPercentageSign(BIOpacitySlider.Value), Mode=OneWay}" />
747+
Text="{x:Bind mtu:Converters.PercentageToPercentageString(Appearance.BackgroundImageOpacity), Mode=OneWay}" />
749748
</Grid>
750749
</local:SettingContainer>
751750
</StackPanel>

src/cascadia/TerminalSettingsEditor/Profiles_Appearance.idl

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@ namespace Microsoft.Terminal.Settings.Editor
1010
Profiles_Appearance();
1111
ProfileViewModel Profile { get; };
1212
IHostedInWindow WindowRoot { get; };
13-
14-
Windows.UI.Xaml.Controls.Slider OpacitySlider { get; };
1513
}
1614
}

src/cascadia/TerminalSettingsEditor/Profiles_Appearance.xaml

+2-3
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,12 @@
7676
<ColumnDefinition Width="*" />
7777
<ColumnDefinition Width="Auto" />
7878
</Grid.ColumnDefinitions>
79-
<Slider x:Name="OpacitySlider"
80-
x:Uid="Profile_OpacitySlider"
79+
<Slider x:Uid="Profile_OpacitySlider"
8180
Grid.Column="0"
8281
Value="{x:Bind mtu:Converters.PercentageToPercentageValue(Profile.Opacity), BindBack=Profile.SetAcrylicOpacityPercentageValue, Mode=TwoWay}" />
8382
<TextBlock Grid.Column="1"
8483
Style="{StaticResource SliderValueLabelStyle}"
85-
Text="{x:Bind mtu:Converters.AppendPercentageSign(OpacitySlider.Value), Mode=OneWay}" />
84+
Text="{x:Bind mtu:Converters.PercentageToPercentageString(Profile.Opacity), Mode=OneWay}" />
8685
</Grid>
8786
</StackPanel>
8887
</local:SettingContainer>

src/cascadia/UIHelpers/Converters.cpp

+45-50
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,84 @@
77

88
namespace winrt::Microsoft::Terminal::UI::implementation
99
{
10-
winrt::hstring Converters::AppendPercentageSign(double value)
10+
// Booleans
11+
bool Converters::InvertBoolean(bool value)
1112
{
12-
return to_hstring(gsl::narrow_cast<uint32_t>(std::lrint(value))) + L"%";
13+
return !value;
1314
}
1415

15-
winrt::Windows::UI::Xaml::Media::SolidColorBrush Converters::ColorToBrush(const winrt::Windows::UI::Color& color)
16+
winrt::Windows::UI::Xaml::Visibility Converters::InvertedBooleanToVisibility(bool value)
1617
{
17-
return Windows::UI::Xaml::Media::SolidColorBrush(color);
18+
return value ? winrt::Windows::UI::Xaml::Visibility::Collapsed : winrt::Windows::UI::Xaml::Visibility::Visible;
1819
}
1920

20-
winrt::Windows::UI::Text::FontWeight Converters::DoubleToFontWeight(double value)
21+
// Numbers
22+
double Converters::PercentageToPercentageValue(double value)
2123
{
22-
return winrt::Windows::UI::Text::FontWeight{ base::ClampedNumeric<uint16_t>(value) };
24+
return value * 100.0;
2325
}
2426

25-
double Converters::FontWeightToDouble(const winrt::Windows::UI::Text::FontWeight& fontWeight)
27+
double Converters::PercentageValueToPercentage(double value)
2628
{
27-
return fontWeight.Weight;
29+
return value / 100.0;
2830
}
2931

30-
bool Converters::InvertBoolean(bool value)
32+
winrt::hstring Converters::PercentageToPercentageString(double value)
3133
{
32-
return !value;
34+
return winrt::hstring{ fmt::format(FMT_COMPILE(L"{:.0f}%"), value * 100.0) };
3335
}
3436

35-
winrt::Windows::UI::Xaml::Visibility Converters::InvertedBooleanToVisibility(bool value)
37+
// Strings
38+
bool Converters::StringsAreNotEqual(const winrt::hstring& expected, const winrt::hstring& actual)
3639
{
37-
return value ? winrt::Windows::UI::Xaml::Visibility::Collapsed : winrt::Windows::UI::Xaml::Visibility::Visible;
40+
return expected != actual;
41+
}
42+
43+
winrt::Windows::UI::Xaml::Visibility Converters::StringNotEmptyToVisibility(const winrt::hstring& value)
44+
{
45+
return value.empty() ? winrt::Windows::UI::Xaml::Visibility::Collapsed : winrt::Windows::UI::Xaml::Visibility::Visible;
46+
}
47+
48+
winrt::hstring Converters::StringOrEmptyIfPlaceholder(const winrt::hstring& placeholder, const winrt::hstring& value)
49+
{
50+
return placeholder == value ? L"" : value;
51+
}
52+
53+
// Misc
54+
winrt::Windows::UI::Text::FontWeight Converters::DoubleToFontWeight(double value)
55+
{
56+
return winrt::Windows::UI::Text::FontWeight{ base::ClampedNumeric<uint16_t>(value) };
57+
}
58+
59+
winrt::Windows::UI::Xaml::Media::SolidColorBrush Converters::ColorToBrush(const winrt::Windows::UI::Color color)
60+
{
61+
return Windows::UI::Xaml::Media::SolidColorBrush(color);
62+
}
63+
64+
double Converters::FontWeightToDouble(const winrt::Windows::UI::Text::FontWeight fontWeight)
65+
{
66+
return fontWeight.Weight;
3867
}
3968

4069
double Converters::MaxValueFromPaddingString(const winrt::hstring& paddingString)
4170
{
42-
const auto singleCharDelim = L',';
43-
std::wstringstream tokenStream(paddingString.c_str());
44-
std::wstring token;
71+
std::wstring_view remaining{ paddingString };
4572
double maxVal = 0;
46-
size_t* idx = nullptr;
4773

4874
// Get padding values till we run out of delimiter separated values in the stream
4975
// Non-numeral values detected will default to 0
50-
// std::getline will not throw exception unless flags are set on the wstringstream
5176
// std::stod will throw invalid_argument exception if the input is an invalid double value
5277
// std::stod will throw out_of_range exception if the input value is more than DBL_MAX
5378
try
5479
{
55-
while (std::getline(tokenStream, token, singleCharDelim))
80+
while (!remaining.empty())
5681
{
82+
const std::wstring token{ til::prefix_split(remaining, L',') };
5783
// std::stod internally calls wcstod which handles whitespace prefix (which is ignored)
5884
// & stops the scan when first char outside the range of radix is encountered
5985
// We'll be permissive till the extent that stod function allows us to be by default
6086
// Ex. a value like 100.3#535w2 will be read as 100.3, but ;df25 will fail
61-
const auto curVal = std::stod(token, idx);
87+
const auto curVal = std::stod(token);
6288
if (curVal > maxVal)
6389
{
6490
maxVal = curVal;
@@ -74,35 +100,4 @@ namespace winrt::Microsoft::Terminal::UI::implementation
74100

75101
return maxVal;
76102
}
77-
78-
int Converters::PercentageToPercentageValue(double value)
79-
{
80-
return base::ClampMul(value, 100u);
81-
}
82-
83-
double Converters::PercentageValueToPercentage(double value)
84-
{
85-
return base::ClampDiv<double, double>(value, 100);
86-
}
87-
88-
bool Converters::StringsAreNotEqual(const winrt::hstring& expected, const winrt::hstring& actual)
89-
{
90-
return expected != actual;
91-
}
92-
winrt::Windows::UI::Xaml::Visibility Converters::StringNotEmptyToVisibility(const winrt::hstring& value)
93-
{
94-
return value.empty() ? winrt::Windows::UI::Xaml::Visibility::Collapsed : winrt::Windows::UI::Xaml::Visibility::Visible;
95-
}
96-
97-
// Method Description:
98-
// - Returns the value string, unless it matches the placeholder in which case the empty string.
99-
// Arguments:
100-
// - placeholder - the placeholder string.
101-
// - value - the value string.
102-
// Return Value:
103-
// - The value string, unless it matches the placeholder in which case the empty string.
104-
winrt::hstring Converters::StringOrEmptyIfPlaceholder(const winrt::hstring& placeholder, const winrt::hstring& value)
105-
{
106-
return placeholder == value ? L"" : value;
107-
}
108103
}

src/cascadia/UIHelpers/Converters.h

+13-7
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,25 @@ namespace winrt::Microsoft::Terminal::UI::implementation
99
{
1010
struct Converters
1111
{
12-
Converters() = default;
13-
static winrt::hstring AppendPercentageSign(double value);
14-
static winrt::Windows::UI::Text::FontWeight DoubleToFontWeight(double value);
15-
static winrt::Windows::UI::Xaml::Media::SolidColorBrush ColorToBrush(const winrt::Windows::UI::Color& color);
16-
static double FontWeightToDouble(const winrt::Windows::UI::Text::FontWeight& fontWeight);
12+
// Booleans
1713
static bool InvertBoolean(bool value);
1814
static winrt::Windows::UI::Xaml::Visibility InvertedBooleanToVisibility(bool value);
19-
static double MaxValueFromPaddingString(const winrt::hstring& paddingString);
20-
static int PercentageToPercentageValue(double value);
15+
16+
// Numbers
17+
static double PercentageToPercentageValue(double value);
2118
static double PercentageValueToPercentage(double value);
19+
static winrt::hstring PercentageToPercentageString(double value);
20+
21+
// Strings
2222
static bool StringsAreNotEqual(const winrt::hstring& expected, const winrt::hstring& actual);
2323
static winrt::Windows::UI::Xaml::Visibility StringNotEmptyToVisibility(const winrt::hstring& value);
2424
static winrt::hstring StringOrEmptyIfPlaceholder(const winrt::hstring& placeholder, const winrt::hstring& value);
25+
26+
// Misc
27+
static winrt::Windows::UI::Text::FontWeight DoubleToFontWeight(double value);
28+
static winrt::Windows::UI::Xaml::Media::SolidColorBrush ColorToBrush(winrt::Windows::UI::Color color);
29+
static double FontWeightToDouble(winrt::Windows::UI::Text::FontWeight fontWeight);
30+
static double MaxValueFromPaddingString(const winrt::hstring& paddingString);
2531
};
2632
}
2733

src/cascadia/UIHelpers/Converters.idl

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ namespace Microsoft.Terminal.UI
1212
static Windows.UI.Xaml.Visibility InvertedBooleanToVisibility(Boolean value);
1313

1414
// Numbers
15-
static Int32 PercentageToPercentageValue(Double value);
15+
static Double PercentageToPercentageValue(Double value);
1616
static Double PercentageValueToPercentage(Double value);
17+
static String PercentageToPercentageString(Double value);
1718

1819
// Strings
1920
static Boolean StringsAreNotEqual(String expected, String actual);
2021
static Windows.UI.Xaml.Visibility StringNotEmptyToVisibility(String value);
2122
static String StringOrEmptyIfPlaceholder(String placeholder, String value);
2223

2324
// Misc
24-
static String AppendPercentageSign(Double value);
2525
static Windows.UI.Text.FontWeight DoubleToFontWeight(Double value);
2626
static Windows.UI.Xaml.Media.SolidColorBrush ColorToBrush(Windows.UI.Color color);
2727
static Double FontWeightToDouble(Windows.UI.Text.FontWeight fontWeight);

0 commit comments

Comments
 (0)