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

Difficult to create an Into<WidgetText> that looks like a composite of RichTexts #3318

Closed
OmegaJak opened this issue Sep 7, 2023 · 1 comment · Fixed by #3319
Closed

Comments

@OmegaJak
Copy link
Contributor

OmegaJak commented Sep 7, 2023

The Problem
I'm looking to using text with mixed formatting as the text of a CollapsingHeader, whose parameter is an Into<WidgetText>. In my case, I am trying to get ordinary body text, with one word in the middle bolded (like this: "Using: Chrome (started using 2 minutes ago)"). As far as I can tell, this currently requires the use of a LayoutJob, whose API, while allowing a great deal of control, is not very ergonomic and takes some work to make it visually match a RichText.

Example
The best way I know how to achieve this currently looks something like this:

let layout_job = LayoutJob::default();
let font_id = ui
    .style()
    .text_styles
    .get(&egui::TextStyle::Body)
    .unwrap()
    .clone();
layout_job.append(
    "Using: ",
    0.0,
    TextFormat {
        font_id: font_id.clone(),
        color: ui.style().visuals.text_color(),
        ..Default::default()
    },
);
layout_job.append(
    &format!("{} ", self.current_window.app_name),
    0.0,
    TextFormat {
        font_id: font_id.clone(),
        color: ui.style().visuals.strong_text_color(),
        ..Default::default()
    },
);
layout_job.append(
    &format!("(started using {})", time_using_current),
    0.0,
    TextFormat {
        font_id: font_id.clone(),
        color: ui.style().visuals.text_color(),
        ..Default::default()
    },
);

I could reduce a little duplication by pulling the 'normal text' TextFormat into a variable, but this is still a lot of code just to get one part of the text bolded. Manipulating TextFormat directly is much more difficult than using a RichText.

My Dream
I would love if there were an easy way to make something implementing Into<WidgetText> from multiple RichTexts. The RichText api is very ergonomic, and this would ensure visual consistency across the application.

I will be raising a PR shortly that takes a small step towards this, but I'm not sure if it's the best solution.

@hailiangyuan
Copy link

good idea

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 a pull request may close this issue.

2 participants