Skip to content

Instantly share code, notes, and snippets.

@sophiajt
Last active June 13, 2018 15:36
Show Gist options
  • Save sophiajt/1481e1221221895a8ab0fce48780ee84 to your computer and use it in GitHub Desktop.
Save sophiajt/1481e1221221895a8ab0fce48780ee84 to your computer and use it in GitHub Desktop.

GUI support in Rust could be built on a central crate that has a common set of GUI components (button, menubar, dropdown, etc). It would also have first-party connectors to the GUI that's "native" for a given platform.

Wrapping native GUI by default means that each app will feel natural for the given platform, including not only the look of the widgets, but also tricky elements like text input would work just as they with native inputs. The native platforms already put a ton of work into getting this right, let's just leverage it.

Because of the difference in platforms, what if the GUI crate didn't take (x,y) coordinates, but instead used a more constraint-based layout mechanism. This may help cut down on the fiddling you have to do when getting it to work well between platforms.

Rust has a very "fluent" style, and it's not uncommon to see people use this to simulate optional named arguments. I think we could use this to give the library a bit of a Rust feel.

Simple example:

extern crate rustui;

use rustui::{Window, Button};

fn main() {
    let window = Window::new()
        .width(600)
        .height(200)
        .title("First Rustice Program")
        .hidden();

    let button = Button::new()
        .label("Click me!")
        .on_click(|_| {
            println!("Clicked!");
        });

    window.add(button);

    let menubar = MenuBar::new();
    let file = Menu::new("File");
    let about = Menu::new("About");
    let quit = Menu::new("Quit");

    menubar
        .append(file)
        .append(about)
        .append(quit);

    window.add(menubar);
    
    window.show();
}

The intent of the crate would be a reasonable set of widgets and controls for GUIs, but it wouldn't have everything. So what about the additional features? Here, we probably want a way to load in additional widgets that fit the given "Widget" trait. This would let people add their own new widgets to crates.io. Maybe they give platform specific functionality. Or maybe they're crossplatform, but open up new functionality like an OpenGL viewport.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment