ruler2/src/app.rs

105 lines
3.4 KiB
Rust

use serde::{Deserialize, Serialize};
use eframe::{egui, epi};
#[derive(Deserialize, Serialize)]
#[serde(default)]
pub struct App {
}
impl Default for App {
fn default() -> Self {
App {
}
}
}
impl epi::App for App {
fn name(&self) -> &str {
"Ruler 2"
}
fn setup(&mut self, _ctx: &egui::Context, _frame: &epi::Frame, storage: Option<&dyn epi::Storage>) {
if let Some(storage) = storage {
*self = epi::get_value(storage, epi::APP_KEY).unwrap_or_default();
}
}
fn save(&mut self, storage: &mut dyn epi::Storage) {
epi::set_value(storage, epi::APP_KEY, self);
}
fn clear_color(&self) -> egui::Rgba {
egui::Rgba::from_black_alpha(0.5)
}
fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
egui::CentralPanel::default()
.frame(egui::Frame {
fill: egui::Color32::TRANSPARENT,
stroke: egui::Stroke {
width: 1.0,
color: egui::Color32::BLACK,
},
..Default::default()
})
.show(ctx, |ui| {
let avail = ui.available_size();
// allocate all available size for drawing and senses
let resp = ui.allocate_response(avail, egui::Sense::drag());
// allow dragging the window by dragging anywhere
if resp.dragged_by(egui::PointerButton::Primary) && !ctx.input().modifiers.shift {
frame.drag_window();
}
// draw lines for x axis
let painter = ui.painter();
let ppp = ui.ctx().pixels_per_point();
let unit = 1.0 / ppp;
for i in 0..avail.x as u64 {
let length = if i % 10 == 0 {
8.0
} else if i % 2 == 0 {
4.0
} else {
continue;
};
let rounded = painter.round_to_pixel(i as f32);
painter.line_segment(
[egui::Pos2::new(rounded, 0.0), egui::Pos2::new(rounded, length)],
(unit, egui::Color32::WHITE),
);
}
// draw line where hovered
let hover = ui.input().pointer.hover_pos().clone();
if let Some(pos) = hover {
let rounded = painter.round_to_pixel(pos.x);
painter.line_segment(
[egui::Pos2::new(rounded, 0.0), egui::Pos2::new(rounded, avail.y)],
(unit, egui::Color32::RED),
);
let text = painter.layout_no_wrap(
format!("{}, {}", rounded * ppp, painter.round_to_pixel(pos.y) * ppp),
egui::FontId::proportional(14.0),
egui::Color32::WHITE,
);
let y = avail.y - text.mesh_bounds.max.y - 4.0;
painter.galley(egui::Pos2::new(4.0, y), text);
}
// show context menu
resp.context_menu(|ui| {
if ui.button("Quit").clicked() {
frame.quit();
}
});
});
}
}