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

Group font options in the json into a single object #10433

Merged
merged 14 commits into from
Jul 1, 2021
6 changes: 3 additions & 3 deletions src/cascadia/TerminalSettingsEditor/Profiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
OBSERVABLE_PROJECTED_SETTING(_profile, UseAcrylic);
OBSERVABLE_PROJECTED_SETTING(_profile, AcrylicOpacity);
OBSERVABLE_PROJECTED_SETTING(_profile, ScrollState);
OBSERVABLE_PROJECTED_SETTING(_profile, FontFace);
OBSERVABLE_PROJECTED_SETTING(_profile, FontSize);
OBSERVABLE_PROJECTED_SETTING(_profile, FontWeight);
OBSERVABLE_PROJECTED_SETTING(_profile.FontInfo(), FontFace);
OBSERVABLE_PROJECTED_SETTING(_profile.FontInfo(), FontSize);
OBSERVABLE_PROJECTED_SETTING(_profile.FontInfo(), FontWeight);
OBSERVABLE_PROJECTED_SETTING(_profile, Padding);
OBSERVABLE_PROJECTED_SETTING(_profile, Commandline);
OBSERVABLE_PROJECTED_SETTING(_profile, StartingDirectory);
Expand Down
14 changes: 11 additions & 3 deletions src/cascadia/TerminalSettingsModel/CascadiaSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,13 @@ winrt::Microsoft::Terminal::Settings::Model::Profile CascadiaSettings::Duplicate
duplicated->##settingName(source.##settingName()); \
}

