prehnite_core/widget/
text.rs1#![doc = "汎用的な[`Text`]のヘルパ"]
2use crate::widget::font::get_font_opt;
3use iced::alignment::Vertical;
4use iced::widget::text::{Rich, Wrapping};
5use iced::widget::{text, Text};
6use iced::{widget, Alignment, Font, Length, Pixels};
7
8macro_rules! text_builder_template {
9 ($self:ident, $res:ident, $func:path, $v:ident) => {
10 let mut $res = $func($v);
11 if let Some(f) = $self.font {
12 $res = $res.font(f);
13 }
14 if let Some(w) = $self.wrapping {
15 $res = $res.wrapping(w);
16 }
17 if let Some(a) = $self.alignment {
18 $res = $res.align_x(a);
19 }
20 if let Some(v) = $self.vertical {
21 $res = $res.align_y(v);
22 }
23 if let Some(w) = $self.width {
24 $res = $res.width(w);
25 }
26 if let Some(h) = $self.height {
27 $res = $res.height(h);
28 }
29 if let Some(s) = $self.size {
30 $res = $res.size(s);
31 }
32 };
33}
34
35#[derive(Default)]
36pub struct TextBuilder {
38 pub font: Option<Font>,
39 pub wrapping: Option<Wrapping>,
40 pub alignment: Option<Alignment>,
41 pub vertical: Option<Vertical>,
42 pub width: Option<Length>,
43 pub height: Option<Length>,
44 pub size: Option<Pixels>,
45}
46
47impl TextBuilder {
48 pub fn text<'a>(&self, txt: impl text::IntoFragment<'a>) -> Text<'a> {
50 text_builder_template!(self, t, text, txt);
51 t
52 }
53
54 pub fn rich<'a, Link, Message>(
56 &self,
57 spans: impl AsRef<[text::Span<'a, Link, Font>]> + 'a,
58 ) -> Rich<'a, Link, Message>
59 where
60 Link: Clone + 'static,
61 {
62 text_builder_template!(self, r, widget::rich_text, spans);
63 r
64 }
65
66 pub fn with_font() -> Self {
68 Self {
69 font: get_font_opt(),
70 ..Default::default()
71 }
72 }
73
74 pub fn font(mut self, v: impl Into<Font>) -> Self {
76 self.font = Some(v.into());
77 self
78 }
79
80 pub fn wrapping(mut self, wrapping: impl Into<Wrapping>) -> Self {
82 self.wrapping = Some(wrapping.into());
83 self
84 }
85
86 pub fn alignment(mut self, alignment: impl Into<Alignment>) -> Self {
88 self.alignment = Some(alignment.into());
89 self
90 }
91
92 pub fn vertical(mut self, vertical: impl Into<Vertical>) -> Self {
94 self.vertical = Some(vertical.into());
95 self
96 }
97
98 pub fn width(mut self, width: impl Into<Length>) -> Self {
100 self.width = Some(width.into());
101 self
102 }
103
104 pub fn height(mut self, height: impl Into<Length>) -> Self {
106 self.height = Some(height.into());
107 self
108 }
109
110 pub fn size(mut self, size: impl Into<Pixels>) -> Self {
112 self.size = Some(size.into());
113 self
114 }
115}