Skip to content

Commit

Permalink
Bind Profile Color Schemes (#8388)
Browse files Browse the repository at this point in the history
## Summary of the Pull Request
Binds `Profile.ColorScheme` to the list of Color Schemes available in the Settings UI.

## References
#1564 - Settings UI

## Detailed Description of the Pull Request / Additional comments
To my knowledge, there is no way to bind an `IMapView` to `ItemsSource`. `ItemsSource` requires an `IObservableVector`, so we need to manually populate `ColorSchemeList` when we navigate to the page.

`CurrentColorScheme` operates mostly like a standard getter/setter, except it needs to account for the upcoming scenario when a color scheme is renamed or deleted. For now, we fallback to Campbell if the scheme was not found. I would like to make it update automatically, but I feel that we have two approaches here:
1. TSM stores `Profile.ColorScheme` as a `ColorScheme` instead of only the name
2. When a color scheme name is modified in SUI, we iterate through all of the profiles and modify `Profile.ColorScheme` accordingly
Open to discuss these options or any other approaches.

## Validation Steps Performed
✅ selected item initialized correctly
✅ list initialized correctly
✅ selecting a new color scheme, then creating a new terminal from that profile uses the new color scheme
✅ setting the color scheme name to a color scheme that does not exist selects "Campbell"
  • Loading branch information
carlos-zamora authored Dec 1, 2020
1 parent cbcda1a commit 62aa0ce
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/cascadia/TerminalSettingsEditor/ColorSchemes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
// - <none>
void ColorSchemes::_UpdateColorSchemeList()
{
auto colorSchemeMap = _State.Globals().ColorSchemes();
for (const auto& pair : _State.Globals().ColorSchemes())
const auto& colorSchemeMap{ _State.Globals().ColorSchemes() };
for (const auto& pair : colorSchemeMap)
{
_ColorSchemeList.Append(pair.Key());
}
Expand Down
16 changes: 9 additions & 7 deletions src/cascadia/TerminalSettingsEditor/Launch.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ the MIT License. See LICENSE in the project root for license information. -->
mc:Ignorable="d">

<Page.Resources>
<SettingsModel:IconPathConverter x:Key="IconSourceConverter"/>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CommonResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
<SettingsModel:IconPathConverter x:Key="IconSourceConverter"/>
</ResourceDictionary>
</Page.Resources>

<ScrollViewer>
Expand All @@ -26,15 +31,12 @@ the MIT License. See LICENSE in the project root for license information. -->
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>

<StackPanel Grid.Row="1" Grid.Column="0" Margin="0,0,100,0">
<ComboBox x:Uid="Globals_DefaultProfile"
x:Name="DefaultProfile"
Margin="0,0,0,20"
ItemsSource="{x:Bind State.Settings.AllProfiles, Mode=OneWay}"
SelectedItem="{x:Bind CurrentDefaultProfile, Mode=TwoWay}"
FontSize="15"
ToolTipService.Placement="Mouse">
SelectedItem="{x:Bind CurrentDefaultProfile, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="SettingsModel:Profile">
<Grid HorizontalAlignment="Stretch" ColumnSpacing="8" >
Expand All @@ -56,7 +58,7 @@ the MIT License. See LICENSE in the project root for license information. -->

<TextBlock Grid.Column="1"
Text="{x:Bind Name}"/>

</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
Expand Down
29 changes: 28 additions & 1 deletion src/cascadia/TerminalSettingsEditor/Profiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ using namespace winrt::Microsoft::Terminal::Settings::Model;

namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
{
Profiles::Profiles()
Profiles::Profiles() :
_ColorSchemeList{ single_threaded_observable_vector<ColorScheme>() }
{
InitializeComponent();

Expand All @@ -31,6 +32,32 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
void Profiles::OnNavigatedTo(const NavigationEventArgs& e)
{
_State = e.Parameter().as<Editor::ProfilePageNavigationState>();

const auto& colorSchemeMap{ _State.Schemes() };
for (const auto& pair : colorSchemeMap)
{
_ColorSchemeList.Append(pair.Value());
}
}

ColorScheme Profiles::CurrentColorScheme()
{
const auto schemeName{ _State.Profile().ColorSchemeName() };
if (const auto scheme{ _State.Schemes().TryLookup(schemeName) })
{
return scheme;
}
else
{
// This Profile points to a color scheme that was renamed or deleted.
// Fallback to Campbell.
return _State.Schemes().TryLookup(L"Campbell");
}
}

void Profiles::CurrentColorScheme(const ColorScheme& val)
{
_State.Profile().ColorSchemeName(val.Name());
}

fire_and_forget Profiles::BackgroundImage_Click(IInspectable const&, RoutedEventArgs const&)
Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/TerminalSettingsEditor/Profiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation

void OnNavigatedTo(const winrt::Windows::UI::Xaml::Navigation::NavigationEventArgs& e);

Model::ColorScheme CurrentColorScheme();
void CurrentColorScheme(const Model::ColorScheme& val);

fire_and_forget BackgroundImage_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
fire_and_forget Commandline_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
fire_and_forget StartingDirectory_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e);

GETSET_PROPERTY(Editor::ProfilePageNavigationState, State, nullptr);
GETSET_PROPERTY(Windows::Foundation::Collections::IObservableVector<Model::ColorScheme>, ColorSchemeList, nullptr);
GETSET_BINDABLE_ENUM_SETTING(CursorShape, winrt::Microsoft::Terminal::TerminalControl::CursorStyle, State().Profile, CursorShape);
GETSET_BINDABLE_ENUM_SETTING(BackgroundImageStretchMode, winrt::Windows::UI::Xaml::Media::Stretch, State().Profile, BackgroundImageStretchMode);
GETSET_BINDABLE_ENUM_SETTING(AntiAliasingMode, winrt::Microsoft::Terminal::TerminalControl::TextAntialiasingMode, State().Profile, AntialiasingMode);
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalSettingsEditor/Profiles.idl
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ namespace Microsoft.Terminal.Settings.Editor

IInspectable CurrentScrollState;
Windows.Foundation.Collections.IObservableVector<Microsoft.Terminal.Settings.Editor.EnumEntry> ScrollStateList { get; };

Microsoft.Terminal.Settings.Model.ColorScheme CurrentColorScheme;
Windows.Foundation.Collections.IObservableVector<Microsoft.Terminal.Settings.Model.ColorScheme> ColorSchemeList { get; };
}
}
18 changes: 8 additions & 10 deletions src/cascadia/TerminalSettingsEditor/Profiles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,14 @@ the MIT License. See LICENSE in the project root for license information. -->
Value="100"
SmallChange="1"
LargeChange="10"/>
<ComboBox x:Uid="Profile_ColorScheme">
<ComboBox.Items>
<ComboBoxItem Content="Campbell"/>
<ComboBoxItem Content="Campbell Powershell"/>
<ComboBoxItem Content="Vintage"/>
<ComboBoxItem Content="One Half Dark"/>
<ComboBoxItem Content="One Half Light"/>
<ComboBoxItem Content="Tango Dark"/>
<ComboBoxItem Content="Tango Light"/>
</ComboBox.Items>
<ComboBox x:Uid="Profile_ColorScheme"
ItemsSource="{x:Bind ColorSchemeList, Mode=OneWay}"
SelectedItem="{x:Bind CurrentColorScheme, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="model:ColorScheme">
<TextBlock Text="{x:Bind Name, Mode=OneWay}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

<StackPanel Orientation="Horizontal">
Expand Down
5 changes: 4 additions & 1 deletion src/cascadia/TerminalSettingsModel/ColorScheme.idl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ namespace Microsoft.Terminal.Settings.Model
Windows.UI.Color SelectionBackground;
Windows.UI.Color CursorColor;

Windows.UI.Color[] Table { get; };
// winrt::com_arrays prevent data binding.
// Instead of representing Table as a property,
// we expose the getter as a function.
Windows.UI.Color[] Table();
void SetColorTableEntry(UInt8 index, Windows.UI.Color value);
}
}

0 comments on commit 62aa0ce

Please sign in to comment.