Skip to content

Commit

Permalink
Merge pull request #621 from woelper/icon_fix
Browse files Browse the repository at this point in the history
fix: make icons unicode-aware
  • Loading branch information
woelper authored Jan 19, 2025
2 parents a4b2669 + b8d8bb4 commit 0da6296
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ quantette = { version = "0.3.0", features = ["threads"] }
sysinfo = "0.33.1"
dicom-pixeldata = { version = "0.8.0", features = ["image"] }
dicom-object = "0.8.0"
unicode-segmentation = "1.12.0"

[features]
default = [
Expand Down
58 changes: 43 additions & 15 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,16 @@ impl EguiExt for Ui {
response
}

/// Draw a justified icon from a string starting with an emoji
/// Draw a justified icon from a string containing an emoji
fn label_i(&mut self, text: impl Into<WidgetText>) -> Response {
let text: WidgetText = text.into();
let text = text.text();

let icon = text.chars().filter(|c| !c.is_ascii()).collect::<String>();
let description = text.chars().filter(|c| c.is_ascii()).collect::<String>();
let (icon, description) = parse_icon_plus_text(text);
let icon = icon.unwrap_or_default();

self.with_layout(egui::Layout::left_to_right(Align::Center), |ui| {
// self.horizontal(|ui| {
ui.add(
// egui::Vec2::new(8., ui.available_height()),
egui::Label::new(RichText::new(icon).color(ui.style().visuals.selection.bg_fill)),
);
ui.label(
Expand All @@ -243,8 +241,9 @@ impl EguiExt for Ui {
let text: WidgetText = title.into();
let text = text.text();

let icon = text.chars().filter(|c| !c.is_ascii()).collect::<String>();
let description = text.chars().filter(|c| c.is_ascii()).collect::<String>();
let (icon, description) = parse_icon_plus_text(text);
let icon = icon.unwrap_or_default();

let spacing = if icon.is_empty() { "" } else { " " };
self.spacing_mut().button_padding = Vec2::new(0., 10.);

Expand All @@ -269,8 +268,8 @@ impl EguiExt for Ui {
let text: WidgetText = text.into();
let text = text.text();

let icon = text.chars().filter(|c| !c.is_ascii()).collect::<String>();
let description = text.chars().filter(|c| c.is_ascii()).collect::<String>();
let (icon, description) = parse_icon_plus_text(text);
let icon = icon.unwrap_or_default();

let spacing = if icon.is_empty() { "" } else { " " };
let r = self.add(
Expand Down Expand Up @@ -298,10 +297,11 @@ impl EguiExt for Ui {
let text = text.text();

let icon_size = 12.;
let icon = text.chars().filter(|c| !c.is_ascii()).collect::<String>();
let description = text.chars().filter(|c| c.is_ascii()).collect::<String>();
self.spacing_mut().button_padding = Vec2::new(8., 0.);

let (icon, description) = parse_icon_plus_text(text);
let icon = icon.unwrap_or_default();

self.spacing_mut().button_padding = Vec2::new(8., 0.);
let spacing = if icon.is_empty() { "" } else { " " };
let r = self.add(
egui::Button::new(format!("{description}{spacing}"))
Expand All @@ -322,7 +322,7 @@ impl EguiExt for Ui {
r
}

/// Draw a justified icon from a string starting with an emoji
/// Draw a right justified label
fn label_right(&mut self, text: impl Into<WidgetText>) -> Response {
self.with_layout(egui::Layout::right_to_left(Align::Center), |ui| {
ui.label(text);
Expand All @@ -348,8 +348,10 @@ impl EguiExt for Ui {
let text: WidgetText = text.into();
let text = text.text();

let icon = text.chars().filter(|c| !c.is_ascii()).collect::<String>();
let description = text.chars().filter(|c| c.is_ascii()).collect::<String>();
let (icon, description) = parse_icon_plus_text(text);
let icon = icon.unwrap_or_default();

// let description = text.chars().filter(|c| c.is_ascii()).collect::<String>();
self.horizontal(|ui| {
let mut r = ui.add_sized(
egui::Vec2::new(30., ui.available_height()),
Expand Down Expand Up @@ -461,6 +463,32 @@ impl EguiExt for Ui {
}
}


fn parse_icon_plus_text(line: &str) -> (Option<String>, String) {
use unicode_segmentation::UnicodeSegmentation;

let trimmed = line.trim();

// 1) Check if the first token is exactly 1 grapheme
if let Some((candidate, remainder)) = trimmed.split_once(' ') {
if candidate.graphemes(true).count() == 1 {
// icon at the front
return (Some(candidate.to_owned()), remainder.to_owned());
}
}

// 2) Otherwise, check from the right for a trailing icon
if let Some((remainder, candidate)) = trimmed.rsplit_once(' ') {
if candidate.graphemes(true).count() == 1 {
// icon at the end
return (Some(candidate.to_owned()), remainder.to_owned());
}
}

// 3) No icon found
(None, trimmed.to_owned())
}

/// Proof-of-concept funtion to draw texture completely with egui
#[allow(unused)]
pub fn image_ui(ctx: &Context, state: &mut OculanteState, gfx: &mut Graphics) {
Expand Down

0 comments on commit 0da6296

Please sign in to comment.