pub struct Style { /* private fields */ }Expand description
Style is a simple container for a named properties. Styles can be based on some other styles, thus allowing cascaded styling. Such cascading allows to define some base style with common properties and then create any number 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 widget styling:
- 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. - Message-based style changes - this way is based on
crate::widget::WidgetMessage::Stylemessage that can be sent to a particular widget (or hierarchy) to force them to update styled properties. - Global style changes - this way is based on
crate::UserInterface::set_stylemethod, 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_embedded(style);
// The button will have a 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,
};
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_embedded(style));
}Implementations§
Source§impl Style
impl Style
Sourcepub const BRUSH_DARKEST: &'static str = "Global.Brush.Darkest"
pub const BRUSH_DARKEST: &'static str = "Global.Brush.Darkest"
The name of the darkest brush.
Sourcepub const BRUSH_DARKER: &'static str = "Global.Brush.Darker"
pub const BRUSH_DARKER: &'static str = "Global.Brush.Darker"
The name of the darker brush.
Sourcepub const BRUSH_DARK: &'static str = "Global.Brush.Dark"
pub const BRUSH_DARK: &'static str = "Global.Brush.Dark"
The name of the dark brush.
Sourcepub const BRUSH_PRIMARY: &'static str = "Global.Brush.Primary"
pub const BRUSH_PRIMARY: &'static str = "Global.Brush.Primary"
The name of the primary brush that is used for the major amount of surface.
Sourcepub const BRUSH_LIGHTER_PRIMARY: &'static str = "Global.Brush.LighterPrimary"
pub const BRUSH_LIGHTER_PRIMARY: &'static str = "Global.Brush.LighterPrimary"
The name of the slightly lighter primary brush.
Sourcepub const BRUSH_LIGHT: &'static str = "Global.Brush.Light"
pub const BRUSH_LIGHT: &'static str = "Global.Brush.Light"
The name of the light brush.
Sourcepub const BRUSH_LIGHTER: &'static str = "Global.Brush.Lighter"
pub const BRUSH_LIGHTER: &'static str = "Global.Brush.Lighter"
The name of the lighter brush.
Sourcepub const BRUSH_LIGHTEST: &'static str = "Global.Brush.Lightest"
pub const BRUSH_LIGHTEST: &'static str = "Global.Brush.Lightest"
The name of the lightest brush.
Sourcepub const BRUSH_BRIGHT: &'static str = "Global.Brush.Bright"
pub const BRUSH_BRIGHT: &'static str = "Global.Brush.Bright"
The name of the bright brush.
Sourcepub const BRUSH_BRIGHTEST: &'static str = "Global.Brush.Brightest"
pub const BRUSH_BRIGHTEST: &'static str = "Global.Brush.Brightest"
The name of the brightest brush.
Sourcepub const BRUSH_BRIGHT_BLUE: &'static str = "Global.Brush.BrightBlue"
pub const BRUSH_BRIGHT_BLUE: &'static str = "Global.Brush.BrightBlue"
The name of the bright blue brush.
Sourcepub const BRUSH_DIM_BLUE: &'static str = "Global.Brush.DimBlue"
pub const BRUSH_DIM_BLUE: &'static str = "Global.Brush.DimBlue"
The name of the dim blue brush.
Sourcepub const BRUSH_TEXT: &'static str = "Global.Brush.Text"
pub const BRUSH_TEXT: &'static str = "Global.Brush.Text"
The name of the text brush.
Sourcepub const BRUSH_FOREGROUND: &'static str = "Global.Brush.Foreground"
pub const BRUSH_FOREGROUND: &'static str = "Global.Brush.Foreground"
The name of the foreground brush.
Sourcepub const BRUSH_INFORMATION: &'static str = "Global.Brush.Information"
pub const BRUSH_INFORMATION: &'static str = "Global.Brush.Information"
The name of the information brush.
Sourcepub const BRUSH_WARNING: &'static str = "Global.Brush.Warning"
pub const BRUSH_WARNING: &'static str = "Global.Brush.Warning"
The name of the warning brush.
Sourcepub const BRUSH_ERROR: &'static str = "Global.Brush.Error"
pub const BRUSH_ERROR: &'static str = "Global.Brush.Error"
The name of the error brush.
Sourcepub const BRUSH_HIGHLIGHT: &'static str = "Global.Brush.Highlight"
pub const BRUSH_HIGHLIGHT: &'static str = "Global.Brush.Highlight"
The name of the highlight brush used to highlight widgets with keyboard focus.
Sourcepub const BRUSH_OK_NORMAL: &'static str = "Global.Brush.Ok.Normal"
pub const BRUSH_OK_NORMAL: &'static str = "Global.Brush.Ok.Normal"
The name of the normal state brush of the ok context action.
Sourcepub const BRUSH_OK_PRESSED: &'static str = "Global.Brush.Ok.Pressed"
pub const BRUSH_OK_PRESSED: &'static str = "Global.Brush.Ok.Pressed"
The name of the pressed state brush of the ok context action.
Sourcepub const BRUSH_OK_HOVER: &'static str = "Global.Brush.Ok.Hover"
pub const BRUSH_OK_HOVER: &'static str = "Global.Brush.Ok.Hover"
The name of the hover state brush of the ok context action.
Sourcepub const BRUSH_CANCEL_NORMAL: &'static str = "Global.Brush.Cancel.Normal"
pub const BRUSH_CANCEL_NORMAL: &'static str = "Global.Brush.Cancel.Normal"
The name of the normal state brush of the cancel context action.
Sourcepub const BRUSH_CANCEL_PRESSED: &'static str = "Global.Brush.Cancel.Pressed"
pub const BRUSH_CANCEL_PRESSED: &'static str = "Global.Brush.Cancel.Pressed"
The name of the pressed state brush of the cancel context action.
Sourcepub const BRUSH_CANCEL_HOVER: &'static str = "Global.Brush.Cancel.Hover"
pub const BRUSH_CANCEL_HOVER: &'static str = "Global.Brush.Cancel.Hover"
The name of the hover state brush of the cancel context action.
Sourcepub fn dark_style() -> Style
pub fn dark_style() -> Style
Creates a new dark style.
Sourcepub fn light_style() -> Style
pub fn light_style() -> Style
Creates a new light style.
Sourcepub fn with(
self,
name: impl Into<ImmutableString>,
property: impl Into<StyleProperty>,
) -> Style
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));Sourcepub fn set_parent(&mut self, parent: Option<Resource<Style>>)
pub fn set_parent(&mut self, parent: Option<Resource<Style>>)
Sets the parent style for this style. Parent style will be used in an attempt to fetch properties that aren’t present in this style.
Sourcepub fn index_of(&self, name: &ImmutableString) -> Option<usize>
pub fn index_of(&self, name: &ImmutableString) -> Option<usize>
Returns an index of the variable with the given name.
Sourcepub fn contains(&self, name: &ImmutableString) -> bool
pub fn contains(&self, name: &ImmutableString) -> bool
Checks if there’s a variable with the given name.
Sourcepub fn merge(&mut self, other: &Style) -> &mut Style
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.
Sourcepub fn set(
&mut self,
name: impl Into<ImmutableString>,
value: impl Into<StyleProperty>,
) -> &mut Style
pub fn set( &mut self, name: impl Into<ImmutableString>, value: 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));Sourcepub fn get_raw(&self, name: impl Into<ImmutableString>) -> Option<StyleProperty>
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).
Sourcepub fn get<P>(&self, name: impl Into<ImmutableString>) -> Option<P>where
StyleProperty: IntoPrimitive<P>,
pub fn get<P>(&self, name: impl Into<ImmutableString>) -> Option<P>where
StyleProperty: IntoPrimitive<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).
Sourcepub fn get_or_default<P>(&self, name: impl Into<ImmutableString>) -> P
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).
Sourcepub fn get_or<P>(&self, name: impl Into<ImmutableString>, default: P) -> Pwhere
StyleProperty: IntoPrimitive<P>,
pub fn get_or<P>(&self, name: impl Into<ImmutableString>, default: P) -> Pwhere
StyleProperty: IntoPrimitive<P>,
Tries to fetch a property with the given name or, if not found, returns the given default value.
Sourcepub fn property<P>(&self, name: impl Into<ImmutableString>) -> StyledProperty<P>
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.
Sourcepub async fn from_file(
path: &Path,
io: &(dyn ResourceIo + 'static),
resource_manager: ResourceManager,
) -> Result<Style, StyleResourceError>
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.
Sourcepub fn inner(&self) -> &Vec<StylePropertyContainer>
pub fn inner(&self) -> &Vec<StylePropertyContainer>
Returns an immutable reference to the internal container with the style properties. Keep in mind that the returned container contains only the properties of the current style! Properties of the parent style(s) should be obtained separately.
Sourcepub fn all_properties(&self) -> Style
pub fn all_properties(&self) -> Style
Collects all the properties in the current and ancestor style chain. Returns a hash map with all property values with their names. Basically, this method merges all the styles with their ancestor style chain.
Trait Implementations§
Source§impl Reflect for Stylewhere
Style: 'static,
impl Reflect for Stylewhere
Style: 'static,
fn source_path() -> &'static str
fn try_clone_box(&self) -> Option<Box<dyn Reflect>>
fn type_name(&self) -> &'static str
fn derived_types() -> &'static [TypeId]
fn query_derived_types(&self) -> &'static [TypeId]
fn doc(&self) -> &'static str
Source§fn assembly_name(&self) -> &'static str
fn assembly_name(&self) -> &'static str
#[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
fn type_assembly_name() -> &'static str
#[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.fn fields_ref(&self, func: &mut dyn FnMut(&[FieldRef<'_, '_>]))
fn fields_mut(&mut self, func: &mut dyn FnMut(&mut [FieldMut<'_, '_>]))
fn into_any(self: Box<Style>) -> Box<dyn Any>
fn set( &mut self, value: Box<dyn Reflect>, ) -> Result<Box<dyn Reflect>, Box<dyn Reflect>>
fn as_any(&self, func: &mut dyn FnMut(&(dyn Any + 'static)))
fn as_any_mut(&mut self, func: &mut dyn FnMut(&mut (dyn Any + 'static)))
fn as_reflect(&self, func: &mut dyn FnMut(&(dyn Reflect + 'static)))
fn as_reflect_mut(&mut self, func: &mut dyn FnMut(&mut (dyn Reflect + 'static)))
Source§fn set_field(
&mut self,
field_name: &str,
value: Box<dyn Reflect>,
func: &mut dyn FnMut(Result<Box<dyn Reflect>, SetFieldError>),
)
fn set_field( &mut self, field_name: &str, value: Box<dyn Reflect>, func: &mut dyn FnMut(Result<Box<dyn Reflect>, SetFieldError>), )
#[reflect(setter = ..)] or falls back to
Reflect::field_mutfn field( &self, name: &str, func: &mut dyn FnMut(Option<&(dyn Reflect + 'static)>), )
fn field_mut( &mut self, name: &str, func: &mut dyn FnMut(Option<&mut (dyn Reflect + 'static)>), )
fn as_array(&self, func: &mut dyn FnMut(Option<&(dyn ReflectArray + 'static)>))
fn as_array_mut( &mut self, func: &mut dyn FnMut(Option<&mut (dyn ReflectArray + 'static)>), )
fn as_list(&self, func: &mut dyn FnMut(Option<&(dyn ReflectList + 'static)>))
fn as_list_mut( &mut self, func: &mut dyn FnMut(Option<&mut (dyn ReflectList + 'static)>), )
fn as_inheritable_variable( &self, func: &mut dyn FnMut(Option<&(dyn ReflectInheritableVariable + 'static)>), )
fn as_inheritable_variable_mut( &mut self, func: &mut dyn FnMut(Option<&mut (dyn ReflectInheritableVariable + 'static)>), )
fn as_hash_map( &self, func: &mut dyn FnMut(Option<&(dyn ReflectHashMap + 'static)>), )
fn as_hash_map_mut( &mut self, func: &mut dyn FnMut(Option<&mut (dyn ReflectHashMap + 'static)>), )
fn as_handle( &self, func: &mut dyn FnMut(Option<&(dyn ReflectHandle + 'static)>), )
fn as_handle_mut( &mut self, func: &mut dyn FnMut(Option<&mut (dyn ReflectHandle + 'static)>), )
Source§impl ResourceData for Style
impl ResourceData for Style
Source§fn save(&mut self, path: &Path) -> Result<(), Box<dyn Error>>
fn save(&mut self, path: &Path) -> Result<(), Box<dyn Error>>
Source§fn can_be_saved(&self) -> bool
fn can_be_saved(&self) -> bool
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§fn try_clone_box(&self) -> Option<Box<dyn ResourceData>>
fn try_clone_box(&self) -> Option<Box<dyn ResourceData>>
None if the underlying type is
non-cloneable.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> AsyncTaskResult for T
impl<T> AsyncTaskResult for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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 Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> FieldValue for Twhere
T: Reflect,
impl<T> FieldValue for Twhere
T: Reflect,
Source§fn field_value_as_any_ref(&self) -> &(dyn Any + 'static)
fn field_value_as_any_ref(&self) -> &(dyn Any + 'static)
self to a &dyn AnySource§fn field_value_as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn field_value_as_any_mut(&mut self) -> &mut (dyn Any + 'static)
self to a &mut dyn Anyfn field_value_as_reflect(&self) -> &(dyn Reflect + 'static)
fn field_value_as_reflect_mut(&mut self) -> &mut (dyn Reflect + 'static)
fn type_name(&self) -> &'static str
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T, U> ObjectOrVariant<T> for Uwhere
PhantomData<U>: ObjectOrVariantHelper<T, U>,
impl<T, U> ObjectOrVariant<T> for Uwhere
PhantomData<U>: ObjectOrVariantHelper<T, U>,
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().Source§impl<T> ReflectBase for Twhere
T: Reflect,
impl<T> ReflectBase for Twhere
T: Reflect,
fn as_any_raw(&self) -> &(dyn Any + 'static)
fn as_any_raw_mut(&mut self) -> &mut (dyn Any + 'static)
Source§impl<T> ResolvePath for Twhere
T: Reflect,
impl<T> ResolvePath for Twhere
T: Reflect,
fn resolve_path<'p>( &self, path: &'p str, func: &mut dyn FnMut(Result<&(dyn Reflect + 'static), ReflectPathError<'p>>), )
fn resolve_path_mut<'p>( &mut self, path: &'p str, func: &mut dyn FnMut(Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>), )
fn get_resolve_path<'p, T>(
&self,
path: &'p str,
func: &mut dyn FnMut(Result<&T, ReflectPathError<'p>>),
)where
T: Reflect,
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<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.