Struct Style

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

Style is a simple container for a named properties. Styles can be based off some other style, thus allowing cascaded styling. Such cascading allows to define some base style with common properties and then create any amount of derived styles. For example, you can define a style for Button widget with corner radius, font size, border thickness and then create two derived styles for light and dark themes that will define colors and brushes. Light or dark theme does not affect all of those base properties, but has different colors.

Styles can contain only specific types of properties (see StyleProperty enumeration), any more complex properties can be built using these primitives.

There are three major ways of widgets styling:

  1. During widget building stage - this way involves crate::BuildContext’s style field. This field defines a style for all widgets that will be built with the context.
  2. Message-based style changes - this way is based on crate::widget::WidgetMessage::Style message that can be sent to a particular widget (or hierarchy) to force them to update styled properties.
  3. Global style changes - this way is based on crate::UserInterface::set_style method, that sends the specified style to all widgets, forcing them to update styled properties.

The most used methods are 1 and 3. The following examples should clarify how to use these approaches.

§Examples

The following example shows how to use a style during widget building stage:

fn build_with_style(ui: &mut UserInterface) {
    // The context will use UI style by default. You can override it using `ui.set_style(..)`.
    let ctx = &mut ui.build_ctx();

    // Create a style resource first and assign it to the build context. All widgets built with
    // the context will use this style.
    let style = Style::light_style()
        .with(Button::CORNER_RADIUS, 6.0f32)
        .with(Button::BORDER_THICKNESS, Thickness::uniform(3.0));

    ctx.style = StyleResource::new_ok(ResourceKind::Embedded, style);

    // The button will have corner radius of 6.0 points and border thickness of 3.0 points on
    // each side.
    ButtonBuilder::new(WidgetBuilder::new()).build(ctx);
}

To change UI style globally after it was built, use something like this:

use fyrox_ui::{
    button::Button,
    style::{resource::StyleResource, Style},
    Thickness, UserInterface,
};
use fyrox_resource::untyped::ResourceKind;

fn apply_style(ui: &mut UserInterface) {
    let style = Style::light_style()
        .with(Button::CORNER_RADIUS, 3.0f32)
        .with(Button::BORDER_THICKNESS, Thickness::uniform(1.0));

    ui.set_style(StyleResource::new_ok(ResourceKind::Embedded, style));
}

Implementations§

Source§

impl Style

Source

pub const PARENT: &'static str = "parent"

Source

pub const VARIABLES: &'static str = "variables"

Source§

impl Style

Source

pub const BRUSH_DARKEST: &'static str = "Global.Brush.Darkest"

The name of the darkest brush.

Source

pub const BRUSH_DARKER: &'static str = "Global.Brush.Darker"

The name of the darker brush.

Source

pub const BRUSH_DARK: &'static str = "Global.Brush.Dark"

The name of the dark brush.

Source

pub const BRUSH_PRIMARY: &'static str = "Global.Brush.Primary"

The name of the primary brush that is used for the major amount of surface.

Source

pub const BRUSH_LIGHTER_PRIMARY: &'static str = "Global.Brush.LighterPrimary"

The name of the slightly lighter primary brush.

Source

pub const BRUSH_LIGHT: &'static str = "Global.Brush.Light"

The name of the light brush.

Source

pub const BRUSH_LIGHTER: &'static str = "Global.Brush.Lighter"

The name of the lighter brush.

Source

pub const BRUSH_LIGHTEST: &'static str = "Global.Brush.Lightest"

The name of the lightest brush.

Source

pub const BRUSH_BRIGHT: &'static str = "Global.Brush.Bright"

The name of the bright brush.

Source

pub const BRUSH_BRIGHTEST: &'static str = "Global.Brush.Brightest"

The name of the brightest brush.

Source

pub const BRUSH_BRIGHT_BLUE: &'static str = "Global.Brush.BrightBlue"

The name of the bright blue brush.

Source

pub const BRUSH_DIM_BLUE: &'static str = "Global.Brush.DimBlue"

The name of the dim blue brush.

Source

pub const BRUSH_TEXT: &'static str = "Global.Brush.Text"

The name of the text brush.

Source

pub const BRUSH_FOREGROUND: &'static str = "Global.Brush.Foreground"

The name of the foreground brush.

Source

pub const BRUSH_INFORMATION: &'static str = "Global.Brush.Information"

The name of the information brush.

Source

pub const BRUSH_WARNING: &'static str = "Global.Brush.Warning"

The name of the warning brush.

Source

pub const BRUSH_ERROR: &'static str = "Global.Brush.Error"

The name of the error brush.

Source

pub const BRUSH_OK: &'static str = "Global.Brush.Ok"

The name of the ok brush.

Source

pub const FONT_SIZE: &'static str = "Global.Font.Size"

The name of the font size property.

Source

pub fn dark_style() -> Style

Creates a new dark style.

Source

pub fn light_style() -> Style

Creates a new light style.

Source

pub fn with( self, name: impl Into<ImmutableString>, property: impl Into<StyleProperty>, ) -> Style

The same as Self::set, but takes self as value and essentially allows chained calls in builder-like style:

Style::default()
    .with("SomeProperty", 0.2f32)
    .with("SomeOtherProperty", Brush::Solid(Color::WHITE));
Source

pub fn set_parent(&mut self, parent: Option<Resource<Style>>)

Sets the parent style for this style. Parent style will be used at attempt to fetch properties that aren’t present in this style.

Source

pub fn parent(&self) -> Option<&Resource<Style>>

Returns parent style of this style.

Source

pub fn merge(&mut self, other: &Style) -> &mut Style

Merges current style with some other style. This method does not overwrite existing values, instead it only adds missing values from the other style.

Source

pub fn set( &mut self, name: impl Into<ImmutableString>, property: impl Into<StyleProperty>, ) -> &mut Style

Registers a new property with the given name and value:

let mut style = Style::default();
style
    .set("SomeProperty", 0.2f32)
    .set("SomeOtherProperty", Brush::Solid(Color::WHITE));
Source

pub fn get_raw(&self, name: impl Into<ImmutableString>) -> Option<StyleProperty>

Tries to fetch a property with the given name. If the property is not found, this method will try to search in the parent style (the search is recursive).

Source

pub fn get<P>(&self, name: impl Into<ImmutableString>) -> Option<P>

Tries to fetch a property with the given name and perform type casting to the requested type. If the property is not found, this method will try to search in the parent style (the search is recursive).

Source

pub fn get_or_default<P>(&self, name: impl Into<ImmutableString>) -> P

Tries to fetch a property with the given name. If the property is not found, this method will try to search in the parent style (the search is recursive). If there’s no such property at all, this method will return its default value (define by Default trait).

Source

pub fn get_or<P>(&self, name: impl Into<ImmutableString>, default: P) -> P

Tries to fetch a property with the given name or, if not found, returns the given default value.

Source

pub fn property<P>(&self, name: impl Into<ImmutableString>) -> StyledProperty<P>

Tries to find a property with the given name or takes the default value of the property’s type and wraps it into StyledProperty, essentially binding the value to the style property.

Source

