Skip to content

Commit 6e34152

Browse files
authored
Add Context::format_modifiers (#7125)
Convenience
1 parent 53098fa commit 6e34152

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

crates/egui/src/context.rs

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ use crate::{
2121
input_state::{InputState, MultiTouchInfo, PointerEvent},
2222
interaction,
2323
layers::GraphicLayers,
24-
load,
25-
load::{Bytes, Loaders, SizedTexture},
24+
load::{self, Bytes, Loaders, SizedTexture},
2625
memory::{Options, Theme},
2726
os::OperatingSystem,
2827
output::FullOutput,
@@ -32,10 +31,10 @@ use crate::{
3231
viewport::ViewportClass,
3332
Align2, CursorIcon, DeferredViewportUiCallback, FontDefinitions, Grid, Id, ImmediateViewport,
3433
ImmediateViewportRendererCallback, Key, KeyboardShortcut, Label, LayerId, Memory,
35-
ModifierNames, NumExt as _, Order, Painter, RawInput, Response, RichText, ScrollArea, Sense,
36-
Style, TextStyle, TextureHandle, TextureOptions, Ui, ViewportBuilder, ViewportCommand,
37-
ViewportId, ViewportIdMap, ViewportIdPair, ViewportIdSet, ViewportOutput, Widget as _,
38-
WidgetRect, WidgetText,
34+
ModifierNames, Modifiers, NumExt as _, Order, Painter, RawInput, Response, RichText,
35+
ScrollArea, Sense, Style, TextStyle, TextureHandle, TextureOptions, Ui, ViewportBuilder,
36+
ViewportCommand, ViewportId, ViewportIdMap, ViewportIdPair, ViewportIdSet, ViewportOutput,
37+
Widget as _, WidgetRect, WidgetText,
3938
};
4039

4140
#[cfg(feature = "accesskit")]
@@ -1484,35 +1483,48 @@ impl Context {
14841483
self.send_cmd(crate::OutputCommand::CopyImage(image));
14851484
}
14861485

1486+
fn can_show_modifier_symbols(&self) -> bool {
1487+
let ModifierNames {
1488+
alt,
1489+
ctrl,
1490+
shift,
1491+
mac_cmd,
1492+
..
1493+
} = ModifierNames::SYMBOLS;
1494+
1495+
let font_id = TextStyle::Body.resolve(&self.style());
1496+
self.fonts(|f| {
1497+
let mut lock = f.lock();
1498+
let font = lock.fonts.font(&font_id);
1499+
font.has_glyphs(alt)
1500+
&& font.has_glyphs(ctrl)
1501+
&& font.has_glyphs(shift)
1502+
&& font.has_glyphs(mac_cmd)
1503+
})
1504+
}
1505+
1506+
/// Format the given modifiers in a human-readable way (e.g. `Ctrl+Shift+X`).
1507+
pub fn format_modifiers(&self, modifiers: Modifiers) -> String {
1508+
let os = self.os();
1509+
1510+
let is_mac = os.is_mac();
1511+
1512+
if is_mac && self.can_show_modifier_symbols() {
1513+
ModifierNames::SYMBOLS.format(&modifiers, is_mac)
1514+
} else {
1515+
ModifierNames::NAMES.format(&modifiers, is_mac)
1516+
}
1517+
}
1518+
14871519
/// Format the given shortcut in a human-readable way (e.g. `Ctrl+Shift+X`).
14881520
///
14891521
/// Can be used to get the text for [`crate::Button::shortcut_text`].
14901522
pub fn format_shortcut(&self, shortcut: &KeyboardShortcut) -> String {
14911523
let os = self.os();
14921524

1493-
let is_mac = matches!(os, OperatingSystem::Mac | OperatingSystem::IOS);
1494-
1495-
let can_show_symbols = || {
1496-
let ModifierNames {
1497-
alt,
1498-
ctrl,
1499-
shift,
1500-
mac_cmd,
1501-
..
1502-
} = ModifierNames::SYMBOLS;
1503-
1504-
let font_id = TextStyle::Body.resolve(&self.style());
1505-
self.fonts(|f| {
1506-
let mut lock = f.lock();
1507-
let font = lock.fonts.font(&font_id);
1508-
font.has_glyphs(alt)
1509-
&& font.has_glyphs(ctrl)
1510-
&& font.has_glyphs(shift)
1511-
&& font.has_glyphs(mac_cmd)
1512-
})
1513-
};
1525+
let is_mac = os.is_mac();
15141526

1515-
if is_mac && can_show_symbols() {
1527+
if is_mac && self.can_show_modifier_symbols() {
15161528
shortcut.format(&ModifierNames::SYMBOLS, is_mac)
15171529
} else {
15181530
shortcut.format(&ModifierNames::NAMES, is_mac)

0 commit comments

Comments
 (0)