### Basic Hello World Example Source: https://github.com/matinaniss/gpui-book/blob/main/src/getting-started/manual-project.md Implement the basic 'Hello World' example in your main.rs file. This snippet is a placeholder for the actual code. ```rust {{ #include snippets/hello_world.rs }} ``` -------------------------------- ### Install create-gpui-app CLI Source: https://github.com/matinaniss/gpui-book/blob/main/src/getting-started/create-gpui-app.md Install the official CLI tool using Cargo. ```properties cargo install create-gpui-app ``` -------------------------------- ### Monolithic App File Structure Source: https://github.com/matinaniss/gpui-book/blob/main/src/getting-started/create-gpui-app.md Example file structure for a GPUI application created with a monolithic setup. ```properties my-app ├── src │ └── main.rs ├── Cargo.toml └── README.md ``` -------------------------------- ### Workspace App File Structure Source: https://github.com/matinaniss/gpui-book/blob/main/src/getting-started/create-gpui-app.md Example file structure for a GPUI application created with a workspace setup. ```properties my-app ├── Cargo.toml ├── crates │ └── my-app │ ├── Cargo.toml │ └── src │ └── main.rs └── README.md ``` -------------------------------- ### Build GPUI Book with mdBook Source: https://github.com/matinaniss/gpui-book/blob/main/README.md Use this command to build the GPUI Book locally. Ensure you have the mdBook CLI tool installed. ```properties mdbook build ``` -------------------------------- ### Rust Hello World Implementation Source: https://github.com/matinaniss/gpui-book/blob/main/src/examples/hello-world.md This snippet shows the basic implementation of a 'Hello World!' program in Rust. It is a fundamental example for beginners. ```rust fn main() { println!("Hello, world!"); } ``` -------------------------------- ### RenderOnce Implementation Example Source: https://github.com/matinaniss/gpui-book/blob/main/src/rendering/render-once.md This snippet demonstrates a basic implementation of the RenderOnce trait. It shows how to define a component that is rendered only once. ```rust use gpui::{prelude::*, Model, View}; struct MyComponent; impl Model for MyComponent { fn update(&mut self, _cx: &mut gpui::AppContext) {} } impl View for MyComponent { fn ui_name(&self) -> &'static str { "MyComponent" } fn render(&self, _cx: &mut gpui::ViewContext) -> gpui::ElementBox { // In a real application, you would return UI elements here. // For this example, we'll just return an empty element. gpui::div().into_any() } } // Note: The actual usage of RenderOnce would involve integrating this component // into a larger GPUI application structure, likely within a retained mode view. // This example focuses solely on the trait implementation structure. ``` -------------------------------- ### Run a GPUI Application Source: https://github.com/matinaniss/gpui-book/blob/main/src/architecture/application.md The `run` function starts the application and executes a callback once the application has loaded. This callback receives a mutable reference to your `App` struct, allowing you to control application aspects like opening windows. ```rust use gpui::App; app.run(move |cx: &mut App| { // Your application logic here // For example, open a window: // cx.open_window(Window::new(WindowConfig::default())); }); ``` -------------------------------- ### Bind Keys to Actions Source: https://github.com/matinaniss/gpui-book/blob/main/src/interactivity/keyboard.md Create a `KeyBinding` struct using `KeyBinding::new` and then bind it to the application using `App::bind_keys`. This example binds the Enter key to the `Enter` action with no specific key context. ```rust let mut key_binding = KeyBinding::new(Keystroke::enter(), Enter); app.bind_keys(key_binding, None); ``` -------------------------------- ### Get Window Bounds Source: https://github.com/matinaniss/gpui-book/blob/main/src/architecture/window.md Retrieves the origin and size of the platform window. This snippet requires the 'bounds.rs' file. ```rust use gpui::Window; fn example(window: &Window) { let bounds = window.bounds(); println!("Window bounds: {:?}", bounds); } ``` -------------------------------- ### Handle Actions with on_action Callback Source: https://github.com/matinaniss/gpui-book/blob/main/src/interactivity/keyboard.md Bind a callback function to an action using `on_action` on an `InteractiveElement`. The element must be focused using `track_focus` for the action to be dispatched. This example prints a message when the `Enter` action is triggered. ```rust let focus_handle = track_focus(); let _ = on_action(focus_handle, Enter, |event| { println!("Enter action dispatched!"); }); ``` -------------------------------- ### On Hover Event Source: https://github.com/matinaniss/gpui-book/blob/main/src/interactivity/mouse.md Binds a callback function to be executed when the user's mouse pointer enters or leaves an element. The callback receives a boolean indicating whether the hover has started (true) or ended (false), along with the Window and App context. ```APIDOC ## On Hover ### Description Allows you to bind a callback when the user hovers on and off the element. The callback is fired when the user enters a hover and leaves the hover. ### Parameters The function takes a closure that supplies a `bool` which represents true if the hover has started and false if the hover has ended, it also supplies the `Window` and `App`. ### Example ```rust {{ #include snippets/on_hover.rs }} ``` ``` -------------------------------- ### Handle Mouse Hovers with on_hover Source: https://github.com/matinaniss/gpui-book/blob/main/src/interactivity/mouse.md Use `on_hover` to bind a callback function that executes when a user's mouse pointer enters or leaves an element. The callback receives a boolean indicating hover start (true) or end (false), along with the `Window` and `App`. ```rust use gpui::{InteractiveElement, Window, App}; struct MyComponent; impl InteractiveElement for MyComponent { fn on_hover(self, handler: impl Fn(bool, &Window, &mut App) + 'static) -> Self { // ... implementation details ... self } // ... other methods ... } // Example usage: let my_element = MyComponent.on_hover(|is_hovering, window, app| { if is_hovering { println!("Mouse entered element."); } else { println!("Mouse left element."); } }); ``` -------------------------------- ### Mutably Accessing Global with Default Fallback Source: https://github.com/matinaniss/gpui-book/blob/main/src/state-management/global.md Get a mutable reference to the global state. If not set, it initializes the global with the type's default value. ```rust use gpui::Global; #[derive(Default)] struct Settings; impl Global for Settings {} fn main() { let settings = Settings::get_or_default_mut(); } ``` -------------------------------- ### Get Mouse Position Relative to Window Source: https://github.com/matinaniss/gpui-book/blob/main/src/architecture/window.md Obtains the x and y coordinates of the mouse cursor relative to the window's client area. This snippet requires the 'mouse_position.rs' file. ```rust use gpui::Window; fn example(window: &Window) { let mouse_pos = window.mouse_position(); println!("Mouse position: {:?}", mouse_pos); } ``` -------------------------------- ### Create a GPUI Application Source: https://github.com/matinaniss/gpui-book/blob/main/src/architecture/application.md Use the `new` function to instantiate the Application, which is the entry point for your GPUI application. ```rust use gpui::Application; let app = Application::new().unwrap(); ``` -------------------------------- ### Create Monolithic GPUI App Source: https://github.com/matinaniss/gpui-book/blob/main/src/getting-started/create-gpui-app.md Use the CLI to create a new GPUI application with a monolithic structure. Navigate into the newly created project directory. ```properties create-gpui-app --name my-app cd my-app ``` -------------------------------- ### Open a Window with App Source: https://github.com/matinaniss/gpui-book/blob/main/src/architecture/app.md Opens a new window using the App's open_window function. Requires WindowOptions and a callback to build the root view. ```rust use gpui::{App, WindowOptions}; App::open_window(WindowOptions::default(), |cx| { // Build your root view here cx.add_view(|_| /* your view */) }); ``` -------------------------------- ### Create Workspace GPUI App Source: https://github.com/matinaniss/gpui-book/blob/main/src/getting-started/create-gpui-app.md Use the CLI to create a new GPUI application with a workspace structure. Navigate into the newly created project directory. ```properties create-gpui-app --workspace --name my-app cd my-app ``` -------------------------------- ### Render String in GPUI Source: https://github.com/matinaniss/gpui-book/blob/main/src/elements/text.md Shows how to render a String. Use sparingly due to potential heap allocations on re-renders. Consider SharedString for better performance. ```rust use gpui::prelude::*; struct App; impl App { fn render(&mut self, cx: &mut WindowContext) -> Element { let mut my_string = String::from("Hello, world!"); my_string.push_str("!"); Text::new(my_string).into() } } ``` -------------------------------- ### Create a Div Element Source: https://github.com/matinaniss/gpui-book/blob/main/src/elements/div.md Demonstrates the basic creation of a Div element in GPUI. This is the foundational step for building UI structures. ```rust use gpui::div; div() ``` -------------------------------- ### Render &'static str in GPUI Source: https://github.com/matinaniss/gpui-book/blob/main/src/elements/text.md Demonstrates rendering a static string slice. This is the most performant option as it avoids heap allocations. ```rust use gpui::prelude::*; struct App; impl App { fn render(&mut self, cx: &mut WindowContext) -> Element { Text::new("Hello, world!").into() } } ``` -------------------------------- ### Create New Rust Project Source: https://github.com/matinaniss/gpui-book/blob/main/src/getting-started/manual-project.md Use cargo to create a new Rust project and navigate into its directory. ```properties cargo new my-app cd my-app ``` -------------------------------- ### Create a GPUI Entity Source: https://github.com/matinaniss/gpui-book/blob/main/src/state-management/entity.md Creates a new Entity with the specified initial state. This is the basic way to introduce state into your application's context. ```rust let entity = Entity::new(0i32); ``` -------------------------------- ### Apply Flexbox and Column Direction with Utility API Source: https://github.com/matinaniss/gpui-book/blob/main/src/styling/index.md Use the utility CSS-like styling functions for a concise way to apply styles like flexbox and column direction. ```rust impl Render for SomeView { fn render(&mut self, _window: &mut Window, _cx: &mut Context) -> impl IntoElement { div().flex().flex_col().child("Hello").child("World") } } ``` -------------------------------- ### Create an Img Element from a File Path Source: https://github.com/matinaniss/gpui-book/blob/main/src/elements/img.md This snippet demonstrates creating an `img` element using a file system path. It utilizes the `CARGO_MANIFEST_DIR` environment variable to locate an image file in the project root. ```rust use gpui::{prelude::*, ImageSource}; let img = ImageSource::new( std::path::Path::new(&format!( "{}/image.png", std::env::var("CARGO_MANIFEST_DIR").unwrap() )) ); img(img) ``` -------------------------------- ### Add GPUI from Git Repository Source: https://github.com/matinaniss/gpui-book/blob/main/src/getting-started/manual-project.md Add the gpui crate as a dependency from a specific git repository to your project's Cargo.toml file. ```properties cargo add gpui --git https://github.com/zed-industries/zed ``` -------------------------------- ### Add GPUI from Crates.io Source: https://github.com/matinaniss/gpui-book/blob/main/src/getting-started/manual-project.md Add the gpui crate as a dependency to your project's Cargo.toml file using cargo add. ```properties cargo add gpui ``` -------------------------------- ### Bind Global Actions with App Source: https://github.com/matinaniss/gpui-book/blob/main/src/architecture/app.md Binds a callback to an action that can be fired globally across the application using App's on_action function. ```rust use gpui::Action; App::on_action(|cx, action: &dyn Action| { // Handle the action here }); ``` -------------------------------- ### Spawn Async Task with App Source: https://github.com/matinaniss/gpui-book/blob/main/src/architecture/app.md Enqueues a future on the main thread using App's spawn function. The future receives an AsyncApp for accessing application state. ```rust use gpui::{AsyncApp, AsyncFnOnce}; App::spawn(async move |cx: AsyncApp| { // Perform asynchronous operations here }); ``` -------------------------------- ### Render StyledText in GPUI Source: https://github.com/matinaniss/gpui-book/blob/main/src/elements/text.md Demonstrates styling specific ranges of text using StyledText and TextRuns. Use this when different parts of the text require distinct styles. ```rust use gpui::{prelude::*, text::{TextRun, StyledText}, style::Color, EntityId}; struct App; impl App { fn render(&mut self, cx: &mut WindowContext) -> Element { StyledText::new([ TextRun::new("This is normal text.", None), TextRun::new(" This is bold text.", Some(Style::new().bold())), TextRun::new(" And this is red.", Some(Style::new().text_color(Color::red()))), ]) .into() } } ``` -------------------------------- ### Render InteractiveText in GPUI Source: https://github.com/matinaniss/gpui-book/blob/main/src/elements/text.md Shows how to make specific text ranges interactive with click and hover listeners using InteractiveText. Requires an ElementId and a StyledText component. ```rust use gpui::{prelude::*, text::{TextRun, StyledText}, style::Color, EntityId}; struct App; impl App { fn render(&mut self, cx: &mut WindowContext) -> Element { let interactive_id = EntityId::new(); InteractiveText::new(interactive_id, StyledText::new([ TextRun::new("Click ", None), TextRun::new("here", Some(Style::new().underline().text_color(Color::blue()))), TextRun::new(" to do something.", None), ])) .into() } } ``` -------------------------------- ### Create a New Animation Source: https://github.com/matinaniss/gpui-book/blob/main/src/animations/animation.md Use the `new` function to create an animation with a specified duration. This animation can then be applied to elements. ```rust let animation = Animation::new(Duration::from_millis(100)); ``` -------------------------------- ### Add GPUI Crate Dependency Source: https://github.com/matinaniss/gpui-book/blob/main/src/getting-started/index.md Specify GPUI as a dependency in your Cargo.toml file. You can use a specific version from crates.io or a git dependency from the Zed repository. ```toml gpui = { version = "*" } # Specify a specific version # OR gpui = { git = "https://github.com/zed-industries/zed" } ``` -------------------------------- ### Render SharedString in GPUI Source: https://github.com/matinaniss/gpui-book/blob/main/src/elements/text.md Illustrates using SharedString for efficient text rendering. It's an immutable string that can be cloned cheaply, avoiding heap allocations in the render function. ```rust use gpui::prelude::*; struct App { shared_string: SharedString, } impl App { fn render(&mut self, cx: &mut WindowContext) -> Element { Text::new(self.shared_string.clone()).into() } } ``` -------------------------------- ### Accessing the Background Executor Source: https://github.com/matinaniss/gpui-book/blob/main/src/async/background-executor.md Retrieves a reference to the platform's BackgroundExecutor. This is the entry point for using background task functionalities. ```rust use gpui::executor::BackgroundExecutor; let background_executor = BackgroundExecutor::get(); ``` -------------------------------- ### Spawning a Task with Foreground Executor Source: https://github.com/matinaniss/gpui-book/blob/main/src/async/foreground-executor.md Enqueues a Future to run on the main thread using the foreground executor. A Task is returned, which can be cancelled by dropping or allowed to complete using detach. ```rust use gpui::executor::foreground_executor; let executor = foreground_executor(); let task = executor.spawn(|_| async { // Your async code here // This will run on the main thread // Avoid long-running blocking operations 42 }); // To let the task run to completion without cancellation on drop: // task.detach(); ``` -------------------------------- ### On Click Event Source: https://github.com/matinaniss/gpui-book/blob/main/src/interactivity/mouse.md Binds a callback function to be executed when the user performs a mouse left click on an element. The callback is triggered upon releasing the left mouse button and receives a ClickEvent, Window, and App context. ```APIDOC ## On Click ### Description Allows you to bind a callback when the user completes a mouse left click on the element. The callback is fired when the user releases the left click. ### Parameters The function takes a closure that supplies a `ClickEvent`, `Window`, and `App`. ### Example ```rust {{ #include snippets/on_click.rs }} ``` ``` -------------------------------- ### Creating a Timer Task Source: https://github.com/matinaniss/gpui-book/blob/main/src/async/background-executor.md Creates a `Task<()>` that completes after a specified `Duration`. This task can be awaited within a future, allowing for delayed operations. ```rust use gpui::executor::BackgroundExecutor; use std::time::Duration; let background_executor = BackgroundExecutor::get(); let timer_task = background_executor.timer(Duration::from_secs(5)); // You can then await this task in an async block or function: // background_executor.spawn(async { // timer_task.await; // println!("Timer finished after 5 seconds"); // }); ``` -------------------------------- ### Handle Mouse Clicks with on_click Source: https://github.com/matinaniss/gpui-book/blob/main/src/interactivity/mouse.md Use `on_click` to bind a callback function that executes when a user releases the left mouse button on an element. This requires the `StatefulInteractiveElement` trait and a stable element ID. ```rust use gpui::{ClickEvent, InteractiveElement, State, StateView, Window, App}; struct MyComponent; impl InteractiveElement for MyComponent { fn on_click(self, handler: impl Fn(ClickEvent, &Window, &mut App) + 'static) -> Self { // ... implementation details ... self } // ... other methods ... } // Example usage: let my_element = MyComponent.on_click(|event, window, app| { println!("Element clicked!"); }); ``` -------------------------------- ### Define Actions with actions Macro Source: https://github.com/matinaniss/gpui-book/blob/main/src/interactivity/keyboard.md Use the `actions` macro to define a namespace and identifiers for new actions. This snippet creates a single action named `Enter` within the `actions_namespace`. ```rust actions! { namespace actions_namespace, Enter } ``` -------------------------------- ### Accessing the Foreground Executor Source: https://github.com/matinaniss/gpui-book/blob/main/src/async/foreground-executor.md Retrieves a reference to the platform's ForegroundExecutor. This is the entry point for managing main thread asynchronous tasks. ```rust use gpui::executor::foreground_executor; let executor = foreground_executor(); ``` -------------------------------- ### Render Trait Implementation Source: https://github.com/matinaniss/gpui-book/blob/main/src/rendering/render.md This snippet shows the basic implementation of the Render trait. It is used to define how a type can be rendered to the screen as a view. ```rust impl Render for App { fn render(&self, _view: &mut View) { // ... } } ``` -------------------------------- ### Applying Animation to an Element Source: https://github.com/matinaniss/gpui-book/blob/main/src/animations/with_animation.md Use `with_animation` to attach an `Animation` to an element. The provided closure receives the element and a progress delta (0.0 to 1.0) to style the element dynamically. ```rust use gpui::{div, prelude::*, Animation, AnimationElement, ElementId}; struct MyComponent; impl MyComponent { fn view(self, cx: &mut gpui::ViewContext) -> Element { div() .id(ElementId::new("my_component")) .with_animation( Animation::new(std::time::Duration::from_secs(1)), |this, delta| { // `this` is `MyComponent` converted to an element // `delta` is the animation progress from 0.0 to 1.0 // You can style the element based on `delta` here this.style().background(gpui::color::Color::from_hsla(delta, 0.5, 0.5, 1.0)) }, ) .child(div().text("Animated Content")) .into_element() } } ``` -------------------------------- ### Create a Deferred Element Source: https://github.com/matinaniss/gpui-book/blob/main/src/elements/deferred.md Use the `deferred` function to create a Deferred element with a child element. This delays the layout and paint of the child. ```rust use gpui::elements::Deferred; fn creating_a_deferred() -> Deferred { Deferred::new(true, || Box::new(Label::new("Deferred"))) } ``` -------------------------------- ### Observing a Global State Source: https://github.com/matinaniss/gpui-book/blob/main/src/state-management/global.md Register a callback to be notified when the global state is updated. This allows reacting to changes in global state. ```rust use gpui::Global; struct Settings; impl Global for Settings {} fn main() { Settings::observe(|s| println!("Settings updated")); } ``` -------------------------------- ### Read a GPUI Entity Source: https://github.com/matinaniss/gpui-book/blob/main/src/state-management/entity.md Retrieves a reference to the state stored within an Entity. This allows you to inspect the current value without modifying it. ```rust let value = entity.get(); ``` -------------------------------- ### Add Multiple Children to a Div Source: https://github.com/matinaniss/gpui-book/blob/main/src/elements/div.md Illustrates how to add multiple child elements to a Div at once using an iterator. This is efficient for managing collections of elements. ```rust use gpui::div; div().children(vec![div(), div()]) ``` -------------------------------- ### Attempting to Access a Global State Source: https://github.com/matinaniss/gpui-book/blob/main/src/state-management/global.md Safely access the global state, returning an `Option`. This avoids panics if the global has not been set. ```rust use gpui::Global; struct Settings; impl Global for Settings {} fn main() { let settings = Settings::try_get(); } ``` -------------------------------- ### Create a Deferred Element with Priority Source: https://github.com/matinaniss/gpui-book/blob/main/src/elements/deferred.md Set a specific priority for a Deferred element to control its rendering order relative to other deferred elements. Higher priority values are drawn on top. ```rust use gpui::elements::Deferred; fn deferred_with_priority() -> Deferred { Deferred::with_priority(100, true, || Box::new(Label::new("Deferred with priority"))) } ``` -------------------------------- ### Spawning a Future onto a Background Thread Source: https://github.com/matinaniss/gpui-book/blob/main/src/async/background-executor.md Enqueues a `Future` to run on a background thread. A `Task` is returned, which can be cancelled by dropping it or allowed to run to completion using `detach`. ```rust use gpui::executor::BackgroundExecutor; use std::future::Future; let background_executor = BackgroundExecutor::get(); let task = background_executor.spawn(async { // Your asynchronous code here 42 }); // To detach the task and let it run to completion without cancellation on drop: // task.detach(); ``` -------------------------------- ### Downgrade a GPUI Entity to WeakEntity Source: https://github.com/matinaniss/gpui-book/blob/main/src/state-management/entity.md Converts an Entity into a WeakEntity, which is a weak pointer. This is useful for preventing reference cycles and managing the lifetime of entities. ```rust let weak_entity = entity.downgrade(); ``` -------------------------------- ### Accessing a Global State Source: https://github.com/matinaniss/gpui-book/blob/main/src/state-management/global.md Retrieve a reference to the global state. Accessing a global that has not been set will result in a panic. ```rust use gpui::Global; struct Settings; impl Global for Settings {} fn main() { Settings::set(Settings); let settings = Settings::get(); } ``` -------------------------------- ### Add a Single Child to a Div Source: https://github.com/matinaniss/gpui-book/blob/main/src/elements/div.md Shows how to append a single child element to an existing Div. This is useful for building nested component structures. ```rust use gpui::div; div().child(div()) ``` -------------------------------- ### Apply Easing to an Animation Source: https://github.com/matinaniss/gpui-book/blob/main/src/animations/animation.md Use the `with_easing` function to apply a custom easing function to an animation. This closure modifies the animation's delta, allowing for effects like smooth acceleration and deceleration. GPUI provides several built-in easing functions. ```rust let eased_animation = animation.with_easing(|delta| ease_in_out(delta)); ``` -------------------------------- ### Notify Entity Update in GPUI Context Source: https://github.com/matinaniss/gpui-book/blob/main/src/architecture/context.md Use the notify function to inform GPUI that an Entity has been updated. Observers will be notified, and if the Context's type implements Render, the view will re-render. ```rust impl Context { /// Alerts GPUI that the entity has been updated and that observers of it should be notified. /// If the `T` type of `Context` implements `Render` then the view will be re-rendered. pub fn notify(&self) { self.app.entity_manager.notify(self.entity); } // ... } ``` -------------------------------- ### Setting a Global State Source: https://github.com/matinaniss/gpui-book/blob/main/src/state-management/global.md Use this to set the global state for a type that implements the `Global` trait. Ensure the type has been marked as global first. ```rust use gpui::Global; struct Settings; impl Global for Settings {} fn main() { Settings::set(Settings); } ``` -------------------------------- ### Apply Flexbox and Column Direction by Modifying Style Struct Source: https://github.com/matinaniss/gpui-book/blob/main/src/styling/index.md Directly modify the underlying Style struct for more verbose, explicit style control. This method achieves the same result as the utility API. ```rust impl Render for SomeView { fn render(&mut self, _window: &mut Window, _cx: &mut Context) -> impl IntoElement { let mut element = div().child("Hello").child("World"); let style = element.style(); style.display = Some(Display::Flex); style.flex_direction = Some(FlexDirection::Column); element } } ``` -------------------------------- ### Update a GPUI Entity Source: https://github.com/matinaniss/gpui-book/blob/main/src/state-management/entity.md Modifies the state held by an Entity. This operation allows for dynamic changes to the application's state. ```rust entity.set(1); ``` -------------------------------- ### Rust Counter Implementation Source: https://github.com/matinaniss/gpui-book/blob/main/src/examples/counter.md This Rust code defines a counter component with increment and decrement buttons. It is typically used within a UI framework that supports hot-reloading. ```rust use gpui::{div, prelude::*}; struct Counter; impl View for Counter { fn ui_name(&self) -> &str { ``` -------------------------------- ### Marking a Type as a Global Source: https://github.com/matinaniss/gpui-book/blob/main/src/state-management/global.md Implement the `Global` marker trait to designate a type for global state management. This is a prerequisite for using other global operations. ```rust use gpui::Global; struct Settings; impl Global for Settings {} ``` -------------------------------- ### Checking if a Global State is Set Source: https://github.com/matinaniss/gpui-book/blob/main/src/state-management/global.md Determine if a global state has been set for a given type. This returns a boolean value. ```rust use gpui::Global; struct Settings; impl Global for Settings {} fn main() { let is_set = Settings::is_set(); } ``` -------------------------------- ### Mutably Accessing a Global State Source: https://github.com/matinaniss/gpui-book/blob/main/src/state-management/global.md Obtain a mutable reference to the global state. Accessing a global that has not been set will cause a panic. ```rust use gpui::Global; struct Settings; impl Global for Settings {} fn main() { Settings::set(Settings); let settings = Settings::get_mut(); } ``` -------------------------------- ### Make Animation Repeat Indefinitely Source: https://github.com/matinaniss/gpui-book/blob/main/src/animations/animation.md The `repeat` function modifies an existing animation to loop continuously. This is useful for animations that should play over and over. ```rust let repeating_animation = animation.repeat(); ``` -------------------------------- ### Updating a Global State Source: https://github.com/matinaniss/gpui-book/blob/main/src/state-management/global.md Update the existing global state for a given type. This operation modifies the current global value. ```rust use gpui::Global; struct Settings; impl Global for Settings {} fn main() { Settings::set(Settings); Settings::update(|s| *s = Settings); } ``` -------------------------------- ### Removing a Global State Source: https://github.com/matinaniss/gpui-book/blob/main/src/state-management/global.md Remove the global state for a specified type. This effectively unsets the global. ```rust use gpui::Global; struct Settings; impl Global for Settings {} fn main() { Settings::set(Settings); Settings::remove(); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.