15 Commits

7 changed files with 445 additions and 145 deletions

26
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,26 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug example 'example_knob'",
"cargo": {
"args": [
"build",
"--example=example_knob",
"--package=egui_knob"
],
"filter": {
"name": "example_knob",
"kind": "example"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
]
}

26
CHANGELOG.md Normal file
View File

@@ -0,0 +1,26 @@
# Changelog
All notable changes to this project will be documented in this file.
## [0.1.9] - 2025-02-04
### 🚀 Features
- Exposed label format
- Add step size configuration
### 🐛 Bug Fixes
- Add label offsets and position in relative to the widget rect
### 📚 Documentation
- Added doc comments
## [0.1.8] - 2025-02-03
### 🐛 Bug Fixes
- Mark response changed when knob was dragged
<!-- generated by git-cliff -->

228
Cargo.lock generated
View File

@@ -34,7 +34,7 @@ dependencies = [
"accesskit_consumer", "accesskit_consumer",
"atspi-common", "atspi-common",
"serde", "serde",
"thiserror", "thiserror 1.0.69",
"zvariant", "zvariant",
] ]
@@ -147,7 +147,7 @@ dependencies = [
"ndk-context", "ndk-context",
"ndk-sys 0.6.0+11769913", "ndk-sys 0.6.0+11769913",
"num_enum", "num_enum",
"thiserror", "thiserror 1.0.69",
] ]
[[package]] [[package]]
@@ -172,11 +172,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df099ccb16cd014ff054ac1bf392c67feeef57164b05c42f037cd40f5d4357f4" checksum = "df099ccb16cd014ff054ac1bf392c67feeef57164b05c42f037cd40f5d4357f4"
dependencies = [ dependencies = [
"clipboard-win", "clipboard-win",
"core-graphics",
"image",
"log", "log",
"objc2", "objc2",
"objc2-app-kit", "objc2-app-kit",
"objc2-foundation", "objc2-foundation",
"parking_lot", "parking_lot",
"windows-sys 0.48.0",
"x11rb", "x11rb",
] ]
@@ -439,6 +442,9 @@ name = "bitflags"
version = "2.8.0" version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "block" name = "block"
@@ -532,7 +538,7 @@ dependencies = [
"polling", "polling",
"rustix", "rustix",
"slab", "slab",
"thiserror", "thiserror 1.0.69",
] ]
[[package]] [[package]]
@@ -570,12 +576,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_aliases"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e"
[[package]] [[package]]
name = "cfg_aliases" name = "cfg_aliases"
version = "0.2.1" version = "0.2.1"
@@ -778,9 +778,9 @@ checksum = "f25c0e292a7ca6d6498557ff1df68f32c99850012b6ea401cf8daf771f22ff53"
[[package]] [[package]]
name = "ecolor" name = "ecolor"
version = "0.30.0" version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d72e9c39f6e11a2e922d04a34ec5e7ef522ea3f5a1acfca7a19d16ad5fe50f5" checksum = "bc4feb366740ded31a004a0e4452fbf84e80ef432ecf8314c485210229672fd1"
dependencies = [ dependencies = [
"bytemuck", "bytemuck",
"emath", "emath",
@@ -788,9 +788,9 @@ dependencies = [
[[package]] [[package]]
name = "eframe" name = "eframe"
version = "0.30.0" version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2f2d9e7ea2d11ec9e98a8683b6eb99f9d7d0448394ef6e0d6d91bd4eb817220" checksum = "d0dfe0859f3fb1bc6424c57d41e10e9093fe938f426b691e42272c2f336d915c"
dependencies = [ dependencies = [
"ahash", "ahash",
"bytemuck", "bytemuck",
@@ -799,7 +799,7 @@ dependencies = [
"egui-wgpu", "egui-wgpu",
"egui-winit", "egui-winit",
"egui_glow", "egui_glow",
"glow 0.16.0", "glow",
"glutin", "glutin",
"glutin-winit", "glutin-winit",
"image", "image",
@@ -824,12 +824,13 @@ dependencies = [
[[package]] [[package]]
name = "egui" name = "egui"
version = "0.30.0" version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "252d52224d35be1535d7fd1d6139ce071fb42c9097773e79f7665604f5596b5e" checksum = "25dd34cec49ab55d85ebf70139cb1ccd29c977ef6b6ba4fe85489d6877ee9ef3"
dependencies = [ dependencies = [
"accesskit", "accesskit",
"ahash", "ahash",
"bitflags 2.8.0",
"emath", "emath",
"epaint", "epaint",
"log", "log",
@@ -839,9 +840,9 @@ dependencies = [
[[package]] [[package]]
name = "egui-wgpu" name = "egui-wgpu"
version = "0.30.0" version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26c1e821d2d8921ef6ce98b258c7e24d9d6aab2ca1f9cdf374eca997e7f67f59" checksum = "d319dfef570f699b6e9114e235e862a2ddcf75f0d1a061de9e1328d92146d820"
dependencies = [ dependencies = [
"ahash", "ahash",
"bytemuck", "bytemuck",
@@ -850,7 +851,7 @@ dependencies = [
"epaint", "epaint",
"log", "log",
"profiling", "profiling",
"thiserror", "thiserror 1.0.69",
"type-map", "type-map",
"web-time", "web-time",
"wgpu", "wgpu",
@@ -859,13 +860,14 @@ dependencies = [
[[package]] [[package]]
name = "egui-winit" name = "egui-winit"
version = "0.30.0" version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e84c2919cd9f3a38a91e8f84ac6a245c19251fd95226ed9fae61d5ea564fce3" checksum = "7d9dfbb78fe4eb9c3a39ad528b90ee5915c252e77bbab9d4ebc576541ab67e13"
dependencies = [ dependencies = [
"accesskit_winit", "accesskit_winit",
"ahash", "ahash",
"arboard", "arboard",
"bytemuck",
"egui", "egui",
"log", "log",
"profiling", "profiling",
@@ -878,14 +880,14 @@ dependencies = [
[[package]] [[package]]
name = "egui_glow" name = "egui_glow"
version = "0.30.0" version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eaf6264cc7608e3e69a7d57a6175f438275f1b3889c1a551b418277721c95e6" checksum = "910906e3f042ea6d2378ec12a6fd07698e14ddae68aed2d819ffe944a73aab9e"
dependencies = [ dependencies = [
"ahash", "ahash",
"bytemuck", "bytemuck",
"egui", "egui",
"glow 0.16.0", "glow",
"log", "log",
"memoffset", "memoffset",
"profiling", "profiling",
@@ -896,7 +898,7 @@ dependencies = [
[[package]] [[package]]
name = "egui_knob" name = "egui_knob"
version = "0.1.4" version = "0.2.0"
dependencies = [ dependencies = [
"eframe", "eframe",
"egui", "egui",
@@ -904,9 +906,9 @@ dependencies = [
[[package]] [[package]]
name = "emath" name = "emath"
version = "0.30.0" version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4fe73c1207b864ee40aa0b0c038d6092af1030744678c60188a05c28553515d" checksum = "9e4cadcff7a5353ba72b7fea76bf2122b5ebdbc68e8155aa56dfdea90083fe1b"
dependencies = [ dependencies = [
"bytemuck", "bytemuck",
] ]
@@ -940,9 +942,9 @@ dependencies = [
[[package]] [[package]]
name = "epaint" name = "epaint"
version = "0.30.0" version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5666f8d25236293c966fbb3635eac18b04ad1914e3bab55bc7d44b9980cafcac" checksum = "41fcc0f5a7c613afd2dee5e4b30c3e6acafb8ad6f0edb06068811f708a67c562"
dependencies = [ dependencies = [
"ab_glyph", "ab_glyph",
"ahash", "ahash",
@@ -958,9 +960,9 @@ dependencies = [
[[package]] [[package]]
name = "epaint_default_fonts" name = "epaint_default_fonts"
version = "0.30.0" version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66f6ddac3e6ac6fd4c3d48bb8b1943472f8da0f43a4303bcd8a18aa594401c80" checksum = "fc7e7a64c02cf7a5b51e745a9e45f60660a286f151c238b9d397b3e923f5082f"
[[package]] [[package]]
name = "equivalent" name = "equivalent"
@@ -1191,18 +1193,6 @@ dependencies = [
"xml-rs", "xml-rs",
] ]
[[package]]
name = "glow"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51fa363f025f5c111e03f13eda21162faeacb6911fe8caa0c0349f9cf0c4483"
dependencies = [
"js-sys",
"slotmap",
"wasm-bindgen",
"web-sys",
]
[[package]] [[package]]
name = "glow" name = "glow"
version = "0.16.0" version = "0.16.0"
@@ -1222,7 +1212,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03642b8b0cce622392deb0ee3e88511f75df2daac806102597905c3ea1974848" checksum = "03642b8b0cce622392deb0ee3e88511f75df2daac806102597905c3ea1974848"
dependencies = [ dependencies = [
"bitflags 2.8.0", "bitflags 2.8.0",
"cfg_aliases 0.2.1", "cfg_aliases",
"cgl", "cgl",
"core-foundation 0.9.4", "core-foundation 0.9.4",
"dispatch", "dispatch",
@@ -1246,7 +1236,7 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85edca7075f8fc728f28cb8fbb111a96c3b89e930574369e3e9c27eb75d3788f" checksum = "85edca7075f8fc728f28cb8fbb111a96c3b89e930574369e3e9c27eb75d3788f"
dependencies = [ dependencies = [
"cfg_aliases 0.2.1", "cfg_aliases",
"glutin", "glutin",
"raw-window-handle", "raw-window-handle",
"winit", "winit",
@@ -1329,6 +1319,12 @@ dependencies = [
"foldhash", "foldhash",
] ]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.4.0" version = "0.4.0"
@@ -1505,6 +1501,7 @@ dependencies = [
"byteorder-lite", "byteorder-lite",
"num-traits", "num-traits",
"png", "png",
"tiff",
] ]
[[package]] [[package]]
@@ -1537,7 +1534,7 @@ dependencies = [
"combine", "combine",
"jni-sys", "jni-sys",
"log", "log",
"thiserror", "thiserror 1.0.69",
"walkdir", "walkdir",
"windows-sys 0.45.0", "windows-sys 0.45.0",
] ]
@@ -1557,6 +1554,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "jpeg-decoder"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5d4a7da358eff58addd2877a45865158f0d78c911d43a5784ceb7bbf52833b0"
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.77" version = "0.3.77"
@@ -1680,9 +1683,9 @@ dependencies = [
[[package]] [[package]]
name = "metal" name = "metal"
version = "0.29.0" version = "0.31.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ecfd3296f8c56b7c1f6fbac3c71cefa9d78ce009850c45000015f206dc7fa21" checksum = "f569fb946490b5743ad69813cb19629130ce9374034abe31614a36402d18f99e"
dependencies = [ dependencies = [
"bitflags 2.8.0", "bitflags 2.8.0",
"block", "block",
@@ -1705,22 +1708,23 @@ dependencies = [
[[package]] [[package]]
name = "naga" name = "naga"
version = "23.1.0" version = "24.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "364f94bc34f61332abebe8cad6f6cd82a5b65cff22c828d05d0968911462ca4f" checksum = "e380993072e52eef724eddfcde0ed013b0c023c3f0417336ed041aa9f076994e"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"bit-set", "bit-set",
"bitflags 2.8.0", "bitflags 2.8.0",
"cfg_aliases 0.1.1", "cfg_aliases",
"codespan-reporting", "codespan-reporting",
"hexf-parse", "hexf-parse",
"indexmap", "indexmap",
"log", "log",
"rustc-hash", "rustc-hash",
"spirv", "spirv",
"strum",
"termcolor", "termcolor",
"thiserror", "thiserror 2.0.12",
"unicode-xid", "unicode-xid",
] ]
@@ -1736,7 +1740,7 @@ dependencies = [
"ndk-sys 0.6.0+11769913", "ndk-sys 0.6.0+11769913",
"num_enum", "num_enum",
"raw-window-handle", "raw-window-handle",
"thiserror", "thiserror 1.0.69",
] ]
[[package]] [[package]]
@@ -1771,7 +1775,7 @@ checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [ dependencies = [
"bitflags 2.8.0", "bitflags 2.8.0",
"cfg-if", "cfg-if",
"cfg_aliases 0.2.1", "cfg_aliases",
"libc", "libc",
"memoffset", "memoffset",
] ]
@@ -2039,6 +2043,15 @@ dependencies = [
"libredox", "libredox",
] ]
[[package]]
name = "ordered-float"
version = "4.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bb71e1b3fa6ca1c61f383464aaf2bb0e2f8e772a1f01d486832464de363b951"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "ordered-stream" name = "ordered-stream"
version = "0.2.0" version = "0.2.0"
@@ -2457,7 +2470,7 @@ dependencies = [
"log", "log",
"memmap2", "memmap2",
"rustix", "rustix",
"thiserror", "thiserror 1.0.69",
"wayland-backend", "wayland-backend",
"wayland-client", "wayland-client",
"wayland-csd-frame", "wayland-csd-frame",
@@ -2515,6 +2528,28 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731" checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731"
[[package]]
name = "strum"
version = "0.26.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.26.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be"
dependencies = [
"heck",
"proc-macro2",
"quote",
"rustversion",
"syn",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.96" version = "2.0.96"
@@ -2566,7 +2601,16 @@ version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl 1.0.69",
]
[[package]]
name = "thiserror"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
dependencies = [
"thiserror-impl 2.0.12",
] ]
[[package]] [[package]]
@@ -2580,6 +2624,28 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "thiserror-impl"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tiff"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba1310fcea54c6a9a4fd1aad794ecc02c31682f6bfbecdf460bf19533eed1e3e"
dependencies = [
"flate2",
"jpeg-decoder",
"weezl",
]
[[package]] [[package]]
name = "tiny-skia" name = "tiny-skia"
version = "0.11.4" version = "0.11.4"
@@ -2992,13 +3058,20 @@ dependencies = [
] ]
[[package]] [[package]]
name = "wgpu" name = "weezl"
version = "23.0.1" version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80f70000db37c469ea9d67defdc13024ddf9a5f1b89cb2941b812ad7cde1735a" checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082"
[[package]]
name = "wgpu"
version = "24.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35904fb00ba2d2e0a4d002fcbbb6e1b89b574d272a50e5fc95f6e81cf281c245"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"cfg_aliases 0.1.1", "bitflags 2.8.0",
"cfg_aliases",
"document-features", "document-features",
"js-sys", "js-sys",
"log", "log",
@@ -3017,14 +3090,14 @@ dependencies = [
[[package]] [[package]]
name = "wgpu-core" name = "wgpu-core"
version = "23.0.1" version = "24.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d63c3c478de8e7e01786479919c8769f62a22eec16788d8c2ac77ce2c132778a" checksum = "671c25545d479b47d3f0a8e373aceb2060b67c6eb841b24ac8c32348151c7a0c"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"bit-vec", "bit-vec",
"bitflags 2.8.0", "bitflags 2.8.0",
"cfg_aliases 0.1.1", "cfg_aliases",
"document-features", "document-features",
"indexmap", "indexmap",
"log", "log",
@@ -3035,25 +3108,25 @@ dependencies = [
"raw-window-handle", "raw-window-handle",
"rustc-hash", "rustc-hash",
"smallvec", "smallvec",
"thiserror", "thiserror 2.0.12",
"wgpu-hal", "wgpu-hal",
"wgpu-types", "wgpu-types",
] ]
[[package]] [[package]]
name = "wgpu-hal" name = "wgpu-hal"
version = "23.0.1" version = "24.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89364b8a0b211adc7b16aeaf1bd5ad4a919c1154b44c9ce27838213ba05fd821" checksum = "f112f464674ca69f3533248508ee30cb84c67cf06c25ff6800685f5e0294e259"
dependencies = [ dependencies = [
"android_system_properties", "android_system_properties",
"arrayvec", "arrayvec",
"ash", "ash",
"bitflags 2.8.0", "bitflags 2.8.0",
"bytemuck", "bytemuck",
"cfg_aliases 0.1.1", "cfg_aliases",
"core-graphics-types", "core-graphics-types",
"glow 0.14.2", "glow",
"glutin_wgl_sys", "glutin_wgl_sys",
"gpu-alloc", "gpu-alloc",
"gpu-descriptor", "gpu-descriptor",
@@ -3067,13 +3140,14 @@ dependencies = [
"ndk-sys 0.5.0+25.2.9519653", "ndk-sys 0.5.0+25.2.9519653",
"objc", "objc",
"once_cell", "once_cell",
"ordered-float",
"parking_lot", "parking_lot",
"profiling", "profiling",
"raw-window-handle", "raw-window-handle",
"renderdoc-sys", "renderdoc-sys",
"rustc-hash", "rustc-hash",
"smallvec", "smallvec",
"thiserror", "thiserror 2.0.12",
"wasm-bindgen", "wasm-bindgen",
"web-sys", "web-sys",
"wgpu-types", "wgpu-types",
@@ -3082,12 +3156,13 @@ dependencies = [
[[package]] [[package]]
name = "wgpu-types" name = "wgpu-types"
version = "23.0.0" version = "24.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "610f6ff27778148c31093f3b03abc4840f9636d58d597ca2f5977433acfe0068" checksum = "50ac044c0e76c03a0378e7786ac505d010a873665e2d51383dcff8dd227dc69c"
dependencies = [ dependencies = [
"bitflags 2.8.0", "bitflags 2.8.0",
"js-sys", "js-sys",
"log",
"web-sys", "web-sys",
] ]
@@ -3195,6 +3270,15 @@ dependencies = [
"windows-targets 0.42.2", "windows-targets 0.42.2",
] ]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.52.0" version = "0.52.0"
@@ -3404,7 +3488,7 @@ dependencies = [
"block2", "block2",
"bytemuck", "bytemuck",
"calloop", "calloop",
"cfg_aliases 0.2.1", "cfg_aliases",
"concurrent-queue", "concurrent-queue",
"core-foundation 0.9.4", "core-foundation 0.9.4",
"core-graphics", "core-graphics",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "egui_knob" name = "egui_knob"
version = "0.1.4" version = "0.2.0"
edition = "2021" edition = "2021"
description = "A simple knob widget for egui" description = "A simple knob widget for egui"
homepage = "https://github.com/obsqrbtz/egui_knob" homepage = "https://github.com/obsqrbtz/egui_knob"
@@ -19,7 +19,7 @@ name = "example_knob"
path = "examples/example_knob.rs" path = "examples/example_knob.rs"
[dependencies] [dependencies]
egui = "0.30" egui = "0.31"
[dev-dependencies] [dev-dependencies]
eframe = { version = "0.30.0", features = ["default_fonts"] } eframe = { version = "0.31.0", features = ["default_fonts"] }

View File

@@ -9,8 +9,9 @@ Simple knob widget for egui.
## Features ## Features
- Adjustable size, font size, and stroke width. - Adjustable size, font size, and stroke width.
- Customizable colors for the knob and line. - Customizable colors for the knob, indicator and text.
- Label positions (Top, Bottom, Left, Right). - Label positions (Top, Bottom, Left, Right).
- Label formatting.
- Two styles: Wiper and Dot. - Two styles: Wiper and Dot.
## Installation ## Installation
@@ -19,8 +20,8 @@ To use the Knob widget in your project, add the following to your `Cargo.toml`:
```toml ```toml
[dependencies] [dependencies]
egui = "0.30" egui = "0.31"
egui_knob = "*" egui_knob = "0.2.0"
``` ```
## Usage example ## Usage example

View File

@@ -10,12 +10,26 @@ fn main() -> eframe::Result<()> {
} }
struct KnobExample { struct KnobExample {
value: f32, basic_value: f32,
purple_value: f32,
large_value: f32,
thick_value: f32,
red_value: f32,
green_value: f32,
blue_value: f32,
} }
impl Default for KnobExample { impl Default for KnobExample {
fn default() -> Self { fn default() -> Self {
Self { value: 0.0 } Self {
basic_value: 0.0,
purple_value: 0.0,
large_value: 0.0,
thick_value: 0.0,
red_value: 0.0,
green_value: 0.0,
blue_value: 0.0,
}
} }
} }
@@ -23,35 +37,96 @@ impl eframe::App for KnobExample {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.add( if ui
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Dot) .add(
.with_label("Gain", egui_knob::LabelPosition::Bottom) Knob::new(&mut self.basic_value, 0.0, 100.0, egui_knob::KnobStyle::Dot)
.with_size(50.0), .with_label("Basic", egui_knob::LabelPosition::Right)
); .with_size(40.0)
ui.add( .with_font_size(10.0)
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Wiper)
.with_label("Gain", egui_knob::LabelPosition::Bottom)
.with_size(50.0),
);
ui.add_space(15.0);
ui.add(
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Dot)
.with_label("Gain", egui_knob::LabelPosition::Bottom)
.with_colors( .with_colors(
egui::Color32::DARK_GRAY, egui::Color32::from_rgb(60, 60, 60),
egui::Color32::WHITE, egui::Color32::from_rgb(150, 150, 150),
egui::Color32::WHITE, egui::Color32::from_rgb(200, 200, 200),
),
)
.changed()
{
println!("Basic value changed: {}", self.basic_value);
}
ui.add(
Knob::new(
&mut self.purple_value,
0.0,
100.0,
egui_knob::KnobStyle::Wiper,
)
.with_label("Purple", egui_knob::LabelPosition::Bottom)
.with_colors(
egui::Color32::from_rgb(60, 30, 80),
egui::Color32::from_rgb(200, 100, 255),
egui::Color32::from_rgb(230, 150, 255),
)
.with_size(50.0)
.with_font_size(14.0)
.with_stroke_width(3.0)
.with_step(0.1),
);
ui.add(
Knob::new(&mut self.large_value, 0.0, 100.0, egui_knob::KnobStyle::Dot)
.with_label("Large", egui_knob::LabelPosition::Bottom)
.with_size(60.0)
.with_font_size(16.0),
);
ui.add(
Knob::new(
&mut self.thick_value,
0.0,
100.0,
egui_knob::KnobStyle::Wiper,
)
.with_label("Thick", egui_knob::LabelPosition::Bottom)
.with_size(50.0)
.with_stroke_width(4.0),
);
ui.add(
Knob::new(&mut self.red_value, 0.0, 100.0, egui_knob::KnobStyle::Dot)
.with_label("Red", egui_knob::LabelPosition::Bottom)
.with_colors(
egui::Color32::from_rgb(80, 30, 30),
egui::Color32::from_rgb(220, 50, 50),
egui::Color32::from_rgb(255, 100, 100),
) )
.with_size(50.0), .with_size(50.0),
); );
ui.add_space(15.0);
ui.add( ui.add(
Knob::new(&mut self.value, 0.0, 100.0, egui_knob::KnobStyle::Wiper) Knob::new(
.with_label("Gain", egui_knob::LabelPosition::Bottom) &mut self.green_value,
0.0,
100.0,
egui_knob::KnobStyle::Wiper,
)
.with_label("Leftandlongtext", egui_knob::LabelPosition::Left)
.with_colors( .with_colors(
egui::Color32::DARK_GRAY, egui::Color32::from_rgb(30, 80, 30),
egui::Color32::WHITE, egui::Color32::from_rgb(50, 220, 50),
egui::Color32::LIGHT_BLUE, egui::Color32::from_rgb(100, 255, 100),
)
.with_size(50.0)
.with_label_format(|v| format!("{:.2}%", v)),
);
ui.add(
Knob::new(&mut self.blue_value, 0.0, 100.0, egui_knob::KnobStyle::Dot)
.with_label("Top", egui_knob::LabelPosition::Top)
.with_colors(
egui::Color32::from_rgb(30, 30, 80),
egui::Color32::from_rgb(50, 50, 220),
egui::Color32::from_rgb(100, 100, 255),
) )
.with_size(50.0), .with_size(50.0),
); );

View File

@@ -1,5 +1,6 @@
use egui::{Align2, Color32, Response, Sense, Stroke, Ui, Vec2, Widget}; use egui::{Align2, Color32, Rect, Response, Sense, Stroke, Ui, Vec2, Widget};
/// Position of the label relative to the knob
pub enum LabelPosition { pub enum LabelPosition {
Top, Top,
Bottom, Bottom,
@@ -7,11 +8,24 @@ pub enum LabelPosition {
Right, Right,
} }
/// Visual style of the knob indicator
pub enum KnobStyle { pub enum KnobStyle {
/// A line extending from the center to the edge
Wiper, Wiper,
/// A dot on the edge of the knob
Dot, Dot,
} }
/// A circular knob widget for egui that can be dragged to change a value
///
/// # Example
/// ```
/// let mut value = 0.5;
/// Knob::new(&mut value, 0.0, 1.0, KnobStyle::Wiper)
/// .with_size(50.0)
/// .with_label("Volume", LabelPosition::Bottom)
/// .with_step(0.1);
/// ```
pub struct Knob<'a> { pub struct Knob<'a> {
value: &'a mut f32, value: &'a mut f32,
min: f32, min: f32,
@@ -25,9 +39,19 @@ pub struct Knob<'a> {
label: Option<String>, label: Option<String>,
label_position: LabelPosition, label_position: LabelPosition,
style: KnobStyle, style: KnobStyle,
label_offset: f32,
label_format: Box<dyn Fn(f32) -> String>,
step: Option<f32>,
} }
impl<'a> Knob<'a> { impl<'a> Knob<'a> {
/// Creates a new knob widget
///
/// # Arguments
/// * `value` - Mutable reference to the value controlled by the knob
/// * `min` - Minimum value
/// * `max` - Maximum value
/// * `style` - Visual style of the knob indicator
pub fn new(value: &'a mut f32, min: f32, max: f32, style: KnobStyle) -> Self { pub fn new(value: &'a mut f32, min: f32, max: f32, style: KnobStyle) -> Self {
Self { Self {
value, value,
@@ -42,24 +66,36 @@ impl<'a> Knob<'a> {
label: None, label: None,
label_position: LabelPosition::Bottom, label_position: LabelPosition::Bottom,
style, style,
label_offset: 1.0,
label_format: Box::new(|v| format!("{:.2}", v)),
step: None,
} }
} }
/// Sets the size of the knob
pub fn with_size(mut self, size: f32) -> Self { pub fn with_size(mut self, size: f32) -> Self {
self.size = size; self.size = size;
self self
} }
/// Sets the font size for the label
pub fn with_font_size(mut self, size: f32) -> Self { pub fn with_font_size(mut self, size: f32) -> Self {
self.font_size = size; self.font_size = size;
self self
} }
/// Sets the stroke width for the knob's outline and indicator
pub fn with_stroke_width(mut self, width: f32) -> Self { pub fn with_stroke_width(mut self, width: f32) -> Self {
self.stroke_width = width; self.stroke_width = width;
self self
} }
/// Sets the colors for different parts of the knob
///
/// # Arguments
/// * `knob_color` - Color of the knob's outline
/// * `line_color` - Color of the indicator
/// * `text_color` - Color of the label text
pub fn with_colors( pub fn with_colors(
mut self, mut self,
knob_color: Color32, knob_color: Color32,
@@ -72,11 +108,43 @@ impl<'a> Knob<'a> {
self self
} }
/// Adds a label to the knob
///
/// # Arguments
/// * `label` - Text to display
/// * `position` - Position of the label relative to the knob
pub fn with_label(mut self, label: impl Into<String>, position: LabelPosition) -> Self { pub fn with_label(mut self, label: impl Into<String>, position: LabelPosition) -> Self {
self.label = Some(label.into()); self.label = Some(label.into());
self.label_position = position; self.label_position = position;
self self
} }
/// Sets the spacing between the knob and its label
pub fn with_label_offset(mut self, offset: f32) -> Self {
self.label_offset = offset;
self
}
/// Sets a custom format function for displaying the value
///
/// # Example
/// ```
/// # let mut value = 0.5;
/// Knob::new(&mut value, 0.0, 1.0, KnobStyle::Wiper)
/// .with_label_format(|v| format!("{:.1}%", v * 100.0));
/// ```
pub fn with_label_format(mut self, format: impl Fn(f32) -> String + 'static) -> Self {
self.label_format = Box::new(format);
self
}
/// Sets the step size for value changes
///
/// When set, the value will snap to discrete steps as the knob is dragged.
pub fn with_step(mut self, step: f32) -> Self {
self.step = Some(step);
self
}
} }
impl Widget for Knob<'_> { impl Widget for Knob<'_> {
@@ -85,40 +153,62 @@ impl Widget for Knob<'_> {
let label_size = if let Some(label) = &self.label { let label_size = if let Some(label) = &self.label {
let font_id = egui::FontId::proportional(self.font_size); let font_id = egui::FontId::proportional(self.font_size);
let max_text = format!("{}: {}", label, (self.label_format)(self.max));
ui.painter() ui.painter()
.layout( .layout(max_text, font_id, Color32::WHITE, f32::INFINITY)
format!("{}: {:.2}", label, self.value),
font_id,
Color32::WHITE,
f32::INFINITY,
)
.size() .size()
} else { } else {
Vec2::ZERO Vec2::ZERO
}; };
let label_padding = 12.0; let label_padding = 2.0;
let adjusted_size = match self.label_position { let adjusted_size = match self.label_position {
LabelPosition::Top | LabelPosition::Bottom => { LabelPosition::Top | LabelPosition::Bottom => Vec2::new(
Vec2::new(knob_size.x, knob_size.y + label_size.y + label_padding) knob_size.x.max(label_size.x + label_padding * 2.0),
} knob_size.y + label_size.y + label_padding * 2.0 + self.label_offset,
LabelPosition::Left | LabelPosition::Right => { ),
Vec2::new(knob_size.x + label_size.x + label_padding, knob_size.y) LabelPosition::Left | LabelPosition::Right => Vec2::new(
} knob_size.x + label_size.x + label_padding * 2.0 + self.label_offset,
knob_size.y.max(label_size.y + label_padding * 2.0),
),
}; };
let (rect, response) = ui.allocate_exact_size(adjusted_size, Sense::drag()); let (rect, mut response) = ui.allocate_exact_size(adjusted_size, Sense::drag());
if response.dragged() { if response.dragged() {
let delta = response.drag_delta().y; let delta = response.drag_delta().y;
let range = self.max - self.min; let range = self.max - self.min;
let step = range * 0.005; let step = self.step.unwrap_or(range * 0.005);
*self.value = (*self.value - delta * step).clamp(self.min, self.max); let new_value = (*self.value - delta * step).clamp(self.min, self.max);
*self.value = if let Some(step) = self.step {
let steps = ((new_value - self.min) / step).round();
(self.min + steps * step).clamp(self.min, self.max)
} else {
new_value
};
response.mark_changed();
} }
let painter = ui.painter(); let painter = ui.painter();
let center = rect.center(); let knob_rect = match self.label_position {
LabelPosition::Left => {
Rect::from_min_size(rect.right_top() + Vec2::new(-knob_size.x, 0.0), knob_size)
}
LabelPosition::Right => Rect::from_min_size(rect.left_top(), knob_size),
LabelPosition::Top => Rect::from_min_size(
rect.left_bottom() + Vec2::new((rect.width() - knob_size.x) / 2.0, -knob_size.y),
knob_size,
),
LabelPosition::Bottom => Rect::from_min_size(
rect.left_top() + Vec2::new((rect.width() - knob_size.x) / 2.0, 0.0),
knob_size,
),
};
let center = knob_rect.center();
let radius = knob_size.x / 2.0; let radius = knob_size.x / 2.0;
let angle = (*self.value - self.min) / (self.max - self.min) * std::f32::consts::PI * 1.5 let angle = (*self.value - self.min) / (self.max - self.min) * std::f32::consts::PI * 1.5
- std::f32::consts::PI; - std::f32::consts::PI;
@@ -144,45 +234,43 @@ impl Widget for Knob<'_> {
} }
if let Some(label) = self.label { if let Some(label) = self.label {
let label_text = format!("{label}: {:.2}", self.value); let label_text = format!("{}: {}", label, (self.label_format)(*self.value));
let font_id = egui::FontId::proportional(self.font_size); let font_id = egui::FontId::proportional(self.font_size);
let text_size = ui
.painter()
.layout(
label_text.clone(),
font_id.clone(),
Color32::WHITE,
f32::INFINITY,
)
.size();
let label_offset = label_padding; let (label_pos, alignment) = match self.label_position {
let label_pos = match self.label_position { LabelPosition::Top => (
LabelPosition::Top => { Vec2::new(
rect.center() rect.center().x,
+ Vec2::new(-text_size.x / 2.0, -radius - label_offset - text_size.y) rect.min.y - self.label_offset + label_padding,
} ),
LabelPosition::Bottom => { Align2::CENTER_TOP,
rect.center() + Vec2::new(-text_size.x / 2.0, radius + label_offset) ),
} LabelPosition::Bottom => (
LabelPosition::Left => { Vec2::new(rect.center().x, rect.max.y + self.label_offset),
rect.center() Align2::CENTER_BOTTOM,
+ Vec2::new(-radius - label_offset - text_size.x, -text_size.y / 2.0) ),
} LabelPosition::Left => (
LabelPosition::Right => { Vec2::new(rect.min.x - self.label_offset, rect.center().y),
rect.center() + Vec2::new(radius + label_offset, -text_size.y / 2.0) Align2::LEFT_CENTER,
} ),
LabelPosition::Right => (
Vec2::new(rect.max.x + self.label_offset, rect.center().y),
Align2::RIGHT_CENTER,
),
}; };
ui.painter().text( ui.painter().text(
label_pos, label_pos.to_pos2(),
Align2::LEFT_TOP, alignment,
label_text, label_text,
font_id, font_id,
self.text_color, self.text_color,
); );
} }
// Draw the bounding rect
//painter.rect_stroke(rect, 0.0, Stroke::new(1.0, Color32::RED));
response response
} }
} }