mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 06:10:06 -04:00
apply style to label
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use emath::Vec2;
|
||||
use epaint::{Color32, FontId, Stroke};
|
||||
use epaint::{Color32, FontId, Shadow, Stroke, text::TextWrapMode};
|
||||
|
||||
use crate::{
|
||||
Frame, Response, Style,
|
||||
Frame, Response, Style, TextStyle,
|
||||
style::{WidgetVisuals, Widgets},
|
||||
};
|
||||
|
||||
@@ -66,11 +66,12 @@ pub struct ImageStyle {
|
||||
}
|
||||
|
||||
pub struct LabelStyle {
|
||||
/// Frame around
|
||||
pub frame: Frame,
|
||||
/// Text style
|
||||
pub text: TextVisuals,
|
||||
pub size: Vec2,
|
||||
pub checkbox_frame: Frame,
|
||||
pub stroke: Stroke,
|
||||
/// Wrap mode used
|
||||
pub wrap_mode: TextWrapMode,
|
||||
}
|
||||
|
||||
pub struct RadioButtonStyle {
|
||||
@@ -149,8 +150,8 @@ impl Style {
|
||||
},
|
||||
stroke: visuals.fg_stroke,
|
||||
text: TextVisuals {
|
||||
color: visuals.fg_stroke.color,
|
||||
font_id: font_id.unwrap_or(FontId::new(13.0, epaint::FontFamily::Proportional)),
|
||||
color: visuals.text_color(),
|
||||
font_id: font_id.unwrap_or(TextStyle::Body.resolve(self)),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -165,7 +166,21 @@ impl Style {
|
||||
|
||||
// pub fn checkbox_style(&self, state: WidgetState) -> CheckboxStylee {}
|
||||
|
||||
// pub fn label_style(&self, state: WidgetState) -> LabelStyle {}
|
||||
pub fn label_style(&self, state: WidgetState) -> LabelStyle {
|
||||
let ws = self.widget_style(state);
|
||||
LabelStyle {
|
||||
frame: Frame {
|
||||
fill: ws.frame.fill,
|
||||
inner_margin: 0.0.into(),
|
||||
outer_margin: 0.0.into(),
|
||||
stroke: Stroke::NONE,
|
||||
shadow: Shadow::NONE,
|
||||
corner_radius: 0.into(),
|
||||
},
|
||||
text: ws.text,
|
||||
wrap_mode: TextWrapMode::Wrap,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn drag_value_style(&self, state: WidgetState) -> DragValueStyle {
|
||||
let ws = self.widget_style(state);
|
||||
|
||||
@@ -290,7 +290,6 @@ impl<'a> Button<'a> {
|
||||
let state = response.map(|r| r.widget_state()).unwrap_or_default();
|
||||
let style = ui.style().button_style(state);
|
||||
|
||||
//
|
||||
let has_frame_margin = frame.unwrap_or_else(|| ui.visuals().button_frame);
|
||||
|
||||
// Apply the correct font and color if Text
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{
|
||||
Align, Direction, FontSelection, Galley, Pos2, Response, Sense, Stroke, TextWrapMode, Ui,
|
||||
Widget, WidgetInfo, WidgetText, WidgetType, epaint, pos2, text_selection::LabelSelectionState,
|
||||
Align, Direction, FontSelection, Frame, Galley, Pos2, Response, RichText, Sense, Stroke,
|
||||
TextWrapMode, Ui, Widget, WidgetInfo, WidgetText, WidgetType, epaint, pos2,
|
||||
text_selection::LabelSelectionState,
|
||||
};
|
||||
|
||||
/// Static text.
|
||||
@@ -179,10 +180,16 @@ impl Label {
|
||||
return (pos, galley, response);
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
let valign = ui.text_valign();
|
||||
let mut layout_job = Arc::unwrap_or_clone(self.text.into_layout_job(
|
||||
ui.style(),
|
||||
FontSelection::Default,
|
||||
style.text.font_id.into(), // Use the style font
|
||||
valign,
|
||||
));
|
||||
|
||||
@@ -266,7 +273,7 @@ impl Label {
|
||||
}
|
||||
|
||||
impl Widget for Label {
|
||||
fn ui(self, ui: &mut Ui) -> Response {
|
||||
fn ui(mut 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.
|
||||
@@ -275,6 +282,19 @@ impl Widget for Label {
|
||||
let selectable = self.selectable;
|
||||
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 (galley_pos, galley, mut response) = self.layout_in_ui(ui);
|
||||
response
|
||||
.widget_info(|| WidgetInfo::labeled(WidgetType::Label, ui.is_enabled(), galley.text()));
|
||||
@@ -292,7 +312,7 @@ impl Widget for Label {
|
||||
}
|
||||
|
||||
let response_color = if interactive {
|
||||
ui.style().interact(&response).text_color()
|
||||
style.text.color
|
||||
} else {
|
||||
ui.style().visuals.text_color()
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user