Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add separate padding settings for left, top, right and bottom #17909

Merged
merged 10 commits into from
Dec 6, 2024
68 changes: 66 additions & 2 deletions src/cascadia/TerminalSettingsEditor/ProfileViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,32 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
Opacity(static_cast<float>(value) / 100.0f);
};

void SetPadding(double value)
void SetLeftPadding(double value)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe SetXXXPadding(double value) functions can be overloaded but idk how to make it work with XAML.

{
Padding(to_hstring(value));
const hstring& padding = _GetNewPadding(PaddingDirection::Left, value);

Padding(padding);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I understand now. This will properly propagate the change notification through to Padding. You may still need to do the other notifications yourself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will allow you to turn this into a setter and a getter with normal two-way binding.

}

void SetTopPadding(double value)
{
const hstring& padding = _GetNewPadding(PaddingDirection::Top, value);

Padding(padding);
}

void SetRightPadding(double value)
{
const hstring& padding = _GetNewPadding(PaddingDirection::Right, value);

Padding(padding);
}

void SetBottomPadding(double value)
{
const hstring& padding = _GetNewPadding(PaddingDirection::Bottom, value);

Padding(padding);
}

winrt::hstring EvaluatedIcon() const
Expand Down Expand Up @@ -140,6 +163,47 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation

Model::CascadiaSettings _appSettings;
Editor::AppearanceViewModel _unfocusedAppearanceViewModel;

enum class PaddingDirection
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used this enum just to clarify the intention.

{
Left = 0,
Top = 1,
Right = 2,
Bottom = 3
};

winrt::hstring _GetNewPadding(PaddingDirection paddingDirection, double newPaddingValue)
{
std::wstringstream result;
std::array<double, 4> values{};
std::wstring_view remaining{ Padding() };
uint32_t paddingIndex = static_cast<uint32_t>(paddingDirection);

try
{
for (uint32_t index = 0; !remaining.empty() && index < values.size(); ++index)
{
const std::wstring token{ til::prefix_split(remaining, L',') };
auto curVal = std::stod(token);

if (paddingIndex == index)
{
curVal = newPaddingValue;
}

values[index] = curVal;
}
}
catch (...)
{
values.fill(0);
LOG_CAUGHT_EXCEPTION();
}

result << values[0] << L", " << values[1] << L", " << values[2] << L", " << values[3];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use fmt instead of sstream almost everywhere. This would also allow us to easily round the padding to e.g. 6 digits during formatting, just in case (to avoid 0.3000000000000001 situations).

Something like this:

const auto result = fmt::format(
    FMT_COMPILE(L"{:.6f},{:.6f},{:.6f},{:.6f}"),
    values[0],
    values[1],
    values[2],
    values[3]
);
return winrt::hstring{ result };

Theoretically you should also have access to fmt::join so this may work:

const auto result = fmt::format(FMT_COMPILE(L"{:.6f}"), fmt::join(values, L","));


return hstring{ result.str() };
}
};

struct DeleteProfileEventArgs :
Expand Down
6 changes: 5 additions & 1 deletion src/cascadia/TerminalSettingsEditor/ProfileViewModel.idl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ namespace Microsoft.Terminal.Settings.Editor
void SetupAppearances(Windows.Foundation.Collections.IObservableVector<ColorSchemeViewModel> schemesList);

void SetAcrylicOpacityPercentageValue(Double value);
void SetPadding(Double value);

void SetLeftPadding(Double value);
void SetTopPadding(Double value);
void SetRightPadding(Double value);
void SetBottomPadding(Double value);

Boolean IsBellStyleFlagSet(UInt32 flag);
void SetBellStyleAudible(Windows.Foundation.IReference<Boolean> on);
Expand Down
92 changes: 78 additions & 14 deletions src/cascadia/TerminalSettingsEditor/Profiles_Appearance.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@
<TextBlock FontFamily="{x:Bind Name}"
Text="{x:Bind LocalizedName}" />
</DataTemplate>

<Style x:Key="PaddingNumberBoxStyle"
TargetType="muxc:NumberBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="muxc:NumberBox">
<Grid x:Name="Root">
<TextBox x:Name="InputBox"
Padding="0,6"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
TextAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Page.Resources>

