1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
use vizia_style::{ColorStop, CornerRadius, Rect};
use super::internal;
use crate::prelude::*;
/// Modifiers for changing the style properties of a view.
pub trait StyleModifiers: internal::Modifiable {
// Selectors
/// Sets the ID name of the view.
///
/// A view can have only one ID name and it must be unique.
/// The ID name can be referenced by a CSS selector.
/// # Example
/// ```
/// # use vizia_core::prelude::*;
/// # let cx = &mut Context::default();
/// Element::new(cx).id("foo");
/// ```
/// css
/// ```css
/// #foo {
/// background-color: red;
/// }
///```
fn id(mut self, id: impl Into<String>) -> Self {
// TODO - What should happen if the id already exists?
let id = id.into();
let entity = self.entity();
self.context().style.ids.insert(entity, id.clone());
self.context().needs_restyle(entity);
self.context().entity_identifiers.insert(id, entity);
self
}
/// Adds a class name to the view.
///
/// A view can have multiple classes.
/// The class name can be referenced by a CSS selector.
/// # Example
/// ```
/// # use vizia_core::prelude::*;
/// # let cx = &mut Context::default();
/// Element::new(cx).class("foo");
/// ```
/// css
/// ```css
/// .foo {
/// background-color: red;
/// }
///```
fn class(mut self, name: &str) -> Self {
let entity = self.entity();
if let Some(class_list) = self.context().style.classes.get_mut(entity) {
class_list.insert(name.to_string());
}
self.context().needs_restyle(entity);
self
}
/// Sets whether a view should have the given class name.
fn toggle_class(mut self, name: &str, applied: impl Res<bool>) -> Self {
let name = name.to_owned();
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
applied.set_or_bind(cx, entity, move |cx, applied| {
let applied = applied.get(cx);
if let Some(class_list) = cx.style.classes.get_mut(entity) {
if applied {
class_list.insert(name.clone());
} else {
class_list.remove(&name);
}
}
cx.needs_restyle(entity);
});
});
self
}
// PseudoClassFlags
// TODO: Should these have their own modifiers trait?
/// Sets the checked state of the view.
fn checked<U: Into<bool>>(mut self, state: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
// Setting a checked state should make it checkable
if let Some(abilities) = self.context().style.abilities.get_mut(entity) {
abilities.set(Abilities::CHECKABLE, true);
}
self.context().with_current(current, move |cx| {
state.set_or_bind(cx, entity, move |cx, val| {
let val = val.get(cx).into();
if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(entity) {
pseudo_classes.set(PseudoClassFlags::CHECKED, val);
}
cx.needs_restyle(entity);
});
});
self
}
/// Sets the focused state of the view.
///
/// Since only one view can have keyboard focus at a time, subsequent calls to this
/// function on other views will cause those views to gain focus and this view to lose it.
fn focused<U: Into<bool>>(mut self, state: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
state.set_or_bind(cx, entity, |cx, val| {
let val = val.get(cx).into();
if val {
cx.focus();
// cx.focus_with_visibility(true);
}
cx.needs_restyle(cx.current);
});
});
self
}
fn read_only<U: Into<bool>>(mut self, state: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
state.set_or_bind(cx, entity, move |cx, val| {
let val = val.get(cx).into();
if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(cx.current) {
pseudo_classes.set(PseudoClassFlags::READ_ONLY, val);
}
cx.needs_restyle(cx.current);
});
});
self
}
fn read_write<U: Into<bool>>(mut self, state: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
state.set_or_bind(cx, entity, move |cx, val| {
let val = val.get(cx).into();
if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(cx.current) {
pseudo_classes.set(PseudoClassFlags::READ_WRITE, val);
}
cx.needs_restyle(cx.current);
});
});
self
}
modifier!(
/// Sets the view to be disabled.
///
/// This property is inherited by the descendants of the view.
disabled,
bool,
SystemFlags::RESTYLE
);
modifier!(
/// Sets whether the view should be positioned and rendered.
///
/// A display value of `Display::None` causes the view to be ignored by both layout and rendering.
display,
Display,
SystemFlags::RELAYOUT | SystemFlags::REDRAW
);
modifier!(
/// Sets whether the view should be rendered.
///
/// The layout system will still compute the size and position of an invisible (hidden) view.
visibility,
Visibility,
SystemFlags::REDRAW
);
modifier!(
/// Sets the opacity of the view.
///
/// Exects a value between 0.0 (transparent) and 1.0 (opaque).
opacity,
Opacity,
SystemFlags::REDRAW
);
/// Sets the z-index of the view.
///
/// Views with a higher z-index will be rendered on top of those with a lower z-order.
/// Views with the same z-index are rendered in tree order.
fn z_index<U: Into<i32>>(mut self, value: impl Res<U>) -> Self {
let entity = self.entity();
// value.set_or_bind(self.context(), entity, |cx, v| {
let cx = self.context();
let value = value.get(cx).into();
cx.style.z_index.insert(entity, value);
cx.needs_redraw(entity);
// });
self
}
/// Sets the clip path for the the view.
fn clip_path<U: Into<ClipPath>>(mut self, value: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
value.set_or_bind(cx, entity, move |cx, v| {
let value = v.get(cx).into();
cx.style.clip_path.insert(cx.current, value);
cx.needs_redraw(entity);
});
});
self
}
fn overflow<U: Into<Overflow>>(mut self, value: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
value.set_or_bind(cx, entity, move |cx, v| {
let value = v.get(cx).into();
cx.style.overflowx.insert(cx.current, value);
cx.style.overflowy.insert(cx.current, value);
cx.needs_redraw(entity);
});
});
self
}
modifier!(
/// Sets the overflow behavior of the view in the horizontal direction.
///
/// The overflow behavior determines whether child views can render outside the bounds of their parent.
overflowx,
Overflow,
SystemFlags::REDRAW
);
modifier!(
/// Sets the overflow behavior of the view in the vertical direction.
///
/// The overflow behavior determines whether child views can render outside the bounds of their parent.
overflowy,
Overflow,
SystemFlags::REDRAW
);
/// Sets the backdrop filter for the view.
fn backdrop_filter<U: Into<Filter>>(mut self, value: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
value.set_or_bind(cx, entity, move |cx, v| {
let value = v.get(cx).into();
cx.style.backdrop_filter.insert(cx.current, value);
cx.needs_redraw(entity);
});
});
self
}
/// Add a shadow to the view.
fn shadow<U: Into<Shadow>>(mut self, value: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
value.set_or_bind(cx, entity, move |cx, v| {
let value = v.get(cx).into();
if let Some(shadows) = cx.style.shadow.get_inline_mut(cx.current) {
shadows.push(value);
} else {
cx.style.shadow.insert(cx.current, vec![value]);
}
cx.needs_redraw(entity);
});
});
self
}
fn shadows<U: Into<Vec<Shadow>>>(mut self, value: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
value.set_or_bind(cx, entity, move |cx, v| {
let value = v.get(cx).into();
cx.style.shadow.insert(cx.current, value);
cx.needs_redraw(entity);
});
});
self
}
fn background_gradient<U: Into<Gradient>>(mut self, value: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
value.set_or_bind(cx, entity, move |cx, v| {
let value = v.get(cx).into();
if let Some(background_images) =
cx.style.background_image.get_inline_mut(cx.current)
{
background_images.push(ImageOrGradient::Gradient(value));
} else {
cx.style
.background_image
.insert(cx.current, vec![ImageOrGradient::Gradient(value)]);
}
cx.needs_redraw(entity);
});
});
self
}
// Background Properties
modifier!(
/// Sets the background color of the view.
background_color,
Color,
SystemFlags::REDRAW
);
fn background_image<'i, U: Into<BackgroundImage<'i>>>(mut self, value: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
value.set_or_bind(cx, entity, move |cx, val| {
let image = val.get(cx).into();
let image = match image {
BackgroundImage::Gradient(gradient) => {
Some(ImageOrGradient::Gradient(*gradient))
}
BackgroundImage::Url(url) => Some(ImageOrGradient::Image(url.url.to_string())),
_ => None,
};
if let Some(image) = image {
cx.style.background_image.insert(cx.current, vec![image]);
}
cx.needs_redraw(entity);
});
});
self
}
// Border Properties
modifier!(
/// Sets the border width of the view.
border_width,
LengthOrPercentage,
SystemFlags::RELAYOUT | SystemFlags::REDRAW
);
modifier!(
/// Sets the border color of the view.
border_color,
Color,
SystemFlags::REDRAW
);
modifier!(
/// Sets the border color of the view.
border_style,
BorderStyleKeyword,
SystemFlags::REDRAW
);
modifier!(
/// Sets the corner radius for the top-left corner of the view.
corner_top_left_radius,
LengthOrPercentage,
SystemFlags::REDRAW
);
modifier!(
/// Sets the corner radius for the top-right corner of the view.
corner_top_right_radius,
LengthOrPercentage,
SystemFlags::REDRAW
);
modifier!(
/// Sets the corner radius for the bottom-left corner of the view.
corner_bottom_left_radius,
LengthOrPercentage,
SystemFlags::REDRAW
);
modifier!(
/// Sets the corner radius for the bottom-right corner of the view.
corner_bottom_right_radius,
LengthOrPercentage,
SystemFlags::REDRAW
);
/// Sets the corner radius for all four corners of the view.
fn corner_radius<U: std::fmt::Debug + Into<CornerRadius>>(
mut self,
value: impl Res<U>,
) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
value.set_or_bind(cx, entity, move |cx, v| {
let value = v.get(cx).into();
cx.style.corner_top_left_radius.insert(cx.current, value.top_left);
cx.style.corner_top_right_radius.insert(cx.current, value.top_right);
cx.style.corner_bottom_left_radius.insert(cx.current, value.bottom_left);
cx.style.corner_bottom_right_radius.insert(cx.current, value.bottom_right);
cx.needs_redraw(entity);
});
});
self
}
modifier!(
/// Sets the corner corner shape for the top-left corner of the view.
corner_top_left_shape,
CornerShape,
SystemFlags::REDRAW
);
modifier!(
/// Sets the corner corner shape for the top-right corner of the view.
corner_top_right_shape,
CornerShape,
SystemFlags::REDRAW
);
modifier!(
/// Sets the corner corner shape for the bottom-left corner of the view.
corner_bottom_left_shape,
CornerShape,
SystemFlags::REDRAW
);
modifier!(
/// Sets the corner corner shape for the bottom-right corner of the view.
corner_bottom_right_shape,
CornerShape,
SystemFlags::REDRAW
);
/// Sets the corner shape for all four corners of the view.
fn corner_shape<U: std::fmt::Debug + Into<Rect<CornerShape>>>(
mut self,
value: impl Res<U>,
) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
value.set_or_bind(cx, entity, move |cx, v| {
let value = v.get(cx).into();
cx.style.corner_top_left_shape.insert(cx.current, value.0);
cx.style.corner_top_right_shape.insert(cx.current, value.1);
cx.style.corner_bottom_right_shape.insert(cx.current, value.2);
cx.style.corner_bottom_left_shape.insert(cx.current, value.3);
cx.needs_redraw(entity);
});
});
self
}
modifier!(
/// Sets the corner smoothing for the top-left corner of the view.
corner_top_left_smoothing,
f32,
SystemFlags::REDRAW
);
modifier!(
/// Sets the corner smoothing for the top-right corner of the view.
corner_top_right_smoothing,
f32,
SystemFlags::REDRAW
);
modifier!(
/// Sets the corner smoothing for the bottom-left corner of the view.
corner_bottom_left_smoothing,
f32,
SystemFlags::REDRAW
);
modifier!(
/// Sets the corner smoothing for the bottom-right corner of the view.
corner_bottom_right_smoothing,
f32,
SystemFlags::REDRAW
);
/// Sets the corner smoothing for all four corners of the view.
fn corner_smoothing<U: std::fmt::Debug + Into<Rect<f32>>>(
mut self,
value: impl Res<U>,
) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
value.set_or_bind(cx, entity, move |cx, v| {
let value = v.get(cx).into();
cx.style.corner_top_left_smoothing.insert(cx.current, value.0);
cx.style.corner_top_right_smoothing.insert(cx.current, value.1);
cx.style.corner_bottom_left_smoothing.insert(cx.current, value.2);
cx.style.corner_bottom_right_smoothing.insert(cx.current, value.3);
cx.needs_redraw(entity);
});
});
self
}
// Outline Properties
modifier!(
/// Sets the outline width of the view.
outline_width,
LengthOrPercentage,
SystemFlags::REDRAW
);
modifier!(
/// Sets the outline color of the view.
outline_color,
Color,
SystemFlags::REDRAW
);
modifier!(
/// Sets the outline offset of the view.
outline_offset,
LengthOrPercentage,
SystemFlags::REDRAW
);
// Cursor Icon
modifier!(
/// Sets the mouse cursor used when the view is hovered.
cursor,
CursorIcon,
SystemFlags::empty()
);
/// Sets whether the view can be become the target of pointer events.
fn pointer_events<U: Into<PointerEvents>>(mut self, value: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
value.set_or_bind(cx, entity, move |cx, v| {
let value = v.get(cx).into();
cx.style.pointer_events.insert(cx.current, value);
});
});
self
}
/// Sets the transform of the view with a list of transform functions.
fn transform<U: Into<Vec<Transform>>>(mut self, value: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
value.set_or_bind(cx, entity, move |cx, v| {
let value = v.get(cx).into();
cx.style.transform.insert(cx.current, value);
cx.needs_redraw(entity);
});
});
self
}
/// Sets the transform origin of the the view.
fn transform_origin<U: Into<Position>>(mut self, value: impl Res<U>) -> Self {
let entity = self.entity();
let current = self.current();
self.context().with_current(current, |cx| {
value.set_or_bind(cx, entity, move |cx, v| {
let value = v.get(cx).into();
let x = value.x.to_length_or_percentage();
let y = value.y.to_length_or_percentage();
cx.style.transform_origin.insert(cx.current, Translate { x, y });
cx.needs_redraw(entity);
});
});
self
}
// Translate
modifier!(
/// Sets the translation offset of the view.
///
/// Translation applies to the rendered view and does not affect layout.
translate,
Translate,
SystemFlags::REDRAW
);
// Rotate
modifier!(
/// Sets the angle of rotation for the view.
///
/// Rotation applies to the rendered view and does not affect layout.
rotate,
Angle,
SystemFlags::REDRAW
);
// Scale
modifier!(
/// Sets the scale of the view.
///
/// Scale applies to the rendered view and does not affect layout.
scale,
Scale,
SystemFlags::REDRAW
);
}
impl<'a, V: View> StyleModifiers for Handle<'a, V> {}
#[derive(Debug, Clone)]
pub struct LinearGradientBuilder {
direction: LineDirection,
stops: Vec<ColorStop<LengthOrPercentage>>,
}
impl Default for LinearGradientBuilder {
fn default() -> Self {
Self::new()
}
}
impl LinearGradientBuilder {
pub fn new() -> Self {
LinearGradientBuilder { direction: LineDirection::default(), stops: Vec::new() }
}
pub fn with_direction(direction: impl Into<LineDirection>) -> Self {
LinearGradientBuilder { direction: direction.into(), stops: Vec::new() }
}
fn build(self) -> Gradient {
Gradient::Linear(LinearGradient { direction: self.direction, stops: self.stops })
}
pub fn add_stop(mut self, stop: impl Into<ColorStop<LengthOrPercentage>>) -> Self {
self.stops.push(stop.into());
self
}
}
impl From<LinearGradientBuilder> for Gradient {
fn from(value: LinearGradientBuilder) -> Self {
value.build()
}
}
#[derive(Debug, Clone)]
pub struct ShadowBuilder {
shadow: Shadow,
}
impl Default for ShadowBuilder {
fn default() -> Self {
Self::new()
}
}
impl ShadowBuilder {
pub fn new() -> Self {
Self { shadow: Shadow::default() }
}
fn build(self) -> Shadow {
self.shadow
}
pub fn x_offset(mut self, offset: impl Into<Length>) -> Self {
self.shadow.x_offset = offset.into();
self
}
pub fn y_offset(mut self, offset: impl Into<Length>) -> Self {
self.shadow.y_offset = offset.into();
self
}
pub fn blur(mut self, radius: Length) -> Self {
self.shadow.blur_radius = Some(radius);
self
}
pub fn spread(mut self, radius: Length) -> Self {
self.shadow.spread_radius = Some(radius);
self
}
pub fn color(mut self, color: Color) -> Self {
self.shadow.color = Some(color);
self
}
pub fn inset(mut self) -> Self {
self.shadow.inset = true;
self
}
}
impl From<ShadowBuilder> for Shadow {
fn from(value: ShadowBuilder) -> Self {
value.build()
}
}