rerun icon indicating copy to clipboard operation
rerun copied to clipboard

Move `re_ui` UI functions to a trait to be implemented by `egui::Ui`

Open abey79 opened this issue 2 years ago • 2 comments

Most UI function in re_ui are #[allow(clippy::unused_self)], but they take a egui::Ui argument (obviously). This could be more cleanly implemented as a trait that would be implemented for egui::Ui.

Something like:

pub trait ReUi {
  fn ui(&mut self) -> &mut egui::Ui;

  fn small_icon_button(&self, icon: &Icon) -> egui::Response {
        ui.add(
            egui::ImageButton::new(icon.as_image().fit_to_exact_size(Self::small_icon_size()))
                .tint(self.ui().visuals().widgets.inactive.fg_stroke.color),
        )
    }

    // etc.
}

impl ReUi for egui::Ui {
  fn ui(&mut self) -> &mut egui::Ui { self }
}

Before (typical):

ctx.re_ui.small_icon_button(ui, icon);

After:

use re_ui::ReUi as _;

ui.small_icon_button(icon);

abey79 avatar Dec 18 '23 08:12 abey79

be mindful that some of those functions are only NOT using self because they are using hard-coded values instead of the design tokens json

emilk avatar Dec 18 '23 14:12 emilk

I'm getting tired of passing around ReUi everywhere, so perhaps it's time to do this.

One thing though: struct ReUi currently store the DesignTokens it uses in some of its member functions. We would need to store that somewhere else, e.g. in egui's data store: ui.data(|data| data.get_temp::<Arc<DesignTokens>>())

emilk avatar May 02 '24 07:05 emilk