aboutsummaryrefslogtreecommitdiff
path: root/demos/input_method/main.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-04-21 17:29:37 +0800
committercrupest <crupest@outlook.com>2020-04-21 17:29:37 +0800
commit977baa3a42a8b813e75a3666809155604cb6713d (patch)
treefaa787e43fe8203139f79f8b39332584c1422a7d /demos/input_method/main.cpp
parent2898b68fc4f7ff40844ddf5a1d0b59f76b06290f (diff)
downloadcru-977baa3a42a8b813e75a3666809155604cb6713d.tar.gz
cru-977baa3a42a8b813e75a3666809155604cb6713d.tar.bz2
cru-977baa3a42a8b813e75a3666809155604cb6713d.zip
...
Diffstat (limited to 'demos/input_method/main.cpp')
-rw-r--r--demos/input_method/main.cpp49
1 files changed, 29 insertions, 20 deletions
diff --git a/demos/input_method/main.cpp b/demos/input_method/main.cpp
index ce96a8c1..ebca7a9d 100644
--- a/demos/input_method/main.cpp
+++ b/demos/input_method/main.cpp
@@ -33,6 +33,8 @@ int main() {
std::shared_ptr<IFont> font = graph_factory->CreateFont("等线", 30);
+ float window_width = 10000;
+
auto prompt_text_layout =
graph_factory->CreateTextLayout(font,
"Alt+F1: Enable IME\n"
@@ -40,29 +42,35 @@ int main() {
"Alt+F3: Complete composition.\n"
"Alt+F4: Cancel composition.");
- auto no_composition_text_layout =
- graph_factory->CreateTextLayout(font, "Not compositioning!");
-
std::optional<CompositionText> optional_composition_text;
std::string committed_text;
+ window->ResizeEvent()->AddHandler(
+ [&prompt_text_layout, &window_width](const Size& size) {
+ prompt_text_layout->SetMaxWidth(size.width);
+ window_width = size.width;
+ });
+
window->PaintEvent()->AddHandler([&](auto) {
auto painter = window->BeginPaint();
painter->Clear(colors::white);
painter->DrawText(Point{}, prompt_text_layout.get(), brush.get());
- auto anchor_y = prompt_text_layout->GetTextBounds().height;
+ const auto anchor_y = prompt_text_layout->GetTextBounds().height;
+
+ auto text_layout = graph_factory->CreateTextLayout(
+ font,
+ committed_text +
+ (optional_composition_text ? optional_composition_text->text : ""));
if (optional_composition_text) {
const auto& composition_text = *optional_composition_text;
- auto composition_text_layout =
- graph_factory->CreateTextLayout(font, composition_text.text);
for (int i = 0; i < composition_text.clauses.size(); i++) {
const auto& clause = composition_text.clauses[i];
- auto rects = composition_text_layout->TextRangeRect(
- TextRange::FromTwoSides(clause.start, clause.end));
+ auto rects = text_layout->TextRangeRect(TextRange::FromTwoSides(
+ clause.start, clause.end, committed_text.size()));
const auto& b = clause.target
? target_clause_brush
: (i % 2 ? odd_clause_brush : even_clause_brush);
@@ -71,22 +79,23 @@ int main() {
painter->FillRectangle(rect, b.get());
}
}
+ }
- painter->DrawText(Point{0, anchor_y}, composition_text_layout.get(),
- brush.get());
+ painter->DrawText(Point{0, anchor_y}, text_layout.get(), brush.get());
- anchor_y += composition_text_layout->GetTextBounds().height;
- } else {
- painter->DrawText(Point{0, anchor_y}, no_composition_text_layout.get(),
- brush.get());
- anchor_y += no_composition_text_layout->GetTextBounds().height;
- }
+ if (optional_composition_text) {
+ const auto& composition_text = *optional_composition_text;
- auto committed_text_layout =
- graph_factory->CreateTextLayout(font, committed_text);
+ const auto cursor_pos = composition_text.selection.position +
+ gsl::narrow_cast<int>(committed_text.size());
- painter->DrawText(Point{0, anchor_y}, committed_text_layout.get(),
- brush.get());
+ const auto cursor_lefttop =
+ text_layout->TextSingleRect(cursor_pos, false);
+
+ painter->FillRectangle(Rect{cursor_lefttop.x, cursor_lefttop.y + anchor_y,
+ 3, font->GetFontSize()},
+ brush.get());
+ }
});
window->KeyDownEvent()->AddHandler(