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

Finish button : retrocompatibility + comments

This commit is contained in:
adrien
2025-10-13 21:46:50 +02:00
parent e99ce8dec7
commit c2e62f87d7
2 changed files with 26 additions and 22 deletions

View File

@@ -1,9 +1,9 @@
use std::{mem, sync::Arc};
use crate::{
Atom, AtomExt as _, AtomKind, AtomLayout, AtomLayoutResponse, Color32, CornerRadius, Frame,
Image, IntoAtoms, NumExt as _, Response, RichText, Sense, Stroke, TextStyle, TextWrapMode, Ui,
Vec2, Widget, WidgetInfo, WidgetText, WidgetType,
Atom, AtomExt as _, AtomKind, AtomLayout, AtomLayoutResponse, Color32, CornerRadius, Image,
IntoAtoms, NumExt as _, Response, RichText, Sense, Stroke, TextStyle, TextWrapMode, Ui, Vec2,
Widget, WidgetInfo, WidgetText, WidgetType, style_trait::WidgetState,
};
/// Clickable button with text.
@@ -255,18 +255,19 @@ impl<'a> Button<'a> {
pub fn atom_ui(self, ui: &mut Ui) -> AtomLayoutResponse {
let Button {
mut layout,
fill,
stroke,
fill: _,
stroke: _,
small,
frame,
frame_when_inactive,
mut min_size,
corner_radius,
selected,
corner_radius: _,
selected: _,
image_tint_follows_text_color,
limit_image_size,
} = self;
// Min size height always equal or greater than interact size if not small
if !small {
min_size.y = min_size.y.at_least(ui.spacing().interact_size.y);
}
@@ -283,14 +284,15 @@ impl<'a> Button<'a> {
let text = layout.text().map(String::from);
// Get the widget style
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().button_style(state);
let has_frame_margin = frame.unwrap_or_else(|| ui.visuals().button_frame);
// Apply the correct font on RichText and Text
layout.map_texts(|t| match t {
WidgetText::RichText(mut text) => {
let text_mut = Arc::make_mut(&mut text);
@@ -304,28 +306,22 @@ impl<'a> Button<'a> {
w => w,
});
let mut prepared = layout.frame(style.frame).min_size(min_size).allocate(ui);
let response = if ui.is_rect_visible(prepared.response.rect) {
let visible_frame = if frame_when_inactive {
has_frame_margin
// Retrocompatibility with button settings
let mut prepared =
if has_frame_margin && (frame_when_inactive || state != WidgetState::Inactive) {
layout.frame(style.frame).min_size(min_size).allocate(ui)
} else {
has_frame_margin
&& (prepared.response.hovered()
|| prepared.response.is_pointer_button_down_on()
|| prepared.response.has_focus())
layout.min_size(min_size).allocate(ui)
};
// Get AtomLayoutResponse, empty if not visible
let response = if ui.is_rect_visible(prepared.response.rect) {
if image_tint_follows_text_color {
prepared.map_images(|image| image.tint(style.text.color));
}
prepared.fallback_text_color = style.text.color;
if visible_frame {
prepared.frame = style.frame;
}
prepared.paint(ui)
} else {
AtomLayoutResponse::empty(prepared.response)

View File

@@ -1,7 +1,7 @@
#![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;
use eframe::egui::{self, Button};
fn main() -> eframe::Result {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
@@ -27,6 +27,14 @@ fn main() -> eframe::Result {
if ui.button("Increment").clicked() {
age += 1;
}
ui.add(Button::new("no frame").frame(false));
ui.add(Button::new("small").small());
ui.add_enabled(
false,
Button::new("no frame inactive").frame_when_inactive(false),
);
ui.label(format!("Hello '{name}', age {age}"));
});
})