style_attributes.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. - [ ] pub display: Display,
  3. - [x] pub position_type: PositionType, --> kinda, stretch doesnt support everything
  4. - [ ] pub direction: Direction,
  5. - [x] pub flex_direction: FlexDirection,
  6. - [x] pub flex_wrap: FlexWrap,
  7. - [x] pub flex_grow: f32,
  8. - [x] pub flex_shrink: f32,
  9. - [x] pub flex_basis: Dimension,
  10. - [x] pub overflow: Overflow, ---> kinda implemented... stretch doesnt have support for directional overflow
  11. - [x] pub align_items: AlignItems,
  12. - [x] pub align_self: AlignSelf,
  13. - [x] pub align_content: AlignContent,
  14. - [x] pub margin: Rect<Dimension>,
  15. - [x] pub padding: Rect<Dimension>,
  16. - [x] pub justify_content: JustifyContent,
  17. - [ ] pub position: Rect<Dimension>,
  18. - [x] pub border: Rect<Dimension>,
  19. - [ ] pub size: Size<Dimension>, ----> ??? seems to only be relevant for input?
  20. - [ ] pub min_size: Size<Dimension>,
  21. - [ ] pub max_size: Size<Dimension>,
  22. - [ ] pub aspect_ratio: Number,
  23. */
  24. use dioxus_core::Attribute;
  25. use dioxus_native_core::{
  26. layout_attributes::{parse_value, UnitSystem},
  27. state::{AttributeMask, NodeMask, NodeView, ParentDepState},
  28. };
  29. use crate::style::{RinkColor, RinkStyle};
  30. #[derive(Default, Clone, PartialEq, Debug)]
  31. pub struct StyleModifier {
  32. pub style: RinkStyle,
  33. pub modifier: TuiModifier,
  34. }
  35. impl ParentDepState for StyleModifier {
  36. type Ctx = ();
  37. type DepState = Self;
  38. // todo: seperate each attribute into it's own class
  39. const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::All, true, true, false);
  40. fn reduce(&mut self, node: NodeView, parent: Option<&Self::DepState>, _: &Self::Ctx) -> bool {
  41. let mut new = StyleModifier::default();
  42. if parent.is_some() {
  43. new.style.fg = None;
  44. }
  45. // handle text modifier elements
  46. if node.namespace().is_none() {
  47. if let Some(tag) = node.tag() {
  48. match tag {
  49. "b" => apply_style_attributes("font-weight", "bold", &mut new),
  50. "strong" => apply_style_attributes("font-weight", "bold", &mut new),
  51. "u" => apply_style_attributes("text-decoration", "underline", &mut new),
  52. "ins" => apply_style_attributes("text-decoration", "underline", &mut new),
  53. "del" => apply_style_attributes("text-decoration", "line-through", &mut new),
  54. "i" => apply_style_attributes("font-style", "italic", &mut new),
  55. "em" => apply_style_attributes("font-style", "italic", &mut new),
  56. "mark" => {
  57. apply_style_attributes("background-color", "rgba(241, 231, 64, 50%)", self)
  58. }
  59. _ => (),
  60. }
  61. }
  62. }
  63. // gather up all the styles from the attribute list
  64. for &Attribute { name, value, .. } in node.attributes() {
  65. apply_style_attributes(name, value, &mut new);
  66. }
  67. // keep the text styling from the parent element
  68. if let Some(parent) = parent {
  69. let mut new_style = new.style.merge(parent.style);
  70. new_style.bg = new.style.bg;
  71. new.style = new_style;
  72. }
  73. if &mut new != self {
  74. *self = new;
  75. true
  76. } else {
  77. false
  78. }
  79. }
  80. }
  81. #[derive(Default, Clone, PartialEq, Debug)]
  82. pub struct TuiModifier {
  83. pub borders: Borders,
  84. }
  85. #[derive(Default, Clone, PartialEq, Debug)]
  86. pub struct Borders {
  87. pub top: BorderEdge,
  88. pub right: BorderEdge,
  89. pub bottom: BorderEdge,
  90. pub left: BorderEdge,
  91. }
  92. impl Borders {
  93. fn slice(&mut self) -> [&mut BorderEdge; 4] {
  94. [
  95. &mut self.top,
  96. &mut self.right,
  97. &mut self.bottom,
  98. &mut self.left,
  99. ]
  100. }
  101. }
  102. #[derive(Clone, PartialEq, Debug)]
  103. pub struct BorderEdge {
  104. pub color: Option<RinkColor>,
  105. pub style: BorderStyle,
  106. pub width: UnitSystem,
  107. pub radius: UnitSystem,
  108. }
  109. impl Default for BorderEdge {
  110. fn default() -> Self {
  111. Self {
  112. color: None,
  113. style: BorderStyle::None,
  114. width: UnitSystem::Point(0.0),
  115. radius: UnitSystem::Point(0.0),
  116. }
  117. }
  118. }
  119. #[derive(Clone, Copy, PartialEq, Debug)]
  120. pub enum BorderStyle {
  121. Dotted,
  122. Dashed,
  123. Solid,
  124. Double,
  125. Groove,
  126. Ridge,
  127. Inset,
  128. Outset,
  129. Hidden,
  130. None,
  131. }
  132. impl BorderStyle {
  133. pub fn symbol_set(&self) -> Option<tui::symbols::line::Set> {
  134. use tui::symbols::line::*;
  135. const DASHED: Set = Set {
  136. horizontal: "╌",
  137. vertical: "╎",
  138. ..NORMAL
  139. };
  140. const DOTTED: Set = Set {
  141. horizontal: "┈",
  142. vertical: "┊",
  143. ..NORMAL
  144. };
  145. match self {
  146. BorderStyle::Dotted => Some(DOTTED),
  147. BorderStyle::Dashed => Some(DASHED),
  148. BorderStyle::Solid => Some(NORMAL),
  149. BorderStyle::Double => Some(DOUBLE),
  150. BorderStyle::Groove => Some(NORMAL),
  151. BorderStyle::Ridge => Some(NORMAL),
  152. BorderStyle::Inset => Some(NORMAL),
  153. BorderStyle::Outset => Some(NORMAL),
  154. BorderStyle::Hidden => None,
  155. BorderStyle::None => None,
  156. }
  157. }
  158. }
  159. /// applies the entire html namespace defined in dioxus-html
  160. pub fn apply_style_attributes(
  161. //
  162. name: &str,
  163. value: &str,
  164. style: &mut StyleModifier,
  165. ) {
  166. match name {
  167. "animation"
  168. | "animation-delay"
  169. | "animation-direction"
  170. | "animation-duration"
  171. | "animation-fill-mode"
  172. | "animation-iteration-count"
  173. | "animation-name"
  174. | "animation-play-state"
  175. | "animation-timing-function" => apply_animation(name, value, style),
  176. "backface-visibility" => {}
  177. "background"
  178. | "background-attachment"
  179. | "background-clip"
  180. | "background-color"
  181. | "background-image"
  182. | "background-origin"
  183. | "background-position"
  184. | "background-repeat"
  185. | "background-size" => apply_background(name, value, style),
  186. "border"
  187. | "border-bottom"
  188. | "border-bottom-color"
  189. | "border-bottom-left-radius"
  190. | "border-bottom-right-radius"
  191. | "border-bottom-style"
  192. | "border-bottom-width"
  193. | "border-collapse"
  194. | "border-color"
  195. | "border-image"
  196. | "border-image-outset"
  197. | "border-image-repeat"
  198. | "border-image-slice"
  199. | "border-image-source"
  200. | "border-image-width"
  201. | "border-left"
  202. | "border-left-color"
  203. | "border-left-style"
  204. | "border-left-width"
  205. | "border-radius"
  206. | "border-right"
  207. | "border-right-color"
  208. | "border-right-style"
  209. | "border-right-width"
  210. | "border-spacing"
  211. | "border-style"
  212. | "border-top"
  213. | "border-top-color"
  214. | "border-top-left-radius"
  215. | "border-top-right-radius"
  216. | "border-top-style"
  217. | "border-top-width"
  218. | "border-width" => apply_border(name, value, style),
  219. "bottom" => {}
  220. "box-shadow" => {}
  221. "box-sizing" => {}
  222. "caption-side" => {}
  223. "clear" => {}
  224. "clip" => {}
  225. "color" => {
  226. if let Ok(c) = value.parse() {
  227. style.style.fg.replace(c);
  228. }
  229. }
  230. "columns" => {}
  231. "content" => {}
  232. "counter-increment" => {}
  233. "counter-reset" => {}
  234. "cursor" => {}
  235. "empty-cells" => {}
  236. "float" => {}
  237. "font" | "font-family" | "font-size" | "font-size-adjust" | "font-stretch"
  238. | "font-style" | "font-variant" | "font-weight" => apply_font(name, value, style),
  239. "letter-spacing" => {}
  240. "line-height" => {}
  241. "list-style" | "list-style-image" | "list-style-position" | "list-style-type" => {}
  242. "opacity" => {}
  243. "order" => {}
  244. "outline" => {}
  245. "outline-color" | "outline-offset" | "outline-style" | "outline-width" => {}
  246. "page-break-after" | "page-break-before" | "page-break-inside" => {}
  247. "perspective" | "perspective-origin" => {}
  248. "pointer-events" => {}
  249. "quotes" => {}
  250. "resize" => {}
  251. "tab-size" => {}
  252. "table-layout" => {}
  253. "text-align"
  254. | "text-align-last"
  255. | "text-decoration"
  256. | "text-decoration-color"
  257. | "text-decoration-line"
  258. | "text-decoration-style"
  259. | "text-indent"
  260. | "text-justify"
  261. | "text-overflow"
  262. | "text-shadow"
  263. | "text-transform" => apply_text(name, value, style),
  264. "transition"
  265. | "transition-delay"
  266. | "transition-duration"
  267. | "transition-property"
  268. | "transition-timing-function" => apply_transition(name, value, style),
  269. "visibility" => {}
  270. "white-space" => {}
  271. _ => {}
  272. }
  273. }
  274. fn apply_background(name: &str, value: &str, style: &mut StyleModifier) {
  275. match name {
  276. "background-color" => {
  277. if let Ok(c) = value.parse() {
  278. style.style.bg.replace(c);
  279. }
  280. }
  281. "background" => {}
  282. "background-attachment" => {}
  283. "background-clip" => {}
  284. "background-image" => {}
  285. "background-origin" => {}
  286. "background-position" => {}
  287. "background-repeat" => {}
  288. "background-size" => {}
  289. _ => {}
  290. }
  291. }
  292. fn apply_border(name: &str, value: &str, style: &mut StyleModifier) {
  293. fn parse_border_style(v: &str) -> BorderStyle {
  294. match v {
  295. "dotted" => BorderStyle::Dotted,
  296. "dashed" => BorderStyle::Dashed,
  297. "solid" => BorderStyle::Solid,
  298. "double" => BorderStyle::Double,
  299. "groove" => BorderStyle::Groove,
  300. "ridge" => BorderStyle::Ridge,
  301. "inset" => BorderStyle::Inset,
  302. "outset" => BorderStyle::Outset,
  303. "none" => BorderStyle::None,
  304. "hidden" => BorderStyle::Hidden,
  305. _ => todo!(),
  306. }
  307. }
  308. match name {
  309. "border" => {}
  310. "border-bottom" => {}
  311. "border-bottom-color" => {
  312. if let Ok(c) = value.parse() {
  313. style.modifier.borders.bottom.color = Some(c);
  314. }
  315. }
  316. "border-bottom-left-radius" => {
  317. if let Some(v) = parse_value(value) {
  318. style.modifier.borders.left.radius = v;
  319. }
  320. }
  321. "border-bottom-right-radius" => {
  322. if let Some(v) = parse_value(value) {
  323. style.modifier.borders.right.radius = v;
  324. }
  325. }
  326. "border-bottom-style" => style.modifier.borders.bottom.style = parse_border_style(value),
  327. "border-bottom-width" => {
  328. if let Some(v) = parse_value(value) {
  329. style.modifier.borders.bottom.width = v;
  330. }
  331. }
  332. "border-collapse" => {}
  333. "border-color" => {
  334. let values: Vec<_> = value.split(' ').collect();
  335. if values.len() == 1 {
  336. if let Ok(c) = values[0].parse() {
  337. style
  338. .modifier
  339. .borders
  340. .slice()
  341. .iter_mut()
  342. .for_each(|b| b.color = Some(c));
  343. }
  344. } else {
  345. for (v, b) in values
  346. .into_iter()
  347. .zip(style.modifier.borders.slice().iter_mut())
  348. {
  349. if let Ok(c) = v.parse() {
  350. b.color = Some(c);
  351. }
  352. }
  353. }
  354. }
  355. "border-image" => {}
  356. "border-image-outset" => {}
  357. "border-image-repeat" => {}
  358. "border-image-slice" => {}
  359. "border-image-source" => {}
  360. "border-image-width" => {}
  361. "border-left" => {}
  362. "border-left-color" => {
  363. if let Ok(c) = value.parse() {
  364. style.modifier.borders.left.color = Some(c);
  365. }
  366. }
  367. "border-left-style" => style.modifier.borders.left.style = parse_border_style(value),
  368. "border-left-width" => {
  369. if let Some(v) = parse_value(value) {
  370. style.modifier.borders.left.width = v;
  371. }
  372. }
  373. "border-radius" => {
  374. let values: Vec<_> = value.split(' ').collect();
  375. if values.len() == 1 {
  376. if let Some(r) = parse_value(values[0]) {
  377. style
  378. .modifier
  379. .borders
  380. .slice()
  381. .iter_mut()
  382. .for_each(|b| b.radius = r);
  383. }
  384. } else {
  385. for (v, b) in values
  386. .into_iter()
  387. .zip(style.modifier.borders.slice().iter_mut())
  388. {
  389. if let Some(r) = parse_value(v) {
  390. b.radius = r;
  391. }
  392. }
  393. }
  394. }
  395. "border-right" => {}
  396. "border-right-color" => {
  397. if let Ok(c) = value.parse() {
  398. style.modifier.borders.right.color = Some(c);
  399. }
  400. }
  401. "border-right-style" => style.modifier.borders.right.style = parse_border_style(value),
  402. "border-right-width" => {
  403. if let Some(v) = parse_value(value) {
  404. style.modifier.borders.right.width = v;
  405. }
  406. }
  407. "border-spacing" => {}
  408. "border-style" => {
  409. let values: Vec<_> = value.split(' ').collect();
  410. if values.len() == 1 {
  411. let border_style = parse_border_style(values[0]);
  412. style
  413. .modifier
  414. .borders
  415. .slice()
  416. .iter_mut()
  417. .for_each(|b| b.style = border_style);
  418. } else {
  419. for (v, b) in values
  420. .into_iter()
  421. .zip(style.modifier.borders.slice().iter_mut())
  422. {
  423. b.style = parse_border_style(v);
  424. }
  425. }
  426. }
  427. "border-top" => {}
  428. "border-top-color" => {
  429. if let Ok(c) = value.parse() {
  430. style.modifier.borders.top.color = Some(c);
  431. }
  432. }
  433. "border-top-left-radius" => {
  434. if let Some(v) = parse_value(value) {
  435. style.modifier.borders.left.radius = v;
  436. }
  437. }
  438. "border-top-right-radius" => {
  439. if let Some(v) = parse_value(value) {
  440. style.modifier.borders.right.radius = v;
  441. }
  442. }
  443. "border-top-style" => style.modifier.borders.top.style = parse_border_style(value),
  444. "border-top-width" => {
  445. if let Some(v) = parse_value(value) {
  446. style.modifier.borders.top.width = v;
  447. }
  448. }
  449. "border-width" => {
  450. let values: Vec<_> = value.split(' ').collect();
  451. if values.len() == 1 {
  452. if let Some(w) = parse_value(values[0]) {
  453. style
  454. .modifier
  455. .borders
  456. .slice()
  457. .iter_mut()
  458. .for_each(|b| b.width = w);
  459. }
  460. } else {
  461. for (v, width) in values
  462. .into_iter()
  463. .zip(style.modifier.borders.slice().iter_mut())
  464. {
  465. if let Some(w) = parse_value(v) {
  466. width.width = w;
  467. }
  468. }
  469. }
  470. }
  471. _ => (),
  472. }
  473. }
  474. fn apply_animation(name: &str, _value: &str, _style: &mut StyleModifier) {
  475. match name {
  476. "animation" => {}
  477. "animation-delay" => {}
  478. "animation-direction =>{}" => {}
  479. "animation-duration" => {}
  480. "animation-fill-mode" => {}
  481. "animation-itera =>{}tion-count" => {}
  482. "animation-name" => {}
  483. "animation-play-state" => {}
  484. "animation-timing-function" => {}
  485. _ => {}
  486. }
  487. }
  488. fn apply_font(name: &str, value: &str, style: &mut StyleModifier) {
  489. use tui::style::Modifier;
  490. match name {
  491. "font" => (),
  492. "font-family" => (),
  493. "font-size" => (),
  494. "font-size-adjust" => (),
  495. "font-stretch" => (),
  496. "font-style" => match value {
  497. "italic" => style.style = style.style.add_modifier(Modifier::ITALIC),
  498. "oblique" => style.style = style.style.add_modifier(Modifier::ITALIC),
  499. _ => (),
  500. },
  501. "font-variant" => todo!(),
  502. "font-weight" => match value {
  503. "bold" => style.style = style.style.add_modifier(Modifier::BOLD),
  504. "normal" => style.style = style.style.remove_modifier(Modifier::BOLD),
  505. _ => (),
  506. },
  507. _ => (),
  508. }
  509. }
  510. fn apply_text(name: &str, value: &str, style: &mut StyleModifier) {
  511. use tui::style::Modifier;
  512. match name {
  513. "text-align" => todo!(),
  514. "text-align-last" => todo!(),
  515. "text-decoration" | "text-decoration-line" => {
  516. for v in value.split(' ') {
  517. match v {
  518. "line-through" => style.style = style.style.add_modifier(Modifier::CROSSED_OUT),
  519. "underline" => style.style = style.style.add_modifier(Modifier::UNDERLINED),
  520. _ => (),
  521. }
  522. }
  523. }
  524. "text-decoration-color" => todo!(),
  525. "text-decoration-style" => todo!(),
  526. "text-indent" => todo!(),
  527. "text-justify" => todo!(),
  528. "text-overflow" => todo!(),
  529. "text-shadow" => todo!(),
  530. "text-transform" => todo!(),
  531. _ => todo!(),
  532. }
  533. }
  534. fn apply_transition(_name: &str, _value: &str, _style: &mut StyleModifier) {
  535. todo!()
  536. }