notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

keybind_hint.rs (2438B)


      1 use egui::{Pos2, Rect, Response, Sense, Ui, Vec2};
      2 
      3 /// A visual keybinding hint - a small framed box with a letter or number inside.
      4 /// Used to indicate keyboard shortcuts in the UI.
      5 pub struct KeybindHint<'a> {
      6     text: &'a str,
      7     size: f32,
      8 }
      9 
     10 impl<'a> KeybindHint<'a> {
     11     /// Create a new keybinding hint with the given text
     12     pub fn new(text: &'a str) -> Self {
     13         Self { text, size: 18.0 }
     14     }
     15 
     16     /// Set the size of the hint box (default: 18.0)
     17     pub fn size(mut self, size: f32) -> Self {
     18         self.size = size;
     19         self
     20     }
     21 
     22     /// Show the keybinding hint and return the response
     23     pub fn show(self, ui: &mut Ui) -> Response {
     24         let (rect, response) = ui.allocate_exact_size(Vec2::splat(self.size), Sense::hover());
     25         self.paint(ui, rect);
     26         response
     27     }
     28 
     29     /// Paint the keybinding hint at a specific position (for use with painters)
     30     pub fn paint_at(self, ui: &Ui, center: Pos2) {
     31         let rect = Rect::from_center_size(center, Vec2::splat(self.size));
     32         self.paint(ui, rect);
     33     }
     34 
     35     fn paint(self, ui: &Ui, rect: Rect) {
     36         let painter = ui.painter();
     37         let visuals = ui.visuals();
     38 
     39         // Frame/border
     40         let stroke_color = visuals.widgets.noninteractive.fg_stroke.color;
     41         let bg_color = visuals.widgets.noninteractive.bg_fill;
     42         let corner_radius = 3.0;
     43 
     44         // Background fill
     45         painter.rect_filled(rect, corner_radius, bg_color);
     46 
     47         // Border stroke
     48         painter.rect_stroke(
     49             rect,
     50             corner_radius,
     51             egui::Stroke::new(1.0, stroke_color.gamma_multiply(0.6)),
     52             egui::StrokeKind::Inside,
     53         );
     54 
     55         // Text in center (slight vertical nudge for better optical centering)
     56         let font_size = self.size * 0.65;
     57         let text_pos = rect.center() + Vec2::new(0.0, 2.0);
     58         painter.text(
     59             text_pos,
     60             egui::Align2::CENTER_CENTER,
     61             self.text,
     62             egui::FontId::monospace(font_size),
     63             visuals.text_color(),
     64         );
     65     }
     66 }
     67 
     68 /// Draw a keybinding hint inline (for use in horizontal layouts)
     69 pub fn keybind_hint(ui: &mut Ui, text: &str) -> Response {
     70     KeybindHint::new(text).show(ui)
     71 }
     72 
     73 /// Draw a keybinding hint at a specific position using the painter
     74 pub fn paint_keybind_hint(ui: &Ui, center: Pos2, text: &str, size: f32) {
     75     KeybindHint::new(text).size(size).paint_at(ui, center);
     76 }