Thanks to visit codestin.com
Credit goes to github.com

Skip to content

yingkitw/cocoanut

Repository files navigation

πŸ₯₯ Cocoanut

License Rust Edition Crates.io

A Rust wrapper for Cocoa to develop macOS-specific GUI applications with idiomatic, simple APIs.

Why Cocoanut?

The Problem

Raw objc and cocoa crates require:

  • Manual pointer management
  • Verbose method calls
  • Manual layout calculations
  • No built-in styling system
  • Steep learning curve

The Solution

Cocoanut provides:

  • βœ… Builder Patterns - Fluent, chainable API
  • βœ… Layout System - Declarative VStack/HStack
  • βœ… Design System - Carbon Design System built-in
  • βœ… Type Safety - No raw pointers in public API
  • βœ… Simplicity - Learn macOS GUI development easily

Simplicity in Action

Before (objc/cocoa)

let button = Button::new("Click")?;
button.set_title("Updated")?;
button.set_size(100.0, 50.0)?;
button.set_enabled(true)?;
window.add_subview(button, 10.0, 10.0, 100.0, 50.0)?;

After (Cocoanut)

let button = Button::builder()
    .title("Updated")
    .size(100.0, 50.0)
    .enabled(true)
    .build()?;

let vstack = VStack::new()
    .spacing(Spacing::standard())
    .alignment(Alignment::Center);

Comparison Table

Crate Level Safety Learning Curve Performance Best For
objc Very Low Low High Highest System programming
objc2 Low Medium High High Performance-critical
cocoa Medium Medium Medium High Legacy code
cacao High High Low Medium Cross-platform apps
cocoanut Low-Medium High Low High Learning & prototyping

Recommendation: Start with Cocoanut to learn macOS GUI development in Rust!

Quick Start

cargo add cocoanut
use cocoanut::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a window
    let window = Window::builder()
        .title("My App")
        .size(600.0, 400.0)
        .center()
        .build()?;
    
    // Run the app - components display automatically!
    app("MyApp")
        .with_window(window)
        .run()?;
    
    Ok(())
}

That's it! The app will display a window with Button, Label, and TextField components automatically.

Run it:

cargo run

Features

  • βœ… Builder Patterns - Fluent, chainable API
  • βœ… Layout System - VStack/HStack for composition
  • βœ… Carbon Design System - Professional styling
  • βœ… Window Management - Native macOS windows
  • βœ… Menu System - Application menus
  • βœ… Controls - Button, Label, TextField
  • βœ… Event Handling - User interactions
  • βœ… Drawing - Core Graphics integration
  • βœ… Type Safety - No raw pointers
  • βœ… Memory Safe - Rust ownership system

Examples

Run examples to see Cocoanut in action:

# Minimal app with components
cargo run --example minimal_app

# Basic window
cargo run --example basic_window

# Menu application
cargo run --example menu_app

# Comprehensive component demo
cargo run --example comprehensive_app

# Window builder patterns
cargo run --example window_builder_example

# Event binding example
cargo run --example event_binding_example

# Phase demos (Streamlit-inspired widgets)
cargo run --example phase1_display_demo
cargo run --example phase2_input_widgets_demo
cargo run --example phase3_advanced_layouts_demo
cargo run --example phase4_state_caching_demo
cargo run --example phase5_advanced_features_demo

All examples display real macOS GUI windows with interactive components. Press Cmd+Q to quit.

See docs/examples/EXAMPLES_AND_TESTS.md for complete examples guide.

Requirements

  • macOS 10.15 or later
  • Rust 1.70 or later
  • Xcode command line tools

Building

cargo build

Running Examples

cargo run --example basic_window
cargo run --example menu_app

Testing

cargo test

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.

πŸ“š Documentation

Complete documentation is available:

Start here: DOCUMENTATION.md - Complete documentation index

Core Documentation

Feature Guides

Docs Folder

πŸ“‹ Changelog

0.2.0 (Current)

  • βœ… 67 Streamlit-Inspired Widgets - Complete Phase 1-5 migration
    • Phase 1: 21 display elements (Write, Text, Markdown, etc.)
    • Phase 2: 21 input widgets (TextInput, Button, Checkbox, etc.)
    • Phase 3: 12 layout containers (Columns, Tabs, Expander, etc.)
    • Phase 4: 8 state & caching widgets (SessionState, DataCache, etc.)
    • Phase 5: 5 advanced features (Navigation, CustomComponent, etc.)
  • βœ… Macro Refactoring - 83 lines of boilerplate eliminated
    • disabled_field!() macro for 12 widgets
    • label_field!() macro for 5 widgets
    • 282 tests passing (100%)
  • βœ… Trait-Based Architecture - Drawable, Textual, Positionable, Clickable traits
  • βœ… macOS Features - Native Feel, Accessibility, Dark Mode, Touch Bar, Continuity
  • βœ… Enhanced Examples - 4 unique purpose-built examples with different GUIs
  • βœ… Real macOS GUI - No mocking, full NSApplication support
  • βœ… Production Ready - Zero compilation errors, all tests passing

0.1.0

  • Initial release
  • Basic window management
  • Menu system
  • UI controls (Button, Label, TextField)
  • Event handling
  • Drawing utilities
  • Comprehensive test suite

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages