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:
- 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::Style
message 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_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
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 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 at attempt to fetch properties that aren’t present in this style.
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>,
property: impl Into<StyleProperty>,
) -> &mut Style
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));
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.
Trait Implementations§
Source§impl Reflect for Stylewhere
Style: 'static,
Option<Resource<Style>>: Reflect,
HashMap<ImmutableString, StyleProperty, BuildHasherDefault<FxHasher>>: Reflect,
impl Reflect for Stylewhere
Style: 'static,
Option<Resource<Style>>: Reflect,
HashMap<ImmutableString, StyleProperty, BuildHasherDefault<FxHasher>>: Reflect,
fn source_path() -> &'static str
fn type_name(&self) -> &'static str
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_info(&self, func: &mut dyn FnMut(&[FieldInfo<'_, '_>]))
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)))
fn fields(&self, func: &mut dyn FnMut(&[&(dyn Reflect + 'static)]))
fn fields_mut( &mut self, func: &mut dyn FnMut(&mut [&mut (dyn Reflect + 'static)]), )
fn 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)>), )
Source§fn set_field(
&mut self,
field: &str,
value: Box<dyn Reflect>,
func: &mut dyn FnMut(Result<Box<dyn Reflect>, Box<dyn Reflect>>),
)
fn set_field( &mut self, field: &str, value: Box<dyn Reflect>, func: &mut dyn FnMut(Result<Box<dyn Reflect>, Box<dyn Reflect>>), )
#[reflect(setter = ..)]
or falls back to
Reflect::field_mut
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)>), )
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§impl Visit for Stylewhere
Option<Resource<Style>>: Visit,
HashMap<ImmutableString, StyleProperty, BuildHasherDefault<FxHasher>>: Visit,
impl Visit for Stylewhere
Option<Resource<Style>>: Visit,
HashMap<ImmutableString, StyleProperty, BuildHasherDefault<FxHasher>>: Visit,
Source§fn visit(&mut self, name: &str, visitor: &mut Visitor) -> Result<(), VisitError>
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> Result<(), VisitError>
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> 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 as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Any
. Could be used to downcast a trait object
to a particular type.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Any
. Could be used to downcast a trait object
to a particular type.fn into_any(self: Box<T>) -> Box<dyn Any>
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> FieldValue for Twhere
T: 'static,
impl<T> FieldValue for Twhere
T: 'static,
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<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<T> ScriptMessagePayload for T
impl<T> ScriptMessagePayload for T
Source§fn as_any_ref(&self) -> &(dyn Any + 'static)
fn as_any_ref(&self) -> &(dyn Any + 'static)
self
as &dyn Any
Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
self
as &dyn Any
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.