#define DUPLICATE_FONT_SETTING_MACRO(settingName) \
if (source.FontInfo().Has##settingName() || \
(source.FontInfo().##settingName##OverrideSource() != nullptr && source.FontInfo().##settingName##OverrideSource().SourceProfile().Origin() != OriginTag::ProfilesDefaults)) \
{ \
duplicated->FontInfo().##settingName(source.FontInfo().##settingName()); \
}
lhecker marked this conversation as resolved.
Show resolved Hide resolved

#define DUPLICATE_APPEARANCE_SETTING_MACRO(settingName) \
if (source.DefaultAppearance().Has##settingName() || \
(source.DefaultAppearance().##settingName##OverrideSource() != nullptr && source.DefaultAppearance().##settingName##OverrideSource().SourceProfile().Origin() != OriginTag::ProfilesDefaults)) \
Expand All @@ -319,9 +326,6 @@ winrt::Microsoft::Terminal::Settings::Model::Profile CascadiaSettings::Duplicate
DUPLICATE_SETTING_MACRO(UseAcrylic);
DUPLICATE_SETTING_MACRO(AcrylicOpacity);
DUPLICATE_SETTING_MACRO(ScrollState);
DUPLICATE_SETTING_MACRO(FontFace);
DUPLICATE_SETTING_MACRO(FontSize);
DUPLICATE_SETTING_MACRO(FontWeight);
DUPLICATE_SETTING_MACRO(Padding);
DUPLICATE_SETTING_MACRO(Commandline);
DUPLICATE_SETTING_MACRO(StartingDirectory);
Expand All @@ -333,6 +337,10 @@ winrt::Microsoft::Terminal::Settings::Model::Profile CascadiaSettings::Duplicate
DUPLICATE_SETTING_MACRO(AltGrAliasing);
DUPLICATE_SETTING_MACRO(BellStyle);

DUPLICATE_FONT_SETTING_MACRO(FontFace);
DUPLICATE_FONT_SETTING_MACRO(FontSize);
DUPLICATE_FONT_SETTING_MACRO(FontWeight);

DUPLICATE_APPEARANCE_SETTING_MACRO(ColorSchemeName);
DUPLICATE_APPEARANCE_SETTING_MACRO(Foreground);
DUPLICATE_APPEARANCE_SETTING_MACRO(Background);
Expand Down
79 changes: 79 additions & 0 deletions src/cascadia/TerminalSettingsModel/FontConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#include "pch.h"
#include "FontConfig.h"
#include "FontConfig.g.cpp"
#include "TerminalSettingsSerializationHelpers.h"
#include "JsonUtils.h"

using namespace Microsoft::Terminal::Settings::Model;
using namespace winrt::Microsoft::Terminal::Settings::Model::implementation;

static constexpr std::string_view FontInfoKey{ "font" };
static constexpr std::string_view FontFaceKey{ "fontFace" };
static constexpr std::string_view FontSizeKey{ "fontSize" };
static constexpr std::string_view FontWeightKey{ "fontWeight" };
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved

winrt::Microsoft::Terminal::Settings::Model::implementation::FontConfig::FontConfig(const winrt::weak_ref<Profile> sourceProfile) :
_sourceProfile(sourceProfile)
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
{
}

winrt::com_ptr<FontConfig> FontConfig::CopyFontInfo(const winrt::com_ptr<FontConfig> source, const winrt::weak_ref<Profile> sourceProfile)
Copy link
Member

Choose a reason for hiding this comment

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

Taking those arguments by value will force the reference count to be incremented for the duration of the call. It's probably a good idea to just take source as a pointer or reference as you aren't storing it elsewhere for later.
Especially because the code around winrt::com_ptr::copy_from feels a bit non-obvious / roundabout to me.

Copy link
Contributor Author

@PankajBhojwani PankajBhojwani Jun 30, 2021

Choose a reason for hiding this comment

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

So this was intentionally done this way to maintain consistency with Profile::CopySettings and AppearanceConfig::CopyAppearance and I'd like to keep it this way because of that

Copy link
Member

Choose a reason for hiding this comment

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

I'm convinced that we'll have to start to clean up such code in the future though.
Isn't it quite seldom that you touch all places simultaneously to clean something up? I don't believe there's (in a rhetoric sense) "any better time to do it than now", as such house-keeping tasks will always be unrelated to any actual task by their nature.

{
auto fontInfo{ winrt::make_self<FontConfig>(sourceProfile) };
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
auto const sourceFontInfo = source.try_as<FontConfig>();
fontInfo->_FontFace = sourceFontInfo->_FontFace;
fontInfo->_FontSize = sourceFontInfo->_FontSize;
fontInfo->_FontWeight = sourceFontInfo->_FontWeight;
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
return fontInfo;
}

Json::Value FontConfig::ToJson() const
{
Json::Value json{ Json::ValueType::objectValue };

JsonUtils::SetValueForKey(json, FontFaceKey, _FontFace);
JsonUtils::SetValueForKey(json, FontSizeKey, _FontSize);
JsonUtils::SetValueForKey(json, FontWeightKey, _FontWeight);

return json;
}

// Method Description:
// - Layer values from the given json object on top of the existing properties
// of this object. For any keys we're expecting to be able to parse in the
// given object, we'll parse them and replace our settings with values from
// the new json object. Properties that _aren't_ in the json object will _not_
// be replaced.
// - Optional values that are set to `null` in the json object
// will be set to nullopt.
// - This is similar to Profile::LayerJson but for FontConfig
// Arguments:
// - json: an object which should be a partial serialization of a FontConfig object.
void FontConfig::LayerJson(const Json::Value& json)
{
// Legacy users may not have a font object defined in their profile,
// so check for that before we decide how to parse this
if (json.isMember(JsonKey(FontInfoKey)))
{
// A font object is defined, use that
const auto fontInfoJson = json[JsonKey(FontInfoKey)];
JsonUtils::GetValueForKey(fontInfoJson, FontFaceKey, _FontFace);
JsonUtils::GetValueForKey(fontInfoJson, FontSizeKey, _FontSize);
JsonUtils::GetValueForKey(fontInfoJson, FontWeightKey, _FontWeight);
}
else
{
// No font object is defined
JsonUtils::GetValueForKey(json, FontFaceKey, _FontFace);
JsonUtils::GetValueForKey(json, FontSizeKey, _FontSize);
JsonUtils::GetValueForKey(json, FontWeightKey, _FontWeight);
}
}

winrt::Microsoft::Terminal::Settings::Model::Profile FontConfig::SourceProfile()
{
return _sourceProfile.get();
}
34 changes: 34 additions & 0 deletions src/cascadia/TerminalSettingsModel/FontConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*++
Copyright (c) Microsoft Corporation
Licensed under the MIT license.
--*/
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved

#pragma once

#include "pch.h"
#include "FontConfig.g.h"
#include "JsonUtils.h"
#include "../inc/cppwinrt_utils.h"
#include "IInheritable.h"
#include <DefaultSettings.h>

namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
struct FontConfig : FontConfigT<FontConfig>, IInheritable<FontConfig>
{
public:
FontConfig(const winrt::weak_ref<Profile> sourceProfile);
static winrt::com_ptr<FontConfig> CopyFontInfo(const winrt::com_ptr<FontConfig> source, const winrt::weak_ref<Profile> sourceProfile);
Json::Value ToJson() const;
void LayerJson(const Json::Value& json);

Model::Profile SourceProfile();

INHERITABLE_SETTING(Model::FontConfig, hstring, FontFace, DEFAULT_FONT_FACE);
INHERITABLE_SETTING(Model::FontConfig, int32_t, FontSize, DEFAULT_FONT_SIZE);
INHERITABLE_SETTING(Model::FontConfig, Windows::UI::Text::FontWeight, FontWeight, DEFAULT_FONT_WEIGHT);

private:
winrt::weak_ref<Profile> _sourceProfile;
};
}
20 changes: 20 additions & 0 deletions src/cascadia/TerminalSettingsModel/FontConfig.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import "Profile.idl";
#include "IInheritable.idl.h"

#define INHERITABLE_FONT_SETTING(Type, Name) \
_BASE_INHERITABLE_SETTING(Type, Name); \
Microsoft.Terminal.Settings.Model.FontConfig Name##OverrideSource { get; }

namespace Microsoft.Terminal.Settings.Model
{
[default_interface] runtimeclass FontConfig {
Microsoft.Terminal.Settings.Model.Profile SourceProfile { get; };

INHERITABLE_FONT_SETTING(String, FontFace);
INHERITABLE_FONT_SETTING(Int32, FontSize);
INHERITABLE_FONT_SETTING(Windows.UI.Text.FontWeight, FontWeight);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
<ClInclude Include="AppearanceConfig.h">
<DependentUpon>AppearanceConfig.idl</DependentUpon>
</ClInclude>
<ClInclude Include="FontConfig.h">
<DependentUpon>FontConfig.idl</DependentUpon>
</ClInclude>
<ClInclude Include="EnumMappings.h">
<DependentUpon>EnumMappings.idl</DependentUpon>
</ClInclude>
Expand Down Expand Up @@ -123,6 +126,9 @@
<ClCompile Include="AppearanceConfig.cpp">
<DependentUpon>AppearanceConfig.idl</DependentUpon>
</ClCompile>
<ClCompile Include="FontConfig.cpp">
<DependentUpon>FontConfig.idl</DependentUpon>
</ClCompile>
<ClCompile Include="TerminalSettings.cpp">
<DependentUpon>TerminalSettings.idl</DependentUpon>
</ClCompile>
Expand Down Expand Up @@ -154,6 +160,7 @@
<Midl Include="KeyChordSerialization.idl" />
<Midl Include="AppearanceConfig.idl" />
<Midl Include="IAppearanceConfig.idl" />
<Midl Include="FontConfig.idl" />
</ItemGroup>
<!-- ========================= Misc Files ======================== -->
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Midl Include="TerminalSettings.idl" />
<Midl Include="AppearanceConfig.idl" />
<Midl Include="IAppearanceConfig.idl" />
<Midl Include="FontConfig.idl" />
<Midl Include="DefaultTerminal.idl" />
</ItemGroup>
<ItemGroup>
Expand Down
49 changes: 35 additions & 14 deletions src/cascadia/TerminalSettingsModel/Profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "LegacyProfileGeneratorNamespaces.h"
#include "TerminalSettingsSerializationHelpers.h"
#include "AppearanceConfig.h"
#include "FontConfig.h"

#include "Profile.g.cpp"

Expand All @@ -34,9 +35,7 @@ static constexpr std::string_view AltGrAliasingKey{ "altGrAliasing" };

static constexpr std::string_view ConnectionTypeKey{ "connectionType" };
static constexpr std::string_view CommandlineKey{ "commandline" };
static constexpr std::string_view FontFaceKey{ "fontFace" };
static constexpr std::string_view FontSizeKey{ "fontSize" };
static constexpr std::string_view FontWeightKey{ "fontWeight" };
static constexpr std::string_view FontInfoKey{ "font" };
static constexpr std::string_view AcrylicTransparencyKey{ "acrylicOpacity" };
static constexpr std::string_view UseAcrylicKey{ "useAcrylic" };
static constexpr std::string_view ScrollbarStateKey{ "scrollbarState" };
Expand Down Expand Up @@ -76,9 +75,6 @@ winrt::com_ptr<Profile> Profile::CopySettings(winrt::com_ptr<Profile> source)
profile->_UseAcrylic = source->_UseAcrylic;
profile->_AcrylicOpacity = source->_AcrylicOpacity;
profile->_ScrollState = source->_ScrollState;
profile->_FontFace = source->_FontFace;
profile->_FontSize = source->_FontSize;
profile->_FontWeight = source->_FontWeight;
profile->_Padding = source->_Padding;
profile->_Commandline = source->_Commandline;
profile->_StartingDirectory = source->_StartingDirectory;
Expand All @@ -92,8 +88,14 @@ winrt::com_ptr<Profile> Profile::CopySettings(winrt::com_ptr<Profile> source)
profile->_ConnectionType = source->_ConnectionType;
profile->_Origin = source->_Origin;

// Copy over the appearance
// Copy over the font info
const auto weakRefToProfile = weak_ref<Model::Profile>(*profile);
winrt::com_ptr<FontConfig> sourceFontInfoImpl;
sourceFontInfoImpl.copy_from(winrt::get_self<FontConfig>(source->_FontInfo));
lhecker marked this conversation as resolved.
Show resolved Hide resolved
auto copiedFontInfo = FontConfig::CopyFontInfo(sourceFontInfoImpl, weakRefToProfile);
profile->_FontInfo = *copiedFontInfo;

// Copy over the appearance
winrt::com_ptr<AppearanceConfig> sourceDefaultAppearanceImpl;
sourceDefaultAppearanceImpl.copy_from(winrt::get_self<AppearanceConfig>(source->_DefaultAppearance));
auto copiedDefaultAppearance = AppearanceConfig::CopyAppearance(sourceDefaultAppearanceImpl, weakRefToProfile);
Expand Down Expand Up @@ -315,6 +317,10 @@ void Profile::LayerJson(const Json::Value& json)
auto defaultAppearanceImpl = winrt::get_self<implementation::AppearanceConfig>(_DefaultAppearance);
defaultAppearanceImpl->LayerJson(json);

// Font Settings
auto fontInfoImpl = winrt::get_self<implementation::FontConfig>(_FontInfo);
fontInfoImpl->LayerJson(json);

// Profile-specific Settings
JsonUtils::GetValueForKey(json, NameKey, _Name);
JsonUtils::GetValueForKey(json, GuidKey, _Guid);
Expand All @@ -327,12 +333,9 @@ void Profile::LayerJson(const Json::Value& json)
JsonUtils::GetValueForKey(json, AltGrAliasingKey, _AltGrAliasing);
JsonUtils::GetValueForKey(json, TabTitleKey, _TabTitle);

// Control Settings
JsonUtils::GetValueForKey(json, FontWeightKey, _FontWeight);
// Control Settings
JsonUtils::GetValueForKey(json, ConnectionTypeKey, _ConnectionType);
JsonUtils::GetValueForKey(json, CommandlineKey, _Commandline);
JsonUtils::GetValueForKey(json, FontFaceKey, _FontFace);
JsonUtils::GetValueForKey(json, FontSizeKey, _FontSize);
JsonUtils::GetValueForKey(json, AcrylicTransparencyKey, _AcrylicOpacity);
JsonUtils::GetValueForKey(json, UseAcrylicKey, _UseAcrylic);
JsonUtils::GetValueForKey(json, SuppressApplicationTitleKey, _SuppressApplicationTitle);
Expand Down Expand Up @@ -392,13 +395,31 @@ void Profile::_FinalizeInheritance()
}
}
}
if (auto fontInfoImpl = get_self<FontConfig>(_FontInfo))
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
{
// Clear any existing parents first, we don't want duplicates from any previous
// calls to this function
fontInfoImpl->ClearParents();
for (auto& parent : _parents)
{
if (auto parentFontInfoImpl = parent->_FontInfo.try_as<FontConfig>())
{
fontInfoImpl->InsertParent(parentFontInfoImpl);
}
}
}
}

winrt::Microsoft::Terminal::Settings::Model::IAppearanceConfig Profile::DefaultAppearance()
{
return _DefaultAppearance;
}

winrt::Microsoft::Terminal::Settings::Model::FontConfig Profile::FontInfo()
{
return _FontInfo;
}

// Method Description:
// - Helper function for expanding any environment variables in a user-supplied starting directory and validating the resulting path
// Arguments:
Expand Down Expand Up @@ -510,11 +531,8 @@ Json::Value Profile::ToJson() const
JsonUtils::SetValueForKey(json, TabTitleKey, _TabTitle);

// Control Settings
JsonUtils::SetValueForKey(json, FontWeightKey, _FontWeight);
JsonUtils::SetValueForKey(json, ConnectionTypeKey, _ConnectionType);
JsonUtils::SetValueForKey(json, CommandlineKey, _Commandline);
JsonUtils::SetValueForKey(json, FontFaceKey, _FontFace);
JsonUtils::SetValueForKey(json, FontSizeKey, _FontSize);
JsonUtils::SetValueForKey(json, AcrylicTransparencyKey, _AcrylicOpacity);
JsonUtils::SetValueForKey(json, UseAcrylicKey, _UseAcrylic);
JsonUtils::SetValueForKey(json, SuppressApplicationTitleKey, _SuppressApplicationTitle);
Expand All @@ -530,6 +548,9 @@ Json::Value Profile::ToJson() const
JsonUtils::SetValueForKey(json, TabColorKey, _TabColor);
JsonUtils::SetValueForKey(json, BellStyleKey, _BellStyle);

// Font settings
json[JsonKey(FontInfoKey)] = winrt::get_self<FontConfig>(_FontInfo)->ToJson();

if (_UnfocusedAppearance)
{
json[JsonKey(UnfocusedAppearanceKey)] = winrt::get_self<AppearanceConfig>(_UnfocusedAppearance.value())->ToJson();
Expand Down
6 changes: 3 additions & 3 deletions src/cascadia/TerminalSettingsModel/Profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Author(s):
#include "JsonUtils.h"
#include <DefaultSettings.h>
#include "AppearanceConfig.h"
#include "FontConfig.h"

// fwdecl unittest classes
namespace SettingsModelLocalTests
Expand Down Expand Up @@ -98,6 +99,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
static guid GetGuidOrGenerateForJson(const Json::Value& json) noexcept;

Model::IAppearanceConfig DefaultAppearance();
Model::FontConfig FontInfo();

void _FinalizeInheritance() override;

Expand All @@ -121,9 +123,6 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
INHERITABLE_SETTING(Model::Profile, double, AcrylicOpacity, 0.5);
INHERITABLE_SETTING(Model::Profile, Microsoft::Terminal::Control::ScrollbarState, ScrollState, Microsoft::Terminal::Control::ScrollbarState::Visible);

INHERITABLE_SETTING(Model::Profile, hstring, FontFace, DEFAULT_FONT_FACE);
INHERITABLE_SETTING(Model::Profile, int32_t, FontSize, DEFAULT_FONT_SIZE);
INHERITABLE_SETTING(Model::Profile, Windows::UI::Text::FontWeight, FontWeight, DEFAULT_FONT_WEIGHT);
INHERITABLE_SETTING(Model::Profile, hstring, Padding, DEFAULT_PADDING);

INHERITABLE_SETTING(Model::Profile, hstring, Commandline, L"cmd.exe");
Expand All @@ -143,6 +142,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

private:
Model::IAppearanceConfig _DefaultAppearance{ winrt::make<AppearanceConfig>(weak_ref<Model::Profile>(*this)) };
Model::FontConfig _FontInfo{ winrt::make<FontConfig>(weak_ref<Model::Profile>(*this)) };
static std::wstring EvaluateStartingDirectory(const std::wstring& directory);

static guid _GenerateGuidForProfile(const hstring& name, const hstring& source) noexcept;
Expand Down
Loading