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

Use explicit Arc::clone to clarify when clones are cheap (#7784)

This commit is contained in:
Emil Ernerfeldt
2025-12-17 17:19:18 +01:00
committed by GitHub
parent 6157a35985
commit 986c2c0ffb
40 changed files with 80 additions and 71 deletions

View File

@@ -1274,7 +1274,7 @@ impl TitleBar {
let text_pos = text_pos - self.title_galley.rect.min.to_vec2();
ui.painter().galley(
text_pos,
self.title_galley.clone(),
Arc::clone(&self.title_galley),
ui.visuals().text_color(),
);

View File

@@ -1959,7 +1959,7 @@ impl Context {
pub fn add_plugin(&self, plugin: impl plugin::Plugin + 'static) {
let handle = plugin::PluginHandle::new(plugin);
let added = self.write(|ctx| ctx.plugins.add(handle.clone()));
let added = self.write(|ctx| ctx.plugins.add(Arc::clone(&handle)));
if added {
handle.lock().dyn_plugin_mut().setup(self);
@@ -2085,13 +2085,13 @@ impl Context {
/// The currently active [`Style`] used by all subsequent popups, menus, etc.
pub fn global_style(&self) -> Arc<Style> {
self.options(|opt| opt.style().clone())
self.options(|opt| Arc::clone(opt.style()))
}
/// 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())
self.options(|opt| Arc::clone(opt.style()))
}
/// Mutate the currently active [`Style`] used by all subsequent popups, menus, etc.
@@ -2165,8 +2165,8 @@ impl Context {
/// 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(),
Theme::Light => opt.light_style.clone(),
Theme::Dark => Arc::clone(&opt.dark_style),
Theme::Light => Arc::clone(&opt.light_style),
})
}
@@ -2360,7 +2360,7 @@ impl Context {
///
/// You can show stats about the allocated textures using [`Self::texture_ui`].
pub fn tex_manager(&self) -> Arc<RwLock<epaint::textures::TextureManager>> {
self.read(|ctx| ctx.tex_manager.0.clone())
self.read(|ctx| Arc::clone(&ctx.tex_manager.0))
}
// ---------------------------------------------------------------------
@@ -3899,7 +3899,7 @@ impl Context {
/// The loaders of bytes, images, and textures.
pub fn loaders(&self) -> Arc<Loaders> {
self.read(|this| this.loaders.clone())
self.read(|this| Arc::clone(&this.loaders))
}
/// Returns `true` if any image is currently being loaded.

View File

@@ -97,11 +97,7 @@ impl DragAndDrop {
where
Payload: Any + Send + Sync,
{
ctx.plugin::<Self>()
.lock()
.payload
.as_ref()?
.clone()
Arc::clone(ctx.plugin::<Self>().lock().payload.as_ref()?)
.downcast()
.ok()
}

View File

@@ -1,3 +1,5 @@
use std::sync::Arc;
use emath::GuiRounding as _;
use crate::{
@@ -102,7 +104,7 @@ impl GridLayout {
Self {
ctx: ui.ctx().clone(),
style: ui.style().clone(),
style: Arc::clone(ui.style()),
id,
is_first_frame,
prev_state,

View File

@@ -609,7 +609,7 @@ impl Default for Loaders {
fn default() -> Self {
let include = Arc::new(DefaultBytesLoader::default());
Self {
bytes: Mutex::new(vec![include.clone()]),
bytes: Mutex::new(vec![Arc::clone(&include) as _]),
image: Mutex::new(Vec::new()),
// By default we only include `DefaultTextureLoader`.
texture: Mutex::new(vec![Arc::new(DefaultTextureLoader::default())]),

View File

@@ -198,7 +198,7 @@ fn menu_popup<'c, R>(
Frame::menu(ui.style())
.show(ui, |ui| {
ui.set_menu_state(Some(menu_state_arc.clone()));
ui.set_menu_state(Some(Arc::clone(menu_state_arc)));
ui.with_layout(Layout::top_down_justified(Align::LEFT), add_contents)
.inner
})

View File

@@ -202,7 +202,7 @@ impl Plugins {
return false;
}
self.plugins.insert(type_id, handle.clone());
self.plugins.insert(type_id, Arc::clone(&handle));
self.plugins_ordered.0.push(handle);
true

View File

@@ -275,7 +275,7 @@ impl Ui {
painter.set_invisible();
}
let sizing_pass = self.sizing_pass || sizing_pass;
let style = style.unwrap_or_else(|| self.style.clone());
let style = style.unwrap_or_else(|| Arc::clone(&self.style));
let sense = sense.unwrap_or_else(Sense::hover);
if sizing_pass {
@@ -305,7 +305,7 @@ impl Ui {
id: unique_id,
layout_direction: layout.main_dir,
info: ui_stack_info,
parent: Some(self.stack.clone()),
parent: Some(Arc::clone(&self.stack)),
min_rect: placer.min_rect(),
max_rect: placer.max_rect(),
};

View File

@@ -787,7 +787,7 @@ impl ViewportBuilder {
};
if is_new {
commands.push(ViewportCommand::Icon(Some(new_icon.clone())));
commands.push(ViewportCommand::Icon(Some(Arc::clone(&new_icon))));
self.icon = Some(new_icon);
}
}

View File

@@ -709,7 +709,7 @@ impl WidgetText {
default_valign,
)),
Self::LayoutJob(job) => job,
Self::Galley(galley) => galley.job.clone(),
Self::Galley(galley) => Arc::clone(&galley.job),
}
}

View File

@@ -759,7 +759,7 @@ impl TextEdit<'_> {
ui.skip_ahead_auto_ids(1);
}
painter.galley(galley_pos, galley.clone(), text_color);
painter.galley(galley_pos, Arc::clone(&galley), text_color);
if has_focus && let Some(cursor_range) = state.cursor.range(&galley) {
let primary_cursor_rect = cursor_rect(&galley, &cursor_range.primary, row_height)