Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make icons unicode-aware #621

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading