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(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 minimal 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(). They 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.
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 - node_
library - thread_
future - thread_
future_ 2
Structs§
- Animation
- Async
Button - Bounding
Box - A bounding box.
- Circle
- Parameters for drawing a circle
- Circle
Arc - Parameters for drawing an arc
- Circle
Pie - Parameters for drawing a pie slice
- Circle
Ring - Parameters for drawing a ring (hollow circle)
- Click
- A struct describing a click event on a GUI node.
- Clip
Rect - A clip rect
- Color
- Color
Brush - RGBA color value for text rendering.
- Component
Key - Dashed
BoxOutline - Parameters for drawing a dashed box outline (composed of segments and corner arcs)
- Dashed
Hexagon Outline - Parameters for drawing a dashed hexagon outline (composed of segments)
- Drag
- A struct describing a drag event on a GUI node.
- Draw
Context - A context for custom drawing.
- Draw
Transform - A simple 2D transform with uniform scale and offset
- Font
Weight - Visual weight class of a font, typically on a scale from 1.0 to 1000.0.
- Gradient
- Gradient definition for shapes
- Grid
- Parameters for drawing a grid
- Grid
Element - How many cells of a grid the node occupies.
- Grid
Flow - Controls in which direction grid children are placed.
- Hexagon
- Parameters for drawing a hexagon
- Horizontal
Tabs State - Hover
- A struct describing a hover event on a GUI node.
- Image
Options - Texture sampling options: border insets for 9-slice scaling and per-axis tiling modes.
- Interact
- The node’s interact behavior.
- Keru
Element Range - Layout
- The node’s layout, size and position.
- Loaded
Image - A loaded image stored in the atlas.
- Nine
Slice Margins - Pixel insets from each edge of a source image that define the 9 slice regions. Each value is a distance in source image pixels from the respective edge.
- Node
- A struct describing the params of a GUI node.
- NodeKey
- An unique key that identifies a GUI node.
- Node
Text - Observer
- A wrapper that keeps track of changes to a value.
- Quadratic
Bezier - Parameters for drawing a quadratic bezier curve from
p0top2, withp1as a control point. - Reactive
- A struct referring to a reactive block created with
Ui::reactive(). - Rect
- The node’s visual appearance.
- Rectangle
- Parameters for drawing a box/rectangle
- 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
- A struct describing a scroll event on a GUI node.
- Segment
- Parameters for drawing a line segment
- Sense
- Shadow
- Slider
- 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.
- Text
Style Flags - Transform
View - Transform
View State - Triangle
- Parameters for drawing a triangle
- Ui
- The central struct of the library, representing the whole GUI state.
- UiKey
Scope - A struct referring to a key scope created with
Ui::key_scope()orUi::named_key_scope(). - UiNode
- UiNode
Children Iter - UiParent
- A struct referring to a node that was
addedon the tree. - 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.
- Children
CanHide - Children
Layout - Determines how the children of the node are laid out in its space.
- Color
Fill - Fill style for shapes - solid color or gradient
- Direction
- Enter
Animation - Exit
Animation - Font
Style - Visual style or “slope” of a font.
- Gradient
Type - Gradient type for shapes
- Grid
Type - Grid type for the grid primitive
- Image
- Data for an image to be displayed
- Line
Height - The height that this text takes up. The default is
MetricsRelative(1.0), which is the given font’s preferred line height. - Main
Axis Cell Size - Specifies how cells are sized along the main axis of a grid layout.
- 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 - Tile
Mode - How a non-corner region of a bordered texture is filled along one axis.
- Vertical
Text Alignment
Constants§
Traits§
- Component
- Trait for a reusable Ui component.
- Simple
Component - Trait for a reusable Ui component.
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§
- Shared
Text Style - A shareable text style.
- Text
Style Property - An individual text style property,
- XyRect
- A two-dimensional rectangle.
Attribute Macros§
- component_
key - node_
key - A macro that creates a unique
NodeKey.