Code Monkey home page Code Monkey logo

vizia's Introduction

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();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.