Skip to content

Commit

Permalink
Add ctrl-shift-alt-S to print matched style rules for hovered element
Browse files Browse the repository at this point in the history
  • Loading branch information
rhelmot authored and geom3trik committed Sep 9, 2022
1 parent e37b113 commit a2f4b4c
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 117 deletions.
27 changes: 26 additions & 1 deletion crates/vizia_core/src/events/event_manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::context::InternalEvent;
use crate::events::EventMeta;
use crate::prelude::*;
use crate::systems::hover_system;
use crate::systems::{compute_matched_rules, hover_system};
use crate::tree::{focus_backward, focus_forward, is_navigatable};
use instant::{Duration, Instant};
use std::any::Any;
Expand Down Expand Up @@ -367,6 +367,31 @@ fn internal_state_updates(context: &mut Context, window_event: &WindowEvent, met
}
}

#[cfg(debug_assertions)]
if *code == Code::KeyS
&& context.modifiers == Modifiers::CTRL | Modifiers::SHIFT | Modifiers::ALT
{
let mut result = vec![];
compute_matched_rules(context, &context.tree, context.hovered, &mut result);

let entity = context.hovered;
println!("/* Matched rules for Entity: {} Parent: {:?} View: {} posx: {} posy: {} width: {} height: {}",
entity,
entity.parent(&context.tree),
context
.views
.get(&entity)
.map_or("<None>", |view| view.element().unwrap_or("<Unnamed>")),
context.cache.get_posx(entity),
context.cache.get_posy(entity),
context.cache.get_width(entity),
context.cache.get_height(entity)
);
for rule in result.into_iter() {
println!("{}", rule);
}
}

if *code == Code::F5 {
context.reload_styles().unwrap();
}
Expand Down
25 changes: 13 additions & 12 deletions crates/vizia_core/src/style/color.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::animation::Interpolator;
use std::fmt;
use std::fmt::Formatter;

/// Describes a color.
///
Expand All @@ -10,6 +11,18 @@ pub struct Color {
pub data: u32,
}

impl std::fmt::Display for Color {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if self.a() == 0 {
write!(f, "transparent")
} else if self.a() == 255 {
write!(f, "#{:02x}{:02x}{:02x}", self.r(), self.g(), self.b())
} else {
write!(f, "#{:02x}{:02x}{:02x}{:02x}", self.r(), self.g(), self.b(), self.a())
}
}
}

impl Color {
// Create a new color from RGB
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Expand Down Expand Up @@ -84,18 +97,6 @@ impl Color {
}
}

impl ToString for Color {
fn to_string(&self) -> String {
if self.a() == 0 {
return String::from("transparent");
}

let data = self.data;

format!("#{:x}", data)
}
}

impl From<&str> for Color {
fn from(s: &str) -> Color {
let clean_hex = s.trim_start_matches('#');
Expand Down
59 changes: 59 additions & 0 deletions crates/vizia_core/src/style/display.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::animation::Interpolator;
use crate::entity::Entity;
use std::fmt::Formatter;
use vizia_id::GenerationalId;

/// Display determines whether an entity will be rendered and acted on by the layout system.
Expand All @@ -12,6 +13,19 @@ pub enum Display {
Flex,
}

impl std::fmt::Display for Display {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Display::None => "none",
Display::Flex => "flex",
}
)
}
}

impl Default for Display {
fn default() -> Self {
Display::Flex
Expand Down Expand Up @@ -45,6 +59,19 @@ pub enum Visibility {
Invisible,
}

impl std::fmt::Display for Visibility {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Visibility::Visible => "visible",
Visibility::Invisible => "invisible",
}
)
}
}

impl From<bool> for Visibility {
fn from(val: bool) -> Self {
if val {
Expand Down Expand Up @@ -73,6 +100,12 @@ impl Interpolator for Visibility {
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Opacity(pub f32);

impl std::fmt::Display for Opacity {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl Default for Opacity {
fn default() -> Self {
Opacity(1.0)
Expand All @@ -94,6 +127,19 @@ pub enum Overflow {
Hidden,
}

impl std::fmt::Display for Overflow {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Overflow::Visible => "visible",
Overflow::Hidden => "hidden",
}
)
}
}

impl Default for Overflow {
fn default() -> Self {
Overflow::Visible
Expand Down Expand Up @@ -123,6 +169,19 @@ pub enum BorderCornerShape {
Bevel,
}

impl std::fmt::Display for BorderCornerShape {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
BorderCornerShape::Round => "round",
BorderCornerShape::Bevel => "bevel",
}
)
}
}

impl Default for BorderCornerShape {
fn default() -> Self {
BorderCornerShape::Round
Expand Down
2 changes: 1 addition & 1 deletion crates/vizia_core/src/style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod parser;
pub use parser::*;

mod style_rule;
use style_rule::StyleRule;
pub(crate) use style_rule::StyleRule;

mod selector;
pub use selector::*;
Expand Down
Loading

0 comments on commit a2f4b4c

Please sign in to comment.