1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-28 07:23:13 -04:00
Commit Graph

105 Commits

Author SHA1 Message Date
Emil Ernerfeldt
2fb0764a24 Fix section_index 2025-03-28 16:08:46 +01:00
Emil Ernerfeldt
b060fd25b0 Remove unwraps 2025-03-28 16:08:35 +01:00
Emil Ernerfeldt
46ef7a1159 code cleanup 2025-03-28 15:55:23 +01:00
Emil Ernerfeldt
47f916ca80 Fix wrong mesh_bounds 2025-03-28 15:34:00 +01:00
Emil Ernerfeldt
947945f0ba Remove done TODO 2025-03-28 15:13:57 +01:00
Emil Ernerfeldt
79d114dfb0 Use similar-asserts for better test output 2025-03-28 15:11:30 +01:00
Emil Ernerfeldt
2ac4c93cc3 Move Galley::concat 2025-03-28 12:54:53 +01:00
Emil Ernerfeldt
29d47c1641 Clean up code slightly 2025-03-28 12:52:07 +01:00
Emil Ernerfeldt
6f4732b068 Simplify the code slightly 2025-03-28 12:38:55 +01:00
Emil Ernerfeldt
3e89613dc1 Add messages to asserts 2025-03-28 12:29:12 +01:00
Emil Ernerfeldt
8a45db62f1 Create should_cache_each_paragraph_individually and heed max_rows 2025-03-28 11:33:56 +01:00
Hubert Głuchowski
085d9b56e9 Merge branch 'master' into cache_galley_lines 2025-03-26 19:08:40 +01:00
Nicolas
58b2ac88c0 Add assert messages and print bad argument values in asserts (#5216)
Enabled the `missing_assert_message` lint

* [x] I have followed the instructions in the PR template

---------

Co-authored-by: Lucas Meurer <lucasmeurer96@gmail.com>
2025-03-25 09:20:29 +01:00
GiGaGon
668abc2838 Add expand_bg to customize size of text background (#5365)
This removes the `expand(1.0)` on text background colors, since it makes
translucent background colors have bad looking bleeding.

There is probably a smarter solution than disabling the highlighting
entirely, but I don't see a way to do that while keeping the area
consumed consistent between translucent/solid colors, or adding a decent
step up in complexity.

Since this makes it impossible to tell if selected text is highlighted,
this also adds a blanket `0.5` gamma multiply to the text selection
background color. If that is undesirable because it's a bad arbitrary
number choice, or if it's too much of an unexpected change and just the
default values should be changed, please let me know.

These changes cause the tests that use screenshots with highlighted text
to fail, though I am not sure how to update those tests to match the
changes.

<details>
<summary>Comparison Images</summary>

Current:


![image](https://github.com/user-attachments/assets/6dc85492-4f8e-4e7a-84b4-3ee10a48b8b3)

After changes:


![image](https://github.com/user-attachments/assets/9b35bbd3-159d-42a9-b22f-80febb707cfa)
 

</details>

<details>
<summary>Code used to make comparison images</summary>

```rs
fn color_text_format(ui: &Ui, color: Color32) -> TextFormat {
    TextFormat { font_id: FontId::monospace(ui.text_style_height(&egui::TextStyle::Monospace)), background: color, ..Default::default() }
}

fn color_sequence_galley(ui: &Ui, text: &str, colors: [Color32; 3]) -> Arc<Galley> {
    let mut layout_job = LayoutJob::default();
    for color in colors {
        layout_job.append(text, 0.0, color_text_format(ui, color));
    }
    ui.fonts(|f| f.layout_job(layout_job))
}

fn color_sequence_row(ui: &mut Ui, label_text: &str, text: &str, colors: [Color32; 3]) {
    ui.label(label_text);
    ui.label(color_sequence_galley(ui, text, colors));
    ui.end_row();
}

egui::Grid::new("comparison display").show(ui, |ui| {
    ui.ctx().set_pixels_per_point(2.0);
    let transparent = Color32::TRANSPARENT;
    let solid = Color32::RED;
    let solid_2 = Color32::GREEN;
    let translucent_1 = Color32::GRAY.gamma_multiply(0.5);
    let translucent_2 = Color32::GREEN.gamma_multiply(0.5);
    color_sequence_row(ui, "Transparent to Solid:", " ", [transparent, solid, transparent]);
    color_sequence_row(ui, "Translucent to Transparent:", " ", [transparent, translucent_1, transparent]);
    color_sequence_row(ui, "Solid to Transparent:", " ", [solid, solid_2, solid]);
    color_sequence_row(ui, "Solid to Solid:", " ", [solid, transparent, solid]);
    color_sequence_row(ui, "Solid to Translucent:", " ", [solid, translucent_1, solid]);
    color_sequence_row(ui, "Translucent to Translucent:", " ", [translucent_1, translucent_2, translucent_1]);
    
    color_sequence_row(ui, "Transparent to Solid:", "a", [transparent, solid, transparent]);
    color_sequence_row(ui, "Translucent to Transparent:", "a", [transparent, translucent_1, transparent]);
    color_sequence_row(ui, "Solid to Transparent:", "a", [solid, solid_2, solid]);
    color_sequence_row(ui, "Solid to Solid:", "a", [solid, transparent, solid]);
    color_sequence_row(ui, "Solid to Translucent:", "a", [solid, translucent_1, solid]);
    color_sequence_row(ui, "Translucent to Translucent:", "a", [translucent_1, translucent_2, translucent_1]);
})
```
</details>

* [x] I have followed the instructions in the PR template

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2025-03-21 13:45:34 +01:00
StratusFearMe21
52732b23a6 impl AsRef<[u8]> for FontData (#5757)
* [x] I have followed the instructions in the PR template

This PR implements `AsRef<[u8]>` for `FontData`, allowing it to be
passed into `fontdb`'s
[`Source`](https://docs.rs/fontdb/0.16.2/fontdb/enum.Source.html) type.
This would allow `egui` and `cosmic_text` to share font data with
eachother
2025-03-20 11:03:17 +01:00
valadaptive
267485976b Simplify the text cursor API (#5785)
<!--
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 test and 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 N/A, but this is part of
https://github.com/emilk/egui/issues/3378
* [x] I have followed the instructions in the PR template

Other text layout libraries in Rust--namely, Parley and Cosmic
Text--have one canonical text cursor type (Parley's is a byte index,
Cosmic Text's also stores the line index). To prepare for migrating egui
to one of those libraries, it should also have only one text cursor
type. I also think simplifying the API is a good idea in and of
itself--having three different cursor types that you have to convert
between (and a `Cursor` struct which contains all three at once) is
confusing.

After a bit of experimentation, I found that the best cursor type to
coalesce around is `CCursor`. In the few places where we need a
paragraph index or row/column position, we can calculate them as
necessary.

I've removed `CursorRange` and `PCursorRange` (the latter appears to
have never been used), merging the functionality with `CCursorRange`. To
preserve the cursor position when navigating row-by-row, `CCursorRange`
now stores the previous horizontal position of the cursor.

I've also removed `PCursor`, and renamed `RowCursor` to `LayoutCursor`
(since it includes not only the row but the column). I have not renamed
either `CCursorRange` or `CCursor` as those names are used in a lot of
places, and I don't want to clutter this PR with a bunch of renames.
I'll leave it for a later PR.

Finally, I've removed the deprecated methods from `TextEditState`--it
made the refactoring easier, and it should be pretty easy to migrate to
the equivalent `TextCursorState` methods.

I'm not sure how many breaking changes people will actually encounter. A
lot of these APIs were technically public, but I don't think many were
useful. The `TextBuffer` trait now takes `&CCursorRange` instead of
`&CursorRange` in a couple of methods, and I renamed
`CCursorRange::sorted` to `CCursorRange::sorted_cursors` to match
`CursorRange`.

I did encounter a couple of apparent minor bugs when testing out text
cursor behavior, but I checked them against the current version of egui
and they're all pre-existing.
2025-03-20 10:49:38 +01:00
Emil Ernerfeldt
164f56f554 Fix some clippy issues found by 1.84.0 (#5603) 2025-01-13 08:29:13 +01:00
Hubert Głuchowski
b36311a98a Fix Glyph::pos doc comment 2025-01-04 19:45:57 +01:00
Emil Ernerfeldt
77c9fd85bd cleanup 2025-01-02 02:29:19 +01:00
Emil Ernerfeldt
9fa294f21f Split out long function 2025-01-02 02:20:29 +01:00
Emil Ernerfeldt
d2f75e974c Merge branch 'master' into cache_galley_lines 2024-12-28 12:55:05 +01:00
Emil Ernerfeldt
dfcc679d5a Round widget coordinates to even multiple of 1/32 (#5517)
* Closes https://github.com/emilk/egui/pull/5197
* Closes https://github.com/emilk/egui/issues/5163

This should help prevent rounding errors in layout code.

@lucasmerlin you may wanna test this with `egui_flex`
2024-12-26 20:54:24 +01:00
Hubert Głuchowski
c094ee8a15 Merge branch 'emilk:master' into cache_galley_lines 2024-12-07 18:44:48 +01:00
Hubert Głuchowski
17a5f1f75e Fix incorrect behavior with LayoutJob::max_rows 2024-12-05 19:15:23 +01:00
Hubert Głuchowski
40f237d43c Add comment and check for newline before multiline layout 2024-12-05 18:45:36 +01:00
Hubert Głuchowski
25da82279f Fix text horizontal alignment 2024-12-05 18:27:38 +01:00
Hubert Głuchowski
c6592ec898 Fix nits 2024-12-04 19:30:13 +01:00
Emil Ernerfeldt
328422dc62 Update MSRV to Rust 1.79 (#5421)
Mostly to fix `cargo-machete` CI
2024-12-01 18:58:35 +01:00
Fishhh
139f28640d Replace a hack with a proper implementation 2024-11-30 19:29:40 +01:00
Fishhh
110a9c39e3 Move some PlacedRow methods back to Row 2024-11-30 18:27:13 +01:00
Fishhh
bbe566256e Fix Row::ends_with_newline docs, explain skipping of last galley row 2024-11-30 18:17:38 +01:00
Fishhh
fd8413c62a Simplify layout_multiline loop loop into a while loop 2024-11-30 18:12:24 +01:00
Fishhh
1be24ba470 Respect LayoutJob::round_output_size_to_nearest_ui_point 2024-11-30 17:51:07 +01:00
Fishhh
66c83c31ce Move ends_with_newline back into Row 2024-11-30 17:51:07 +01:00
Fishhh
6147ff3668 Round PlacedRow positions to pixels during multiline layout 2024-11-30 16:59:38 +01:00
Fishhh
6d6bc3befb Respect first_row_min_height during multiline Galley layout 2024-11-30 16:47:18 +01:00
Fishhh
abbc561ae1 Correctly handle empty lines 2024-11-30 15:59:50 +01:00
Hubert Głuchowski
bc86bec1cb Properly handle row repositioning 2024-11-29 15:57:47 +01:00
Hubert Głuchowski
f028154da8 Move cached-multiline-layout code into a helper function 2024-11-29 13:02:49 +01:00
Hubert Głuchowski
3de1723659 Don't add leading space to more than one split layout section 2024-11-29 11:55:15 +01:00
Hubert Głuchowski
4e3f162801 Fix lints 2024-11-29 11:50:24 +01:00
Hubert Głuchowski
db32a1ed44 Make Galleys share Rows and store their offsets 2024-11-28 18:54:54 +01:00
Hubert Głuchowski
150c0f662b Cache individual lines of text in GalleyCache 2024-11-28 18:53:45 +01:00
Hubert Głuchowski
622b848c00 Fix typo 2024-11-28 16:47:05 +01:00
StarStarJ
3f5cd74de7 Put font data into Arc to reduce memory consumption (#5276)
egui never accesses the `FontDefinitions`' member fields mutably, except
in `fonts_tweak_ui` where it cloned the `FontDefinitions` object anyway.

This patch reduces system memory consumption for shared font
definitions.
And also removes some overhead from copying (e.g. for the per
`pixel_per_points` font atlas)

Also it allows to keep a copy of the font definitions outside of egui.

In my App that uses international fonts:
Before:

![image](https://github.com/user-attachments/assets/f8dfb4f4-a21c-447c-8cf9-83025ad6e960)

New:

![image](https://github.com/user-attachments/assets/9f297fbd-e620-4b7e-a32a-65073ee805ed)


Note: If `Arc` is not wanted, then it could ofc be abstracted away.

I know this is quite a breaking change API wise, but would like to hear
your opinion.
2024-11-01 13:30:02 +01:00
rustbasic
288c74e332 Expand max font atlas size from 8k to 16k (#5257)
When using fonts with an average of 50,000 characters,
'epaint texture atlas overflowed!' may be printed and cause problems.
It is necessary to expand the max value related to texture.

* Closes #5256

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2024-10-29 10:42:28 +01:00
frederik-uni
04ab5e7574 Context::add_font (#5228)
make it easier to add fonts. 

For example if I want to add a custom FontFamily or if the user wants to
add a Chinese fallback
* [x] I have followed the instructions in the PR template

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2024-10-23 14:29:44 +02:00
Emil Ernerfeldt
92adfa57dc Improve comment in text layout code 2024-09-26 09:43:27 +02:00
Emil Ernerfeldt
f97f85089d Prevent text shrinking in tooltips; round wrap-width to integer (#5161)
* Closes https://github.com/emilk/egui/pull/5106
* Closes https://github.com/emilk/egui/issues/5084


Protect against rounding errors in egui layout code.

Say the user asks to wrap at width 200.0.
The text layout wraps, and reports that the final width was 196.0
points.
This than trickles up the `Ui` chain and gets stored as the width for a
tooltip (say).
On the next frame, this is then set as the max width for the tooltip,
and we end up calling the text layout code again, this time with a wrap
width of 196.0.
Except, somewhere in the `Ui` chain with added margins etc, a rounding
error was introduced,
so that we actually set a wrap-width of 195.9997 instead.
Now the text that fit perfectly at 196.0 needs to wrap one word earlier,
and so the text re-wraps and reports a new width of 185.0 points.
And then the cycle continues.

So this PR limits the text wrap-width to be an integer.

Related issues:
* https://github.com/emilk/egui/issues/4927
* https://github.com/emilk/egui/issues/4928
* https://github.com/emilk/egui/issues/5163

--- 

Pleas test this @rustbasic
2024-09-25 11:31:41 +02:00
rustbasic
7c7190f98d Clamp font size to between 0.1 and 2048 (#5139)
Fix: Font size limit to prevent panic
2024-09-22 19:14:03 +02:00