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

Fix non-standard textbox behavior in Windows #489

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions masonry/src/text/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,18 @@ impl<T: EditableText> TextEditor<T> {
}
Key::Named(_) => Handled::No,
Key::Character(c) => {
let selection = self.inner.selection.unwrap_or(Selection {
anchor: 0,
active: 0,
active_affinity: Affinity::Downstream,
h_pos: None,
});
self.text_mut().edit(selection.range(), &**c);
self.inner.selection = Some(Selection::caret(
selection.min() + c.len(),
// We have just added this character, so we are "affined" with it
Affinity::Downstream,
));
let contents = self.text().as_str().to_string();
ctx.submit_action(Action::TextChanged(contents));
Handled::Yes
self.insert_text(event.text.as_ref().unwrap_or(c), ctx)
}
Key::Unidentified(_) => {
if cfg!(windows) {
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved
event
.text
.as_ref()
.map_or(Handled::No, |c| self.insert_text(c, ctx))
} else {
Handled::No
}
}
Key::Unidentified(_) => Handled::No,
Key::Dead(d) => {
eprintln!("Got dead key {d:?}. Will handle");
Handled::No
Expand Down Expand Up @@ -354,6 +349,24 @@ impl<T: EditableText> TextEditor<T> {
TextEvent::FocusChange(_) => Handled::No,
}
}

fn insert_text(&mut self, c: &winit::keyboard::SmolStr, ctx: &mut EventCtx) -> Handled {
let selection = self.inner.selection.unwrap_or(Selection {
anchor: 0,
active: 0,
active_affinity: Affinity::Downstream,
h_pos: None,
});
self.text_mut().edit(selection.range(), &**c);
self.inner.selection = Some(Selection::caret(
selection.min() + c.len(),
// We have just added this character, so we are "affined" with it
Affinity::Downstream,
));
let contents = self.text().as_str().to_string();
ctx.submit_action(Action::TextChanged(contents));
Handled::Yes
}
}

impl<T: EditableText> Deref for TextEditor<T> {
Expand Down
12 changes: 10 additions & 2 deletions masonry/src/text/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,16 @@ impl<T: Selectable> TextWithSelection<T> {
}
_ => Handled::No,
},
winit::keyboard::Key::Unidentified(_) => todo!(),
winit::keyboard::Key::Dead(_) => todo!(),
winit::keyboard::Key::Unidentified(key) => {
// TODO: figure out correct behavior
tracing::warn!(?key, "got unidentified key, did not handle");
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved
Handled::No
}
winit::keyboard::Key::Dead(key) => {
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved
// TODO: figure out correct behavior
tracing::warn!(?key, "got dead key, did not handle");
Handled::No
}
}
}
TextEvent::KeyboardKey(_, _) => Handled::No,
Expand Down