| Crates.io | mkgraphic |
| lib.rs | mkgraphic |
| version | 0.2.1 |
| created_at | 2025-12-16 04:06:23.0083+00 |
| updated_at | 2025-12-18 04:48:03.261799+00 |
| description | A Rust port of the cycfi/elements GUI framework |
| homepage | |
| repository | https://github.com/mkaudio-company/mkgraphic |
| max_upload_size | |
| id | 1987175 |
| size | 515,985 |
A Rust port of the cycfi/elements C++ GUI framework.
mkgraphic is a lightweight, modular GUI framework for Rust that provides an element-based architecture for building user interfaces. It follows the design principles of the original Elements library while leveraging Rust's safety guarantees and modern ecosystem.
src/
├── lib.rs # Library entry point
├── support/ # Core utilities
│ ├── point.rs # Point, Extent, Axis types
│ ├── rect.rs # Rectangle geometry
│ ├── color.rs # RGBA colors
│ ├── canvas.rs # 2D drawing abstraction
│ ├── font.rs # Font handling
│ └── theme.rs # Theming system
├── element/ # UI element system
│ ├── mod.rs # Element trait
│ ├── context.rs # Render/event context
│ ├── composite.rs # Container elements
│ ├── tile.rs # VTile/HTile layouts
│ ├── align.rs # Alignment elements
│ ├── margin.rs # Margin elements
│ ├── size.rs # Size constraints
│ ├── layer.rs # Layer/Deck stacking
│ ├── label.rs # Text labels
│ ├── button.rs # Button widgets
│ ├── text_box.rs # Text input
│ ├── slider.rs # Slider control
│ ├── dial.rs # Rotary dial/knob
│ ├── checkbox.rs # Checkbox and radio buttons
│ ├── switch.rs # Toggle switches
│ ├── thumbwheel.rs # Thumbwheel control
│ ├── progress.rs # Progress indicators
│ ├── list.rs # List and dropdown
│ ├── menu.rs # Menus and native menu bar
│ ├── tabs.rs # Tab bar
│ ├── tooltip.rs # Tooltips
│ ├── status_bar.rs # Status bar
│ ├── grid.rs # Grid layout
│ ├── floating.rs # Floating elements
│ └── scroll.rs # Scroll view
├── view/ # View management
│ └── mod.rs # Events and input handling
└── host/ # Platform layer
├── macos.rs # macOS (objc2)
├── windows.rs # Windows (Win32)
└── linux.rs # Linux (X11)
tiny-skia - Pure Rust 2D graphicsfontdb / rustybuzz / ttf-parser - Font handling and text shapingbitflags - Modifier key flagsobjc2, objc2-foundation, objc2-app-kitwindows crate with Win32 featuresx11rb for X11 supportAdd to your Cargo.toml:
[dependencies]
mkgraphic = "0.2"
use mkgraphic::element::{label, button, vtile, share};
use mkgraphic::element::margin::margin;
// Create a simple UI
let ui = vtile![
label("Hello, World!"),
margin(10.0, button("Click Me").on_click(|| println!("Clicked!"))),
];
use mkgraphic::element::{vtile, htile, share};
use mkgraphic::element::text_box::text_box;
use mkgraphic::element::slider::slider;
use mkgraphic::element::dial::dial;
// Create interactive controls
let ui = vtile![
text_box()
.placeholder("Enter text...")
.on_change(|text| println!("Text: {}", text)),
htile![
slider().on_change(|v| println!("Slider: {:.2}", v)),
dial().on_change(|v| println!("Dial: {:.2}", v)),
],
];
use mkgraphic::element::{htile, vtile, share};
use mkgraphic::element::align::{halign, valign};
use mkgraphic::element::size::fixed_size;
use mkgraphic::element::label::label;
use mkgraphic::element::button::button;
// Horizontal layout with centered content
let layout = htile![
halign(0.5, label("Centered")),
fixed_size(100.0, 50.0, button("Fixed Size")),
];
use mkgraphic::prelude::*;
fn main() {
// Configure menu bar before creating app
set_native_menu_bar(
native_menu_bar()
.app_name("My App")
.add_menu(native_menu("File")
.add_item(native_menu_item("New")
.shortcut_cmd('n')
.on_select(|| println!("New")))
.add_item(native_menu_item("Open...")
.shortcut_cmd('o'))
.add_item(native_separator())
.add_item(native_menu_item("Save")
.shortcut_cmd('s')))
.add_menu(native_menu("View")
.add_item(native_menu_item("Zoom In")
.shortcut_cmd('+'))
.add_item(native_menu_item("Zoom Out")
.shortcut_cmd('-')))
.include_app_menu(true)
.include_edit_menu(true)
.include_window_menu(true)
);
let mut app = App::new();
// ... create windows
app.run();
}
All UI components implement the Element trait:
pub trait Element: Send + Sync + Any {
fn limits(&self, ctx: &BasicContext) -> ViewLimits;
fn draw(&self, ctx: &Context);
fn hit_test(&self, ctx: &Context, p: Point, leaf: bool, control: bool) -> Option<&dyn Element>;
fn handle_click(&self, ctx: &Context, btn: MouseButton) -> bool;
fn handle_drag(&self, ctx: &Context, btn: MouseButton);
fn handle_key(&self, ctx: &Context, k: KeyInfo) -> bool;
fn handle_text(&self, ctx: &Context, info: TextInfo) -> bool;
fn handle_scroll(&self, ctx: &Context, dir: Point, p: Point) -> bool;
// ... more methods
}
The Context provides access to:
Elements can receive keyboard focus through the focus system:
wants_focus() - Whether the element can receive focusbegin_focus() / end_focus() - Focus lifecycleclear_focus() - Clears focus from all elements (used when clicking elsewhere)| Platform | Backend | Status |
|---|---|---|
| macOS | Cocoa/AppKit via objc2 | Working |
| Windows | Win32 API | Basic |
| Linux | X11 via x11rb | Basic |
Run the elements gallery to see all available widgets:
cargo run --example elements_gallery
# Check compilation
cargo check
# Build
cargo build
# Build with release optimizations
cargo build --release
# Run tests
cargo test
MIT
This project is a Rust translation of the Elements C++ GUI library by Joel de Guzman and Cycfi Research.