Skip to main content

Playground

Struct Playground 

Source
pub struct Playground { /* private fields */ }
Expand description

A place to write code and see what it makes, for documentation, tutorials and tools.

The code is on one side and the result on the other; below 768 pixels they become two tabs. Run (or Ctrl/Cmd+Enter) hands the code to the Runner. When it has problems, they are listed under the editor and marked on their lines, and the last result that worked stays, labeled as such. Examples fill the editor, Restore puts the chosen example back, and the app can add buttons that receive the code, such as one that copies a link.

use ravenrs_widgets::{AnyView, CodeDiagnostic, Playground, Text, Ui};

let runner = |source: &str| -> Result<AnyView, Vec<CodeDiagnostic>> {
    match source.trim().strip_prefix("say ") {
        Some(words) => Ok(Box::new(Text::new(words.to_owned()))),
        None => Err(vec![CodeDiagnostic::error(1, 1, 3, "Start with `say`")]),
    }
};
let playground = Playground::new(runner).example("Hello", "say hello").height(400.0);
let mut ui = Ui::new(playground, ravenrs_style::Theme::raven_dark());
ui.update();

Implementations§

Source§

impl Playground

Source

pub fn new(runner: impl Runner + 'static) -> Self

A playground whose code runner runs.

Source

pub fn example(self, name: impl Into<String>, source: impl Into<String>) -> Self

An example to start from, listed in the bar; the first one fills the editor.

Source

pub fn source(self, source: Binding<String>) -> Self

Keeps the code in source, so the app can save it, share it or start from it. While it is empty, or without it, the playground starts from the first example.

Source

pub fn chosen_example(self, example: Binding<usize>) -> Self

Keeps which example is chosen in example.

Source

pub fn language(self, language: CodeLanguage) -> Self

The language the editor colors; Rust by default.

Source

pub fn title(self, title: impl Into<String>) -> Self

A title at the start of the bar.

Source

pub fn note(self, note: impl Into<String>) -> Self

A line under the bar, such as what this playground can and cannot run yet.

Source

pub fn action( self, label: impl Into<String>, icon: IconName, then: impl Fn(&str) + 'static, ) -> Self

A button in the bar that gets the code as it is.

Source

pub fn download( self, make: impl Fn(&str) -> Result<(String, Vec<u8>), Vec<CodeDiagnostic>> + 'static, ) -> Self

A button that saves what make turns the code into, such as a project to build, with the system’s dialog on a desktop or as a download in a browser. make gives the file’s name and bytes, or the problems to mark in the code.

Source

pub fn output_open(self, open: bool) -> Self

Whether the output starts open (it does by default). Folded, the result keeps the room, as on a small page about one example; a problem opens it all the same.

Source

pub fn height(self, height: f32) -> Self

A fixed height; by default it takes the room its parent gives.

Trait Implementations§

Source§

impl Debug for Playground

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl View for Playground

Source§

fn build(&self, cx: &mut BuildCx<'_>) -> NodeId

Creates this view’s node for the current build.
Source§

fn type_name(&self) -> &'static str

The name the diagnostics panel shows this view’s cost under.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V> ViewExt for V
where V: View,

Source§

fn padding(self, padding: impl Into<Insets>) -> Styled<Self>

Adds padding inside the box, in logical pixels.
Source§

fn background(self, fill: impl Into<Fill>) -> Styled<Self>

Paints a background behind the content.
Source§

fn frame(self, width: Option<f32>, height: Option<f32>) -> Styled<Self>

Fixes the width and/or height in logical pixels; None keeps the content size.
Source§

fn width(self, width: f32) -> Styled<Self>

Fixes the width in logical pixels.
Source§

fn height(self, height: f32) -> Styled<Self>

Fixes the height in logical pixels.
Source§

fn grow(self, grow: f32) -> Styled<Self>

Takes a share of the free space in the parent stack.
Source§

fn stretch(self) -> Styled<Self>

Stretches across the parent stack’s cross axis.
Source§

fn centered(self) -> Styled<Self>

Centers the content inside the box.
Source§

fn tooltip(self, text: impl Into<String>) -> Tooltip<Self>

Shows text above the view while the pointer is over it; see Tooltip.
Source§

fn opacity(self, opacity: f32) -> Faded<Self>

Draws the view and everything in it at this opacity, from 0.0 to 1.0, easing to it when it changes. Layout and input stay as they are; see Faded.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more