aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 5707b98580a04efeb108cfdcc9338ecbfd418afc (plain)
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
//! A text selection API built around [`iced`]'s [`Paragraph`].
//!
//! [`iced`]: https://iced.rs
//! [`Paragraph`]: https://docs.iced.rs/iced_graphics/text/paragraph/struct.Paragraph.html

#[cfg(feature = "markdown")]
pub mod markdown;
pub mod operation;
pub mod selectable;
pub mod selection;
pub mod text;

use core::Element;

use iced_widget::core;
use iced_widget::graphics::text::Paragraph;
#[cfg(feature = "markdown")]
pub use markdown::view as markdown;
#[doc(no_inline)]
pub use text::Text;

/// Creates a new [`Text`] widget with the provided content.
///
/// [`Text`]: core::widget::Text
///
/// This macro uses the same syntax as [`format!`], but creates a new [`Text`] widget instead.
///
/// See [the formatting documentation in `std::fmt`](std::fmt)
/// for details of the macro argument syntax.
///
/// # Examples
///
/// ```no_run,ignore
/// use iced_selection::text;
///
/// enum Message {
///     // ...
/// }
///
/// fn view(_state: &State) -> Element<Message> {
///     let simple = text!("Hello, world!");
///
///     let keyword = text!("Hello, {}", "world!");
///
///     let planet = "Earth";
///     let local_variable = text!("Hello, {planet}!");
///     // ...
/// }
/// ```
#[macro_export]
macro_rules! text {
    ($($arg:tt)*) => {
        $crate::Text::new(format!($($arg)*))
    };
}

/// Creates some [`Rich`] text with the given spans.
///
/// # Example
/// ```no_run,ignore
/// use iced::font;
/// use iced_selection::{rich_text, span};
/// use iced::{color, never, Font};
///
/// #[derive(Debug, Clone)]
/// enum Message {
///     // ...
/// }
///
/// fn view(state: &State) -> Element<'_, Message> {
///     rich_text![
///         span("I am red!").color(color!(0xff0000)),
///         span(" "),
///         span("And I am bold!").font(Font { weight: font::Weight::Bold, ..Font::default() }),
///     ]
///     .on_link_click(never)
///     .size(20)
///     .into()
/// }
/// ```
///
/// [`Rich`]: crate::text::Rich
#[macro_export]
macro_rules! rich_text {
    () => (
        $crate::text::Rich::new()
    );
    ($($x:expr),+ $(,)?) => (
        $crate::text::Rich::from_iter([$($crate::text::Span::from($x)),+])
    );
}

/// Creates a new [`Span`] of text with the provided content.
///
/// A [`Span`] is a fragment of some [`Rich`] text.
///
/// This macro uses the same syntax as [`format!`], but creates a new [`Span`] widget instead.
///
/// See [the formatting documentation in `std::fmt`](std::fmt)
/// for details of the macro argument syntax.
///
/// # Example
/// ```no_run,ignore
/// use iced::font;
/// use iced_selection::{rich_text, span};
/// use iced::{color, never, Font};
///
/// #[derive(Debug, Clone)]
/// enum Message {
///     // ...
/// }
///
/// fn view(state: &State) -> Element<'_, Message> {
///     rich_text![
///         span!("I am {}!", red).color(color!(0xff0000)),
///         " ",
///         span!("And I am bold!").font(Font { weight: font::Weight::Bold, ..Font::default() }),
///     ]
///     .on_link_click(never)
///     .size(20)
///     .into()
/// }
/// ```
///
/// [`Rich`]: crate::text::Rich
/// [`Span`]: crate::text::Span
#[macro_export]
macro_rules! span {
    ($($arg:tt)*) => {
        $crate::text::Span::new(format!($($arg)*))
    };
}

/// Creates a new [`Text`] widget with the provided content.
///
/// # Example
/// ```no_run,ignore
/// use iced_selection::text;
///
/// enum Message {
///     // ...
/// }
///
/// fn view(state: &State) -> Element<'_, Message> {
///     text("Hello, this is iced!")
///         .size(20)
///         .into()
/// }
/// ```
pub fn text<'a, Theme, Renderer>(
    text: impl text::IntoFragment<'a>,
) -> Text<'a, Theme, Renderer>
where
    Theme: text::Catalog + 'a,
    Renderer: core::text::Renderer,
{
    Text::new(text)
}

