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

Rename Context::style to global_style; avoid confusion w/ Ui::style (#7772)

This is important after
* https://github.com/emilk/egui/pull/7770
This commit is contained in:
Emil Ernerfeldt
2025-12-14 16:41:13 +01:00
committed by GitHub
parent 5ea94dd0d7
commit bc48599f65
25 changed files with 94 additions and 58 deletions

View File

@@ -571,7 +571,7 @@ impl GlowWinitRunning<'_> {
.options_mut(|opt| opt.begin_pass(&raw_input));
let clear_color = self
.app
.clear_color(&self.integration.egui_ctx.style().visuals);
.clear_color(&self.integration.egui_ctx.global_style().visuals);
let has_many_viewports = self.glutin.borrow().viewports.len() > 1;
let clear_before_update = !has_many_viewports; // HACK: for some reason, an early clear doesn't "take" on Mac with multiple viewports.

View File

@@ -690,7 +690,7 @@ impl WgpuWinitRunning<'_> {
let vsync_secs = painter.paint_and_update_textures(
viewport_id,
pixels_per_point,
app.clear_color(&egui_ctx.style().visuals),
app.clear_color(&egui_ctx.global_style().visuals),
&clipped_primitives,
&textures_delta,
screenshot_commands,

View File

@@ -331,7 +331,7 @@ impl AppRunner {
}
if let Err(err) = self.painter.paint_and_update_textures(
self.app.clear_color(&self.egui_ctx.style().visuals),
self.app.clear_color(&self.egui_ctx.global_style().visuals),
&clipped_primitives,
self.egui_ctx.pixels_per_point(),
&textures_delta,

View File

@@ -469,7 +469,7 @@ impl Area {
// during the sizing pass we will use this as the max size
let mut size = default_size;
let default_area_size = ctx.style().spacing.default_area_size;
let default_area_size = ctx.global_style().spacing.default_area_size;
if size.x.is_nan() {
size.x = default_area_size.x;
}
@@ -634,7 +634,8 @@ impl Prepared {
{
let age =
ctx.input(|i| (i.time - last_became_visible_at) as f32 + i.predicted_dt / 2.0);
let opacity = crate::remap_clamp(age, 0.0..=ctx.style().animation_time, 0.0..=1.0);
let opacity =
crate::remap_clamp(age, 0.0..=ctx.global_style().animation_time, 0.0..=1.0);
let opacity = emath::easing::quadratic_out(opacity); // slow fade-out = quick fade-in
ui.multiply_opacity(opacity);
if opacity < 1.0 {

View File

@@ -934,8 +934,8 @@ impl Panel {
};
let get_spacing_size = || match panel.side {
PanelSide::Vertical(_) => ctx.style().spacing.interact_size.x,
PanelSide::Horizontal(_) => ctx.style().spacing.interact_size.y,
PanelSide::Vertical(_) => ctx.global_style().spacing.interact_size.x,
PanelSide::Horizontal(_) => ctx.global_style().spacing.interact_size.y,
};
PanelState::load(ctx, panel.id)

View File

@@ -369,7 +369,7 @@ impl Resize {
state.store(ui.ctx(), id);
#[cfg(debug_assertions)]
if ui.ctx().style().debug.show_resize {
if ui.global_style().debug.show_resize {
ui.ctx().debug_painter().debug_rect(
Rect::from_min_size(content_ui.min_rect().left_top(), state.desired_size),
Color32::GREEN,

View File

@@ -41,7 +41,7 @@ impl Tooltip<'_> {
parent_widget: Id,
anchor: impl Into<PopupAnchor>,
) -> Self {
let width = ctx.style().spacing.tooltip_width;
let width = ctx.global_style().spacing.tooltip_width;
Self {
popup: Popup::new(parent_widget, ctx, anchor.into(), parent_layer)
.kind(PopupKind::Tooltip)
@@ -58,7 +58,7 @@ impl Tooltip<'_> {
let popup = Popup::from_response(response)
.kind(PopupKind::Tooltip)
.gap(4.0)
.width(response.ctx.style().spacing.tooltip_width)
.width(response.ctx.global_style().spacing.tooltip_width)
.sense(Sense::hover());
Self {
popup,
@@ -229,7 +229,7 @@ impl Tooltip<'_> {
return false;
}
let style = response.ctx.style();
let style = response.ctx.global_style();
let tooltip_delay = style.interaction.tooltip_delay;
let tooltip_grace_time = style.interaction.tooltip_grace_time;

View File

@@ -440,9 +440,11 @@ impl Window<'_> {
fade_out,
} = self;
let header_color =
frame.map_or_else(|| ctx.style().visuals.widgets.open.weak_bg_fill, |f| f.fill);
let mut window_frame = frame.unwrap_or_else(|| Frame::window(&ctx.style()));
let header_color = frame.map_or_else(
|| ctx.global_style().visuals.widgets.open.weak_bg_fill,
|f| f.fill,
);
let mut window_frame = frame.unwrap_or_else(|| Frame::window(&ctx.global_style()));
let is_explicitly_closed = matches!(open, Some(false));
let is_open = !is_explicitly_closed || ctx.memory(|mem| mem.everything_is_visible());
@@ -474,7 +476,7 @@ impl Window<'_> {
// Calculate roughly how much larger the full window inner size is compared to the content rect
let (title_bar_height_with_margin, title_content_spacing) = if with_title_bar {
let style = ctx.style();
let style = ctx.global_style();
let title_bar_inner_height = ctx
.fonts_mut(|fonts| title.font_height(fonts, &style))
.at_least(style.spacing.interact_size.y);
@@ -930,8 +932,8 @@ fn resize_interaction(
let id = Id::new(layer_id).with("edge_drag");
let side_grab_radius = ctx.style().interaction.resize_grab_radius_side;
let corner_grab_radius = ctx.style().interaction.resize_grab_radius_corner;
let side_grab_radius = ctx.global_style().interaction.resize_grab_radius_side;
let corner_grab_radius = ctx.global_style().interaction.resize_grab_radius_corner;
let vetrtical_rect = |a: Pos2, b: Pos2| {
Rect::from_min_max(a, b).expand2(vec2(side_grab_radius, -corner_grab_radius))

View File

@@ -1148,7 +1148,7 @@ impl Context {
let content_rect = self.content_rect();
let text = format!("🔥 {text}");
let color = self.style().visuals.error_fg_color;
let color = self.global_style().visuals.error_fg_color;
let painter = self.debug_painter();
painter.rect_stroke(widget_rect, 0.0, (1.0, color), StrokeKind::Outside);
@@ -1603,7 +1603,7 @@ impl Context {
..
} = ModifierNames::SYMBOLS;
let font_id = TextStyle::Body.resolve(&self.style());
let font_id = TextStyle::Body.resolve(&self.global_style());
self.fonts_mut(|f| {
let mut font = f.fonts.font(&font_id.family);
font.has_glyphs(alt)
@@ -2052,13 +2052,13 @@ impl Context {
}
/// The [`Theme`] used to select the appropriate [`Style`] (dark or light)
/// used by all subsequent windows, panels etc.
/// used by all subsequent popups, menus, etc.
pub fn theme(&self) -> Theme {
self.options(|opt| opt.theme())
}
/// The [`Theme`] used to select between dark and light [`Self::style`]
/// as the active style used by all subsequent windows, panels etc.
/// as the active style used by all subsequent popups, menus, etc.
///
/// Example:
/// ```
@@ -2069,37 +2069,70 @@ impl Context {
self.options_mut(|opt| opt.theme_preference = theme_preference.into());
}
/// The currently active [`Style`] used by all subsequent windows, panels etc.
/// The currently active [`Style`] used by all subsequent popups, menus, etc.
pub fn global_style(&self) -> Arc<Style> {
self.options(|opt| opt.style().clone())
}
/// The currently active [`Style`] used by all subsequent popups, menus, etc.
#[deprecated = "Renamed to `global_style` to avoid confusion with `ui.style()`"]
pub fn style(&self) -> Arc<Style> {
self.options(|opt| opt.style().clone())
}
/// Mutate the currently active [`Style`] used by all subsequent windows, panels etc.
/// Mutate the currently active [`Style`] used by all subsequent popups, menus, etc.
/// Use [`Self::all_styles_mut`] to mutate both dark and light mode styles.
///
/// Example:
/// ```
/// # let mut ctx = egui::Context::default();
/// ctx.style_mut(|style| {
/// ctx.global_style_mut(|style| {
/// style.spacing.item_spacing = egui::vec2(10.0, 20.0);
/// });
/// ```
pub fn global_style_mut(&self, mutate_style: impl FnOnce(&mut Style)) {
self.options_mut(|opt| mutate_style(Arc::make_mut(opt.style_mut())));
}
/// Mutate the currently active [`Style`] used by all subsequent popups, menus, etc.
/// Use [`Self::all_styles_mut`] to mutate both dark and light mode styles.
///
/// Example:
/// ```
/// # let mut ctx = egui::Context::default();
/// ctx.global_style_mut(|style| {
/// style.spacing.item_spacing = egui::vec2(10.0, 20.0);
/// });
/// ```
#[deprecated = "Renamed to `global_style_mut` to avoid confusion with `ui.style_mut()`"]
pub fn style_mut(&self, mutate_style: impl FnOnce(&mut Style)) {
self.options_mut(|opt| mutate_style(Arc::make_mut(opt.style_mut())));
}
/// The currently active [`Style`] used by all new windows, panels etc.
/// The currently active [`Style`] used by all new popups, menus, etc.
///
/// Use [`Self::all_styles_mut`] to mutate both dark and light mode styles.
///
/// You can also change this using [`Self::global_style_mut`].
///
/// You can use [`Ui::style_mut`] to change the style of a single [`Ui`].
pub fn set_global_style(&self, style: impl Into<Arc<Style>>) {
self.options_mut(|opt| *opt.style_mut() = style.into());
}
/// The currently active [`Style`] used by all new popups, menus, etc.
///
/// Use [`Self::all_styles_mut`] to mutate both dark and light mode styles.
///
/// You can also change this using [`Self::style_mut`].
///
/// You can use [`Ui::style_mut`] to change the style of a single [`Ui`].
#[deprecated = "Renamed to `set_global_style` to avoid confusion with `ui.set_style()`"]
pub fn set_style(&self, style: impl Into<Arc<Style>>) {
self.options_mut(|opt| *opt.style_mut() = style.into());
}
/// Mutate the [`Style`]s used by all subsequent windows, panels etc. in both dark and light mode.
/// Mutate the [`Style`]s used by all subsequent popups, menus, etc. in both dark and light mode.
///
/// Example:
/// ```
@@ -2115,7 +2148,7 @@ impl Context {
});
}
/// The [`Style`] used by all subsequent windows, panels etc.
/// The [`Style`] used by all subsequent popups, menus, etc.
pub fn style_of(&self, theme: Theme) -> Arc<Style> {
self.options(|opt| match theme {
Theme::Dark => opt.dark_style.clone(),
@@ -2123,7 +2156,7 @@ impl Context {
})
}
/// Mutate the [`Style`] used by all subsequent windows, panels etc.
/// Mutate the [`Style`] used by all subsequent popups, menus, etc.
///
/// Example:
/// ```
@@ -2139,7 +2172,7 @@ impl Context {
});
}
/// The [`Style`] used by all new windows, panels etc.
/// The [`Style`] used by all new popups, menus, etc.
/// Use [`Self::set_theme`] to choose between dark and light mode.
///
/// You can also change this using [`Self::style_mut_of`].
@@ -2153,7 +2186,7 @@ impl Context {
});
}
/// The [`crate::Visuals`] used by all subsequent windows, panels etc.
/// The [`crate::Visuals`] used by all subsequent popups, menus, etc.
///
/// You can also use [`Ui::visuals_mut`] to change the visuals of a single [`Ui`].
///
@@ -2166,7 +2199,7 @@ impl Context {
self.style_mut_of(theme, |style| style.visuals = visuals);
}
/// The [`crate::Visuals`] used by all subsequent windows, panels etc.
/// The [`crate::Visuals`] used by all subsequent popups, menus, etc.
///
/// You can also use [`Ui::visuals_mut`] to change the visuals of a single [`Ui`].
///
@@ -2385,7 +2418,7 @@ impl Context {
}
};
if self.style().debug.show_interactive_widgets {
if self.global_style().debug.show_interactive_widgets {
// Show all interactive widgets:
let rects = self.write(|ctx| ctx.viewport().this_pass.widgets.clone());
for (layer_id, rects) in rects.layers() {
@@ -2463,7 +2496,7 @@ impl Context {
}
}
if self.style().debug.show_widget_hits {
if self.global_style().debug.show_widget_hits {
let hits = self.write(|ctx| ctx.viewport().hits.clone());
let WidgetHits {
close,
@@ -3035,7 +3068,7 @@ impl Context {
/// The animation time is taken from [`Style::animation_time`].
#[track_caller] // To track repaint cause
pub fn animate_bool(&self, id: Id, value: bool) -> f32 {
let animation_time = self.style().animation_time;
let animation_time = self.global_style().animation_time;
self.animate_bool_with_time_and_easing(id, value, animation_time, emath::easing::linear)
}
@@ -3051,7 +3084,7 @@ impl Context {
/// Like [`Self::animate_bool`] but allows you to control the easing function.
#[track_caller] // To track repaint cause
pub fn animate_bool_with_easing(&self, id: Id, value: bool, easing: fn(f32) -> f32) -> f32 {
let animation_time = self.style().animation_time;
let animation_time = self.global_style().animation_time;
self.animate_bool_with_time_and_easing(id, value, animation_time, easing)
}

View File

@@ -99,7 +99,7 @@ impl DebugTextPlugin {
let available_width = ctx.content_rect().max.x - pos.x;
let galley = text.into_galley_impl(
ctx,
&ctx.style(),
&ctx.global_style(),
text::TextWrapping::wrap_at_width(available_width),
font_id.clone().into(),
Align::TOP,

View File

@@ -186,7 +186,7 @@ fn menu_popup<'c, R>(
.kind(UiKind::Menu)
.order(Order::Foreground)
.fixed_pos(pos)
.default_width(ctx.style().spacing.menu_width)
.default_width(ctx.global_style().spacing.menu_width)
.sense(Sense::hover());
let mut sizing_pass = false;
@@ -395,9 +395,9 @@ impl MenuRoot {
// or button hovered while other menu is open
let mut pos = button.rect.left_bottom();
let menu_frame = Frame::menu(&button.ctx.style());
let menu_frame = Frame::menu(&button.ctx.global_style());
pos.x -= menu_frame.total_margin().left; // Make fist button in menu align with the parent button
pos.y += button.ctx.style().spacing.menu_spacing;
pos.y += button.ctx.global_style().spacing.menu_spacing;
if let Some(root) = root.inner.as_mut() {
let menu_rect = root.menu_state.read().rect;

View File

@@ -322,7 +322,7 @@ impl Painter {
}
pub fn error(&self, pos: Pos2, text: impl std::fmt::Display) -> Rect {
let color = self.ctx.style().visuals.error_fg_color;
let color = self.ctx.global_style().visuals.error_fg_color;
self.debug_text(pos, Align2::LEFT_TOP, color, format!("🔥 {text}"))
}

View File

@@ -95,7 +95,7 @@ impl DebugRect {
// Paint rectangle around widget:
{
// Print width and height:
let text_color = if ctx.style().visuals.dark_mode {
let text_color = if ctx.global_style().visuals.dark_mode {
Color32::WHITE
} else {
Color32::BLACK

View File

@@ -761,7 +761,7 @@ impl Response {
/// # });
/// ```
pub fn scroll_to_me(&self, align: Option<Align>) {
self.scroll_to_me_animation(align, self.ctx.style().scroll_animation);
self.scroll_to_me_animation(align, self.ctx.global_style().scroll_animation);
}
/// Like [`Self::scroll_to_me`], but allows you to specify the [`crate::style::ScrollAnimation`].

View File

@@ -136,7 +136,7 @@ impl Ui {
let clip_rect = max_rect;
let layout = layout.unwrap_or_default();
let disabled = disabled || invisible;
let style = style.unwrap_or_else(|| ctx.style());
let style = style.unwrap_or_else(|| ctx.global_style());
let sense = sense.unwrap_or_else(Sense::hover);
let placer = Placer::new(max_rect, layout);
@@ -438,7 +438,7 @@ impl Ui {
/// Reset to the default style set in [`Context`].
pub fn reset_style(&mut self) {
self.style = self.ctx().style();
self.style = self.ctx().global_style();
}
/// The current spacing options for this [`Ui`].

View File

@@ -378,7 +378,7 @@ fn color_picker_hsvag_2d(ui: &mut Ui, hsvag: &mut HsvaGamma, alpha: Alpha) {
}
fn input_type_button_ui(ui: &mut Ui) {
let mut input_type = ui.ctx().style().visuals.numeric_color_space;
let mut input_type = ui.global_style().visuals.numeric_color_space;
if input_type.toggle_button_ui(ui).changed() {
ui.ctx().all_styles_mut(|s| {
s.visuals.numeric_color_space = input_type;

View File

@@ -222,10 +222,10 @@ fn syntax_highlighting(
) -> Option<ColoredText> {
let extension_and_rest: Vec<&str> = response.url.rsplitn(2, '.').collect();
let extension = extension_and_rest.first()?;
let theme = egui_extras::syntax_highlighting::CodeTheme::from_style(&ctx.style());
let theme = egui_extras::syntax_highlighting::CodeTheme::from_style(&ctx.global_style());
Some(ColoredText(egui_extras::syntax_highlighting::highlight(
ctx,
&ctx.style(),
&ctx.global_style(),
&theme,
text,
extension,

View File

@@ -94,7 +94,7 @@ impl BackendPanel {
self.egui_windows.checkboxes(ui);
#[cfg(debug_assertions)]
if ui.ctx().style().debug.debug_on_hover_with_all_modifiers {
if ui.global_style().debug.debug_on_hover_with_all_modifiers {
ui.separator();
ui.label("Press down all modifiers and hover a widget to see a callstack for it");
}

View File

@@ -406,7 +406,7 @@ impl WrapApp {
if is_mobile(ui.ctx()) {
ui.menu_button("💻 Backend", |ui| {
ui.set_style(ui.ctx().style()); // ignore the "menu" style set by `menu_button`.
ui.set_style(ui.global_style()); // ignore the "menu" style set by `menu_button`.
self.backend_panel_contents(ui, frame, cmd);
});
} else {
@@ -474,7 +474,7 @@ impl WrapApp {
content_rect.center(),
Align2::CENTER_CENTER,
text,
TextStyle::Heading.resolve(&ctx.style()),
TextStyle::Heading.resolve(&ctx.global_style()),
Color32::WHITE,
);
}

View File

@@ -114,7 +114,7 @@ impl ScrollAppearance {
visibility,
} = self;
let mut scroll = ui.ctx().style().spacing.scroll;
let mut scroll = ui.global_style().spacing.scroll;
scroll.ui(ui);

View File

@@ -66,7 +66,7 @@ fn highlight_inner(
settings: Option<HighlightSettings<'_>>,
) -> LayoutJob {
// We take in both context and style so that in situations where ui is not available such as when
// performing it at a separate thread (ctx, ctx.style()) can be used and when ui is available
// performing it at a separate thread (ctx, ctx.global_style()) can be used and when ui is available
// (ui.ctx(), ui.style()) can be used
#[expect(non_local_definitions)]
@@ -301,7 +301,7 @@ impl CodeTheme {
///
/// There is one dark and one light theme stored at any one time.
pub fn store_in_memory(self, ctx: &egui::Context) {
let id = if ctx.style().visuals.dark_mode {
let id = if ctx.global_style().visuals.dark_mode {
egui::Id::new("dark")
} else {
egui::Id::new("light")

View File

@@ -141,7 +141,7 @@ fn window_children() {
fn accesskit_output_single_egui_frame(run_ui: impl FnMut(&Context)) -> TreeUpdate {
let ctx = Context::default();
// Disable animations, so we do not need to wait for animations to end to see the result.
ctx.style_mut(|style| style.animation_time = 0.0);
ctx.global_style_mut(|style| style.animation_time = 0.0);
ctx.enable_accesskit();
let output = ctx.run(RawInput::default(), run_ui);

View File

@@ -181,7 +181,7 @@ impl Keypad {
let is_first_show = ctx.wants_keyboard_input() && state.focus != focus;
if is_first_show {
let y = ctx.style().spacing.interact_size.y * 1.25;
let y = ctx.global_style().spacing.interact_size.y * 1.25;
state.open = true;
state.start_pos = ctx.input(|i| {
i.pointer

View File

@@ -46,9 +46,9 @@ fn custom_window_frame(ctx: &egui::Context, title: &str, add_contents: impl FnOn
use egui::{CentralPanel, UiBuilder};
let panel_frame = egui::Frame::new()
.fill(ctx.style().visuals.window_fill())
.fill(ctx.global_style().visuals.window_fill())
.corner_radius(10)
.stroke(ctx.style().visuals.widgets.noninteractive.fg_stroke)
.stroke(ctx.global_style().visuals.widgets.noninteractive.fg_stroke)
.outer_margin(1); // so the stroke is within the bounds
CentralPanel::default().frame(panel_frame).show(ctx, |ui| {

View File

@@ -113,7 +113,7 @@ fn preview_files_being_dropped(ctx: &egui::Context) {
content_rect.center(),
Align2::CENTER_CENTER,
text,
TextStyle::Heading.resolve(&ctx.style()),
TextStyle::Heading.resolve(&ctx.global_style()),
Color32::WHITE,
);
}