mirror of
https://github.com/emilk/egui.git
synced 2026-06-26 22:53:14 -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()
|
||||
};
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
||||
#![allow(rustdoc::missing_crate_level_docs)] // it's an example
|
||||
|
||||
use eframe::egui::{self, Button};
|
||||
use std::process::exit;
|
||||
|
||||
use eframe::egui::{self, Color32, FontId, Label, RichText, Sense, Stroke, style::WidgetVisuals};
|
||||
|
||||
fn main() -> eframe::Result {
|
||||
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
|
||||
@@ -17,24 +19,51 @@ fn main() -> eframe::Result {
|
||||
|
||||
eframe::run_simple_native("My egui App", options, move |ctx, _frame| {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.heading("My egui Application");
|
||||
ui.horizontal(|ui| {
|
||||
let name_label = ui.label("Your name: ");
|
||||
ui.text_edit_singleline(&mut name)
|
||||
.labelled_by(name_label.id);
|
||||
ui.ctx().style_mut(|s| {
|
||||
s.visuals.widgets.inactive = WidgetVisuals {
|
||||
fg_stroke: Stroke::new(1.0, Color32::LIGHT_GRAY),
|
||||
..s.visuals.widgets.inactive
|
||||
};
|
||||
s.visuals.widgets.active = WidgetVisuals {
|
||||
fg_stroke: Stroke::new(1.0, Color32::BLUE),
|
||||
..s.visuals.widgets.inactive
|
||||
};
|
||||
s.visuals.widgets.hovered = WidgetVisuals {
|
||||
fg_stroke: Stroke::new(1.0, Color32::YELLOW),
|
||||
..s.visuals.widgets.inactive
|
||||
};
|
||||
s.visuals.widgets.noninteractive = WidgetVisuals {
|
||||
fg_stroke: Stroke::new(1.0, Color32::RED),
|
||||
..s.visuals.widgets.inactive
|
||||
};
|
||||
});
|
||||
ui.add(egui::Slider::new(&mut age, 0..=120).text("age"));
|
||||
if ui.button("Increment").clicked() {
|
||||
age += 1;
|
||||
}
|
||||
|
||||
// ui.heading("My egui Application");
|
||||
// ui.horizontal(|ui| {
|
||||
// let name_label = ui.label("Your name: ");
|
||||
// ui.text_edit_singleline(&mut name)
|
||||
// .labelled_by(name_label.id);
|
||||
// });
|
||||
// ui.add(egui::Slider::new(&mut age, 0..=120).text("age"));
|
||||
// if ui.button("Increment").clicked() {
|
||||
// age += 1;
|
||||
// }
|
||||
|
||||
// Button test
|
||||
ui.add(Button::new("no frame").frame(false));
|
||||
ui.add(Button::new("small").small());
|
||||
ui.add_enabled(false, Button::new("disabled"));
|
||||
ui.add(Button::new("no frame inactive").frame_when_inactive(false));
|
||||
// ui.add(Button::new("no frame").frame(false));
|
||||
// ui.add(Button::new("small").small());
|
||||
// ui.add_enabled(false, Button::new("disabled"));
|
||||
// ui.add(Button::new("no frame inactive").frame_when_inactive(false));
|
||||
|
||||
ui.label(format!("Hello '{name}', age {age}"));
|
||||
ui.label("Normal text");
|
||||
// Should not be affected by WidgetStyle
|
||||
ui.label(
|
||||
RichText::new("Unaffected by style")
|
||||
.font(FontId::monospace(15.0))
|
||||
.color(Color32::KHAKI),
|
||||
);
|
||||
|
||||
ui.add(Label::new("test").sense(Sense::click()))
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user