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

egui eframe TextEdit on mobile #2231

Closed
Philipp-Sc opened this issue Nov 3, 2022 · 2 comments · Fixed by #4561
Closed

egui eframe TextEdit on mobile #2231

Philipp-Sc opened this issue Nov 3, 2022 · 2 comments · Fixed by #4561
Labels
bug Something is broken

Comments

@Philipp-Sc
Copy link

Philipp-Sc commented Nov 3, 2022

Hi, thank you for creating this awesome repository. It's a lot of fun!

Is there a way to disable the following behavior?

On Mobile when the TextEdit gets focus (via touch, or click on the TextEdit area), the keyboard pops up AND the position of the Canvas is pushed up 30 to 45% leaving a gray space between the canvas and keyboard.

Am I missing something?

This can be reproduced by using this code here, a live version can be found here. (live version now contains a workaround)

Relevant code:

eframe/src/web/text_agent.rs

            // Move up canvas so that text edit is shown at ~30% of screen height.
            // Only on touch screens, when keyboard popups.
            if let Some(latest_touch_pos) = runner.input.latest_touch_pos {
                let window_height = window.inner_height().ok()?.as_f64()? as f32;
                let current_rel = latest_touch_pos.y / window_height;

                // estimated amount of screen covered by keyboard
                let keyboard_fraction = 0.5;

                if current_rel > keyboard_fraction {
                    // below the keyboard

                    let target_rel = 0.3;

                    // Note: `delta` is negative, since we are moving the canvas UP
                    let delta = target_rel - current_rel;

                    let delta = delta.max(-keyboard_fraction); // Don't move it crazy much

                    let new_pos_percent = format!("{}%", (delta * 100.0).round());

                    canvas_style.set_property("position", "absolute").ok()?;
                    canvas_style.set_property("top", &new_pos_percent).ok()?;
                }

I am using the latest egui/eframe release: 0.19.0

Thank you in advance for investigating this.
Philipp-Sc

@Philipp-Sc Philipp-Sc added the bug Something is broken label Nov 3, 2022
@Philipp-Sc
Copy link
Author

I updated my code to use the latest code from GitHub. The issue persists.

@Philipp-Sc
Copy link
Author

Philipp-Sc commented Nov 4, 2022

I added a workaround, to debug this issue, it can be reproduced using the following rev:
https://github.com/Philipp-Sc/librelearning/tree/0e7ad454f18fcf200b4a446a6b2246f6c409da56

The workaround consists of adding a CSS class with "top: 0% !important" to the canvas that enforces the position.
That fixed the problem for me.

The function to adjust the canvas position still fires, but has no effect, optimally in a future release it can be removed or made configurable (turn on/off).

samcarey pushed a commit to solve-social/areyougoing that referenced this issue Dec 24, 2022
@jprochazk jprochazk mentioned this issue May 28, 2024
5 tasks
hacknus pushed a commit to hacknus/egui that referenced this issue Oct 30, 2024
- Closes emilk#4060 - no longer aligned
to top
- Closes emilk#4479 - `canvas.style` is
not set anywhere anymore
- Closes emilk#2231 - same as emilk#4060
- Closes emilk#3618 - there is now one
`<input>` per `eframe` app, and it's removed transitively by
`WebRunner::destroy -> AppRunner::drop -> TextAgent::drop`

This PR improves the text agent to make fewer assumptions about how
`egui` is embedded into the page:
- Text agent no longer sets the canvas position
- There is now a text agent for each instance of `WebRunner`
- The input element is now moved to the correct position, so the OS can
display the IME window in the correct place. Before it would typically
be outside of the viewport

The best way to test this is to build & server the web demo locally:
```
scripts/build_demo_web.sh && scripts/start_server.sh
```

Then open the EasyMark editor, and try using IME to input some emojis:
http://localhost:8888/#EasyMarkEditor

To open the emoji keyboard use:
- <kbd>win + .</kbd> on Windows
- <kbd>ctrl + cmd + space</kbd> on Mac

Tested on:
- [x] Windows
- [x] Linux
- [x] MacOS
- [x] Android
- [x] iOS

## Migration guide

The canvas no longer controls its own size/position on the page. This
means that those properties can now be controlled entirely via HTML and
CSS, and multiple separate `eframe` apps can coexist better on a single
page.

To match the old behavior, set the `canvas` width and height to 100% of
the `body` element:

```html
<html>
  <body>
    <canvas></canvas>
  </body>
</html>
```

```css
/* remove default margins and use full viewport */
html, body {
  margin: 0;
  width: 100%;
  height: 100%;
}

canvas {
  /* match parent element size */
  width: 100%;
  height: 100%;
}
```

Note that there is no need to set `position: absolute`/`left: 50%;
transform: translateX(-50%)`/etc., and setting those properties may
poorly affect the sharpness of `egui`-rendered text.

Because `eframe` no longer updates the canvas style in any way, it also
means that on mobile, the canvas no longer collapses upwards to make
space for a mobile keyboard. This should be solved in other ways:
emilk#4572
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant