-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Make text cursor always appear on click #5420
Changes from all commits
5f71284
4ee19b4
2ec0c9d
1c4f222
b3bdce7
a154a3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,8 +96,19 @@ pub fn paint_text_selection( | |
pub fn paint_cursor_end(painter: &Painter, visuals: &Visuals, cursor_rect: Rect) { | ||
let stroke = visuals.text_cursor.stroke; | ||
|
||
let top = cursor_rect.center_top(); | ||
let bottom = cursor_rect.center_bottom(); | ||
// Ensure the cursor is aligned to the pixel grid for whole number widths. | ||
// See https://github.com/emilk/egui/issues/5164 | ||
let (top, bottom) = if (stroke.width as usize) % 2 == 0 { | ||
( | ||
painter.round_pos_to_pixels(cursor_rect.center_top()), | ||
painter.round_pos_to_pixels(cursor_rect.center_bottom()), | ||
) | ||
} else { | ||
( | ||
painter.round_pos_to_pixel_center(cursor_rect.center_top()), | ||
painter.round_pos_to_pixel_center(cursor_rect.center_bottom()), | ||
) | ||
}; | ||
Comment on lines
+101
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could use a comment explaining what is going on, and ideally with a link to #5164 so we can remember to update this code if we move the rounding to epaint There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment added! |
||
|
||
painter.line_segment([top, bottom], (stroke.width, stroke.color)); | ||
|
||
|
@@ -121,14 +132,14 @@ pub fn paint_text_cursor( | |
ui: &Ui, | ||
painter: &Painter, | ||
primary_cursor_rect: Rect, | ||
time_since_last_edit: f64, | ||
time_since_last_interaction: f64, | ||
) { | ||
if ui.visuals().text_cursor.blink { | ||
let on_duration = ui.visuals().text_cursor.on_duration; | ||
let off_duration = ui.visuals().text_cursor.off_duration; | ||
let total_duration = on_duration + off_duration; | ||
|
||
let time_in_cycle = (time_since_last_edit % (total_duration as f64)) as f32; | ||
let time_in_cycle = (time_since_last_interaction % (total_duration as f64)) as f32; | ||
|
||
let wake_in = if time_in_cycle < on_duration { | ||
// Cursor is visible | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would think it's safe to assume that everyone will be using either 1px or 2px cursor thickness. This should make it pixel-perfect for all whole-number thickness values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this work when the scale is 150%?
Also it'd be nice to have a screenshot test that shows the cursor, maybe you can add one?