Expand Down Expand Up @@ -107,20 +125,66 @@
<local:SettingContainer x:Uid="Profile_Padding"
ClearSettingValue="{x:Bind Profile.ClearPadding}"
HasSettingValue="{x:Bind Profile.HasPadding, Mode=OneWay}"
SettingOverrideSource="{x:Bind Profile.PaddingOverrideSource, Mode=OneWay}">
<Grid Style="{StaticResource CustomSliderControlGridStyle}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Slider x:Name="PaddingSlider"
x:Uid="Profile_PaddingSlider"
Grid.Column="0"
Value="{x:Bind mtu:Converters.MaxValueFromPaddingString(Profile.Padding), BindBack=Profile.SetPadding, Mode=TwoWay}" />
<TextBlock Grid.Column="1"
Style="{StaticResource SliderValueLabelStyle}"
Text="{Binding ElementName=PaddingSlider, Path=Value, Mode=OneWay}" />
</Grid>
SettingOverrideSource="{x:Bind Profile.PaddingOverrideSource, Mode=OneWay}"
Style="{StaticResource ExpanderSettingContainerStyle}">
<Border Margin="0,12,0,12"
Padding="2,0,2,0"
HorizontalAlignment="Left"
BorderBrush="{ThemeResource SystemControlForegroundBaseMediumLowBrush}"
BorderThickness="1"
CornerRadius="8">
<Grid>

<Grid.RowDefinitions>
<RowDefinition Height="48" />
<RowDefinition Height="48" />
<RowDefinition Height="48" />
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="72" />
<ColumnDefinition Width="72" />
<ColumnDefinition Width="72" />
</Grid.ColumnDefinitions>

<muxc:NumberBox Grid.Row="1"
Grid.Column="0"
LargeChange="10"
Maximum="100"
Minimum="1"
SmallChange="1"
Style="{StaticResource PaddingNumberBoxStyle}"
Value="{x:Bind mtu:Converters.PaddingValueFromIndex(Profile.Padding, 0), BindBack=Profile.SetLeftPadding, Mode=TwoWay}" />

<muxc:NumberBox Grid.Row="0"
Grid.Column="1"
LargeChange="10"
Maximum="100"
Minimum="1"
SmallChange="1"
Style="{StaticResource PaddingNumberBoxStyle}"
Value="{x:Bind mtu:Converters.PaddingValueFromIndex(Profile.Padding, 1), BindBack=Profile.SetTopPadding, Mode=TwoWay}" />

<muxc:NumberBox Grid.Row="1"
Grid.Column="2"
LargeChange="10"
Maximum="100"
Minimum="1"
SmallChange="1"
Style="{StaticResource PaddingNumberBoxStyle}"
Value="{x:Bind mtu:Converters.PaddingValueFromIndex(Profile.Padding, 2), BindBack=Profile.SetRightPadding, Mode=TwoWay}" />

<muxc:NumberBox Grid.Row="2"
Grid.Column="1"
LargeChange="10"
Maximum="100"
Minimum="1"
SmallChange="1"
Style="{StaticResource PaddingNumberBoxStyle}"
Value="{x:Bind mtu:Converters.PaddingValueFromIndex(Profile.Padding, 3), BindBack=Profile.SetBottomPadding, Mode=TwoWay}" />

</Grid>
</Border>
</local:SettingContainer>

<!-- Scrollbar Visibility -->
Expand Down
31 changes: 31 additions & 0 deletions src/cascadia/UIHelpers/Converters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,35 @@ namespace winrt::Microsoft::Terminal::UI::implementation

return maxVal;
}

double Converters::PaddingValueFromIndex(const winrt::hstring& paddingString, uint32_t paddingIndex)
Copy link
Contributor Author

@nukoseer nukoseer Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should also take an enum parameter instead of index but this is only called from XAML.

{
std::wstring_view remaining{ paddingString };
double paddingVal = 0;
uint32_t index = 0;

try
{
while (!remaining.empty())
{
const std::wstring token{ til::prefix_split(remaining, L',') };
const auto curVal = std::stod(token);

if (paddingIndex == index)
{
paddingVal = curVal;
break;
}

++index;
}
}
catch (...)
{
paddingVal = 0;
LOG_CAUGHT_EXCEPTION();
}

return paddingVal;
}
}
1 change: 1 addition & 0 deletions src/cascadia/UIHelpers/Converters.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ 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);
static double PaddingValueFromIndex(const winrt::hstring& paddingString, uint32_t index);
};
}

Expand Down
1 change: 1 addition & 0 deletions src/cascadia/UIHelpers/Converters.idl
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ 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);
static Double PaddingValueFromIndex(String paddingString, UInt32 index);
}
}
Loading