Skip to content

Commit

Permalink
wip: bidi and affinity
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcur committed Sep 13, 2024
1 parent 62a4691 commit 37d1ad0
Showing 1 changed file with 63 additions and 30 deletions.
93 changes: 63 additions & 30 deletions examples/vello_editor/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,11 @@ impl Editor {
// Send that rectangle to the platform to suggest placement for the IME
// candidate box.
let mut union_rect = None;

debug_assert!(!text_range.is_empty());
let preedit_selection = Selection::from_cursors(
Cursor::from_index(&self.layout, text_range.start, Affinity::Downstream),
Cursor::from_index(&self.layout, text_range.end, Affinity::Downstream),
Cursor::from_index(&self.layout, text_range.end - 1, Affinity::Upstream),
);

preedit_selection.geometry_with(&self.layout, |rect| {
Expand Down Expand Up @@ -385,11 +387,21 @@ impl Editor {
};

self.update_layout(self.width, 1.0);
self.selection = Selection::from_index(
&self.layout,
commit_start + text.len(),
Affinity::Downstream,
);

if text.is_empty() {
// is this case ever hit?
self.selection = Selection::from_index(
&self.layout,
commit_start,
Affinity::Downstream,
);
} else {
self.selection = Selection::from_index(
&self.layout,
commit_start + text.len() - 1,
Affinity::Upstream,
);
}
}
Ime::Preedit(text, compose_cursor) => {
let preedit_start = if let Some(text_range) = self.preedit_range.take() {
Expand All @@ -404,35 +416,56 @@ impl Editor {
};

if text.is_empty() {
self.preedit_range = None;
} else {
self.preedit_range = Some(preedit_start..preedit_start + text.len());
}

self.update_layout(self.width, 1.0);
self.update_layout(self.width, 1.0);

if let Some(compose_cursor) = compose_cursor {
// Select the location indicated by the IME.
self.selection = Selection::from_cursors(
Cursor::from_index(
if preedit_start > 0 {
self.selection = Selection::from_index(
&self.layout,
preedit_start + compose_cursor.0,
Affinity::Downstream,
),
Cursor::from_index(
preedit_start - 1,
Affinity::Upstream,
);
} else {
self.selection = Selection::from_index(
&self.layout,
preedit_start + compose_cursor.1,
preedit_start,
Affinity::Downstream,
),
);
);
}
} else {
// IME indicates nothing is to be selected: collapse the selection to a
// caret just in front of the preedit.
self.selection = Selection::from_index(
&self.layout,
preedit_start,
Affinity::Downstream,
);
self.preedit_range = Some(preedit_start..preedit_start + text.len());
self.update_layout(self.width, 1.0);

if let Some(compose_cursor) = compose_cursor {
// Select the location indicated by the IME.
if compose_cursor.0 == compose_cursor.1 {
self.selection = Selection::from_index(
&self.layout,
preedit_start + compose_cursor.0,
Affinity::Downstream,
)
} else {
self.selection = Selection::from_cursors(
Cursor::from_index(
&self.layout,
preedit_start + compose_cursor.0,
Affinity::Downstream,
),
Cursor::from_index(
&self.layout,
preedit_start + compose_cursor.1 - 1,
Affinity::Upstream,
),
);
}
} else {
// IME indicates nothing is to be selected: collapse the selection to a
// caret just in front of the preedit.
self.selection = Selection::from_index(
&self.layout,
preedit_start,
Affinity::Downstream,
);
}
}

self.set_ime_cursor_area(window);
Expand Down

0 comments on commit 37d1ad0

Please sign in to comment.