Iced GUI Library

repository·master·Indexed 12 days ago

https://github.com/iced-rs/iced

A cross-platform, type-safe GUI library for Rust inspired by The Elm Architecture. It provides a reactive programming model for building desktop and web applications with a modular approach, including foundational types in iced_core and low-level drawing capabilities via the iced_wgpu backend.

Tokens
61.1K
Snippets
235
Records
289
Agent score
99%

What's inside Iced

  1. Overview of iced_core

    master
    The iced_core crate provides the foundational, reusable types used across the Iced ecosystem. It is designed as the starting point for building an Iced runtime and contains essential geometric and measurement data types such as Point, Rectangle, and Length.
  2. Overview of iced_runtime

    master
    The iced_runtime crate is responsible for building a functional runtime on top of iced_core. While iced_core provides the fundamental abstractions for the Iced ecosystem, iced_runtime provides the execution engine necessary to actually run an application.
  3. Overview of the `iced_wgpu` renderer

    master

    iced_wgpu is a renderer for iced_runtime based on the wgpu graphics library. It is currently the default renderer for Iced on native platforms. Because it uses wgpu, it provides cross-platform support for modern graphics backends including:

    • Vulkan
    • Metal
    • DX12
    • OpenGL
    • WebGPU
  4. What is Iced?

    master

    Iced is a cross-platform GUI library for Rust focused on simplicity and type-safety. It is inspired by The Elm Architecture and provides a reactive programming model.

    Key features include:

    • Cross-platform support: Windows, macOS, Linux, and the Web.
    • Responsive layout and built-in widgets (text inputs, scrollables, etc.).
    • Async support: First-class support for async actions using futures.
    • Modular ecosystem: Includes a renderer-agnostic native runtime, windowing shell, and built-in renderers (iced_wgpu for Vulkan/Metal/DX12 and iced_tiny_skia for software fallback).
    • Debug tooling: Performance metrics and time traveling capabilities.

    Note: Iced is currently experimental software.

  5. Use the PaneGrid widget

    master

    The PaneGrid widget is a complex layout component used for creating interfaces with multiple panes. It supports the following interactive features:

    • Splitting: Create both vertical and horizontal splits.
    • Resizing: Adjust pane boundaries using the mouse.
    • Reorganizing: Move panes around using drag and drop.
    • Navigation: Tracks the last active pane and supports hotkeys.
    • Customization: Configurable modifier keys for interactions.
    • Programmatic Control: The API allows you to perform actions like split, swap, and resize via code rather than just user interaction.
  6. How The Elm Architecture works in Iced

    master

    Iced is inspired by The Elm Architecture, which requires splitting your user interface into four distinct, interacting concepts:

    1. State: The data representing the current condition of your application.
    2. Messages: An enumeration of user interactions or meaningful events (e.g., button clicks).
    3. View logic: A function that transforms your State into a layout of widgets. These widgets are configured to produce Messages when interacted with.
    4. Update logic: A function that receives Messages and modifies the State accordingly.

    When you run an Iced application, the runtime handles the lifecycle: it executes the view logic to layout widgets, processes system events to produce messages, and triggers the update logic to keep the state in sync with the UI.

    // 1. State
    struct Counter {
        value: i32,
    }
    
    // 2. Messages
    enum Message {
        Increment,
        Decrement,
    }
    
    // 3. View logic
    impl Counter {
        pub fn view(&self) -> Column<'_, Message> {
            column![
                button("+").on_press(Message::Increment),
                text(self.value).size(50),
                button("-").on_press(Message::Decrement),
            ]
        }
    
        // 4. Update logic
        pub fn update(&mut self, message: Message) {
            match message {
                Message::Increment => self.value += 1,
                Message::Decrement => self.value -= 1,
            }
        }
    }
    
    // Execution
    fn main() -> iced::Result {
        iced::run("A cool counter", Counter::update, Counter::view)
    }
  7. Implement a custom event loop with `iced_winit`

    master
    If you need full control over the windowing lifecycle or want to integrate iced into an existing winit event loop, use the conversion module. This module provides the necessary tools to bridge iced_native logic with a custom winit event loop implementation.
  8. Understand the Iced application structure via the Tour example

    master

    The Tour example demonstrates how to build cross-platform GUIs that work on both native platforms and the web. The application logic is organized into four primary pillars:

    1. State: The data representing the current condition of the UI.
    2. Messages: The events or signals that trigger changes.
    3. Update logic: The function that handles messages to mutate the state.
    4. View logic: The function that describes how the state is rendered into widgets.

    All the implementation details for this example are contained within the src/main.rs file of the tour package.

  9. Quickstart with `iced_winit` using the `Application` trait

    master
    To quickly start developing a GUI application using winit, implement the renderer-agnostic Application trait provided by iced_winit. Once implemented, you can run your application with a single function call. This is the recommended path for most users who want to leverage winit as their windowing backend without manually managing the event loop.
  10. Set up Iced development environment on NixOS using Nix Flakes

    master

    For users preferring Nix Flakes, you can use a flake.nix to create a dev shell. This method supports multiple systems and provides a consistent environment for Iced development.

    To activate the environment, run:

    nix develop
    {
      inputs = {
        nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
        systems.url = "github:nix-systems/default";
      };
    
      outputs = { nixpkgs, systems, ... }: 
        let
          eachSystem = nixpkgs.lib.genAttrs (import systems);
          pkgsFor = nixpkgs.legacyPackages;
        in {
          devShells = eachSystem (system: 
            let
              pkgs = pkgsFor.${system};
              dlopenLibraries = with pkgs; [
                libxkbcommon
    
                # GPU backend
                vulkan-loader
                # libGL
    
                # Window system
                wayland
                # xorg.libX11
                # xorg.libXcursor
                # xorg.libXi
              ];
            in {
              default = pkgs.mkShell {
                nativeBuildInputs = with pkgs; [
                  cargo
                  rustc
                ];
    
                # additional libraries that your project
                # links to at build time, e.g. OpenSSL
                buildInputs = [];
    
                env.RUSTFLAGS = "-C link-arg=-Wl,-rpath,${nixpkgs.lib.makeLibraryPath dlopenLibraries}";
              };
            });
        };