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

Implement MVVM for Color Schemes #13179

Merged
40 commits merged into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
d447166
invoking rightmost breadcrumb does nothing
PankajBhojwani Feb 2, 2022
5430de6
partway through color scheme MVVM
PankajBhojwani Feb 4, 2022
447c1be
main page now holds vector of csvms
PankajBhojwani Feb 4, 2022
1342d92
conflict
PankajBhojwani May 4, 2022
93b8ecb
need to update event handlers
PankajBhojwani May 10, 2022
987002e
propagate changes to scheme
PankajBhojwani May 11, 2022
f5fef4e
color table colors ...
PankajBhojwani May 18, 2022
152974d
delete works
PankajBhojwani May 20, 2022
65fc789
page view model is up
PankajBhojwani May 24, 2022
a51c166
rename and delete work
PankajBhojwani May 25, 2022
d0c9881
add new works
PankajBhojwani May 25, 2022
946f10a
remove nav state
PankajBhojwani May 25, 2022
9be1b68
some fixes
PankajBhojwani May 26, 2022
322766d
select last selected scheme
PankajBhojwani May 26, 2022
ac75ed3
move to header
PankajBhojwani May 26, 2022
c91ade5
neaten
PankajBhojwani May 26, 2022
219a329
update settings
PankajBhojwani May 27, 2022
0fe0612
Format
PankajBhojwani May 27, 2022
79d92d4
nits
PankajBhojwani May 27, 2022
face837
nits
PankajBhojwani May 31, 2022
264f4dd
rename property
PankajBhojwani May 31, 2022
d76c3c3
fix settings reload issue
PankajBhojwani Jun 1, 2022
44cf7f6
don't expose model object
PankajBhojwani Jun 1, 2022
003f9fc
rename
PankajBhojwani Jun 1, 2022
eee069d
init in ctor
PankajBhojwani Jun 1, 2022
9c297b6
omg i forgot to add the labels back in
PankajBhojwani Jun 10, 2022
941e7c0
move in box schemes to cpp
PankajBhojwani Jun 15, 2022
5ca7a16
conflict
PankajBhojwani Jul 11, 2022
ff761a7
Select first scheme if no scheme found
PankajBhojwani Jul 26, 2022
93b4e02
make sure to update map
PankajBhojwani Jul 27, 2022
c38a3ea
remove this
PankajBhojwani Jul 27, 2022
d34b50e
fix color entry
PankajBhojwani Jul 27, 2022
9993610
selection changed to view model
PankajBhojwani Jul 27, 2022
814b4e1
don't need request anymore
PankajBhojwani Jul 27, 2022
51192df
Merge branch 'main' of https://github.com/microsoft/terminal into dev…
PankajBhojwani Jul 27, 2022
4eecacc
current scheme fixes
PankajBhojwani Jul 28, 2022
e428e2b
format
PankajBhojwani Jul 28, 2022
7a1b534
remove request add new
PankajBhojwani Jul 28, 2022
bdf142c
move this to vm
PankajBhojwani Jul 28, 2022
2daba74
don't need this
PankajBhojwani Jul 28, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions src/cascadia/TerminalSettingsEditor/ColorSchemeViewModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#include "pch.h"
#include "ColorSchemeViewModel.h"
#include "ColorSchemeViewModel.g.cpp"

#include <LibraryResources.h>
#include "..\WinRTUtils\inc\Utils.h"

namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
{
static const std::array<hstring, 16> TableColorNames = {
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
RS_(L"ColorScheme_Black/Header"),
RS_(L"ColorScheme_Red/Header"),
RS_(L"ColorScheme_Green/Header"),
RS_(L"ColorScheme_Yellow/Header"),
RS_(L"ColorScheme_Blue/Header"),
RS_(L"ColorScheme_Purple/Header"),
RS_(L"ColorScheme_Cyan/Header"),
RS_(L"ColorScheme_White/Header"),
RS_(L"ColorScheme_BrightBlack/Header"),
RS_(L"ColorScheme_BrightRed/Header"),
RS_(L"ColorScheme_BrightGreen/Header"),
RS_(L"ColorScheme_BrightYellow/Header"),
RS_(L"ColorScheme_BrightBlue/Header"),
RS_(L"ColorScheme_BrightPurple/Header"),
RS_(L"ColorScheme_BrightCyan/Header"),
RS_(L"ColorScheme_BrightWhite/Header")
};

ColorSchemeViewModel::ColorSchemeViewModel(const Model::ColorScheme scheme) :
_scheme{ scheme },
_CurrentNonBrightColorTable{ single_threaded_observable_vector<Editor::ColorTableEntry>() },
_CurrentBrightColorTable{ single_threaded_observable_vector<Editor::ColorTableEntry>() }
{
_Name = scheme.Name();

const auto colorEntryChangedHandler = [&](const IInspectable& sender, const PropertyChangedEventArgs& args) {
if (const auto entry{ sender.try_as<ColorTableEntry>() })
{
if (args.PropertyName() == L"Color")
{
const til::color newColor{ entry->Color() };
if (const auto& tag{ entry->Tag() })
{
if (const auto index{ tag.try_as<uint8_t>() })
{
_scheme.SetColorTableEntry(*index, newColor);
}
else if (const auto stringTag{ tag.try_as<hstring>() })
{
if (stringTag == ForegroundColorTag)
{
_scheme.Foreground(newColor);
}
else if (stringTag == BackgroundColorTag)
{
_scheme.Background(newColor);
}
else if (stringTag == CursorColorTag)
{
_scheme.CursorColor(newColor);
}
else if (stringTag == SelectionBackgroundColorTag)
{
_scheme.SelectionBackground(newColor);
}
}
}
}
}
};

for (uint8_t i = 0; i < ColorTableSize; ++i)
{
til::color currentColor{ scheme.Table()[i] };
const auto& entry{ winrt::make<ColorTableEntry>(i, currentColor) };
entry.PropertyChanged(colorEntryChangedHandler);
if (i < ColorTableDivider)
{
_CurrentNonBrightColorTable.Append(entry);
}
else
{
_CurrentBrightColorTable.Append(entry);
}
}

_CurrentForegroundColor = winrt::make<ColorTableEntry>(ForegroundColorTag, til::color(scheme.Foreground()));
_CurrentBackgroundColor = winrt::make<ColorTableEntry>(BackgroundColorTag, til::color(scheme.Background()));
_CurrentCursorColor = winrt::make<ColorTableEntry>(CursorColorTag, til::color(scheme.CursorColor()));
_CurrentSelectionBackgroundColor = winrt::make<ColorTableEntry>(SelectionBackgroundColorTag, til::color(scheme.SelectionBackground()));

_CurrentForegroundColor.PropertyChanged(colorEntryChangedHandler);
_CurrentBackgroundColor.PropertyChanged(colorEntryChangedHandler);
_CurrentCursorColor.PropertyChanged(colorEntryChangedHandler);
_CurrentSelectionBackgroundColor.PropertyChanged(colorEntryChangedHandler);
}

winrt::hstring ColorSchemeViewModel::Name()
{
return _Name;
}

void ColorSchemeViewModel::Name(winrt::hstring newName)
{
_scheme.Name(newName);
_Name = newName;
}

Editor::ColorTableEntry ColorSchemeViewModel::ColorEntryAt(uint32_t index)
{
if (index < ColorTableDivider)
{
return _CurrentNonBrightColorTable.GetAt(index);
}
else
{
return _CurrentBrightColorTable.GetAt(index - ColorTableDivider);
}
}

Model::ColorScheme ColorSchemeViewModel::SettingsModelObject()
{
return _scheme;
}

ColorTableEntry::ColorTableEntry(uint8_t index, Windows::UI::Color color)
{
Name(TableColorNames[index]);
Tag(winrt::box_value<uint8_t>(index));
Color(color);
}

ColorTableEntry::ColorTableEntry(std::wstring_view tag, Windows::UI::Color color)
{
Name(LocalizedNameForEnumName(L"ColorScheme_", tag, L"Text"));
Tag(winrt::box_value(tag));
Color(color);
}
}
79 changes: 79 additions & 0 deletions src/cascadia/TerminalSettingsEditor/ColorSchemeViewModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#pragma once

#include "ColorSchemeViewModel.g.h"
#include "Utils.h"
#include "ViewModelHelpers.h"
#include "ColorSchemes.h"

static constexpr uint8_t ColorTableDivider{ 8 };
static constexpr uint8_t ColorTableSize{ 16 };

