Expand description
Keru is an experimental graphical user interface library.
§Code Example
// Define a unique identity for the button
#[node_key] const INCREASE: NodeKey;
// Create a Node struct describing a button
let increase_button = BUTTON
.color(RED)
.text("Increase")
.key(INCREASE);
// Place the nodes into the tree and define the layout
ui.v_stack().nest(|| {
ui.add(increase_button);
ui.label(&state.count.to_string());
});
// Change the state in response to events
if ui.is_clicked(INCREASE) {
state.count += 1;
}
// `is_clicked()` can be also called as a chained method after `ui.add(increase_button)`.
// In that case, using a key wouldn't be necessary.See the counter_small example in the repository for a full working version of this code.
§Window Loop
Keru is meant to be used as part of a regular winit/wgpu window loop managed by the library user, as shown in the window_loop example in the repository. However, it also includes a one-line window loop that can be used for quick experimentation.
Once you have a window loop, you can create a Ui struct and store it in your main program state.
§Building the GUI
Every frame, start a new GUI frame, rerun all your GUI building code, then finish the frame.
ui.begin_frame();
ui.v_stack().nest(|| {
ui.label("Hello");
ui.label("World");
});
ui.finish_frame();The Ui struct retains the state of the whole GUI, so even if you do this on every frame, it doesn’t mean that the GUI is rerendering or doing a full relayout every time. The library can detect differences and apply only the minimal updates or partial relayouts needed.
-
In Keru, everything is a
Node. Whether you want a button, an image, a text element, a stack container, or anything else, the way is always toadd()a node with the right values. -
There are also “components”, like
Slider. Components are added withUi::add_component()and they are a way to wrap multiple nodes into a reusable structure. You can define custom components with theSimpleComponentandComponenttraits. -
Uihas some convenience methods likeUi::label(). These work the same way as components, but with more natural syntax. -
To check interactions on a node, use
Node::key()to associate aNodeKeyto aNode, then call methods likeUi::is_clicked()with the sameNodeKey. -
The
Ui::reactive()function provides an experimental way to improve performance in complex GUIs with many independent components.
Re-exports§
pub use bumpalo;pub use keru_draw::keru_text;
Modules§
- basic_
window_ loop - Helper functions for
winitandwgpu. - example_
window_ loop - A very simple way to start a
winit/wgpuwindow loop and to draw a Keru GUI inside it. - key_
events - mouse_
events - thread_
future - thread_
future_ 2
Structs§
- Animation
- Async
Button - Bounding
Box - A bounding box.
- Click
- A struct describing a click event on a GUI node.
- Color
- Color
Brush - RGBA color value for text rendering.
- Component
Key - Drag
- A struct describing a drag event on a GUI node.
- Font
Weight - Visual weight class of a font, typically on a scale from 1.0 to 1000.0.
- Full
Node - An extended version of
Nodethat can hold text or other borrowed data. - Gradient
- Gradient definition for shapes
- Horizontal
Tabs State - Hover
- A struct describing a hover event on a GUI node.
- Interact
- The node’s interact behavior.
- Keru
Element Range - Layout
- The node’s layout, size and position.
- Node
- A struct describing the params of a GUI node.
- NodeKey
- An unique key that identifies a GUI node.
- Observer
- A wrapper that keeps track of changes to a value.
- Reactive
- A struct referring to a reactive block created with
Ui::reactive(). - Rect
- The node’s visual appearance.
- Render
Info - The data needed for rendering a node with custom code.
- Reorder
Stack - Rounded
Corners - Bitflags specifying which corners of a rectangle should be rounded.
- Scroll
Event - A struct describing a scroll event on a GUI node.
- Sense
- Slider
- Stack
- Options for stack container nodes.
- State
Transition - Stateful
Transform View - Stateful
Vertical Tabs - Stroke
- The visual style of a stroke.
- Style
Handle - Handle for a text style. Use with Text methods to apply styles to text.
- Tab
- A tab for
Ui::vertical_tabs - TabContainer
- Text
Options - Options for text nodes.
- Transform
View - Transform
View State - Ui
- The central struct of the library, representing the whole GUI state.
- UiNode
- UiNode
Children Iter - UiParent
- A struct referring to a node that was
addedon the tree. - UiSubtree
- A struct referring to a subtree created with
Ui::subtree()orUi::named_subtree(). - UiWaker
- A handle that can be used to wake up the
Uifrom another thread. - Vertical
Tabs State - Xy
- A generic container for two-dimensional data.
Enums§
- Anchor
- Anchor point within a node for positioning.
- Arrange
- Options for the arrangement of child nodes within a stack node.
- Axis
- The X or Y axes.
- Cap
- Children
CanHide - Color
Fill - Fill style for shapes - solid color or gradient
- Enter
Animation - Exit
Animation - Font
Style - Visual style or “slope” of a font.
- Image
- Data for an image to be displayed
- Join
- Line
Height - The height that this text takes up. The default is
MetricsRelative(1.0), which is the given font’s preferred line height. - Node
Text - Pos
- A node’s position relative to its parent.
- Render
Command - A single render command in the list provided by Ui::render_commands().
- Shape
- The node’s shape.
- Size
- A node’s size.
- Slide
Direction - Slide
Edge
Constants§
- BUTTON
Nodefor a button.- CONTAINER
Nodefor a container.- CUSTOM_
RENDERED_ PANEL Nodefor a custom rendered node.- DEFAULT
Nodefor a default.- DEFAULT_
CORNER_ RADIUS - H_LINE
Nodefor a horizontal divider line.- H_
SPACER Nodefor a invisible spacer element that fills all the available space in the X direction.- H_STACK
Nodefor a horizontal stack.- ICON
Nodefor an icon element.- ICON_
BUTTON Nodefor an icon button.- ICON_
DELETE - ICON_
EDIT - ICON_
LEFT - ICON_
MINUS - ICON_
PLUS - ICON_
RIGHT - IMAGE
Nodefor an image.- IMAGE_
BUTTON Nodefor an icon button.- LABEL
Nodefor a label.- MARGIN
Nodefor a margin.- MULTILINE_
LABEL Nodefor a label containing a multi-line paragraph.- NO_
ANIMATION - PANEL
Nodefor a panel.- SPACER
Nodefor a spacer element.- TEXT
Nodefor a text element.- TEXT_
EDIT Nodefor a multiline text edit box.- TEXT_
EDIT_ LINE Nodefor a single line text edit box.- TEXT_
PARAGRAPH Nodefor a text element containing a multi-line paragraph.- V_
SCROLL_ STACK Nodefor a vertically scrollable vertical stack.- V_
SPACER Nodefor a invisible spacer element that fills all the available space in the Y direction.- V_STACK
Nodefor a vertical stack.- Z_STEP
Statics§
- ORIGINAL_
DEFAULT_ TEXT_ STYLE - The original default text style that can be restored with Ctrl+0
Traits§
- Component
- Trait for a reusable Ui component.
- Simple
Component - A simpler version of
Componentfor stateless components that don’t have nested children.
Functions§
- is_
in_ skipped_ reactive_ block - Returns
trueif currently in a reactive block that is being skipped. - with_
arena - Access keru’s thread-local bump arena for temporary allocations. Useful for small local allocations without passing an arena around, like formatting strings to show in the gui.
- with_
clipboard - Runs the given closure with mutable access to the thread-local
Clipboard.
Type Aliases§
- StateId
- Subtree
Key - Text
Style - Text style.
- XyRect
- A two-dimensional rectangle.
Attribute Macros§
- component_
key - node_
key - A macro that creates a unique
NodeKey. - state_
key