1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 06:40:06 -04:00

Finish label

This commit is contained in:
Adrien Zianne
2025-10-16 13:22:09 +02:00
parent f66b5b599b
commit e39d8c4345
5 changed files with 35 additions and 26 deletions

View File

@@ -12,6 +12,9 @@ pub struct TextVisuals {
pub font_id: FontId, pub font_id: FontId,
/// Font color /// Font color
pub color: Color32, pub color: Color32,
/// Text decoration
pub underline: Stroke,
pub strikethrough: Stroke,
} }
/// General widget style /// General widget style
@@ -152,6 +155,8 @@ impl Style {
text: TextVisuals { text: TextVisuals {
color: visuals.text_color(), color: visuals.text_color(),
font_id: font_id.unwrap_or(TextStyle::Body.resolve(self)), 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::{ use crate::{
Atom, AtomExt as _, AtomKind, AtomLayout, AtomLayoutResponse, Color32, CornerRadius, Image, Atom, AtomExt as _, AtomKind, AtomLayout, AtomLayoutResponse, Color32, CornerRadius, Image,

View File

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