/// Creates some [`Rich`] text with the given spans.
///
/// [`Rich`]: crate::text::Rich
///
/// # Example
/// ```no_run,ignore
/// use iced::font;
/// use iced_selection::{rich_text, span};
/// use iced::{color, never, Font};
///
/// #[derive(Debug, Clone)]
/// enum Message {
///     LinkClicked(&'static str),
///     // ...
/// }
///
/// fn view(state: &State) -> Element<'_, Message> {
///     rich_text([
///         span("I am red!").color(color!(0xff0000)),
///         span(" "),
///         span("And I am bold!").font(Font { weight: font::Weight::Bold, ..Font::default() }),
///     ])
///     .on_link_click(never)
///     .size(20)
///     .into()
/// }
/// ```
pub fn rich_text<'a, Link, Message, Theme, Renderer>(
    spans: impl AsRef<[text::Span<'a, Link, core::Font>]> + 'a,
) -> text::Rich<'a, Link, Message, Theme, Renderer>
where
    Link: Clone + 'static,
    Theme: text::Catalog + 'a,
    Renderer: core::text::Renderer<Paragraph = Paragraph, Font = core::Font>,
{
    text::Rich::with_spans(spans)
}

/// Creates a new [`Span`] of text with the provided content.
///
/// A [`Span`] is a fragment of some [`Rich`] text.
///
/// [`Rich`]: crate::text::Rich
/// [`Span`]: crate::text::Span
///
/// # Example
/// ```no_run,ignore
/// use iced::font;
/// use iced_selection::{rich_text, span};
/// use iced::{color, never, Font};
///
/// #[derive(Debug, Clone)]
/// enum Message {
///     // ...
/// }
///
/// fn view(state: &State) -> Element<'_, Message> {
///     rich_text![
///         span("I am red!").color(color!(0xff0000)),
///         " ",
///         span("And I am bold!").font(Font { weight: font::Weight::Bold, ..Font::default() }),
///     ]
///     .on_link_click(never)
///     .size(20)
///     .into()
/// }
/// ```
pub fn span<'a, Link>(
    text: impl text::IntoFragment<'a>,
) -> text::Span<'a, Link, core::Font> {
    text::Span::new(text)
}

/// Creates some [`Selectable`] with the given content. This [`Selectable`] will make all inner
/// [`Text`] or [`Rich`] change their behavior from an independent selectable to a global
/// selectable which makes it so you can select across widgets and start selections out of bounds
/// of widgets. This [`Selectable`] will also handle the 'ctrl + c' shortcut by getting all the
/// selected text inside it and write it to the clipboard.
///
/// WARNING: You should only have one [`Selectable`] on your app, preferrably as the root of your
/// app to make sure that any [`Text`] or [`Rich`] is inside it. Having multiple selectables or a
/// selectable along side some [`Text`] or [`Rich`] is undefined behavior and even though the
/// selection might look ok for you the copied text will probably not be what you expect.
///
/// [`Selectable`]: crate::selectable::Selectable
/// [`Text`]: crate::Text
/// [`Rich`]: crate::text::Rich
///
/// # Example
/// ```no_run,ignore
/// use iced::font;
/// use iced_selection::{rich_text, selectable, span};
/// use iced::{color, column, never, Font};
///
/// #[derive(Debug, Clone)]
/// enum Message {
///     LinkClicked(&'static str),
///     // ...
/// }
///
/// fn view(state: &State) -> Element<'_, Message> {
///     selectable(
///         column![
///             rich_text([
///                 span("I am red!").color(color!(0xff0000)),
///                 span(" "),
///                 span("And I am bold!").font(Font { weight: font::Weight::Bold, ..Font::default() }),
///             ])
///             .on_link_click(never)
///             .size(20),
///             text("Hello, this is iced!"),
///         ]
///     )
///     .into()
/// }
/// ```
pub fn selectable<'a, Message, Theme, Renderer>(
    content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> selectable::Selectable<'a, Message, Theme, Renderer>
where
    Theme: 'a,
    Renderer: core::Renderer,
{
    selectable::Selectable::new(content)
}