static constexpr std::wstring_view ForegroundColorTag{ L"Foreground" };
static constexpr std::wstring_view BackgroundColorTag{ L"Background" };
static constexpr std::wstring_view CursorColorTag{ L"CursorColor" };
static constexpr std::wstring_view SelectionBackgroundColorTag{ L"SelectionBackground" };

static const std::array<std::wstring, 9> InBoxSchemes = {
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
L"Campbell",
L"Campbell Powershell",
L"Vintage",
L"One Half Dark",
L"One Half Light",
L"Solarized Dark",
L"Solarized Light",
L"Tango Dark",
L"Tango Light"
};

namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
{
struct ColorSchemeViewModel : ColorSchemeViewModelT<ColorSchemeViewModel>, ViewModelHelper<ColorSchemeViewModel>
{
public:
ColorSchemeViewModel(const Model::ColorScheme scheme);

winrt::hstring Name();
void Name(winrt::hstring newName);

Editor::ColorTableEntry ColorEntryAt(uint32_t index);

Model::ColorScheme SettingsModelObject();

WINRT_CALLBACK(PropertyChanged, Windows::UI::Xaml::Data::PropertyChangedEventHandler);

WINRT_PROPERTY(Windows::Foundation::Collections::IVector<Editor::ColorTableEntry>, CurrentNonBrightColorTable, nullptr);
WINRT_PROPERTY(Windows::Foundation::Collections::IVector<Editor::ColorTableEntry>, CurrentBrightColorTable, nullptr);

WINRT_OBSERVABLE_PROPERTY(Editor::ColorTableEntry, CurrentForegroundColor, _PropertyChangedHandlers, nullptr);
WINRT_OBSERVABLE_PROPERTY(Editor::ColorTableEntry, CurrentBackgroundColor, _PropertyChangedHandlers, nullptr);
WINRT_OBSERVABLE_PROPERTY(Editor::ColorTableEntry, CurrentCursorColor, _PropertyChangedHandlers, nullptr);
WINRT_OBSERVABLE_PROPERTY(Editor::ColorTableEntry, CurrentSelectionBackgroundColor, _PropertyChangedHandlers, nullptr);

private:
winrt::hstring _Name;
Model::ColorScheme _scheme;
};

struct ColorTableEntry : ColorTableEntryT<ColorTableEntry>
{
public:
ColorTableEntry(uint8_t index, Windows::UI::Color color);
ColorTableEntry(std::wstring_view tag, Windows::UI::Color color);

WINRT_CALLBACK(PropertyChanged, Windows::UI::Xaml::Data::PropertyChangedEventHandler);
WINRT_OBSERVABLE_PROPERTY(Windows::UI::Color, Color, _PropertyChangedHandlers);
WINRT_OBSERVABLE_PROPERTY(winrt::hstring, Name, _PropertyChangedHandlers);
WINRT_OBSERVABLE_PROPERTY(IInspectable, Tag, _PropertyChangedHandlers);

private:
Windows::UI::Color _color;
};
};

namespace winrt::Microsoft::Terminal::Settings::Editor::factory_implementation
{
BASIC_FACTORY(ColorSchemeViewModel);
}
35 changes: 35 additions & 0 deletions src/cascadia/TerminalSettingsEditor/ColorSchemeViewModel.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import "ColorSchemes.idl";

namespace Microsoft.Terminal.Settings.Editor
{
runtimeclass ColorSchemeViewModel : Windows.UI.Xaml.Data.INotifyPropertyChanged
{
ColorSchemeViewModel(Microsoft.Terminal.Settings.Model.ColorScheme scheme);

String Name;

// Terminal Colors
Windows.Foundation.Collections.IVector<ColorTableEntry> CurrentNonBrightColorTable;
Windows.Foundation.Collections.IVector<ColorTableEntry> CurrentBrightColorTable;

Microsoft.Terminal.Settings.Model.ColorScheme SettingsModelObject { get; };

ColorTableEntry ColorEntryAt(UInt32 Index);

// System Colors
ColorTableEntry CurrentForegroundColor;
ColorTableEntry CurrentBackgroundColor;
ColorTableEntry CurrentCursorColor;
ColorTableEntry CurrentSelectionBackgroundColor;
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
}

[default_interface] runtimeclass ColorTableEntry : Windows.UI.Xaml.Data.INotifyPropertyChanged
{
String Name { get; };
IInspectable Tag;
Windows.UI.Color Color;
}
}
Loading