notedeck

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

icons.rs (2969B)


      1 use egui::{vec2, Color32, Stroke};
      2 
      3 use crate::{app_images, AnimationHelper};
      4 
      5 pub static ICON_WIDTH: f32 = 40.0;
      6 pub static ICON_EXPANSION_MULTIPLE: f32 = 1.2;
      7 
      8 /// Creates a magnifying glass icon widget
      9 pub fn search_icon(size: f32, height: f32) -> impl egui::Widget {
     10     move |ui: &mut egui::Ui| {
     11         // Use the provided height parameter
     12         let desired_size = vec2(size, height);
     13         let (rect, response) = ui.allocate_exact_size(desired_size, egui::Sense::hover());
     14 
     15         // Calculate center position - this ensures the icon is centered in its allocated space
     16         let center_pos = rect.center();
     17         let stroke = Stroke::new(1.5, Color32::from_rgb(150, 150, 150));
     18 
     19         // Draw circle
     20         let circle_radius = size * 0.35;
     21         ui.painter()
     22             .circle(center_pos, circle_radius, Color32::TRANSPARENT, stroke);
     23 
     24         // Draw handle
     25         let handle_start = center_pos + vec2(circle_radius * 0.7, circle_radius * 0.7);
     26         let handle_end = handle_start + vec2(size * 0.25, size * 0.25);
     27         ui.painter()
     28             .line_segment([handle_start, handle_end], stroke);
     29 
     30         response
     31     }
     32 }
     33 
     34 pub fn notifications_button(
     35     ui: &mut egui::Ui,
     36     size: f32,
     37     unseen_indicator: bool,
     38 ) -> egui::Response {
     39     expanding_button(
     40         "notifications-button",
     41         size,
     42         app_images::notifications_light_image(),
     43         app_images::notifications_dark_image(),
     44         ui,
     45         unseen_indicator,
     46     )
     47 }
     48 
     49 pub fn home_button(ui: &mut egui::Ui, size: f32) -> egui::Response {
     50     expanding_button(
     51         "home-button",
     52         size,
     53         app_images::home_light_image(),
     54         app_images::home_dark_image(),
     55         ui,
     56         false,
     57     )
     58 }
     59 
     60 pub fn expanding_button(
     61     name: &'static str,
     62     img_size: f32,
     63     light_img: egui::Image,
     64     dark_img: egui::Image,
     65     ui: &mut egui::Ui,
     66     unseen_indicator: bool,
     67 ) -> egui::Response {
     68     let max_size = ICON_WIDTH * ICON_EXPANSION_MULTIPLE; // max size of the widget
     69     let img = if ui.visuals().dark_mode {
     70         dark_img
     71     } else {
     72         light_img
     73     };
     74 
     75     let helper = AnimationHelper::new(ui, name, egui::vec2(max_size, max_size));
     76 
     77     let cur_img_size = helper.scale_1d_pos(img_size);
     78 
     79     let paint_rect = helper
     80         .get_animation_rect()
     81         .shrink((max_size - cur_img_size) / 2.0);
     82     img.paint_at(ui, paint_rect);
     83 
     84     if unseen_indicator {
     85         paint_unseen_indicator(ui, paint_rect, helper.scale_1d_pos(3.0));
     86     }
     87 
     88     helper.take_animation_response()
     89 }
     90 
     91 fn paint_unseen_indicator(ui: &mut egui::Ui, rect: egui::Rect, radius: f32) {
     92     let center = rect.center();
     93     let top_right = rect.right_top();
     94     let distance = center.distance(top_right);
     95     let midpoint = {
     96         let mut cur = center;
     97         cur.x += distance / 2.0;
     98         cur.y -= distance / 2.0;
     99         cur
    100     };
    101 
    102     let painter = ui.painter_at(rect);
    103     painter.circle_filled(midpoint, radius, crate::colors::PINK);
    104 }