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

Add cursor timer reset on input #20

Merged
merged 5 commits into from
Feb 18, 2024
Merged

Add cursor timer reset on input #20

merged 5 commits into from
Feb 18, 2024

Conversation

chompaa
Copy link
Contributor

@chompaa chompaa commented Feb 18, 2024

Resets the cursor's timer on inputs (other than KeyCode::Enter), solving #10

I'm not sure if this solution is ideal and aligns with the project's goals. Please let me know what you think.

@rparrett
Copy link
Owner

rparrett commented Feb 18, 2024

I'm not sure if this solution is ideal and aligns with the project's goals. Please let me know what you think.

Yeah, I'd love to not involve any bespoke macros here.

I actually think this might be relatively simple to accomplish by adding a should_reset field to TextInputCursorTimer and handling that case in one of the existing cursor systems, or a new cursor system.

@chompaa
Copy link
Contributor Author

chompaa commented Feb 18, 2024

I actually think this might be relatively simple to accomplish by adding a should_reset field to TextInputCursorTimer and handling that case in one of the existing cursor systems, or a new cursor systems.

This sounds a lot simpler, I'll revert my changes and try to implement it this way instead.

@rparrett
Copy link
Owner

Just a heads up: I merged #21 in the mean time which might cause some confusion.

@chompaa
Copy link
Contributor Author

chompaa commented Feb 18, 2024

I've attempted the suggested implementation. I don't love that InnerText is being queried every frame now though, is this okay?

@rparrett
Copy link
Owner

rparrett commented Feb 18, 2024

I don't love that InnerText is being queried every frame now though, is this okay?

I am a bit on the fence about

  • Being okay with that.
  • Adding a reset_cursor system .before(blink_cursor)
  • Something like
    diff --git a/src/lib.rs b/src/lib.rs
    index 5f4ba31..d194d60 100644
    --- a/src/lib.rs
    +++ b/src/lib.rs
    @@ -328,6 +328,11 @@ fn show_hide_cursor(
                 continue;
             }
     
    +        let finished = cursor_timer.timer.tick(time.delta()).just_finished();
    +        if !finished || cursor_timer.should_reset {
    +            continue;
    +        };
    +
             let Some(mut text) = inner_text.get_mut(entity) else {
                 continue;
             };
    @@ -335,9 +340,6 @@ fn show_hide_cursor(
             if cursor_timer.should_reset {
                 cursor_timer.timer.reset();
                 text.sections[1].style.color = style.0.color;
    -        }
    -
    -        if !cursor_timer.timer.tick(time.delta()).just_finished() {
                 continue;
             }

I think I have a slight preference for the third option.

src/lib.rs Outdated
@@ -150,6 +162,8 @@ fn keyboard(

for event in events.read() {
if !event.state.is_pressed() {
// Resume blinking the cursor when the user releases a key.
cursor_timer.should_reset = false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd rather take the hit of dealing with this in every match arm rather than potentially resetting the cursor when users press e.g. shift or other keys that don't affect the cursor position.

Copy link
Contributor Author

@chompaa chompaa Feb 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So something like this for handling when the key is up?

if !event.state.is_pressed() {
    match event.key_code {
        KeyCode::ArrowLeft
        | KeyCode::ArrowRight
        | KeyCode::Backspace
        | KeyCode::Delete
        | KeyCode::Space => {
            cursor_timer.should_reset = false;
        }
        _ => {}
    }

    if let Key::Character(_) = event.logical_key {
        cursor_timer.should_reset = false;
    }

    continue;
};

Then having the following in each match arm (besides KeyCode::Enter):

cursor_timer.should_reset = true;
continue;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes on the second part, but I think we can just reset cursor_timer.should_reset to false right after doing the reset in the other system.

@chompaa
Copy link
Contributor Author

chompaa commented Feb 18, 2024

I think I have a slight preference for the third option.

The only issue I have with the third option is that the cursor could be stuck in an invisible state if the user types whilst it is invisible.

If you desire this behavior then I'd be happy to just do that. If not, any ideas on how this could be solved?

@rparrett
Copy link
Owner

If you desire this behavior then I'd be happy to just do that.

Definitely don't want that behavior. I think I just made a mistake in the diff above though.

if !finished && !cursor_timer.should_reset {

@chompaa
Copy link
Contributor Author

chompaa commented Feb 18, 2024

Definitely don't want that behavior. I think I just made a mistake in the diff above though.

This makes a lot more sense, thanks!

As an alternative, this solution also works and avoids the unnecessary query to InnerText, but I'll just commit whichever you prefer.

// ...

if cursor_timer.is_changed() && cursor_timer.should_reset {
    cursor_timer.timer.reset();
    cursor_timer.should_reset = false;
    if let Some(mut text) = inner_text.get_mut(entity) {
        text.sections[1].style.color = style.0.color;
    }
    continue;
}

if !cursor_timer.timer.tick(time.delta()).just_finished() {
    continue;
}

@rparrett
Copy link
Owner

As an alternative, this solution also works and avoids the unnecessary query to InnerText, but I'll just commit whichever you prefer.

Your solution looks nice.

@chompaa
Copy link
Contributor Author

chompaa commented Feb 18, 2024

Your solution looks nice.

Perfect, I'll commit these changes now.

@rparrett
Copy link
Owner

Thanks for working with me through that!

Can't promise I won't change my mind and refactor into a separate system at some point, but this looks good to me.

@rparrett rparrett merged commit a2fbbca into rparrett:main Feb 18, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants