Skip to content

Instantly share code, notes, and snippets.

View emilk's full-sized avatar

Emil Ernerfeldt emilk

View GitHub Profile

egui

One of the most popular GUI libraries for Rust in 2024 is egui.

egui is an immediate mode GUI and aims to be simple, easy to use, and highly portable.

At a glance

fn ui_counter(ui: &mut egui::Ui, counter: &mut i32) {
    ui.horizontal(|ui| {
 if ui.button("−").clicked() {

Emil's Rust Style Guide

If you are only using it in one function, put the use statement in that function. This improves locality, making it easier to read and move the code.

When importing a trait to use it's trait methods, do this: use Trait as _;. That lets the reader know why you imported it, even though it seems unused.

Prefer anyhow::Result<_> over Result<_, anyhow::Error>; Don't do use anyhow::Result; - be explicit.

@emilk
emilk / svg_nsvg.rs
Created February 8, 2022 20:43
Using `nsvg` to show and SVG in egui.
//! This uses `nsvg` to parse and rasterize an SVG image.
//!
//! Unfortunately `nsvg` does not preserve SVG size and aspect ratio :(
use eframe::{egui, epi};
struct Svg {
image: egui::ColorImage,
/// original size of the svg
svg_size: egui::Vec2,
}
@emilk
emilk / svg_lyon_usvg.rs
Last active February 9, 2022 08:44
A bad way to show an SVG in egui
//! A not so great way to show an SVG in egui.
//!
//! This keeps the SVG as vector graphics to make it scalable,
//! but it has a few problems:
//!
//! * There is no anti-aliasing (would require MSAA backend)
//! * No support for gradients and text in SVG
//! * Has some bugs in it
//! * It is slow
//! * It is a lot of code
@emilk
emilk / oklab.rs
Created January 21, 2021 09:45
OkLab in Rust
//! https://bottosson.github.io/posts/oklab/
/// oklab from linear rgb (0-1 ranges)
pub fn lab_from_rgb([r, g, b]: [f32; 3]) -> (f32, f32, f32) {
let x = 0.4121656120 * r + 0.5362752080 * g + 0.0514575653 * b;
let y = 0.2118591070 * r + 0.6807189584 * g + 0.1074065790 * b;
let z = 0.0883097947 * r + 0.2818474174 * g + 0.6302613616 * b;
let x = x.cbrt();
let y = y.cbrt();
@emilk
emilk / dual.rs
Created November 8, 2020 19:51
Dual numbers in Rust, for automatic differentiation
use std::{
fmt,
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};
pub trait Scalar:
fmt::Debug
+ fmt::Display
+ std::iter::Sum
+ Copy
@emilk
emilk / DefaultKeyBinding.dict
Created May 10, 2020 15:21
~/Library/KeyBindings/DefaultKeyBinding.dict
{
/* Remap Home / End keys to be correct */
"\UF729" = "moveToBeginningOfLine:"; /* Home */
"\UF72B" = "moveToEndOfLine:"; /* End */
"$\UF729" = "moveToBeginningOfLineAndModifySelection:"; /* Shift + Home */
"$\UF72B" = "moveToEndOfLineAndModifySelection:"; /* Shift + End */
"^\UF729" = "moveToBeginningOfDocument:"; /* Ctrl + Home */
"^\UF72B" = "moveToEndOfDocument:"; /* Ctrl + End */
"$^\UF729" = "moveToBeginningOfDocumentAndModifySelection:"; /* Shift + Ctrl + Home */
"$^\UF72B" = "moveToEndOfDocumentAndModifySelection:"; /* Shift + Ctrl + End */
#-------------------------------------------------------------
# Shell Prompt
#-------------------------------------------------------------
if [[ "${DISPLAY%%:0*}" != "" ]]; then
HILIT=${red} # remote machine: prompt will be partly red
else
HILIT=${cyan} # local machine: prompt will be partly cyan
fi
{
"file_regex": "([^\\s@]*?):([0-9]+):([0-9]+)(.*)$",
"selector": "source.rust",
"shell_cmd": "cargo check --all-features --benches --tests && cargo clippy --all-features --benches",
"working_dir": "${project_path}",
}
{
"file_regex": "([^\\s@]*?):([0-9]+):([0-9]+)(.*)$",
"selector": "source.rust",
"shell_cmd": "cargo check --lib --all-features",
"working_dir": "${project_path}",
}