move from converter to argwrapper

This commit is contained in:
Pankaj Bhojwani
2025-02-20 17:11:18 -08:00
parent 4a780e189c
commit 289ecec719
7 changed files with 186 additions and 167 deletions

View File

@@ -390,6 +390,153 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
}
}
winrt::hstring ArgWrapper::UnboxString(const Windows::Foundation::IInspectable& value)
{
return winrt::unbox_value<winrt::hstring>(value);
}
int32_t ArgWrapper::UnboxInt32(const Windows::Foundation::IInspectable& value)
{
return winrt::unbox_value<int32_t>(value);
}
uint32_t ArgWrapper::UnboxUInt32(const Windows::Foundation::IInspectable& value)
{
return winrt::unbox_value<uint32_t>(value);
}
uint32_t ArgWrapper::UnboxUInt32Optional(const Windows::Foundation::IInspectable& value)
{
const auto unboxed = winrt::unbox_value<winrt::Windows::Foundation::IReference<uint32_t>>(value);
if (unboxed)
{
return unboxed.Value();
}
else
{
return 0;
}
}
float ArgWrapper::UnboxFloat(const Windows::Foundation::IInspectable& value)
{
return winrt::unbox_value<float>(value);
}
bool ArgWrapper::UnboxBool(const Windows::Foundation::IInspectable& value)
{
return winrt::unbox_value<bool>(value);
}
winrt::Windows::Foundation::IReference<bool> ArgWrapper::UnboxBoolOptional(const Windows::Foundation::IInspectable& value)
{
if (!value)
{
return nullptr;
}
return winrt::unbox_value<winrt::Windows::Foundation::IReference<bool>>(value);
}
winrt::Windows::Foundation::IReference<Microsoft::Terminal::Core::Color> ArgWrapper::UnboxTerminalCoreColorOptional(const Windows::Foundation::IInspectable& value)
{
if (value)
{
return unbox_value<Windows::Foundation::IReference<Microsoft::Terminal::Core::Color>>(value);
}
else
{
return nullptr;
}
}
winrt::Windows::Foundation::IReference<Microsoft::Terminal::Core::Color> ArgWrapper::UnboxWindowsUIColorOptional(const Windows::Foundation::IInspectable& value)
{
if (value)
{
const auto winUIColor = unbox_value<Windows::Foundation::IReference<Windows::UI::Color>>(value).Value();
const Microsoft::Terminal::Core::Color terminalColor{ winUIColor.R, winUIColor.G, winUIColor.B, winUIColor.A };
return Windows::Foundation::IReference<Microsoft::Terminal::Core::Color>{ terminalColor };
}
else
{
return nullptr;
}
}
void ArgWrapper::StringBindBack(const winrt::hstring& newValue)
{
Value(box_value(newValue));
}
void ArgWrapper::DoubleBindBack(const double newValue)
{
Value(box_value(static_cast<uint32_t>(newValue)));
}
void ArgWrapper::DoubleOptionalBindBack(const double newValue)
{
Value(box_value(static_cast<uint32_t>(newValue)));
}
void ArgWrapper::FloatBindBack(const double newValue)
{
Value(box_value(static_cast<float>(newValue)));
}
void ArgWrapper::BoolBindBack(const Windows::Foundation::IReference<bool> newValue)
{
if (newValue)
{
Value(box_value(newValue));
}
else
{
Value(nullptr);
}
}
void ArgWrapper::TerminalCoreColorBindBack(const winrt::Windows::Foundation::IReference<Microsoft::Terminal::Core::Color> newValue)
{
if (newValue)
{
Value(box_value(newValue));
}
else
{
Value(nullptr);
}
}
void ArgWrapper::WindowsUIColorBindBack(const winrt::Windows::Foundation::IReference<Microsoft::Terminal::Core::Color> newValue)
{
if (newValue)
{
const auto terminalCoreColor = unbox_value<Windows::Foundation::IReference<Microsoft::Terminal::Core::Color>>(newValue).Value();
const Windows::UI::Color winuiColor{
.A = terminalCoreColor.A,
.R = terminalCoreColor.R,
.G = terminalCoreColor.G,
.B = terminalCoreColor.B
};
// only set to the new value if our current value is not the same
// unfortunately the Value setter does not do this check properly since
// we create a whole new IReference even for the same underlying color
if (_Value)
{
const auto currentValue = unbox_value<Windows::Foundation::IReference<Windows::UI::Color>>(_Value).Value();
if (currentValue == winuiColor)
{
return;
}
}
Value(box_value(Windows::Foundation::IReference<Windows::UI::Color>{ winuiColor }));
}
else
{
Value(nullptr);
}
}
ActionArgsViewModel::ActionArgsViewModel(const Model::ActionAndArgs actionAndArgs) :
_actionAndArgs{ actionAndArgs }
{

View File

@@ -176,106 +176,34 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
void Initialize();
winrt::hstring Name() const noexcept { return _name; };
winrt::hstring Type() const noexcept { return _type; };
bool Required() const noexcept { return _required; };
Windows::Foundation::Collections::IObservableVector<Microsoft::Terminal::Settings::Editor::EnumEntry> EnumList() const noexcept { return _EnumList; };
Windows::Foundation::Collections::IObservableVector<Microsoft::Terminal::Settings::Editor::FlagEntry> FlagList() const noexcept { return _FlagList; };
void StringBindBack(const winrt::hstring& newValue) { Value(box_value(newValue)); };
void DoubleBindBack(const double newValue) { Value(box_value(static_cast<uint32_t>(newValue))); };
void DoubleOptionalBindBack(const double newValue) { Value(box_value(static_cast<uint32_t>(newValue))); };
void FloatBindBack(const double newValue) { Value(box_value(static_cast<float>(newValue))); };
void BoolBindBack(const Windows::Foundation::IReference<bool> newValue)
{
if (newValue)
{
Value(box_value(newValue));
}
else
{
Value(nullptr);
}
}
winrt::Windows::Foundation::IReference<Microsoft::Terminal::Core::Color> UnboxTerminalCoreColorOptional(const Windows::Foundation::IInspectable& value)
{
if (value)
{
return unbox_value<Windows::Foundation::IReference<Microsoft::Terminal::Core::Color>>(value);
}
else
{
return nullptr;
}
}
void TerminalCoreColorBindBack(const winrt::Windows::Foundation::IReference<Microsoft::Terminal::Core::Color> newValue)
{
if (newValue)
{
Value(box_value(newValue));
}
else
{
Value(nullptr);
}
}
winrt::Windows::Foundation::IReference<Microsoft::Terminal::Core::Color> UnboxWindowsUIColorOptional(const Windows::Foundation::IInspectable& value)
{
if (value)
{
const auto winUIColor = unbox_value<Windows::Foundation::IReference<Windows::UI::Color>>(value).Value();
const Microsoft::Terminal::Core::Color terminalColor{ winUIColor.R, winUIColor.G, winUIColor.B, winUIColor.A };
return Windows::Foundation::IReference<Microsoft::Terminal::Core::Color>{ terminalColor };
}
else
{
return nullptr;
}
}
void WindowsUIColorBindBack(const winrt::Windows::Foundation::IReference<Microsoft::Terminal::Core::Color> newValue)
{
if (newValue)
{
const auto terminalCoreColor = unbox_value<Windows::Foundation::IReference<Microsoft::Terminal::Core::Color>>(newValue).Value();
const Windows::UI::Color winuiColor{
.A = terminalCoreColor.A,
.R = terminalCoreColor.R,
.G = terminalCoreColor.G,
.B = terminalCoreColor.B
};
// only set to the new value if our current value is not the same
// unfortunately the Value setter does not do this check properly since
// we create a whole new IReference even for the same underlying color
if (_Value)
{
const auto currentValue = unbox_value<Windows::Foundation::IReference<Windows::UI::Color>>(_Value).Value();
if (currentValue == winuiColor)
{
return;
}
}
Value(box_value(Windows::Foundation::IReference<Windows::UI::Color>{ winuiColor }));
}
else
{
Value(nullptr);
}
}
// We cannot use the macro here because we need to implement additional logic for the setter
Windows::Foundation::IInspectable EnumValue() const noexcept { return _EnumValue; };
void EnumValue(const Windows::Foundation::IInspectable& value);
Windows::Foundation::Collections::IObservableVector<Microsoft::Terminal::Settings::Editor::EnumEntry> EnumList() const noexcept { return _EnumList; };
Windows::Foundation::Collections::IObservableVector<Microsoft::Terminal::Settings::Editor::FlagEntry> FlagList() const noexcept { return _FlagList; };
// unboxing functions
winrt::hstring UnboxString(const Windows::Foundation::IInspectable& value);
int32_t UnboxInt32(const Windows::Foundation::IInspectable& value);
uint32_t UnboxUInt32(const Windows::Foundation::IInspectable& value);
uint32_t UnboxUInt32Optional(const Windows::Foundation::IInspectable& value);
float UnboxFloat(const Windows::Foundation::IInspectable& value);
bool UnboxBool(const Windows::Foundation::IInspectable& value);
winrt::Windows::Foundation::IReference<bool> UnboxBoolOptional(const Windows::Foundation::IInspectable& value);
winrt::Windows::Foundation::IReference<Microsoft::Terminal::Core::Color> UnboxTerminalCoreColorOptional(const Windows::Foundation::IInspectable& value);
winrt::Windows::Foundation::IReference<Microsoft::Terminal::Core::Color> UnboxWindowsUIColorOptional(const Windows::Foundation::IInspectable& value);
// Bind back functions
void StringBindBack(const winrt::hstring& newValue);
void DoubleBindBack(const double newValue);
void DoubleOptionalBindBack(const double newValue);
void FloatBindBack(const double newValue);
void BoolBindBack(const Windows::Foundation::IReference<bool> newValue);
void TerminalCoreColorBindBack(const winrt::Windows::Foundation::IReference<Microsoft::Terminal::Core::Color> newValue);
void WindowsUIColorBindBack(const winrt::Windows::Foundation::IReference<Microsoft::Terminal::Core::Color> newValue);
til::typed_event<IInspectable, Editor::ArgWrapper> ColorSchemeRequested;

View File

@@ -89,8 +89,18 @@ namespace Microsoft.Terminal.Settings.Editor
Windows.Foundation.Collections.IObservableVector<Microsoft.Terminal.Settings.Editor.FlagEntry> FlagList { get; };
ColorSchemeViewModel DefaultColorScheme;
// unboxing functions
String UnboxString(Object value);
UInt32 UnboxInt32(Object value);
UInt32 UnboxUInt32(Object value);
UInt32 UnboxUInt32Optional(Object value);
Single UnboxFloat(Object value);
Boolean UnboxBool(Object value);
Windows.Foundation.IReference<Boolean> UnboxBoolOptional(Object value);
Windows.Foundation.IReference<Microsoft.Terminal.Core.Color> UnboxTerminalCoreColorOptional(Object value);
Windows.Foundation.IReference<Microsoft.Terminal.Core.Color> UnboxWindowsUIColorOptional(Object value);
// bind back functions
void StringBindBack(String newValue);
void DoubleBindBack(Double newValue);
void DoubleOptionalBindBack(Double newValue);

View File

@@ -237,7 +237,7 @@
Spacing="8">
<TextBlock Text="{x:Bind Name}"
VerticalAlignment="Center" />
<muxc:NumberBox Value="{x:Bind mtu:Converters.UnboxInt32(Value), Mode=TwoWay, BindBack=DoubleBindBack}"
<muxc:NumberBox Value="{x:Bind UnboxInt32(Value), Mode=TwoWay, BindBack=DoubleBindBack}"
Style="{StaticResource NumberBoxSettingStyle}"
Minimum="0"
Maximum="999"
@@ -254,7 +254,7 @@
Spacing="8">
<TextBlock Text="{x:Bind Name}"
VerticalAlignment="Center" />
<muxc:NumberBox Value="{x:Bind mtu:Converters.UnboxUInt32(Value), Mode=TwoWay, BindBack=DoubleBindBack}"
<muxc:NumberBox Value="{x:Bind UnboxUInt32(Value), Mode=TwoWay, BindBack=DoubleBindBack}"
Style="{StaticResource NumberBoxSettingStyle}"
Minimum="0"
Maximum="999"
@@ -271,7 +271,7 @@
Spacing="8">
<TextBlock Text="{x:Bind Name}"
VerticalAlignment="Center" />
<muxc:NumberBox Value="{x:Bind mtu:Converters.UnboxUInt32Optional(Value), Mode=TwoWay, BindBack=DoubleOptionalBindBack}"
<muxc:NumberBox Value="{x:Bind UnboxUInt32Optional(Value), Mode=TwoWay, BindBack=DoubleOptionalBindBack}"
Style="{StaticResource NumberBoxSettingStyle}"
Minimum="0"
Maximum="999"
@@ -288,7 +288,7 @@
Spacing="8">
<TextBlock Text="{x:Bind Name}"
VerticalAlignment="Center" />
<muxc:NumberBox Value="{x:Bind mtu:Converters.UnboxFloat(Value), Mode=TwoWay, BindBack=FloatBindBack}"
<muxc:NumberBox Value="{x:Bind UnboxFloat(Value), Mode=TwoWay, BindBack=FloatBindBack}"
Style="{StaticResource NumberBoxSettingStyle}"
Minimum="0"
Maximum="999"
@@ -305,7 +305,7 @@
Spacing="8">
<TextBlock Text="{x:Bind Name}"
VerticalAlignment="Center" />
<TextBox Text="{x:Bind mtu:Converters.UnboxString(Value), Mode=TwoWay, BindBack=StringBindBack}" />
<TextBox Text="{x:Bind UnboxString(Value), Mode=TwoWay, BindBack=StringBindBack}" />
</StackPanel>
</ListViewItem>
</DataTemplate>
@@ -317,7 +317,7 @@
Spacing="8">
<TextBlock Text="{x:Bind Name}"
VerticalAlignment="Center" />
<CheckBox IsChecked="{x:Bind mtu:Converters.UnboxBool(Value), Mode=TwoWay, BindBack=BoolBindBack}" />
<CheckBox IsChecked="{x:Bind UnboxBool(Value), Mode=TwoWay, BindBack=BoolBindBack}" />
</StackPanel>
</ListViewItem>
</DataTemplate>
@@ -329,7 +329,7 @@
Spacing="8">
<TextBlock Text="{x:Bind Name}"
VerticalAlignment="Center" />
<CheckBox IsChecked="{x:Bind mtu:Converters.UnboxBoolOptional(Value), Mode=TwoWay, BindBack=BoolBindBack}"
<CheckBox IsChecked="{x:Bind UnboxBoolOptional(Value), Mode=TwoWay, BindBack=BoolBindBack}"
IsThreeState="True" />
</StackPanel>
</ListViewItem>

View File

@@ -109,52 +109,4 @@ namespace winrt::Microsoft::Terminal::UI::implementation
return maxVal;
}
// Unboxing
winrt::hstring Converters::UnboxString(const Windows::Foundation::IInspectable& value)
{
return winrt::unbox_value<winrt::hstring>(value);
}
int32_t Converters::UnboxInt32(const Windows::Foundation::IInspectable& value)
{
return winrt::unbox_value<int32_t>(value);
}
uint32_t Converters::UnboxUInt32(const Windows::Foundation::IInspectable& value)
{
return winrt::unbox_value<uint32_t>(value);
}
uint32_t Converters::UnboxUInt32Optional(const Windows::Foundation::IInspectable& value)
{
const auto unboxed = winrt::unbox_value<winrt::Windows::Foundation::IReference<uint32_t>>(value);
if (unboxed)
{
return unboxed.Value();
}
else
{
return 0;
}
}
float Converters::UnboxFloat(const Windows::Foundation::IInspectable& value)
{
return winrt::unbox_value<float>(value);
}
bool Converters::UnboxBool(const Windows::Foundation::IInspectable& value)
{
return winrt::unbox_value<bool>(value);
}
winrt::Windows::Foundation::IReference<bool> Converters::UnboxBoolOptional(const Windows::Foundation::IInspectable& value)
{
if (!value)
{
return nullptr;
}
return winrt::unbox_value<winrt::Windows::Foundation::IReference<bool>>(value);
}
}

View File

@@ -29,15 +29,6 @@ namespace winrt::Microsoft::Terminal::UI::implementation
static winrt::Windows::UI::Xaml::Media::SolidColorBrush ColorToBrush(winrt::Windows::UI::Color color);
static double FontWeightToDouble(winrt::Windows::UI::Text::FontWeight fontWeight);
static double MaxValueFromPaddingString(const winrt::hstring& paddingString);
// Unboxing
static winrt::hstring UnboxString(const Windows::Foundation::IInspectable& value);
static int32_t UnboxInt32(const Windows::Foundation::IInspectable& value);
static uint32_t UnboxUInt32(const Windows::Foundation::IInspectable& value);
static uint32_t UnboxUInt32Optional(const Windows::Foundation::IInspectable& value);
static float UnboxFloat(const Windows::Foundation::IInspectable& value);
static bool UnboxBool(const Windows::Foundation::IInspectable& value);
static winrt::Windows::Foundation::IReference<bool> UnboxBoolOptional(const Windows::Foundation::IInspectable& value);
};
}

View File

@@ -27,14 +27,5 @@ namespace Microsoft.Terminal.UI
static Windows.UI.Xaml.Media.SolidColorBrush ColorToBrush(Windows.UI.Color color);
static Double FontWeightToDouble(Windows.UI.Text.FontWeight fontWeight);
static Double MaxValueFromPaddingString(String paddingString);
// Unboxing
static String UnboxString(Object value);
static UInt32 UnboxInt32(Object value);
static UInt32 UnboxUInt32(Object value);
static UInt32 UnboxUInt32Optional(Object value);
static Single UnboxFloat(Object value);
static Boolean UnboxBool(Object value);
static Windows.Foundation.IReference<Boolean> UnboxBoolOptional(Object value);
}
}