-
Notifications
You must be signed in to change notification settings - Fork 8.4k
/
Copy pathIconPathConverter.cpp
209 lines (189 loc) · 8.6 KB
/
IconPathConverter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "pch.h"
#include "IconPathConverter.h"
#include "IconPathConverter.g.cpp"
#include "Utils.h"
using namespace winrt::Windows;
using namespace winrt::Windows::UI::Xaml;
namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
// These are templates that help us figure out which BitmapIconSource/FontIconSource to use for a given IconSource.
// We have to do this because some of our code still wants to use WUX/MUX IconSources.
#pragma region BitmapIconSource
template<typename TIconSource>
struct BitmapIconSource
{
};
template<>
struct BitmapIconSource<winrt::Microsoft::UI::Xaml::Controls::IconSource>
{
using type = winrt::Microsoft::UI::Xaml::Controls::BitmapIconSource;
};
template<>
struct BitmapIconSource<winrt::Windows::UI::Xaml::Controls::IconSource>
{
using type = winrt::Windows::UI::Xaml::Controls::BitmapIconSource;
};
#pragma endregion
#pragma region FontIconSource
template<typename TIconSource>
struct FontIconSource
{
};
template<>
struct FontIconSource<winrt::Microsoft::UI::Xaml::Controls::IconSource>
{
using type = winrt::Microsoft::UI::Xaml::Controls::FontIconSource;
};
template<>
struct FontIconSource<winrt::Windows::UI::Xaml::Controls::IconSource>
{
using type = winrt::Windows::UI::Xaml::Controls::FontIconSource;
};
#pragma endregion
// Method Description:
// - Creates an IconSource for the given path. The icon returned is a colored
// icon. If we couldn't create the icon for any reason, we return an empty
// IconElement.
// Template Types:
// - <TIconSource>: The type of IconSource (MUX, WUX) to generate.
// Arguments:
// - path: the full, expanded path to the icon.
// Return Value:
// - An IconElement with its IconSource set, if possible.
template<typename TIconSource>
TIconSource _getColoredBitmapIcon(const winrt::hstring& path)
{
// FontIcon uses glyphs in the private use area, whereas valid URIs only contain ASCII characters.
// To skip throwing on Uri construction, we can quickly check if the first character is ASCII.
if (!path.empty() && path.front() < 128)
{
try
{
winrt::Windows::Foundation::Uri iconUri{ path };
BitmapIconSource<TIconSource>::type iconSource;
// Make sure to set this to false, so we keep the RGB data of the
// image. Otherwise, the icon will be white for all the
// non-transparent pixels in the image.
iconSource.ShowAsMonochrome(false);
iconSource.UriSource(iconUri);
return iconSource;
}
CATCH_LOG();
}
return nullptr;
}
// Method Description:
// - Creates an IconSource for the given path.
// * If the icon is a path to an image, we'll use that.
// * If it isn't, then we'll try and use the text as a FontIcon. If the
// character is in the range of symbols reserved for the Segoe MDL2
// Asserts, well treat it as such. Otherwise, we'll default to a Sego
// UI icon, so things like emoji will work.
// * If we couldn't create the icon for any reason, we return an empty
// IconElement.
// Template Types:
// - <TIconSource>: The type of IconSource (MUX, WUX) to generate.
// Arguments:
// - path: the unprocessed path to the icon.
// Return Value:
// - An IconElement with its IconSource set, if possible.
template<typename TIconSource>
TIconSource _getIconSource(const winrt::hstring& iconPath)
{
TIconSource iconSource{ nullptr };
if (iconPath.size() != 0)
{
const auto expandedIconPath{ _expandIconPath(iconPath) };
iconSource = _getColoredBitmapIcon<TIconSource>(expandedIconPath);
// If we fail to set the icon source using the "icon" as a path,
// let's try it as a symbol/emoji.
//
// Anything longer than 2 wchar_t's _isn't_ an emoji or symbol, so
// don't do this if it's just an invalid path.
if (!iconSource && iconPath.size() <= 2)
{
try
{
FontIconSource<TIconSource>::type icon;
const auto ch = iconPath[0];
// The range of MDL2 Icons isn't explicitly defined, but
// we're using this based off the table on:
// https://docs.microsoft.com/en-us/windows/uwp/design/style/segoe-ui-symbol-font
const auto isMDL2Icon = ch >= L'\uE700' && ch <= L'\uF8FF';
if (isMDL2Icon)
{
icon.FontFamily(winrt::Windows::UI::Xaml::Media::FontFamily{ L"Segoe Fluent Icons, Segoe MDL2 Assets" });
}
else
{
// Note: you _do_ need to manually set the font here.
icon.FontFamily(winrt::Windows::UI::Xaml::Media::FontFamily{ L"Segoe UI" });
}
icon.FontSize(12);
icon.Glyph(iconPath);
iconSource = icon;
}
CATCH_LOG();
}
}
if (!iconSource)
{
// Set the default IconSource to a BitmapIconSource with a null source
// (instead of just nullptr) because there's a really weird crash when swapping
// data bound IconSourceElements in a ListViewTemplate (i.e. CommandPalette).
// Swapping between nullptr IconSources and non-null IconSources causes a crash
// to occur, but swapping between IconSources with a null source and non-null IconSources
// work perfectly fine :shrug:.
BitmapIconSource<TIconSource>::type icon;
icon.UriSource(nullptr);
iconSource = icon;
}
return iconSource;
}
static winrt::hstring _expandIconPath(hstring iconPath)
{
if (iconPath.empty())
{
return iconPath;
}
winrt::hstring envExpandedPath{ wil::ExpandEnvironmentStringsW<std::wstring>(iconPath.c_str()) };
return envExpandedPath;
}
// Method Description:
// - Attempt to convert something into another type. For the
// IconPathConverter, we support a variety of icons:
// * If the icon is a path to an image, we'll use that.
// * If it isn't, then we'll try and use the text as a FontIcon. If the
// character is in the range of symbols reserved for the Segoe MDL2
// Asserts, well treat it as such. Otherwise, we'll default to a Sego
// UI icon, so things like emoji will work.
// - MUST BE CALLED ON THE UI THREAD.
// Arguments:
// - value: the input object to attempt to convert into an IconSource.
// Return Value:
// - Visible if the object was a string and wasn't the empty string.
Foundation::IInspectable IconPathConverter::Convert(const Foundation::IInspectable& value,
const Windows::UI::Xaml::Interop::TypeName& /* targetType */,
const Foundation::IInspectable& /* parameter */,
const hstring& /* language */)
{
const auto& iconPath = winrt::unbox_value_or<winrt::hstring>(value, L"");
return _getIconSource<Controls::IconSource>(iconPath);
}
// unused for one-way bindings
Foundation::IInspectable IconPathConverter::ConvertBack(const Foundation::IInspectable& /* value */,
const Windows::UI::Xaml::Interop::TypeName& /* targetType */,
const Foundation::IInspectable& /* parameter */,
const hstring& /* language */)
{
throw hresult_not_implemented();
}
Windows::UI::Xaml::Controls::IconSource IconPathConverter::IconSourceWUX(hstring path)
{
return _getIconSource<Windows::UI::Xaml::Controls::IconSource>(path);
}
Microsoft::UI::Xaml::Controls::IconSource IconPathConverter::IconSourceMUX(hstring path)
{
return _getIconSource<Microsoft::UI::Xaml::Controls::IconSource>(path);
}
}