Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Cocoanut
A Rust wrapper for Cocoa to develop macOS-specific GUI applications.
Features
- Builder Patterns: Fluent, chainable API for creating UI components
- Layout System: VStack/HStack for declarative UI composition
- Carbon Design System: Professional styling and theming out of the box
- Window Management: Create and manage native macOS windows
- Menu System: Build application menus and context menus
- Controls: Native macOS UI controls and widgets
- Event Handling: Comprehensive event system for user interactions
- Drawing: Core Graphics integration for custom drawing
- Thread Safety: Safe cross-thread operations
- Memory Safe: Rust's ownership system prevents common GUI bugs
Comparison with Other Rust GUI Crates
Cocoanut is designed as an educational wrapper around Cocoa, positioned between raw Objective-C calls and high-level frameworks. Here's how it compares to other Rust crates for macOS GUI development:
objc (Original)
- Level: Very low-level, raw FFI
- Features: Direct access to Objective-C runtime, manual memory management
- Use Case: System programming, performance-critical apps
- Example:
use ;
let app: *mut Object = msg_send!;
objc2 (Modern Successor)
- Level: Low-level but with better safety
- Features: Improved memory safety, better error handling, ARC support
- Use Case: When you want low-level control with better safety
- Example:
use ;
let app: *mut Object = msg_send!;
cocoa (Mid-level Wrapper)
- Level: Medium-level abstraction
- Features: Pre-built wrappers for common Cocoa classes, some memory management automation
- Use Case: When you want some abstraction but still need control
- Example:
use ;
let app = sharedApplication;
cacao (High-level Framework)
- Level: High-level abstraction
- Features: Rust-native API design, automatic memory management, cross-platform
- Use Case: Modern apps, cross-platform development
- Example:
use ;
;
cocoanut (Our Crate)
- Level: Low to mid-level abstraction with high-level ergonomics
- Features: Builder patterns, layout system, Carbon Design System, fluent API, educational approach
- Use Case: Learning Cocoa development, rapid prototyping, modern Rust GUI apps
- Example:
use *;
let app = new?;
let window = new?;
let button = builder
.title
.size
.build?;
let vstack = new
.spacing
.alignment;
Comparison Table
| Crate | Level | Safety | Learning Curve | Performance | Maintenance |
|---|---|---|---|---|---|
objc |
Very Low | Low | High | Highest | Active |
objc2 |
Low | Medium | High | High | Active |
cocoa |
Medium | Medium | Medium | High | Stagnant |
cacao |
High | High | Low | Medium | Active |
cocoanut |
Low-Medium | High | Low | High | Active |
Recommendation
- For learning: Start with
cocoanutorcacao - For production: Use
cacaofor modern apps,objc2for performance-critical apps - For legacy: Use
cocoaif you need specific Cocoa features - For system programming: Use
objcorobjc2
What Makes Cocoanut Different?
Simplified API Over Raw objc/cocoa
Cocoanut provides idiomatic Rust abstractions that make GUI development easier:
- Builder Patterns: Fluent API instead of multiple method calls
- Layout System: Declarative VStack/HStack instead of manual positioning
- Design System: Carbon Design System colors and typography built-in
- Type Safety: No raw pointers or unsafe code in public API
See SIMPLIFICATION_GUIDE.md for detailed comparisons.
Quick Start
Add Cocoanut to your Cargo.toml:
[]
= "0.1.0"
Create a simple window:
use *;
Create UI with builders and layout:
use *;
let button = builder
.title
.size
.build?;
let label = builder
.text
.build?;
let vstack = new
.spacing
.alignment;
Architecture
graph TB
A[Application] --> B[Window]
A --> C[Menu]
B --> D[Controls]
B --> E[Events]
C --> F[MenuItem]
D --> G[Button]
D --> H[Label]
D --> I[TextField]
E --> J[EventManager]
E --> K[EventHandler]
B --> L[Drawing]
L --> M[Color]
L --> N[Point]
L --> O[Size]
L --> P[Rect]
Examples
Basic Window
use *;
Menu Application
use *;
Controls
use *;
Custom Drawing
use *;
API Reference
Application
The main application class for managing the macOS application lifecycle.
let app = new?;
app.run?;
Window
Create and manage native macOS windows.
let window = new?;
window.show?;
window.set_title?;
window.set_size?;
Menu System
Build application menus and context menus.
let menu = new?;
let item = new?;
menu.add_item?;
Controls
Native macOS UI controls and widgets.
let button = new?;
let label = new?;
let text_field = new?;
Drawing
Core Graphics integration for custom drawing.
let context = new?;
let color = rgb?;
let rect = from_xywh?;
context.set_fill_color?;
context.fill_rect?;
Events
Comprehensive event system for user interactions.
let mut manager = new;
manager.add_handler;
let event = ButtonClick;
manager.process_event?;
Requirements
- macOS 10.15 or later
- Rust 1.70 or later
- Xcode command line tools
Building
Running Examples
Testing
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Changelog
0.1.0
- Initial release
- Basic window management
- Menu system
- UI controls (Button, Label, TextField)
- Event handling
- Drawing utilities
- Comprehensive test suite