pub async fn from_file( path: &Path, io: &(dyn ResourceIo + 'static), resource_manager: ResourceManager, ) -> Result<Style, StyleResourceError>

Tries to load a style from the given path.

Trait Implementations§

Source§

impl Debug for Style

Source§

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

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

impl Default for Style

Source§

fn default() -> Style

Returns the “default value” for a type. Read more
Source§

impl Reflect for Style

Source§

fn source_path() -> &'static str

Source§

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

Source§

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

Source§

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

Returns a parent assembly name of the type that implements this trait. WARNING: You should use proc-macro (#[derive(Reflect)]) to ensure that this method will return correct assembly name. In other words - there’s no guarantee, that any implementation other than proc-macro will return a correct name of the assembly. Alternatively, you can use env!("CARGO_PKG_NAME") as an implementation.
Source§

fn type_assembly_name() -> &'static str

Returns a parent assembly name of the type that implements this trait. WARNING: You should use proc-macro (#[derive(Reflect)]) to ensure that this method will return correct assembly name. In other words - there’s no guarantee, that any implementation other than proc-macro will return a correct name of the assembly. Alternatively, you can use env!("CARGO_PKG_NAME") as an implementation.
Source§

fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo<'_, '_>]))

Source§

fn into_any(self: Box<Style>) -> Box<dyn Any>

Source§

fn set( &mut self, value: Box<dyn Reflect>, ) -> Result<Box<dyn Reflect>, Box<dyn Reflect>>

Source§

fn as_any(&self, func: &mut dyn FnMut(&(dyn Any + 'static)))

Source§

fn as_any_mut(&mut self, func: &mut dyn FnMut(&mut (dyn Any + 'static)))

Source§

fn as_reflect(&self, func: &mut dyn FnMut(&(dyn Reflect + 'static)))

Source§

fn as_reflect_mut(&mut self, func: &mut dyn FnMut(&mut (dyn Reflect + 'static)))

Source§

fn fields(&self, func: &mut dyn FnMut(&[&(dyn Reflect + 'static)]))

Source§

fn fields_mut( &mut self, func: &mut dyn FnMut(&mut [&mut (dyn Reflect + 'static)]), )

Source§

fn field( &self, name: &str, func: &mut dyn FnMut(Option<&(dyn Reflect + 'static)>), )

Source§

fn field_mut( &mut self, name: &str, func: &mut dyn FnMut(Option<&mut (dyn Reflect + 'static)>), )

Source§

fn set_field( &mut self, field: &str, value: Box<dyn Reflect>, func: &mut dyn FnMut(Result<Box<dyn Reflect>, Box<dyn Reflect>>), )

Calls user method specified with #[reflect(setter = ..)] or falls back to Reflect::field_mut
Source§

fn as_array(&self, func: &mut dyn FnMut(Option<&(dyn ReflectArray + 'static)>))

Source§

fn as_array_mut( &mut self, func: &mut dyn FnMut(Option<&mut (dyn ReflectArray + 'static)>), )

Source§

fn as_list(&self, func: &mut dyn FnMut(Option<&(dyn ReflectList + 'static)>))

Source§

fn as_list_mut( &mut self, func: &mut dyn FnMut(Option<&mut (dyn ReflectList + 'static)>), )

Source§

fn as_inheritable_variable( &self, func: &mut dyn FnMut(Option<&(dyn ReflectInheritableVariable + 'static)>), )

Source§

fn as_inheritable_variable_mut( &mut self, func: &mut dyn FnMut(Option<&mut (dyn ReflectInheritableVariable + 'static)>), )

Source§

fn as_hash_map( &self, func: &mut dyn FnMut(Option<&(dyn ReflectHashMap + 'static)>), )

Source§

fn as_hash_map_mut( &mut self, func: &mut dyn FnMut(Option<&mut (dyn ReflectHashMap + 'static)>), )

Source§

impl ResourceData for Style

Source§

fn type_uuid(&self) -> Uuid

Returns unique data type id.
Source§

fn save(&mut self, path: &Path) -> Result<(), Box<dyn Error>>

Saves the resource data a file at the specified path. This method is free to decide how the resource data is saved. This is needed, because there are multiple formats that defines various kinds of resources. For example, a rectangular texture could be saved into a bunch of formats, such as png, bmp, tga, jpg etc., but in the engine it is single Texture resource. In any case, produced file should be compatible with a respective resource loader.
Source§

fn can_be_saved(&self) -> bool

Returns true if the resource data can be saved to a file, false - otherwise. Not every resource type supports saving, for example there might be temporary resource type that is used only at runtime which does not need saving at all.
Source§

impl TypeUuidProvider for Style

Source§

fn type_uuid() -> Uuid

Return type UUID.
Source§

impl Visit for Style

Source§

fn visit(&mut self, name: &str, visitor: &mut Visitor) -> Result<(), VisitError>

Read or write this value, depending on whether Visitor::is_reading() is true or false. Read more

Auto Trait Implementations§

§

impl Freeze for Style

§

impl !RefUnwindSafe for Style

§

impl Send for Style

§

impl Sync for Style

§

impl Unpin for Style

§

impl !UnwindSafe for Style

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> AsyncTaskResult for T
where T: Any + Send + 'static,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

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
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts self reference as a reference to Any. Could be used to downcast a trait object to a particular type.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts self reference as a reference to Any. Could be used to downcast a trait object to a particular type.
Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

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

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Casts self to a &dyn Any
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<R> GetField for R
where R: Reflect,

Source§

fn get_field<T>(&self, name: &str, func: &mut dyn FnMut(Option<&T>))
where T: 'static,

Source§

fn get_field_mut<T>(&mut self, name: &str, func: &mut dyn FnMut(Option<&mut T>))
where T: 'static,

Source§

impl<T> Instrument for T

Source§

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

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> ReflectBase for T
where T: Reflect,

Source§

fn as_any_raw(&self) -> &(dyn Any + 'static)

Source§

fn as_any_raw_mut(&mut self) -> &mut (dyn Any + 'static)

Source§

impl<T> ResolvePath for T
where T: Reflect,

Source§

fn resolve_path<'p>( &self, path: &'p str, func: &mut dyn FnMut(Result<&(dyn Reflect + 'static), ReflectPathError<'p>>), )

Source§

fn resolve_path_mut<'p>( &mut self, path: &'p str, func: &mut dyn FnMut(Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>), )

Source§

fn get_resolve_path<'p, T>( &self, path: &'p str, func: &mut dyn FnMut(Result<&T, ReflectPathError<'p>>), )
where T: Reflect,

Source§

fn get_resolve_path_mut<'p, T>( &mut self, path: &'p str, func: &mut dyn FnMut(Result<&mut T, ReflectPathError<'p>>), )
where T: Reflect,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ScriptMessagePayload for T
where T: 'static + Send + Debug,

Source§

fn as_any_ref(&self) -> &(dyn Any + 'static)

Returns self as &dyn Any
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Returns self as &dyn Any
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> ResourceLoadError for T
where T: 'static + Debug + Send + Sync,

Source§

impl<T> TypedResourceData for T