Created
April 24, 2023 16:54
-
-
Save MattBlack85/df69962bdce7aa17ce269c08a15199da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use eframe::egui; | |
fn main() -> Result<(), eframe::Error> { | |
//env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`). | |
let options = eframe::NativeOptions { | |
initial_window_size: Some(egui::vec2(320.0, 240.0)), | |
..Default::default() | |
}; | |
eframe::run_native( | |
"My egui App", | |
options, | |
Box::new(|_cc| Box::new(IndiUI::new())), | |
) | |
} | |
pub fn toggle_ui(ui: &mut egui::Ui, on: &mut bool) -> egui::Response { | |
// Widget code can be broken up in four steps: | |
// 1. Decide a size for the widget | |
// 2. Allocate space for it | |
// 3. Handle interactions with the widget (if any) | |
// 4. Paint the widget | |
// 1. Deciding widget size: | |
// You can query the `ui` how much space is available, | |
// but in this example we have a fixed size widget based on the height of a standard button: | |
let desired_size = ui.spacing().interact_size.y * egui::vec2(2.0, 1.0); | |
// 2. Allocating space: | |
// This is where we get a region of the screen assigned. | |
// We also tell the Ui to sense clicks in the allocated region. | |
let (rect, mut response) = ui.allocate_exact_size(desired_size, egui::Sense::click()); | |
// 3. Interact: Time to check for clicks! | |
if response.clicked() { | |
*on = !*on; | |
response.mark_changed(); // report back that the value changed | |
} | |
// Attach some meta-data to the response which can be used by screen readers: | |
response.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::Checkbox, *on, "")); | |
// 4. Paint! | |
// Make sure we need to paint: | |
if ui.is_rect_visible(rect) { | |
// Let's ask for a simple animation from egui. | |
// egui keeps track of changes in the boolean associated with the id and | |
// returns an animated value in the 0-1 range for how much "on" we are. | |
let how_on = ui.ctx().animate_bool(response.id, *on); | |
// We will follow the current style by asking | |
// "how should something that is being interacted with be painted?". | |
// This will, for instance, give us different colors when the widget is hovered or clicked. | |
let visuals = ui.style().interact_selectable(&response, *on); | |
// All coordinates are in absolute screen coordinates so we use `rect` to place the elements. | |
let rect = rect.expand(visuals.expansion); | |
let radius = 0.5 * rect.height(); | |
ui.painter() | |
.rect(rect, radius, visuals.bg_fill, visuals.bg_stroke); | |
// Paint the circle, animating it from left to right with `how_on`: | |
let circle_x = egui::lerp((rect.left() + radius)..=(rect.right() - radius), how_on); | |
let center = egui::pos2(circle_x, rect.center().y); | |
ui.painter() | |
.circle(center, 0.75 * radius, visuals.bg_fill, visuals.fg_stroke); | |
} | |
// All done! Return the interaction response so the user can check what happened | |
// (hovered, clicked, ...) and maybe show a tooltip: | |
response | |
} | |
pub fn toggle(on: &mut bool) -> impl egui::Widget + '_ { | |
move |ui: &mut egui::Ui| toggle_ui(ui, on) | |
} | |
struct IndiUI { | |
binaries: Vec<(String, String, bool)>, | |
filter: String, | |
} | |
impl IndiUI { | |
fn new() -> Self { | |
Self { | |
binaries: indi_ui::fetch_indi_binaries(), | |
filter: String::new(), | |
} | |
} | |
} | |
impl eframe::App for IndiUI { | |
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { | |
egui::CentralPanel::default().show(ctx, |ui| { | |
ui.with_layout(egui::Layout::top_down_justified(egui::Align::TOP), |ui| { | |
ui.vertical_centered(|ui| { | |
ui.heading("Indiserver"); | |
}); | |
ui.separator(); | |
}); | |
ui.horizontal(|ui| { | |
let name_label = ui.label("Filter: "); | |
ui.text_edit_singleline(&mut self.filter) | |
.labelled_by(name_label.id); | |
}); | |
ui.separator(); | |
ui.vertical_centered(|ui| { | |
egui::ScrollArea::vertical() | |
.auto_shrink([false; 2]) | |
.show(ui, |ui| { | |
egui::Grid::new("binaries") | |
.num_columns(2) | |
.spacing([10.0, 10.0]) | |
.striped(true) | |
.show(ui, |ui| { | |
for el in self.binaries.iter_mut() { | |
if &self.filter == "" || el.0.contains(&self.filter) { | |
ui.label(el.0.to_owned()); | |
ui.add(toggle(&mut el.2)); | |
ui.end_row(); | |
}; | |
} | |
}); | |
}); | |
}); | |
}); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment