prokopyl / VIZIA

A declarative GUI library written in Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

VIZIA

License (MIT) Build Status Audit Status Discord

VIZIA is a (in development) declarative GUI framework for the Rust programming language.

WARNING - VIZIA is currently experimental and not a fully functioning framework. The code in this repositiory is not considered stable and is likely to change.

Views

Views form the basic building blocks of a GUI. A view could describe a widget like a label or button, or a more complex container such as a list.

Composing Views

Views can be easily composed to form a more complex GUI application:

Application::new(WindowDescription::new(), |cx|{
	HStack::new(cx, |cx|{
		Label::new(cx, "Hello");
		Label::new(cx, "World");
	});
}).run();

Layout and Styling

Inline properties used for layout and styling can be set directly on views:

Application::new(WindowDescription::new(), |cx|{
	HStack::new(cx, |cx|{
		Label::new(cx, "Hello")
			.width(Pixels(200.0));
		Label::new(cx, "World")
			.background_color(Color::blue());
	}).col_between(Pixels(10.0));
}).run();

Styling can also be done with CSS stylesheets:

/* style.css */
label {
	width: 100px;
	height: 30px;
	border-width: 1px;
	border-color: black;
}

.foo {
	background-color: red;
}
Application::new(window_description, |cx|{

	cx.add_style("style.css");

	HStack::new(cx, |cx|{
		Label::new(cx, "Hello");
		Label::new(cx, "World").class("foo");
	});
}).run();

State

State describes the data provided by the user which the GUI will modify. VIZIA is reactive, which means that changes to the state will update the views.

Defining State

State is defined as a struct/enum which implements the Model trait:

#[derive(Lens)]
struct AppData {
	count: i32,
}

impl Model for AppData {}

The Lens derive macro enables binding views to fields of some state.

Binding to State

To use the state we need to build it into the view tree and then bind to it:

Application::new(window_description, |cx|{
	
	// Build the state into the tree
	AppData{
		count: 0,
	}.build(cx);

	HStack::new(cx, |cx|{
		// Bind to the state
		Binding::new(cx, AppData::count, |cx, some_data|{
			Label::new(cx, *count.get(cx));
		});
	});
}).run();

Anything within the body of the binding view will be updated when the bound data changes.

Mutating State

To keep the separation of data changes and updates to the view tree, mutations of the data are done through events:

pub enum AppEvent {
	Increment,
	Decrement,
}

impl Model for AppData {
	fn event(&mut self, cx: &mut Context, event: &mut MyEvent) {
		if let Some(app_event) = event.message.downcast() {
			match app_event {
				AppEvent::Increment => self.count += 1,	
				AppEvent::Decrement => self.count -= 1,
			}
		}
	}
}

Events and Callbacks

Some views have a built-in action or can be modified to add an action. Actions are callbacks which can be used to send events:

Application::new(WindowDescription::new(), |cx|{
	
	AppData{
		count: 0,
	}.build(cx);

	HStack::new(cx, |cx|{
		// Buttons take an action callback and a label
		Button::new(cx, |cx| cx.emit(AppEvent::Increment), |cx|{
			Label::new(cx, "Increment")
		});

		// The action will be called when the button is pressed
		Button::new(cx, |cx| cx.emit(AppEvent::Decrement), |cx|{
			Label::new(cx, "Decrement")
		});

		Binding::new(cx, AppData::count, |cx, some_data|{
			Label::new(cx, &count.get(cx).to_string());
		});
	});
}).run();

About

A declarative GUI library written in Rust

License:MIT License


Languages

Language:Rust 98.8%Language:CSS 1.2%