]> git.nega.tv - josh/narcissus/commitdiff
narcissus-core: Add title to svg rect elements
authorJoshua Simmons <josh@nega.tv>
Wed, 7 Jun 2023 21:37:52 +0000 (23:37 +0200)
committerJoshua Simmons <josh@nega.tv>
Sun, 9 Jul 2023 22:08:28 +0000 (00:08 +0200)
libs/narcissus-core/src/svg.rs

index 6fdba40550ce626d06cd9b242eeaa7324a3b3416..25b2af0c588e0053e9772904ea13acb6fbdc3a15 100644 (file)
@@ -44,7 +44,7 @@ pub fn svg_end() -> SvgEnd {
     SvgEnd
 }
 
-pub fn rect(x: f32, y: f32, w: f32, h: f32) -> Rect {
+pub fn rect(x: f32, y: f32, w: f32, h: f32) -> Rect<'static> {
     Rect {
         x,
         y,
@@ -52,6 +52,7 @@ pub fn rect(x: f32, y: f32, w: f32, h: f32) -> Rect {
         h,
         style: Default::default(),
         border_radius: 0.0,
+        title: None,
     }
 }
 
@@ -180,8 +181,8 @@ impl fmt::Display for Stroke {
 
 #[derive(Copy, Clone, PartialEq)]
 pub struct Style {
-    fill: Fill,
-    stroke: Stroke,
+    pub fill: Fill,
+    pub stroke: Stroke,
 }
 
 impl Default for Style {
@@ -200,16 +201,17 @@ impl fmt::Display for Style {
 }
 
 #[derive(Copy, Clone, PartialEq)]
-pub struct Rect {
+pub struct Rect<'a> {
     pub x: f32,
     pub y: f32,
     pub w: f32,
     pub h: f32,
     pub style: Style,
     pub border_radius: f32,
+    pub title: Option<&'a str>,
 }
 
-impl Rect {
+impl<'a> Rect<'a> {
     pub fn style(mut self, style: Style) -> Self {
         self.style = style;
         self
@@ -219,15 +221,28 @@ impl Rect {
         self.border_radius = border_radius;
         self
     }
+
+    pub fn title(mut self, title: &'a str) -> Self {
+        self.title = Some(title);
+        self
+    }
 }
 
-impl fmt::Display for Rect {
+impl<'a> fmt::Display for Rect<'a> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(
-            f,
-            r#"<rect x="{}" y="{}" width="{}" height="{}" ry="{}" style="{}" />""#,
-            self.x, self.y, self.w, self.h, self.border_radius, self.style
-        )
+        if let Some(title) = self.title {
+            write!(
+                f,
+                r#"<rect x="{}" y="{}" width="{}" height="{}" ry="{}" style="{}"><title>{title}</title></rect>""#,
+                self.x, self.y, self.w, self.h, self.border_radius, self.style
+            )
+        } else {
+            write!(
+                f,
+                r#"<rect x="{}" y="{}" width="{}" height="{}" ry="{}" style="{}" />""#,
+                self.x, self.y, self.w, self.h, self.border_radius, self.style
+            )
+        }
     }
 }