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

Replace ui.ctx().foo with ui.foo in a few places (#7774)

Internal code cleanup after
* https://github.com/emilk/egui/pull/7770
This commit is contained in:
Emil Ernerfeldt
2025-12-14 17:12:10 +01:00
committed by GitHub
parent 453e34b4df
commit bfaf1b44f2
53 changed files with 108 additions and 128 deletions

View File

@@ -69,7 +69,7 @@ impl CollapsingState {
pub fn toggle(&mut self, ui: &Ui) {
self.state.open = !self.state.open;
ui.ctx().request_repaint();
ui.request_repaint();
}
/// 0 for closed, 1 for open, with tweening

View File

@@ -207,7 +207,7 @@ impl MenuState {
/// egui::MenuBar::new().ui(ui, |ui| {
/// ui.menu_button("File", |ui| {
/// if ui.button("Quit").clicked() {
/// ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close);
/// ui.send_viewport_cmd(egui::ViewportCommand::Close);
/// }
/// });
/// });
@@ -556,7 +556,7 @@ impl SubMenu {
if is_moving_towards_rect {
// We need to repaint while this is true, so we can detect when
// the pointer is no longer moving towards the rect
ui.ctx().request_repaint();
ui.request_repaint();
}
let hovering_other_menu_entry = is_open
&& !is_hovered

View File

@@ -713,7 +713,7 @@ impl Panel {
}
if resize_hover || is_resizing {
ui.ctx().set_cursor_icon(self.cursor_icon(&panel_sizer));
ui.set_cursor_icon(self.cursor_icon(&panel_sizer));
}
PanelState { rect }.store(ui.ctx(), id);

View File

@@ -213,7 +213,7 @@ impl Resize {
});
let mut state = State::load(ui.ctx(), id).unwrap_or_else(|| {
ui.ctx().request_repaint(); // counter frame delay
ui.request_repaint(); // counter frame delay
let default_size = self
.default_size
@@ -362,7 +362,7 @@ impl Resize {
paint_resize_corner(ui, &corner_response);
if corner_response.hovered() || corner_response.dragged() {
ui.ctx().set_cursor_icon(CursorIcon::ResizeNwSe);
ui.set_cursor_icon(CursorIcon::ResizeNwSe);
}
}
@@ -370,12 +370,12 @@ impl Resize {
#[cfg(debug_assertions)]
if ui.global_style().debug.show_resize {
ui.ctx().debug_painter().debug_rect(
ui.debug_painter().debug_rect(
Rect::from_min_size(content_ui.min_rect().left_top(), state.desired_size),
Color32::GREEN,
"desired_size",
);
ui.ctx().debug_painter().debug_rect(
ui.debug_painter().debug_rect(
Rect::from_min_size(content_ui.min_rect().left_top(), state.last_content_size),
Color32::LIGHT_BLUE,
"last_content_size",

View File

@@ -244,8 +244,8 @@ impl Scene {
&& resp.contains_pointer()
{
let pointer_in_scene = to_global.inverse() * mouse_pos;
let zoom_delta = ui.ctx().input(|i| i.zoom_delta());
let pan_delta = ui.ctx().input(|i| i.smooth_scroll_delta());
let zoom_delta = ui.input(|i| i.zoom_delta());
let pan_delta = ui.input(|i| i.smooth_scroll_delta());
// Most of the time we can return early. This is also important to
// avoid `ui_from_scene` to change slightly due to floating point errors.

View File

@@ -856,11 +856,11 @@ impl ScrollArea {
if response.dragged()
&& let Some(cursor) = on_drag_cursor
{
ui.ctx().set_cursor_icon(cursor);
ui.set_cursor_icon(cursor);
} else if response.hovered()
&& let Some(cursor) = on_hover_cursor
{
ui.ctx().set_cursor_icon(cursor);
ui.set_cursor_icon(cursor);
}
}
@@ -1125,7 +1125,7 @@ impl Prepared {
target_offset,
});
}
ui.ctx().request_repaint();
ui.request_repaint();
}
}
}
@@ -1178,7 +1178,7 @@ impl Prepared {
&& direction_enabled[0] != direction_enabled[1];
for d in 0..2 {
if direction_enabled[d] {
let scroll_delta = ui.ctx().input(|input| {
let scroll_delta = ui.input(|input| {
if always_scroll_enabled_direction {
// no bidirectional scrolling; allow horizontal scrolling without pressing shift
input.smooth_scroll_delta()[0] + input.smooth_scroll_delta()[1]
@@ -1195,7 +1195,7 @@ impl Prepared {
state.offset[d] -= scroll_delta;
// Clear scroll delta so no parent scroll will use it:
ui.ctx().input_mut(|input| {
ui.input_mut(|input| {
if always_scroll_enabled_direction {
input.smooth_scroll_delta()[0] = 0.0;
input.smooth_scroll_delta()[1] = 0.0;
@@ -1475,7 +1475,7 @@ impl Prepared {
ui.advance_cursor_after_rect(outer_rect);
if show_scroll_this_frame != state.show_scroll {
ui.ctx().request_repaint();
ui.request_repaint();
}
let available_offset = content_size - inner_rect.size();

View File

@@ -1143,8 +1143,7 @@ impl TitleBar {
title_bar_height_with_margin: f32,
) -> Self {
if false {
ui.ctx()
.debug_painter()
ui.debug_painter()
.debug_rect(ui.min_rect(), Color32::GREEN, "outer_min_rect");
}
@@ -1172,8 +1171,7 @@ impl TitleBar {
let min_rect = Rect::from_min_size(ui.min_rect().min, min_inner_size);
if false {
ui.ctx()
.debug_painter()
ui.debug_painter()
.debug_rect(min_rect, Color32::LIGHT_BLUE, "min_rect");
}
@@ -1210,8 +1208,7 @@ impl TitleBar {
let title_inner_rect = self.inner_rect;
if false {
ui.ctx()
.debug_painter()
ui.debug_painter()
.debug_rect(self.inner_rect, Color32::RED, "TitleBar");
}
@@ -1250,8 +1247,7 @@ impl TitleBar {
// Paint separator between title and content:
let content_rect = content_response.rect;
if false {
ui.ctx()
.debug_painter()
ui.debug_painter()
.debug_rect(content_rect, Color32::RED, "content_rect");
}
let y = title_inner_rect.bottom() + window_frame.stroke.width / 2.0;
@@ -1265,11 +1261,8 @@ impl TitleBar {
let double_click_rect = title_inner_rect.shrink2(vec2(32.0, 0.0));
if false {
ui.ctx().debug_painter().debug_rect(
double_click_rect,
Color32::GREEN,
"double_click_rect",
);
ui.debug_painter()
.debug_rect(double_click_rect, Color32::GREEN, "double_click_rect");
}
let id = ui.unique_id().with("__window_title_bar");

View File

@@ -1505,7 +1505,7 @@ impl Context {
Painter::new(self.clone(), layer_id, content_rect)
}
/// Paint on top of everything else
/// Paint on top of _everything_ else (even on top of tooltips and popups).
pub fn debug_painter(&self) -> Painter {
Self::layer_painter(self, LayerId::debug())
}
@@ -3554,9 +3554,7 @@ impl Context {
let response =
ui.add(Label::new(RichText::new(text).monospace()).sense(Sense::click()));
if response.hovered() && is_visible {
ui.ctx()
.debug_painter()
.debug_rect(area.rect(), Color32::RED, "");
ui.debug_painter().debug_rect(area.rect(), Color32::RED, "");
}
} else {
ui.monospace(layer_id.short_debug_format());

View File

@@ -449,7 +449,7 @@ impl Grid {
if ui.is_visible() {
// Try to cover up the glitchy initial frame:
ui.ctx().request_discard("new Grid");
ui.request_discard("new Grid");
}
// Hide the ui this frame, and make things as narrow as possible:

View File

@@ -89,7 +89,7 @@ impl ThemePreference {
/// Show radio-buttons to switch between light mode, dark mode and following the system theme.
pub fn radio_buttons(&mut self, ui: &mut crate::Ui) {
ui.horizontal(|ui| {
let system_theme = ui.ctx().input(|i| i.raw.system_theme);
let system_theme = ui.input(|i| i.raw.system_theme);
ui.selectable_value(self, Self::System, "💻 System")
.on_hover_ui(|ui| {

View File

@@ -701,7 +701,7 @@ impl MenuState {
if self.moving_towards_current_submenu(&pointer) {
// We don't close the submenu if the pointer is on its way to hover it.
// ensure to repaint once even when pointer is not moving
ui.ctx().request_repaint();
ui.request_repaint();
} else if !open && button.hovered() {
// TODO(emilk): open menu to the left if there isn't enough space to the right
let mut pos = button.rect.right_top();

View File

@@ -499,7 +499,7 @@ impl LabelSelectionState {
let global_from_galley = global_from_layer * layer_from_galley;
if response.hovered() {
ui.ctx().set_cursor_icon(CursorIcon::Text);
ui.set_cursor_icon(CursorIcon::Text);
}
self.any_hovered |= response.hovered();

View File

@@ -172,7 +172,7 @@ pub fn paint_text_cursor(
total_duration - time_in_cycle
};
ui.ctx().request_repaint_after_secs(wake_in);
ui.request_repaint_after_secs(wake_in);
} else {
paint_cursor_end(painter, ui.visuals(), primary_cursor_rect);
}

View File

@@ -3207,7 +3207,7 @@ fn register_rect(ui: &Ui, rect: Rect) {
// Use the debug-painter to avoid clip rect,
// otherwise the content of the widget may cover what we paint here!
let painter = ui.ctx().debug_painter();
let painter = ui.debug_painter();
if debug.hover_shows_next {
ui.placer.debug_paint_cursor(&painter, "next");

View File

@@ -402,9 +402,9 @@ fn srgba_edit_ui(ui: &mut Ui, [r, g, b, a]: &mut [u8; 4], alpha: Alpha) -> bool
.clicked()
{
if alpha == Alpha::Opaque {
ui.ctx().copy_text(format!("{r}, {g}, {b}"));
ui.copy_text(format!("{r}, {g}, {b}"));
} else {
ui.ctx().copy_text(format!("{r}, {g}, {b}, {a}"));
ui.copy_text(format!("{r}, {g}, {b}, {a}"));
}
}
edited |= DragValue::new(r).speed(0.5).prefix("R ").ui(ui).changed();
@@ -443,10 +443,9 @@ fn rgba_edit_ui(ui: &mut Ui, [r, g, b, a]: &mut [f32; 4], alpha: Alpha) -> bool
.clicked()
{
if alpha == Alpha::Opaque {
ui.ctx().copy_text(format!("{r:.03}, {g:.03}, {b:.03}"));
ui.copy_text(format!("{r:.03}, {g:.03}, {b:.03}"));
} else {
ui.ctx()
.copy_text(format!("{r:.03}, {g:.03}, {b:.03}, {a:.03}"));
ui.copy_text(format!("{r:.03}, {g:.03}, {b:.03}, {a:.03}"));
}
}

View File

@@ -624,7 +624,7 @@ impl Widget for DragValue<'_> {
ui.memory_mut(|mem| mem.request_focus(id));
select_all_text(ui, id, response.id, &value_text);
} else if response.dragged() {
ui.ctx().set_cursor_icon(cursor_icon);
ui.set_cursor_icon(cursor_icon);
let mdelta = response.drag_delta();
let delta_points = mdelta.x - mdelta.y; // Increase to the right and up

View File

@@ -65,7 +65,7 @@ impl Widget for Link {
}
if response.hovered() {
ui.ctx().set_cursor_icon(CursorIcon::PointingHand);
ui.set_cursor_icon(CursorIcon::PointingHand);
}
}
@@ -130,12 +130,12 @@ impl Widget for Hyperlink {
let response = ui.add(Link::new(text));
if response.clicked_with_open_in_background() {
ui.ctx().open_url(crate::OpenUrl {
ui.open_url(crate::OpenUrl {
url: url.clone(),
new_tab: true,
});
} else if response.clicked() {
ui.ctx().open_url(crate::OpenUrl {
ui.open_url(crate::OpenUrl {
url: url.clone(),
new_tab,
});

View File

@@ -143,7 +143,7 @@ pub fn global_theme_preference_switch(ui: &mut Ui) {
/// Show larger buttons for switching between light and dark mode (globally).
pub fn global_theme_preference_buttons(ui: &mut Ui) {
let mut theme_preference = ui.ctx().options(|opt| opt.theme_preference);
let mut theme_preference = ui.options(|opt| opt.theme_preference);
theme_preference.radio_buttons(ui);
ui.ctx().set_theme(theme_preference);
}

View File

@@ -135,7 +135,7 @@ impl Widget for ProgressBar {
if ui.is_rect_visible(response.rect) {
if animate {
ui.ctx().request_repaint();
ui.request_repaint();
}
let visuals = ui.style().visuals.clone();

View File

@@ -687,7 +687,7 @@ impl Slider<'_> {
let mut increment = 0usize;
if response.has_focus() {
ui.ctx().memory_mut(|m| {
ui.memory_mut(|m| {
m.set_focus_lock_filter(
response.id,
EventFilter {

View File

@@ -37,7 +37,7 @@ impl Spinner {
/// Paint the spinner in the given rectangle.
pub fn paint_at(&self, ui: &Ui, rect: Rect) {
if ui.is_rect_visible(rect) {
ui.ctx().request_repaint(); // because it is animated
ui.request_repaint(); // because it is animated
let color = self
.color

View File

@@ -605,12 +605,12 @@ impl TextEdit<'_> {
if did_interact || response.clicked() {
ui.memory_mut(|mem| mem.request_focus(response.id));
state.last_interaction_time = ui.ctx().input(|i| i.time);
state.last_interaction_time = ui.input(|i| i.time);
}
}
if interactive && response.hovered() {
ui.ctx().set_cursor_icon(CursorIcon::Text);
ui.set_cursor_icon(CursorIcon::Text);
}
let mut cursor_range = None;
@@ -768,7 +768,7 @@ impl TextEdit<'_> {
}
if text.is_mutable() && interactive {
let now = ui.ctx().input(|i| i.time);
let now = ui.input(|i| i.time);
if response.changed() || selection_changed {
state.last_interaction_time = now;
}
@@ -777,7 +777,7 @@ impl TextEdit<'_> {
// This is for two reasons:
// * Don't give the impression that the user can type into a window without focus
// * Don't repaint the ui because of a blinking cursor in an app that is not in focus
let viewport_has_focus = ui.ctx().input(|i| i.focused);
let viewport_has_focus = ui.input(|i| i.focused);
if viewport_has_focus {
text_selection::visuals::paint_text_cursor(
ui,
@@ -922,7 +922,7 @@ fn events(
let copy_if_not_password = |ui: &Ui, text: String| {
if !password {
ui.ctx().copy_text(text);
ui.copy_text(text);
}
};