Expand description
Simple data-oriented GUI.
Druid lets you build simple interactive graphical applications that can be deployed on Windows, macOS, Linux, OpenBSD, FreeBSD and the web.
Druid is built on top of druid-shell
, which implements all of the
lower-level, platform-specific code, providing a common abstraction
for things like key and mouse events, creating windows, and launching
an application. Below druid-shell
is piet
, which is a cross-platform
2D graphics library, providing a simple and familiar drawing API that can be
implemented for various platforms.
Druid is a data-driven, declarative framework. You describe your application
model in terms of the Data
trait, and then you build up a tree of
widget
s that can display and modify your data.
Your widgets handle Event
s, such as mouse movement, and can modify the data;
these changes are then delivered to relevant widgets, which can update
their state and redraw.
As your application grows, you can use Lens
es to expose only certain
subsets of your data model to certain subsets of your widget tree.
For more information you should read the Druid book.
Examples
For many more examples, see druid/examples
.
use druid::widget::{Align, Flex, Label, TextBox};
use druid::{AppLauncher, Data, Env, Lens, LocalizedString, Widget, WindowDesc, WidgetExt};
const VERTICAL_WIDGET_SPACING: f64 = 20.0;
const TEXT_BOX_WIDTH: f64 = 200.0;
const WINDOW_TITLE: LocalizedString<HelloState> = LocalizedString::new("Hello World!");
#[derive(Clone, Data, Lens)]
struct HelloState {
name: String,
}
fn main() {
// describe the main window
let main_window = WindowDesc::new(build_root_widget())
.title(WINDOW_TITLE)
.window_size((400.0, 400.0));
// create the initial app state
let initial_state = HelloState {
name: "World".into(),
};
// start the application
AppLauncher::with_window(main_window)
.launch(initial_state)
.expect("Failed to launch application");
}
fn build_root_widget() -> impl Widget<HelloState> {
// a label that will determine its text based on the current app data.
let label = Label::new(|data: &HelloState, _env: &Env| format!("Hello {}!", data.name));
// a textbox that modifies `name`.
let textbox = TextBox::new()
.with_placeholder("Who are we greeting?")
.fix_width(TEXT_BOX_WIDTH)
.lens(HelloState::name);
// arrange the two widgets vertically, with some padding
let layout = Flex::column()
.with_child(label)
.with_spacer(VERTICAL_WIDGET_SPACING)
.with_child(textbox);
// center the two widgets in the available space
Align::centered(layout)
}
Optional Features
Utility features:
im
- Efficient immutable data structures using theim
crate, which is made available via theim
module.svg
- Scalable Vector Graphics for icons and other scalable images using theusvg
crate.image
- Bitmap image support using theimage
crate.x11
- Work-in-progress X11 backend instead of GTK.wayland
- Work-in-progress Wayland backend, very experimental.serde
- Serde support for some internal types (most Kurbo primitives).
Image format features:
- png
- jpeg
- jpeg_rayon
- gif
- bmp
- ico
- tiff
- webp
- pnm
- dds
- tga
- farbfeld
- dxt
- hdr
You can enable all these formats with image-all
.
Features can be added with cargo
. For example, in your Cargo.toml
:
[dependencies.druid]
version = "0.8.2"
features = ["im", "svg", "image"]
Note for Windows apps
By default, Windows will open a console with your application’s window. If you don’t want
the console to be shown, use #![windows_subsystem = "windows"]
at the beginning of your
crate.
Re-exports
pub use shell::keyboard_types;
pub use data::Data;
pub use env::Env;
pub use env::Key;
pub use env::KeyOrValue;
pub use env::Value;
pub use env::ValueType;
pub use env::ValueTypeError;
pub use menu::Menu;
pub use menu::MenuItem;
Modules
Lens
], a way of focusing on subfields of data.Macros
Structs
AppDelegate
functions.druid-shell
WinHandler
trait.lifecycle
method on widgets.MouseButton
s.Widget
to its ancestors.Enums
WidgetPod
.WidgetPod
.TextLayout
.Traits
Type Definitions
Modifiers
.