mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 21:30:03 -04:00
Finish button : retrocompatibility + comments
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
use std::{mem, sync::Arc};
|
use std::{mem, sync::Arc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Atom, AtomExt as _, AtomKind, AtomLayout, AtomLayoutResponse, Color32, CornerRadius, Frame,
|
Atom, AtomExt as _, AtomKind, AtomLayout, AtomLayoutResponse, Color32, CornerRadius, Image,
|
||||||
Image, IntoAtoms, NumExt as _, Response, RichText, Sense, Stroke, TextStyle, TextWrapMode, Ui,
|
IntoAtoms, NumExt as _, Response, RichText, Sense, Stroke, TextStyle, TextWrapMode, Ui, Vec2,
|
||||||
Vec2, Widget, WidgetInfo, WidgetText, WidgetType,
|
Widget, WidgetInfo, WidgetText, WidgetType, style_trait::WidgetState,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Clickable button with text.
|
/// Clickable button with text.
|
||||||
@@ -255,18 +255,19 @@ impl<'a> Button<'a> {
|
|||||||
pub fn atom_ui(self, ui: &mut Ui) -> AtomLayoutResponse {
|
pub fn atom_ui(self, ui: &mut Ui) -> AtomLayoutResponse {
|
||||||
let Button {
|
let Button {
|
||||||
mut layout,
|
mut layout,
|
||||||
fill,
|
fill: _,
|
||||||
stroke,
|
stroke: _,
|
||||||
small,
|
small,
|
||||||
frame,
|
frame,
|
||||||
frame_when_inactive,
|
frame_when_inactive,
|
||||||
mut min_size,
|
mut min_size,
|
||||||
corner_radius,
|
corner_radius: _,
|
||||||
selected,
|
selected: _,
|
||||||
image_tint_follows_text_color,
|
image_tint_follows_text_color,
|
||||||
limit_image_size,
|
limit_image_size,
|
||||||
} = self;
|
} = self;
|
||||||
|
|
||||||
|
// Min size height always equal or greater than interact size if not small
|
||||||
if !small {
|
if !small {
|
||||||
min_size.y = min_size.y.at_least(ui.spacing().interact_size.y);
|
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);
|
let text = layout.text().map(String::from);
|
||||||
|
|
||||||
|
// Get the widget style
|
||||||
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().button_style(state);
|
let style = ui.style().button_style(state);
|
||||||
|
|
||||||
let has_frame_margin = frame.unwrap_or_else(|| ui.visuals().button_frame);
|
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 {
|
layout.map_texts(|t| match t {
|
||||||
WidgetText::RichText(mut text) => {
|
WidgetText::RichText(mut text) => {
|
||||||
let text_mut = Arc::make_mut(&mut text);
|
let text_mut = Arc::make_mut(&mut text);
|
||||||
@@ -304,28 +306,22 @@ impl<'a> Button<'a> {
|
|||||||
w => w,
|
w => w,
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut prepared = layout.frame(style.frame).min_size(min_size).allocate(ui);
|
// Retrocompatibility with button settings
|
||||||
|
let mut prepared =
|
||||||
let response = if ui.is_rect_visible(prepared.response.rect) {
|
if has_frame_margin && (frame_when_inactive || state != WidgetState::Inactive) {
|
||||||
let visible_frame = if frame_when_inactive {
|
layout.frame(style.frame).min_size(min_size).allocate(ui)
|
||||||
has_frame_margin
|
|
||||||
} else {
|
} else {
|
||||||
has_frame_margin
|
layout.min_size(min_size).allocate(ui)
|
||||||
&& (prepared.response.hovered()
|
|
||||||
|| prepared.response.is_pointer_button_down_on()
|
|
||||||
|| prepared.response.has_focus())
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Get AtomLayoutResponse, empty if not visible
|
||||||
|
let response = if ui.is_rect_visible(prepared.response.rect) {
|
||||||
if image_tint_follows_text_color {
|
if image_tint_follows_text_color {
|
||||||
prepared.map_images(|image| image.tint(style.text.color));
|
prepared.map_images(|image| image.tint(style.text.color));
|
||||||
}
|
}
|
||||||
|
|
||||||
prepared.fallback_text_color = style.text.color;
|
prepared.fallback_text_color = style.text.color;
|
||||||
|
|
||||||
if visible_frame {
|
|
||||||
prepared.frame = style.frame;
|
|
||||||
}
|
|
||||||
|
|
||||||
prepared.paint(ui)
|
prepared.paint(ui)
|
||||||
} else {
|
} else {
|
||||||
AtomLayoutResponse::empty(prepared.response)
|
AtomLayoutResponse::empty(prepared.response)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
#![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
|
#![allow(rustdoc::missing_crate_level_docs)] // it's an example
|
||||||
|
|
||||||
use eframe::egui;
|
use eframe::egui::{self, Button};
|
||||||
|
|
||||||
fn main() -> eframe::Result {
|
fn main() -> eframe::Result {
|
||||||
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
|
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() {
|
if ui.button("Increment").clicked() {
|
||||||
age += 1;
|
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}"));
|
ui.label(format!("Hello '{name}', age {age}"));
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user