1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 13:50:04 -04:00

rename style_trait.rs -> widget_style.rs and font/color fallback for button

This commit is contained in:
adrien
2025-11-19 09:26:28 +01:00
parent 0b9f70d47b
commit 58992a635a
5 changed files with 15 additions and 29 deletions

View File

@@ -434,7 +434,6 @@ mod plugin;
pub mod response; pub mod response;
mod sense; mod sense;
pub mod style; pub mod style;
pub mod style_trait;
pub mod text_selection; pub mod text_selection;
mod ui; mod ui;
mod ui_builder; mod ui_builder;
@@ -442,6 +441,7 @@ mod ui_stack;
pub mod util; pub mod util;
pub mod viewport; pub mod viewport;
mod widget_rect; mod widget_rect;
pub mod widget_style;
pub mod widget_text; pub mod widget_text;
pub mod widgets; pub mod widgets;

View File

@@ -1,12 +1,10 @@
use std::{mem, sync::Arc};
use epaint::Margin; use epaint::Margin;
use crate::{ use crate::{
Atom, AtomExt as _, AtomKind, AtomLayout, AtomLayoutResponse, Color32, CornerRadius, Frame, Atom, AtomExt as _, AtomKind, AtomLayout, AtomLayoutResponse, Color32, CornerRadius, Frame,
Image, IntoAtoms, NumExt as _, Response, RichText, Sense, Stroke, TextStyle, TextWrapMode, Ui, Image, IntoAtoms, NumExt as _, Response, Sense, Stroke, TextStyle, TextWrapMode, Ui, Vec2,
Vec2, Widget, WidgetInfo, WidgetText, WidgetType, Widget, WidgetInfo, WidgetText, WidgetType,
style_trait::{ButtonStyle, WidgetState}, widget_style::{ButtonStyle, WidgetState},
}; };
/// Clickable button with text. /// Clickable button with text.
@@ -327,24 +325,10 @@ impl<'a> Button<'a> {
frame = frame.inner_margin(button_padding); frame = frame.inner_margin(button_padding);
// Apply the correct font and color if Text // Apply the style font and color as fallback
// We assume that the other WidgetText have already a Fontid and color layout = layout
layout.map_texts(|t| match t { .fallback_font(text_style.font_id.clone())
WidgetText::Text(text) => { .fallback_text_color(text_style.color);
let rich_text = RichText::new(text.clone())
.font(text_style.font_id.clone())
.color(text_style.color);
WidgetText::RichText(Arc::new(rich_text))
}
WidgetText::RichText(mut text) => {
let text_mut = Arc::make_mut(&mut text);
*text_mut = mem::take(text_mut)
.font(text_style.font_id.clone())
.color(text_style.color);
WidgetText::RichText(text)
}
w => w,
});
// Retrocompatibility with button settings // Retrocompatibility with button settings
layout = if has_frame_margin && (state != WidgetState::Inactive || frame_when_inactive) { layout = if has_frame_margin && (state != WidgetState::Inactive || frame_when_inactive) {

View File

@@ -2,7 +2,7 @@ use emath::Rect;
use crate::{ use crate::{
Atom, AtomLayout, Atoms, Id, IntoAtoms, NumExt as _, Response, Sense, Shape, Ui, Vec2, Widget, Atom, AtomLayout, Atoms, Id, IntoAtoms, NumExt as _, Response, Sense, Shape, Ui, Vec2, Widget,
WidgetInfo, WidgetType, epaint, pos2, style_trait::CheckboxStyle, WidgetInfo, WidgetType, epaint, pos2, widget_style::CheckboxStyle,
}; };
// TODO(emilk): allow checkbox without a text label // TODO(emilk): allow checkbox without a text label

View File

@@ -1,4 +1,4 @@
use crate::{Response, Sense, Ui, Vec2, Widget, vec2}; use crate::{Response, Sense, Ui, Vec2, Widget, vec2, widget_style::SeparatorStyle};
/// A visual separator. A horizontal or vertical line (depending on [`crate::Layout`]). /// A visual separator. A horizontal or vertical line (depending on [`crate::Layout`]).
/// ///
@@ -97,10 +97,13 @@ impl Widget for Separator {
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 state = response.map(|r| r.widget_state()).unwrap_or_default();
let style = ui.style().separator_style(state); let SeparatorStyle {
spacing: spacing_style,
stroke,
} = ui.style().separator_style(state);
// override the spacing if not set // override the spacing if not set
let spacing = spacing.unwrap_or(style.spacing); let spacing = spacing.unwrap_or(spacing_style);
let is_horizontal_line = is_horizontal_line let is_horizontal_line = is_horizontal_line
.unwrap_or_else(|| ui.is_grid() || !ui.layout().main_dir().is_horizontal()); .unwrap_or_else(|| ui.is_grid() || !ui.layout().main_dir().is_horizontal());
@@ -120,7 +123,6 @@ impl Widget for Separator {
let (rect, response) = ui.allocate_at_least(size, Sense::hover()); let (rect, response) = ui.allocate_at_least(size, Sense::hover());
if ui.is_rect_visible(response.rect) { if ui.is_rect_visible(response.rect) {
let stroke = style.stroke;
let painter = ui.painter(); let painter = ui.painter();
if is_horizontal_line { if is_horizontal_line {
painter.hline( painter.hline(