UI Native is a declarative, reactive UI framework for building native macOS applications with Rust. It provides a SwiftUI-like API with chain-able methods, reactive state management, and 20+ pre-built components.
ui-native/
├── src/
│ ├── lib.rs # Library entry point
│ ├── examples.rs # Example usage
│ ├── core/ # Core systems
│ │ ├── mod.rs
│ │ ├── types.rs # Core type definitions
│ │ ├── state.rs # Reactive state management
│ │ ├── layout.rs # Layout system (Size, Constraints)
│ │ ├── style.rs # Style system (Color, Font, Shadow)
│ │ └── event.rs # Event handling system
│ ├── builder/ # UI builder
│ │ ├── mod.rs
│ │ ├── builder.rs # Core UIBuilder with chainable methods
│ │ └── rect.rs # Rect/Box component
│ └── components/ # UI components (20+)
│ ├── mod.rs
│ ├── button.rs
│ ├── label.rs
│ ├── text_field.rs
│ ├── image.rs
│ ├── slider.rs
│ ├── progress.rs
│ ├── checkbox.rs
│ ├── radio.rs
│ ├── toggle.rs
│ ├── scroll.rs
│ ├── list.rs
│ ├── table.rs
│ ├── picker.rs
│ ├── date_picker.rs
│ ├── color_picker.rs
│ ├── stepper.rs
│ ├── segmented.rs
│ ├── toolbar.rs
│ ├── tabs.rs
│ ├── split.rs
│ ├── search.rs
│ ├── menu.rs
│ ├── alert.rs
│ └── stack.rs
├── examples/
│ ├── counter.rs # Counter app example
│ ├── form.rs # Form example
│ └── gallery.rs # Gallery example
├── Cargo.toml # Package configuration
└── README.md # Documentation
Reactive state system with automatic updates:
pub struct State<T> {
value: Rc<RefCell<T>>,
listeners: Rc<RefCell<Vec<Box<dyn Fn(&T)>>>>,
}
pub fn use_state<T>(factory: F) -> State<T>Features:
- Reactive updates on write
- Subscribe to changes
- Clone-able for use in closures
- Automatically notifies listeners
Flexible sizing and constraints:
pub enum Size {
Fixed(f64), // Fixed size
Percent(f64), // Percentage of parent
Fill, // Fill available space
Auto, // Based on content
}Comprehensive styling:
pub struct Color { r, g, b, a }
pub enum FontWeight { Thin, Light, Regular, Bold, ... }
pub struct Shadow { offset, blur, spread, color }
pub struct Border { width, color }Type-safe event handling:
pub trait EventHandler {
fn handle(&mut self, sender: &AnyObject);
}
pub fn create_action_target<F>(callback: F) -> Retained<ActionTarget>The core builder provides chainable methods for any NSView-based component:
pub struct UIBuilder<T = NSView> {
view: Retained<T>,
children: Vec<Retained<NSView>>,
constraints: Vec<Retained<NSLayoutConstraint>>,
}Methods:
- Layout:
width(),height(),center(),padding(),margin() - Style:
background(),color(),corner_radius(),shadow(),alpha() - Font:
font_size(),font_weight() - Structure:
child(),text_child() - Layout modes:
horizontal(),vertical(),spacing()
- Rect/Box - Container view
- Label - Text display
- Button - Clickable button
- TextField - Text input
- Image - Image display
- Slider - Value slider
- Stepper - Increment/decrement
- Checkbox - Toggle checkbox
- RadioButton - Radio selection
- Toggle - On/off switch
- Picker - Dropdown picker
- SegmentedControl - Multi-segment
- DatePicker - Date selection
- ColorPicker - Color selection
- SearchField - Search input
- Stack - Horizontal/vertical layout
- ScrollView - Scrollable container
- SplitView - Split panel
- TabView - Tabbed interface
- ListView - List display
- TableView - Table display
- ProgressBar - Progress indicator
- Toolbar - Window toolbar
- Menu - Context menu
- Alert - Alert dialog
Build UIs by describing what you want:
rect()
.width(Size::fill())
.background((240, 240, 245))
.child(label)Every method returns self for fluent API:
Button::new()
.title("Click Me")
.width(Size::Fixed(100.0))
.height(Size::Fixed(44.0))
.background((0, 122, 255))
.corner_radius(8.0)
.on_press(|_| {})Leverage Rust's type system:
UIBuilder<NSButton> // Button-specific methods
UIBuilder<NSView> // Generic view methodsState changes automatically update UI:
let count = use_state(|| 0);
*count.write() += 1; // Triggers UI updateDirect bindings to AppKit with minimal overhead:
unsafe {
msg_send![view, setBackgroundColor: color]
}let count = use_state(|| 0);let ui = rect()
.child(label)
.child(button);Button::new()
.on_press(move |_| {
*count.write() += 1;
})let view = ui.build();
window.setContentView(Some(&view));- objc2: Safe Objective-C bindings
- objc2-foundation: Foundation framework types
- objc2-app-kit: AppKit UI components
- Uses
Retained<T>for automatic reference counting - Safe ownership with Rust's type system
- Proper cleanup on drop
- Zero-cost abstractions over AppKit
- Direct native calls
- Minimal allocations
- Efficient state updates
- Type-safe wrappers around unsafe code
- Compile-time guarantees
- Proper lifetime management
- Safe event handling
- Animations - Smooth transitions and animations
- Gestures - Pan, pinch, rotate recognizers
- Accessibility - Full a11y support
- Custom Drawing - Canvas API for custom graphics
- Layout Algorithms - Advanced layout systems
- Performance - Optimize rendering and updates
- Testing - Comprehensive test suite
- Documentation - API docs and tutorials
- Better state binding to UI elements
- Diffing algorithm for efficient updates
- Virtual DOM for complex UIs
- Theme system for consistent styling
- Animation DSL
- Custom components macro
- Follow Rust API guidelines
- Comprehensive documentation
- Unit tests for core functionality
- Integration tests for components
- Example apps for common patterns
- Profile and optimize hot paths
- Minimize allocations
- Efficient state propagation
- Lazy evaluation where possible
- Minimize unsafe code
- Document all unsafe blocks
- Maintain invariants
- Proper error handling
Advantages:
- Type safety
- Memory safety
- Modern API design
- Reactive state
Trade-offs:
- Learning curve
- Some boilerplate
Advantages:
- Rust ecosystem integration
- More explicit control
- No Xcode required
Trade-offs:
- Manually managed updates
- Less mature tooling
Advantages:
- True native performance
- Small binary size
- No web tech overhead
Trade-offs:
- macOS only
- More complex API
UI Native provides a modern, safe, and efficient way to build native macOS applications with Rust. It combines the best of declarative UI frameworks with the performance and safety of Rust, while maintaining direct access to native AppKit capabilities.
The framework is designed for production use with professional code standards, comprehensive documentation, and a focus on developer experience.