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 NodeParams struct describing a button
let increase_button = BUTTON
.color(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 to
add()
a node with the rightNodeParams
. -
Ui
has some convenience methods likeUi::label()
. These are always equivalent toadding
one or more nodes with specificNodeParams
. -
To check interactions on a node, use
NodeParams::key()
to associate aNodeKey
to aNodeParams
, then call methods likeUi::is_clicked()
with the sameNodeKey
. -
You can use the
NodeKey::sibling()
function to create keys dynamically at runtime. This is useful for dynamic GUIs where you can’t identify every node with a staticNodeKey
in the way the basic examples do it. -
To create reusable “components”, you can just wrap the GUI building code in a function, like the builtin convenience functions like
Ui::label()
do. If the code uses uniqueNodeKeys
, however, you’ll need to wrap it in asubtree
.This allows multiple calls to the same component function to reuse the same key multiple times without conflicts.
-
The
Ui::reactive()
function provides an experimental way to improve performance in complex GUIs with many independent components.
Modules§
- basic_
window_ loop - Helper functions for
winit
andwgpu
. - example_
window_ loop - A very simple way to start a
winit
/wgpu
window loop and to draw a Keru GUI inside it. - winit_
key_ events - winit_
mouse_ events
Structs§
- Color
- A
rgba
color - Full
Node Params - An extended version of
NodeParams
that can hold text or other borrowed data. - Immut
- A wrapper struct for a value that will never change during its lifetime.
- Interact
- The node’s interact behavior.
- Layout
- The node’s layout, size and position.
- NodeKey
- An unique key that identifies a GUI node.
- Node
Params - A struct describing the params of 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.
- Rounded
Corners - A bitflag struct defining which corners of a rectangle are rounded
- Stack
- Options for stack container nodes.
- Static
- A wrapper struct for a
'static
value that will never change during its lifetime. - Tab
- A tab for
Ui::vertical_tabs
- Text
Options - Options for text nodes.
- Ui
- The central struct of the library, representing the whole GUI state.
- UiParent
- A struct referring to a node that was
added
on the tree. - UiSubtree
- A struct referring to a subtree created with
Ui::subtree()
orUi::named_subtree()
. - Vertex
Colors - A node’s vertex colors.
- Xy
- A generic container for two-dimensional data.
Enums§
- Arrange
- Options for the arrangement of child nodes within a stack node.
- Axis
- The X or Y axes.
- Len
- A length on the screen, expressed either as pixels or as a fraction of a parent rectangle.
- Position
- A node’s position relative to its parent.
- Shape
- The node’s shape.
- Size
- A node’s size.
Constants§
- BUTTON
NodeParams
for a button.- CONTAINER
NodeParams
for a container.- CUSTOM_
RENDERED_ PANEL NodeParams
for a custom rendered node.- DEFAULT
NodeParams
for a default.- H_STACK
NodeParams
for a horizontal stack.- ICON_
BUTTON NodeParams
for an icon button.- IMAGE
NodeParams
for an image.- IMAGE_
BUTTON NodeParams
for an icon button.- LABEL
NodeParams
for a label.- MARGIN
NodeParams
for a margin.- MULTILINE_
LABEL NodeParams
for a label containing a multi-line paragraph.- PANEL
NodeParams
for a panel.- SPACER
NodeParams
for a spacer element.- TEXT
NodeParams
for a text element.- TEXT_
EDIT NodeParams
for a text element.- TEXT_
PARAGRAPH NodeParams
for a text element containing a multi-line paragraph.- V_
SCROLL_ STACK NodeParams
for a vertically scrollable vertical stack.- V_STACK
NodeParams
for a vertical stack.
Traits§
Functions§
- is_
in_ skipped_ reactive_ block - Returns
true
if currently in a reactive block that is being skipped.
Type Aliases§
- Subtree
Key - XyRect
- A two-dimensional rectangle.