-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsWindow.cpp
More file actions
143 lines (130 loc) · 4.46 KB
/
SettingsWindow.cpp
File metadata and controls
143 lines (130 loc) · 4.46 KB
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
#include "SettingsWindow.h"
#include <winrt/Windows.UI.Xaml.Controls.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <Windows.h>
#include <unordered_set>
#include <string>
#include <winrt/Windows.UI.Xaml.Controls.Primitives.h>
#include <microsoft.ui.xaml.window.h>
#include <ranges>
#include "ITextDisplay.h"
static auto GetFonts(HDC hdc)
{
LOGFONT font{ .lfCharSet = DEFAULT_CHARSET };
std::unordered_set<std::wstring> fonts;
EnumFontFamiliesExW(
hdc,
&font,
+[](LOGFONT const* font, TEXTMETRIC const* lpntme, DWORD fontType, LPARAM param)
{
//OutputDebugString(font->lfFaceName);
reinterpret_cast<std::unordered_set<std::wstring>*>(param)->insert(font->lfFaceName);
//OutputDebugString(std::format(L"{}\n", font->lfFaceName).data());
return 1;
},
reinterpret_cast<LPARAM>(&fonts),
0
);
std::vector<winrt::Windows::Foundation::IInspectable> retItems;
retItems.reserve(fonts.size());
std::ranges::transform(fonts, std::back_inserter(retItems), [](std::wstring const& fontName) {return winrt::box_value(fontName); });
return retItems;
}
void SettingsWindow::textChanged(
winrt::Windows::Foundation::IInspectable const& sender,
winrt::Windows::UI::Xaml::Controls::TextChangedEventArgs const&)
{
auto newText = sender.as<winrt::Windows::UI::Xaml::Controls::TextBox>().Text();
for (auto display : m_displays)
display->SetText(newText);
}
void SettingsWindow::fontFamilyChanged(
winrt::Windows::Foundation::IInspectable const& sender,
winrt::Windows::UI::Xaml::Controls::SelectionChangedEventArgs const& args)
{
auto fontFamily = args.AddedItems().GetAt(0).as<winrt::hstring>();
for (auto display : m_displays)
display->SetFontFamily(fontFamily);
}
void SettingsWindow::fontSizeChanged(
winrt::Windows::Foundation::IInspectable const& sender,
winrt::Windows::UI::Xaml::Controls::SelectionChangedEventArgs const& args)
{
auto fontSize = winrt::unbox_value<int>(args.AddedItems().GetAt(0));
for (auto display : m_displays)
display->SetFontSize(fontSize);
}
SettingsWindow::SettingsWindow()
{
auto fonts = GetFonts(GetDC(m_hwnd.get()));
winrt::Windows::UI::Xaml::Controls::Grid rootGrid;
{
winrt::Windows::UI::Xaml::Controls::RowDefinition rows[2];
rows[0].Height(winrt::Windows::UI::Xaml::GridLength{
.GridUnitType = winrt::Windows::UI::Xaml::GridUnitType::Auto
});
rows[1].Height(winrt::Windows::UI::Xaml::GridLength{
.Value = 1.0,
.GridUnitType = winrt::Windows::UI::Xaml::GridUnitType::Star
});
rootGrid.RowDefinitions().ReplaceAll(rows);
}
{
winrt::Windows::UI::Xaml::Controls::ColumnDefinition columns[2];
for (auto& column : columns)
{
column.Width(winrt::Windows::UI::Xaml::GridLength{
.Value = 1.0,
.GridUnitType = winrt::Windows::UI::Xaml::GridUnitType::Star
});
}
rootGrid.ColumnDefinitions().ReplaceAll(columns);
}
winrt::Windows::UI::Xaml::Controls::ComboBox fontFamilyComboBox;
{
fontFamilyComboBox.Header(winrt::box_value(L"Font family"));
winrt::Windows::UI::Xaml::Controls::Grid::SetColumn(fontFamilyComboBox, 1);
fontFamilyComboBox.Items().ReplaceAll(fonts);
fontFamilyComboBox.SelectionChanged({ this, &SettingsWindow::fontFamilyChanged });
}
winrt::Windows::UI::Xaml::Controls::ComboBox fontSizeComboBox;
{
fontSizeComboBox.Header(winrt::box_value(L"Font size"));
fontSizeComboBox.Items().ReplaceAll({
winrt::box_value(8),
winrt::box_value(9),
winrt::box_value(10),
winrt::box_value(11),
winrt::box_value(12),
winrt::box_value(14),
winrt::box_value(16),
winrt::box_value(18),
winrt::box_value(20),
winrt::box_value(24),
winrt::box_value(28),
winrt::box_value(36),
winrt::box_value(48),
winrt::box_value(56),
winrt::box_value(72)
});
fontSizeComboBox.SelectedIndex(13);
fontSizeComboBox.SelectionChanged({ this, &SettingsWindow::fontSizeChanged });
}
winrt::Windows::UI::Xaml::Controls::TextBox inputText;
{
inputText.Header(winrt::box_value(L"Input here to test font:"));
inputText.Text(L"微软雅黑");
winrt::Windows::UI::Xaml::Controls::Grid::SetRow(inputText, 1);
winrt::Windows::UI::Xaml::Controls::Grid::SetColumnSpan(inputText, 2);
inputText.TextChanged({ this, &SettingsWindow::textChanged });
inputText.AcceptsReturn(true);
inputText.TextWrapping(winrt::Windows::UI::Xaml::TextWrapping::Wrap);
}
rootGrid.Children().ReplaceAll({ fontFamilyComboBox, fontSizeComboBox, inputText });
SetContent(rootGrid);
}
SettingsWindow& SettingsWindow::operator<<(ITextDisplay& window)
{
m_displays.push_back(&window);
return *this;
}