1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-26 22:53:14 -04:00

Finish label

This commit is contained in:
Adrien Zianne
2025-10-16 13:22:09 +02:00
committed by adrien
parent 5265d4ef0c
commit a40959b047
5 changed files with 35 additions and 26 deletions

View File

@@ -12,6 +12,9 @@ pub struct TextVisuals {
pub font_id: FontId,
/// Font color
pub color: Color32,
/// Text decoration
pub underline: Stroke,
pub strikethrough: Stroke,
}
/// General widget style
@@ -152,6 +155,8 @@ impl Style {
text: TextVisuals {
color: visuals.text_color(),
font_id: font_id.unwrap_or(TextStyle::Body.resolve(self)),
strikethrough: Stroke::NONE,
underline: Stroke::NONE,
},
}
}

View File

@@ -1,4 +1,4 @@
use std::{mem, sync::Arc};
use std::sync::Arc;
use crate::{
Atom, AtomExt as _, AtomKind, AtomLayout, AtomLayoutResponse, Color32, CornerRadius, Image,

View File

@@ -1,9 +1,8 @@
use std::sync::Arc;
use crate::{
Align, Direction, FontSelection, Frame, Galley, Pos2, Response, RichText, Sense, Stroke,
TextWrapMode, Ui, Widget, WidgetInfo, WidgetText, WidgetType, epaint, pos2,
text_selection::LabelSelectionState,
Align, Direction, Galley, Pos2, Response, Sense, TextWrapMode, Ui, Widget, WidgetInfo,
WidgetText, WidgetType, epaint, pos2, text_selection::LabelSelectionState,
};
/// Static text.
@@ -169,8 +168,8 @@ impl Label {
sense |= select_sense;
}
// If the user said "use this specific galley", then just use it:
if let WidgetText::Galley(galley) = self.text {
// If the user said "use this specific galley", then just use it:
let (rect, response) = ui.allocate_exact_size(galley.size(), sense);
let pos = match galley.job.halign {
Align::LEFT => rect.left_top(),
@@ -189,7 +188,7 @@ impl Label {
let valign = ui.text_valign();
let mut layout_job = Arc::unwrap_or_clone(self.text.into_layout_job(
ui.style(),
style.text.font_id.into(), // Use the style font
style.text.font_id.into(), // Use the label style font
valign,
));
@@ -203,7 +202,6 @@ impl Label {
{
// On a wrapping horizontal layout we want text to start after the previous widget,
// then continue on the line below! This will take some extra work:
let cursor = ui.cursor();
let first_row_indentation = available_width - ui.available_size_before_wrap().x;
debug_assert!(
@@ -273,7 +271,7 @@ impl Label {
}
impl Widget for Label {
fn ui(mut self, ui: &mut Ui) -> Response {
fn ui(self, ui: &mut Ui) -> Response {
// Interactive = the uses asked to sense interaction.
// We DON'T want to have the color respond just because the text is selectable;
// the cursor is enough to communicate that.
@@ -283,22 +281,16 @@ impl Widget for Label {
let show_tooltip_when_elided = self.show_tooltip_when_elided;
// Get the widget style by reading the rect from the previous pass
let id = ui.next_auto_id();
let response: Option<Response> = ui.ctx().read_response(id);
let state = response.map(|r| r.widget_state()).unwrap_or_default();
let style = ui.style().label_style(state);
// if let WidgetText::Text(text) = self.text {
// let rich_text = RichText::new(text)
// .font(style.text.font_id)
// .color(style.text.color);
// self.text = WidgetText::RichText(Arc::new(rich_text));
// }
// let id = ui.next_auto_id();
// let response: Option<Response> = ui.ctx().read_response(id);
let (galley_pos, galley, mut response) = self.layout_in_ui(ui);
response
.widget_info(|| WidgetInfo::labeled(WidgetType::Label, ui.is_enabled(), galley.text()));
let state = response.widget_state();
let style = ui.style().label_style(state);
if ui.is_rect_visible(response.rect) {
if show_tooltip_when_elided && galley.elided {
// Keep the sections and text, but reset everything else (especially wrapping):
@@ -317,11 +309,12 @@ impl Widget for Label {
ui.style().visuals.text_color()
};
let underline = if response.has_focus() || response.highlighted() {
Stroke::new(1.0, response_color)
} else {
Stroke::NONE
};
// let underline = if response.has_focus() || response.highlighted() {
// Stroke::new(1.0, response_color)
// } else {
// Stroke::NONE
// };
let underline = style.text.underline;
let selectable = selectable.unwrap_or_else(|| ui.style().interaction.selectable_labels);
if selectable {

View File

@@ -0,0 +1,9 @@
## Button
- If hovering a button add a stroke, the ui shift. Maybe add the option to avoid this resize by making the frame smaller ?
## Label
- I understand checking if a sense has been set by the user, but why check if it's different than hover ? Is it for technical purpose or purely to avoid confusion with a link ?
- Selecting the text while being underlined move the underline

View File

@@ -25,7 +25,7 @@ fn main() -> eframe::Result {
..s.visuals.widgets.inactive
};
s.visuals.widgets.active = WidgetVisuals {
fg_stroke: Stroke::new(1.0, Color32::BLUE),
fg_stroke: Stroke::new(1.0, Color32::LIGHT_BLUE),
..s.visuals.widgets.inactive
};
s.visuals.widgets.hovered = WidgetVisuals {
@@ -63,7 +63,9 @@ fn main() -> eframe::Result {
.color(Color32::KHAKI),
);
ui.add(Label::new("test").sense(Sense::click()))
ui.add(Label::new("interaction click").sense(Sense::click()));
ui.add(Label::new("focusable").sense(Sense::focusable_noninteractive()))
.request_focus();
});
})
}