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 expand_bg to text layouting #5365

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add expand_bg and change select defaults
MeGaGiGaGon committed Nov 12, 2024
commit 67d78fa66cd1ad11a865a947520869f4dffb37f0
4 changes: 2 additions & 2 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
@@ -1387,14 +1387,14 @@ impl Default for Visuals {
impl Selection {
fn dark() -> Self {
Self {
bg_fill: Color32::from_rgb(0, 92, 128),
bg_fill: Color32::from_rgb(0, 92, 128).gamma_multiply(0.75),
stroke: Stroke::new(1.0, Color32::from_rgb(192, 222, 255)),
}
}

fn light() -> Self {
Self {
bg_fill: Color32::from_rgb(144, 209, 255),
bg_fill: Color32::from_rgb(144, 209, 255).gamma_multiply(0.75),
stroke: Stroke::new(1.0, Color32::from_rgb(0, 83, 125)),
}
}
2 changes: 1 addition & 1 deletion crates/egui/src/text_selection/visuals.rs
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ pub fn paint_text_selection(

// Start by appending the selection rectangle to end of the mesh, as two triangles (= 6 indices):
let num_indices_before = mesh.indices.len();
mesh.add_colored_rect(rect, color.gamma_multiply(0.5));
mesh.add_colored_rect(rect, color);
assert_eq!(num_indices_before + 6, mesh.indices.len());

// Copy out the new triangles:
3 changes: 3 additions & 0 deletions crates/egui/src/widget_text.rs
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ pub struct RichText {
family: Option<FontFamily>,
text_style: Option<TextStyle>,
background_color: Color32,
expand_bg: f32,
text_color: Option<Color32>,
code: bool,
strong: bool,
@@ -360,6 +361,7 @@ impl RichText {
family,
text_style,
background_color,
expand_bg,
text_color: _, // already used by `get_text_color`
code,
strong: _, // already used by `get_text_color`
@@ -425,6 +427,7 @@ impl RichText {
underline,
strikethrough,
valign,
expand_bg,
},
)
}
12 changes: 7 additions & 5 deletions crates/epaint/src/text/text_layout.rs
Original file line number Diff line number Diff line change
@@ -753,9 +753,10 @@ fn add_row_backgrounds(job: &LayoutJob, row: &Row, mesh: &mut Mesh) {
return;
}

let mut end_run = |start: Option<(Color32, Rect)>, stop_x: f32| {
if let Some((color, start_rect)) = start {
let mut end_run = |start: Option<(Color32, Rect, f32)>, stop_x: f32| {
if let Some((color, start_rect, expand)) = start {
let rect = Rect::from_min_max(start_rect.left_top(), pos2(stop_x, start_rect.bottom()));
let rect = rect.expand(expand);
mesh.add_colored_rect(rect, color);
}
};
@@ -770,18 +771,19 @@ fn add_row_backgrounds(job: &LayoutJob, row: &Row, mesh: &mut Mesh) {

if color == Color32::TRANSPARENT {
end_run(run_start.take(), last_rect.right());
} else if let Some((existing_color, start)) = run_start {
} else if let Some((existing_color, start, expand)) = run_start {
if existing_color == color
&& start.top() == rect.top()
&& start.bottom() == rect.bottom()
&& format.expand_bg == expand
{
// continue the same background rectangle
} else {
end_run(run_start.take(), last_rect.right());
run_start = Some((color, rect));
run_start = Some((color, rect, format.expand_bg));
}
} else {
run_start = Some((color, rect));
run_start = Some((color, rect, format.expand_bg));
}

last_rect = rect;
8 changes: 8 additions & 0 deletions crates/epaint/src/text/text_layout_types.rs
Original file line number Diff line number Diff line change
@@ -271,6 +271,11 @@ pub struct TextFormat {

pub background: Color32,

/// Amount to expand background fill by.
///
/// Default: 1.0
pub expand_bg: f32,

pub italics: bool,

pub underline: Stroke,
@@ -298,6 +303,7 @@ impl Default for TextFormat {
line_height: None,
color: Color32::GRAY,
background: Color32::TRANSPARENT,
expand_bg: 1.0,
italics: false,
underline: Stroke::NONE,
strikethrough: Stroke::NONE,
@@ -315,6 +321,7 @@ impl std::hash::Hash for TextFormat {
line_height,
color,
background,
expand_bg,
italics,
underline,
strikethrough,
@@ -327,6 +334,7 @@ impl std::hash::Hash for TextFormat {
}
color.hash(state);
background.hash(state);
emath::OrderedFloat(*expand_bg).hash(state);
italics.hash(state);
underline.hash(state);
strikethrough.hash(state);