Commit Graph

3259 Commits

Author SHA1 Message Date
Sven Niederberger 1f008fb730
ScrollArea: Prevent drag interaction outside the area (#4611)
<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

* Closes https://github.com/emilk/egui/issues/4349
2024-06-06 15:36:20 +02:00
Emil Ernerfeldt 29b12e1760
Easing functions (#4630)
This adds most of the "standard" easing functions from
https://easings.net/ to `emath::easing`, and adds helpers in `egui` for
using them.

In particular there is now `ctx.animate_bool_with_easing` and
`ctx.animate_bool_responsive`, that uses a cubic easing function.

All animations in egui now uses cubic ease-out, for a more responsive
feeling (fast at the start, slower towards the end).
2024-06-06 13:09:52 +02:00
Jan Procházka 3d632cd333
Move first `request_animation_frame` into resize observer (#4628)
This PR ensures the first animation frame happens _after_ the resize
observer has had a chance to do its job.

* Closes https://github.com/emilk/egui/issues/4622

The first commit contains some `log` calls to observe the changed
behavior:

Before:
```
[eframe::web::app_runner] eframe/src/web/app_runner.rs:191: LOGIC [300.0 150.0]
[eframe::web::events] eframe/src/web/events.rs:633: ResizeObserver canvas=[300.0 150.0] to=(1920, 993)
[eframe::web::app_runner] eframe/src/web/app_runner.rs:191: LOGIC [1920.0 993.0]
[eframe::web::app_runner] eframe/src/web/app_runner.rs:191: LOGIC [1920.0 993.0]
```

After:
```
[eframe::web::events] eframe/src/web/events.rs:633: ResizeObserver canvas=[300.0 150.0] to=(1920, 993)
[eframe::web::app_runner] eframe/src/web/app_runner.rs:191: LOGIC [1920.0 993.0]
[eframe::web::app_runner] eframe/src/web/app_runner.rs:191: LOGIC [1920.0 993.0]
```
2024-06-06 10:30:32 +02:00
Emil Ernerfeldt cbb5d6aa93
Handle tooltips so large that they cover the widget (#4623)
Previously a tooltip that overlapped the original widget would case an
invisible or flickering tooltip.

* Closes https://github.com/emilk/egui/issues/4616
2024-06-05 19:08:51 +02:00
Emil Ernerfeldt bb8400853f
Add tags to `UiStack` (#4617)
You can now set custom tags on the `UiStack`. This allows you to write
code that is situationally aware at runtime. For instance, you could
decide wether or not a label should truncate its text depending on what
part of your ui it is in, without having to pass that info down via the
callstack.
2024-06-05 18:46:50 +02:00
Emil Ernerfeldt 321d2441c1
Make pinch-to-zoom more responsive on web (#4621)
It's 2x faster than before, more closely matching how pinch-to-zoom
feels natively on Mac
2024-06-05 18:32:04 +02:00
Emil Ernerfeldt 4837dc68b3
Fix pinch-to-zoom on web by using the "artificial" modifier keys (#4619)
* Introduced in https://github.com/emilk/egui/pull/4524
* Closes https://github.com/emilk/egui/issues/4615
2024-06-05 18:05:48 +02:00
Emil Ernerfeldt d72de1eab3
Deprecate `ui.set_enabled` and `set_visbile` (#4614)
These were confusing, because `set_enabled(true)` and
`set_visible(true)` did nothing.

Instead use one of:
* `ui.add_enabled`, `ui.add_enabled_ui` or `ui.disable()`
* `ui.add_visible`, `ui.add_visible_ui` or `ui.set_invisible()`

* Closes https://github.com/emilk/egui/issues/4327
2024-06-05 13:20:54 +02:00
Emil Ernerfeldt 0028764e02
Set the `sizing_pass` flag in first frame of `egui_extras::Table` (#4613)
* Closes https://github.com/emilk/egui/issues/4535

This should improve the auto-sizing of columns when nesting expanding
widgets (e.g. `Separator`), or centered or justified layouts.
2024-06-05 12:17:15 +02:00
Emil Ernerfeldt a50f253b76
`Grid`: set the `sizing_pass` flag during the initial sizing pass (#4612)
* Part of https://github.com/emilk/egui/issues/4535

This should improve the auto-sizing of columns when nesting expanding
widgets (e.g. `Separator`), or centered or justified layouts.
2024-06-05 12:17:08 +02:00
Antoine Beyeler a28792194d
Introduce `UiStack` (#4588)
* Closes #4534

This PR:
- Introduces `Ui::stack()`, which returns the `UiStack` structure
providing information on the current `Ui` hierarchy.
- **BREAKING**: `Ui::new()` now takes a `UiStackInfo` argument, which is
used to populate some of this `Ui`'s `UiStack`'s fields.
- **BREAKING**: `Ui::child_ui()` and `Ui::child_ui_with_id_source()` now
take an `Option<UiStackInfo>` argument, which is used to populate some
of the children `Ui`'s `UiStack`'s fields.
- New `Area::kind()` builder function, to set the `UiStackKind` value of
the `Area`'s `Ui`.
- Adds a (minimalistic) demo to egui demo (in the "Misc Demos" window).
- Adds a more thorough `test_ui_stack` test/playground demo.

TODO:
- [x] benchmarks
- [x] add example to demo

Future work:
- Add `UiStackKind` and related support for more container (e.g.
`CollapsingHeader`, etc.)
- Add a tag/property system that would allow adding arbitrary data to a
stack node. This data could then be queried by nested `Ui`s. Probably
needed for #3284.
- Add support to track columnar layouts.

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2024-06-04 10:12:23 +02:00
Emil Ernerfeldt c0a9800d05
Support interactive widgets in tooltips (#4596)
* Closes https://github.com/emilk/egui/issues/1010

### In short
You can now put interactive widgets, like buttons and hyperlinks, in an
tooltip using `on_hover_ui`. If you do, the tooltip will stay open as
long as the user hovers it.

There is a new demo for this in the egui demo app (egui.rs):


![interactive-tooltips](https://github.com/emilk/egui/assets/1148717/97335ba6-fa3e-40dd-9da0-1276a051dbf2)

### Design
Tooltips can now contain interactive widgets, such as buttons and links.
If they do, they will stay open when the user moves their pointer over
them.

Widgets that do not contain interactive widgets disappear as soon as you
no longer hover the underlying widget, just like before. This is so that
they won't annoy the user.

To ensure not all tooltips with text in them are considered interactive,
`selectable_labels` is `false` for tooltips contents by default. If you
want selectable text in tooltips, either change the `selectable_labels`
setting, or use `Label::selectable`.

```rs
ui.label("Hover me").on_hover_ui(|ui| {
    ui.style_mut().interaction.selectable_labels = true;
    ui.label("This text can be selected.");

    ui.add(egui::Label::new("This too.").selectable(true));
});
```

### Changes
* Layers in `Order::Tooltip` can now be interacted with
2024-06-03 11:37:06 +02:00
trevyn 7b3752fde9
Update README to maintained Tetra integration (#4606)
Original integration https://github.com/tesselode/egui-tetra is
archived, so I forked and updated for latest `egui` and `tetra`.
2024-06-03 10:21:40 +02:00
Onè 4d9aed8f83
Add missing word (#4603)
Was reading the docs and noticed a typo.
<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

* Closes <https://github.com/emilk/egui/issues/THE_RELEVANT_ISSUE>
2024-06-02 21:14:40 +02:00
Emil Ernerfeldt 78dfdb3684
`Rect::intersects_ray`: another bug fix (#4597)
Make sure it returns `true` if the ray starts inside the box
2024-05-31 17:28:38 +02:00
Emil Ernerfeldt b6805a8006
Fix bug in ray-rect intersection test (#4595)
This made submenu popups stay open when they perhaps shouldn't be
2024-05-31 17:10:46 +02:00
Emil Ernerfeldt d4ac91ab6d
Refactor: remove extra tooltip id salt (#4594) 2024-05-31 16:50:39 +02:00
Emil Ernerfeldt 86560554bc
Fix bug in context menu positioning when at bottom of the screen (#4593)
I think this broke with the sizing pass
2024-05-31 15:56:52 +02:00
rustbasic d3ea90f5ef
Fix dragging of `custom_window_frame` on Windows (#4592)
Fix: example `custom_window_frame`

Sending `ViewportCommand::StartDrag` when the secondary button is
clicked in Windows will disable drag, and drag will not be possible with
the primary button afterwards.

So in the example `custom_window_frame`, modified to use
`dragged_by(PointerButton::Primary)` instead of
`is_pointer_button_down_on()`.
2024-05-31 15:22:46 +02:00
Emil Ernerfeldt df8b9c2d75
Constrain `Area`s to screen by default (#4591)
Call `area.constrain(false)` if you want your areas to extend outside
the screen rect.
2024-05-31 10:48:06 +02:00
Emil Ernerfeldt c0479cadf3
The default constrain rect for `Area/Window` is now `ctx.screen_rect` (#4590)
By default, `Windows` can now cover already added panels. For instance:
in the egui demo app (egui.rs), windows can now cover the side-panels
and top bar.

To get the old behaviour, use
`window.constrain_to(ctx.available_rect())`.
2024-05-31 10:01:45 +02:00
sor-ca 8f5986125d
Make `egu::menu` types public (#4544)
Sometimes we need to fix the current state of the application in the
moment when you open the context menu, to save it, and to use it during
creation of context menu and response handling. Making some structs,
related with menu creating, allow us to create functions for this cases.
For example,
```rust
pub fn context_menu_custom<'a, T>(
    response: &Response,
    //variable for fixing state in the moment when you open context menu
    state: &mut T,
    //function which allow to get some king of state.
    //In this case state depends on cursor position, in other cases it may depend on system time or something else
    get_state: impl FnOnce(Pos2) -> T,
    //set contents of menu depending on state
    set_contents: impl 'a + FnOnce(&T) -> Box<dyn 'a + FnOnce(&mut Ui)>,
) -> Option<InnerResponse<()>> {
    let menu_id = Id::new("__egui::context_menu");
    let mut bar_state = BarState::load(&response.ctx, menu_id);
    let root = &mut bar_state;

    let menu_response = MenuRoot::context_interaction(response, root);
    if let egui::menu::MenuResponse::Create(p, _) = &menu_response {
        *state = get_state(*p);
    };

    let add_contents = set_contents(&state);

    MenuRoot::handle_menu_response(root, menu_response);
    let inner_response = bar_state.show(response, add_contents);

    bar_state.store(&response.ctx, menu_id);
    inner_response
}
```
The example of using such function you may see in [`my
repository`](https://github.com/sor-ca/context_menu)
It is very simple example, and is this case, the problem may be solved
without fn context_menu_custom, but in more complex situations, it may
be very useful
Related issue: https://github.com/emilk/egui/issues/4162

<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2024-05-31 09:02:02 +02:00
Emil Ernerfeldt 84d204246f
Fade in windows, tooltips, popups, etc (#4587)
All `Area`s now have a quick fade-in animation. You can turn it off with
`Area::fade_in` or `Window::fade_in` .

The `Window` fade-out animation is now nicer: it fades all elements of
the window, not just the frame.
It can be controlled with `Window::fade_out`.
2024-05-30 16:22:12 +02:00
Emil Ernerfeldt 6282844f65
Add `opacity` and `multiply_opacity` functions to `Ui` and `Painter` (#4586) 2024-05-30 16:07:42 +02:00
Emil Ernerfeldt a7eed0ae3c
Once you have waited for a tooltip to show, show the next one right away (#4585)
* Closes https://github.com/emilk/egui/issues/4582

User-story: there are multiple icons in a row, and the user wants them
explained. They hover over one until the tooltips appears. They then
move on to the next and don't want to wait for that tooltip again.
2024-05-30 13:23:01 +02:00
Emil Ernerfeldt 89968e6f96
Bug fix: ui opacity and gray-out not affecting strokes (#4581)
Bug introduced in https://github.com/emilk/egui/pull/4353
2024-05-29 22:37:55 +02:00
Emil Ernerfeldt 16277ebb86
Add `Response::show_tooltip_ui` and `show_tooltip_text` (#4580)
These functions will always show a tooltip under the widget when called,
even if the user is not hovering the widget.

This can be useful for tutorials and notification and similar.

* Closes https://github.com/emilk/egui/issues/890
2024-05-29 21:28:56 +02:00
Emil Ernerfeldt 00396145d1
Improve tooltip positioning (#4579)
This simplifies and improves the tooltip positioning

* Closes https://github.com/emilk/egui/issues/4568

### For a follow-up PR
* [ ] Test if it closes https://github.com/emilk/egui/issues/4471
* [ ] Add an API to close https://github.com/emilk/egui/issues/890
2024-05-29 21:18:08 +02:00
Emil Ernerfeldt cc3b3629b8
Round text galley sizes to nearest ui point size (#4578)
Previously, many labels had non-integer widths. This lead to rounding
errors.

This was most notable for the new `Area` sizing code:

We would run the initial sizing pass, to measure the size of e.g. a
tooltip.
Say the tooltip contains text that was 100.123 ui points wide. With a
16pt border, that becomes 116.123, which is stored in the `Area` state
as the width. The next frame, we use that stored size as the wrapping
width. With perfect precision, we would then tell the label to wrap to
100.123 pts, which the text would _just_ fit in. However, due to
rounding errors we might end up asking it to wrap to 100.12**2** pts,
meaning the last word would now wrap and end up on the next line.

By rounding label sizes to perfect integers, we avoid such rounding
errors, and most ui elements will now end up on perfect integer point
coordinates (and `f32` can precisely express and do arithmetic on all
integers < 2^24).

Visually this has very little impact. Some labels move by a pixel here
and there, mostly for the better.
2024-05-29 18:23:11 +02:00
Emil Ernerfeldt 66f40de7a1
Don't persist `Area` state (#4577)
If you change your code for what is contained in a menu, tooltips,
combobox etc, it is helpful if egui will recompute the size of that Area
next frame. Now it will!

The user-chosen size of a `Window` is still persisted via other means.
2024-05-29 15:59:51 +02:00
Emil Ernerfeldt c7cb524bdb
Make `Area` state public (#4576) 2024-05-29 15:48:52 +02:00
Emil Ernerfeldt 913cef3361
Enforce eframe errors are `Send` and `Sync` (#4574)
* Follow-up to https://github.com/emilk/egui/pull/4565
2024-05-29 13:13:00 +02:00
Jan Procházka 514ee0c433
Improve web text agent (#4561)
- Closes https://github.com/emilk/egui/issues/4060 - no longer aligned
to top
- Closes https://github.com/emilk/egui/issues/4479 - `canvas.style` is
not set anywhere anymore
- Closes https://github.com/emilk/egui/issues/2231 - same as #4060
- Closes https://github.com/emilk/egui/issues/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:
https://github.com/emilk/egui/issues/4572
2024-05-29 12:54:33 +02:00
Emil Ernerfeldt 5eee463851 Replace some `...` with `…` 2024-05-29 11:48:50 +02:00
Emil Ernerfeldt ffbc63e147
`ComboBox`: fix justified layout of popup if wider than parent button (#4570)
* Closes https://github.com/emilk/egui/issues/4452

The `ComboBox` popup has a justified layout to make selection of items
easier.

Thanks to [the new sizing pass
logic](https://github.com/emilk/egui/issues/4535) we don't have to know
the final width in advance:


![image](https://github.com/emilk/egui/assets/1148717/53b0dda7-14c9-43be-a073-ad49865e69a6)
2024-05-29 11:47:10 +02:00
Emil Ernerfeldt a768d74411
Add `Ui::is_sizing_pass` for better size estimation of `Area`s, and menus in particular (#4557)
* Part of https://github.com/emilk/egui/issues/4535
* Closes https://github.com/emilk/egui/issues/3974

This adds a special `sizing_pass` mode to `Ui`, in which we have no
centered or justified layouts, and everything is hidden. This is used by
`Area` to use the first frame to measure the size of its contents so
that it can then set the perfectly correct size the subsequent frames.

For menus, where buttons are justified (span the full width), this
finally the problem of auto-sizing. Before you would have to pick a
width manually, and all buttons would expand to that width. If it was
too wide, it looked weird. If it was too narrow, text would wrap. Now
all menus are exactly the width they need to be. By default menus will
wrap at `Spacing::menu_width`.

This affects all situations when you have something that should be as
small as possible, but still span the full width/height of the parent.
For instance: the `egui::Separator` widget now checks the
`ui.is_sizing_pass` flag before deciding on a size. In the sizing pass a
horizontal separator is always 0 wide, and only in subsequent passes
will it span the full width.
2024-05-29 10:27:04 +02:00
Emil Ernerfeldt 942fe4ab31
Support returning errors when creating the app (#4565)
The closure passed to `eframe::run_native` now returns a `Result`,
allowing you to return an error during app creation, which will be
returned to the caller of `run_native`.

This means you need to wrap your `Box::new(MyApp::new(…))` in an
`Ok(…)`.

* Closes https://github.com/emilk/egui/issues/4474
2024-05-28 21:59:19 +02:00
Emil Ernerfeldt 54429e0549
Revert update to wgpu 0.20 => downgrade to wgpu 0.19.1 (#4559)
0.20 has a bunch of bugs that will be fixed by:

* https://github.com/gfx-rs/wgpu/pull/5681

At Rerun, we don't want to wait for the wgpu 0.20.1 patch release before
we update egui, so we will temporarily downgrade to wgpu 0.19

After reverting I'll open a new PR that will update to 0.20 again, with
the intention of merging that once 0.20.1 is released.
2024-05-28 14:40:43 +02:00
Emil Ernerfeldt 1888d19b4a
Better spacing and sizes for (menu) buttons (#4558) 2024-05-28 14:19:25 +02:00
Antoine Beyeler 26206526d6
Hide toolip when opening `ComboBox` drop-down (#4546)
- Fixes #4338


https://github.com/emilk/egui/assets/49431240/73ea87a1-41ad-40b1-b451-d6be2b38c7e0



Tested using `example/hello_world` modified to:
```rust
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![allow(rustdoc::missing_crate_level_docs)] // it's an example

use eframe::egui;

fn main() -> Result<(), eframe::Error> {
    env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
    let options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
        ..Default::default()
    };
    eframe::run_native(
        "My egui App",
        options,
        Box::new(|cc| {
            // This gives us image support:
            egui_extras::install_image_loaders(&cc.egui_ctx);

            Box::<MyApp>::default()
        }),
    )
}

struct MyApp {
    name: String,
    age: u32,
}

impl Default for MyApp {
    fn default() -> Self {
        Self {
            name: "Arthur".to_owned(),
            age: 42,
        }
    }
}

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            ui.heading("My egui Application");
            egui::ComboBox::new("combo", "combo box")
                .selected_text(&self.name)
                .show_ui(ui, |ui| {
                    ui.selectable_value(&mut self.name, "Arthur".into(), "Arthur")
                        .on_hover_text("This is Arthur");
                    ui.selectable_value(&mut self.name, "Ford".into(), "Ford")
                        .on_hover_text("This is Ford");
                    ui.selectable_value(&mut self.name, "Trillian".into(), "Trillian")
                        .on_hover_text("This is Trillian");
                })
                .response
                .on_hover_text("This is a combo box");
        });
    }
}
```

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2024-05-28 14:17:37 +02:00
Antoine Beyeler bcd91f27a1
Add support for text truncation to `egui::Style` (#4556)
* Closes #4473

This PR introduce `Style::wrap_mode`, which adds support for text
truncation in addition to text wrapping. This PR also update some width
calculation of the ComboBox.

#### Core

- Add `egui::TextWrapMode` (pure enum with `Extend`, `Wrap`, `Truncate`)
- Add `Style::wrap_mode: Option<tTextWrapMode>`
- **DEPRECATED**: `Style::wrap`, use `Style::wrap_mode` instead.
- Add `Ui::wrap_mode()` to return the wrap mode to use in the current
ui. If specified in `Style`, return it. Otherwise, return
`TextWrapMode::Wrap` for vertical layout and wrapping horizontal layout,
and `TextWrapMode::Extend` otherwise.
- **DEPRECATED**: `Ui::wrap_text()`, use `Ui::wrap_mode` instead.

#### Widget

- Update the width calculation of the `ComboBox` button (_not_ its popup
menu).
- Now, `ComboBox::width()` (defaulting to `Spacing::combo_width`) is
always considered a minimum width and will extend the `Ui`, regardless
of the selected text width and wrap mode.
- Introduce `ComboBox::wrap_mode`, which overrides `Ui::wrap_mode` for
the selected text layout.
- Note: since `ComboBox` uses `ui.horizontal` internally, the default
wrap mode is always `TextWrapMode::Extend`, regardless of the caller's
`Ui`'s layout.
- The `ComboBox` button no longer extend to `ui.available_width()` with
wrapping is enabled.
- **BREAKING**: `ComboBox::wrap()` no longer has a `bool` argument and
is now a short-hand for `ComboBox::wrap_mode(TextWrapMode::Wrap)`.
- Added `ComboBox::truncate()` as short-hand for
`ComboBox::wrap_mode(TextWrapMode::Truncate)`.
- Update `Label`
  - Add `Label::wrap_mode()` to specify the text wrap mode.
- **BREAKING**: `Label::wrap()` no longer has a `bool` argument and is
now a short-hand for `Label::wrap_mode(TextWrapMode::Wrap)`.
- **BREAKING**: `Label::truncate()` no longer has a `bool` argument and
is now a short-hand for `Label::wrap_mode(TextWrapMode::Truncate)`.
- Update `Button`
  - Add `Button::wrap_mode()` to specify the text wrap mode.
- **BREAKING**: `Button::wrap()` no longer has a `bool` argument and is
now a short-hand for `Button::wrap_mode(TextWrapMode::Wrap)`.
- Added `Button::truncate()` as short-hand for
`Button::wrap_mode(TextWrapMode::Truncate)`.

#### Low-level

- **BREAKING**: `WidgetText::into_galley()` now takes an
`Option<TextWrapMode>` instead of a `Option<bool>` argument.
- **BREAKING**: `WidgetText::into_galley_impl(()` now takes a
`TextWrapping` argument instead of `wrap: bool` and `availalbe_width:
f32` arguments.

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2024-05-28 13:10:41 +02:00
lucasmerlin 4b59c6d414
Fix `Ui::scroll_with_delta` only scrolling if the `ScrollArea` is focused (#4303)
<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to add commits to your PR.
* Remember to run `cargo fmt` and `cargo cranky`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

This introduces the boolean field force_current_scroll_area to
InputState which will be set when scroll_with_delta is called, causing
the ScrollArea to skip the check whether it is focused and always
consume the smooth scroll delta.

* Closes #2783 
* Related to #4295

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2024-05-28 09:21:35 +02:00
Varphone Wong 6f59a14c4d
Add `Options::reduce_texture_memory` to free up RAM (#4431)
## Summary

This PR introduces a new configuration option `reduce_texture_memory` in
`egui`.

## Changes

- Added `reduce_texture_memory` option in `egui`. When set to `true`,
`egui` will discard the loaded image data after the texture is uploaded
to the GPU, reducing memory usage. This is beneficial when handling a
large number of images and retaining the image data is unnecessary,
potentially saving substantial memory. However, this makes it impossible
to serialize the loaded images or render on non-GPU systems. Default is
`false`.

## Impact

This new configuration option gives users more control over their memory
usage, especially when dealing with a large number or large resolution
of images. It allows users to optimize their applications based on their
specific needs and constraints.
2024-05-28 09:13:43 +02:00
Jan Procházka d131b2b580
Fix: Don't `.forget()` RAF closure (#4551)
The closure is now stored in `WebRunner` and dropped in the next
`request_animation_frame` instead of being "leaked" via `forget`

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2024-05-27 21:55:23 +02:00
Jan Procházka 759c8fd2c9
Use ResizeObserver instead of `resize` event (#4536)
Currently, if the size of the canvas element changes independently of
the size of the browser window (e.g. due to its parent element
shrinking), then no repaints are scheduled.

This PR replaces the `resize` event with a `ResizeObserver`, which
ensures that _any_ resize of the canvas element (including those caused
by browser window resizes) trigger a repaint. The repaint is done
synchronously as part of the resize event, to reduce any potential
flickering.

The result seems to pass the rendering tests on most platform+browser
combinations. We tested:
- Chrome, Firefox, Safari on macOS
- Chrome, Firefox on Linux (ubuntu and arch, both running wayland)
- Chrome, Firefox on Windows

Firefox still has some antialiasing issues on Linux platforms, but this
antialiasing also happens on `master`, so this PR is not a regression
there.

The code setting `canvas.style.width` and `canvas.style.height` at the
start of `AppRunner::logic` was also removed - the canvas _display_ size
is now fully controlled by CSS, e.g. by setting `canvas { width: 100%;
height: 100%; }`.

The approach used here is described in
https://webglfundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html

Note: The only remaining place where egui updates the style of the
canvas it is rendering to is some of the IME/mobile input handling code.
Fixing that is out of scope for this PR, and will be done in a followup
PR.
2024-05-27 21:41:28 +02:00
Emil Ernerfeldt a8b50e6aa1
Move test crates to own folder (#4554) 2024-05-27 21:28:33 +02:00
YgorSouza 7a17a6d6ad
Plot now respects the `interact_radius` set in the UI's style (#4520)
* Closes #4519
2024-05-27 19:23:15 +02:00
lucasmerlin 8553e738e0
eframe: Add `NativeOptions::persistence_path` (#4423)
This allows customizing the persistence path in NativeOptions.
Previously, persistence wouldn't work with android because
directories-next doesn't support android so eframe would just fail to
find a place where it could store its config.

* Closes #4098 (android users can now specify a path that works with
android, by e.g. using app_dirs2, which supports android)
2024-05-27 18:57:39 +02:00
Antoine Beyeler 192a111272
Hide all other series when alt-clicking in the legend (#4549)
https://github.com/emilk/egui/assets/49431240/75d32f53-4c1c-4713-b25e-e1787e465a48

* Closes #4548
2024-05-27 18:56:16 +02:00
zaaarf ff7a3832b6
TextEdit hint text styling (#4517)
Simply adds a `hint_text_font` (any suggestions for a better name?
hint_text_style seemed misleading as it doesn't only accept `TextStyle`)
method to the TextEdit builder that allows to set the styling
(specifically, a `FontSelection`) for the hint text.

My personal use-case for this was having body-sized hint text in a
heading-sized TextEdit, but I'm sure there's more out there.

The PR shouldn't break compatibility, as it's stored as an `Option` that
defaults to the `font_id` (which was the only behaviour prior to this)
when empty.

It looked too trivial to add something to the actual demo, but I have it
locally, so let me know if I should commit that.

Ran the check script locally, had no complaints.
2024-05-27 17:28:03 +02:00