Skip to content

Commit 3319719

Browse files
authored
Merge pull request #137 from embedded-graphics/multiple-plugins
Allow using multiple plugins
2 parents 763f32b + 80f3883 commit 3319719

File tree

6 files changed

+137
-4
lines changed

6 files changed

+137
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Unreleased
99
- `TabSize::default()`
1010
* [#134] `Tail` plugin
1111
* [#135] Allow using the built-in plugins without the `plugin` feature.
12+
* [#137] Allow using multiple plugins.
1213

1314
## Changed:
1415

@@ -21,6 +22,7 @@ Unreleased
2122
[#133]: https://github.com/embedded-graphics/embedded-text/pull/133
2223
[#134]: https://github.com/embedded-graphics/embedded-text/pull/134
2324
[#135]: https://github.com/embedded-graphics/embedded-text/pull/135
25+
[#137]: https://github.com/embedded-graphics/embedded-text/pull/137
2426

2527
0.5.0-beta.2 (2021-07-10)
2628
==========================

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ az = "1.1"
3636
embedded-graphics = "0.7.0"
3737
ansi-parser = { version = "0.8.0", default-features = false, optional = true }
3838
as-slice = { version = "0.1.4", optional = true }
39+
object-chain = "0.1"
3940

4041
[dev-dependencies]
4142
embedded-graphics-simulator = "0.3.0"

src/lib.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ use embedded_graphics::{
128128
text::renderer::{CharacterStyle, TextRenderer},
129129
transform::Transform,
130130
};
131+
use object_chain::{Chain, ChainElement, Link};
131132
pub use parser::{ChangeTextStyle, Token};
132133
pub use rendering::TextBoxProperties;
133134

@@ -242,7 +243,7 @@ where
242243

243244
/// Adds a new plugin to the `TextBox`.
244245
#[inline]
245-
pub fn add_plugin<M>(self, plugin: M) -> TextBox<'a, S, M>
246+
pub fn add_plugin<M>(self, plugin: M) -> TextBox<'a, S, Chain<M>>
246247
where
247248
M: Plugin<'a, <S as TextRenderer>::Color>,
248249
{
@@ -252,7 +253,35 @@ where
252253
character_style: self.character_style,
253254
style: self.style,
254255
vertical_offset: self.vertical_offset,
255-
plugin: PluginWrapper::new(plugin),
256+
plugin: PluginWrapper::new(Chain::new(plugin)),
257+
};
258+
textbox.style.height_mode.apply(&mut textbox);
259+
260+
textbox
261+
}
262+
}
263+
264+
impl<'a, S, P> TextBox<'a, S, P>
265+
where
266+
<S as TextRenderer>::Color: From<Rgb888>,
267+
S: TextRenderer + CharacterStyle,
268+
P: Plugin<'a, <S as TextRenderer>::Color> + ChainElement,
269+
{
270+
/// Adds a new plugin to the `TextBox`.
271+
#[inline]
272+
pub fn add_plugin<M>(self, plugin: M) -> TextBox<'a, S, Link<M, P>>
273+
where
274+
M: Plugin<'a, <S as TextRenderer>::Color>,
275+
{
276+
let parent = self.plugin.inner.into_inner();
277+
278+
let mut textbox = TextBox {
279+
text: self.text,
280+
bounds: self.bounds,
281+
character_style: self.character_style,
282+
style: self.style,
283+
vertical_offset: self.vertical_offset,
284+
plugin: PluginWrapper::new(parent.plugin.append(plugin)),
256285
};
257286
textbox.style.height_mode.apply(&mut textbox);
258287

src/plugin/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ where
7171
C: PixelColor,
7272
{
7373
lookahead: M,
74-
plugin: M,
74+
pub plugin: M,
7575
state: ProcessingState,
7676
peeked_token: (usize, Option<Token<'a, C>>),
7777
}
@@ -81,7 +81,7 @@ pub(crate) struct PluginWrapper<'a, M, C>
8181
where
8282
C: PixelColor,
8383
{
84-
inner: RefCell<PluginInner<'a, M, C>>,
84+
pub inner: RefCell<PluginInner<'a, M, C>>,
8585
}
8686

8787
impl<'a, M, C> Hash for PluginWrapper<'a, M, C>

src/plugin/private.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use embedded_graphics::{
66
primitives::Rectangle,
77
text::renderer::{CharacterStyle, TextRenderer},
88
};
9+
use object_chain::{Chain, ChainElement, Link};
910

1011
use crate::{parser::Token, rendering::cursor::Cursor, TextBoxProperties};
1112

@@ -70,3 +71,102 @@ where
7071
}
7172

7273
impl<'a, C> Plugin<'a, C> for super::NoPlugin<C> where C: PixelColor {}
74+
75+
impl<'a, C, P> Plugin<'a, C> for Chain<P>
76+
where
77+
P: Plugin<'a, C>,
78+
C: PixelColor,
79+
Chain<P>: Clone,
80+
{
81+
fn new_line(&mut self) {
82+
self.object.new_line();
83+
}
84+
85+
fn next_token(
86+
&mut self,
87+
next_token: impl FnMut() -> Option<Token<'a, C>>,
88+
) -> Option<Token<'a, C>> {
89+
self.object.next_token(next_token)
90+
}
91+
92+
fn render_token(&mut self, token: Token<'a, C>) -> Option<Token<'a, C>> {
93+
self.object.render_token(token)
94+
}
95+
96+
fn post_render<T, D>(
97+
&mut self,
98+
draw_target: &mut D,
99+
character_style: &T,
100+
text: &str,
101+
bounds: Rectangle,
102+
) -> Result<(), D::Error>
103+
where
104+
T: TextRenderer<Color = C>,
105+
D: DrawTarget<Color = C>,
106+
{
107+
self.object
108+
.post_render(draw_target, character_style, text, bounds)
109+
}
110+
111+
fn on_start_render<S: CharacterStyle>(
112+
&mut self,
113+
cursor: &mut Cursor,
114+
props: TextBoxProperties<'_, S>,
115+
) {
116+
self.object.on_start_render(cursor, props)
117+
}
118+
}
119+
120+
impl<'a, C, P, CE> Plugin<'a, C> for Link<P, CE>
121+
where
122+
CE: ChainElement + Plugin<'a, C>,
123+
P: Plugin<'a, C>,
124+
C: PixelColor,
125+
Link<P, CE>: Clone,
126+
{
127+
fn new_line(&mut self) {
128+
self.parent.new_line();
129+
self.object.new_line();
130+
}
131+
132+
fn next_token(
133+
&mut self,
134+
mut next_token: impl FnMut() -> Option<Token<'a, C>>,
135+
) -> Option<Token<'a, C>> {
136+
let parent = &mut self.parent;
137+
let next_token = || parent.next_token(&mut next_token);
138+
self.object.next_token(next_token)
139+
}
140+
141+
fn render_token(&mut self, token: Token<'a, C>) -> Option<Token<'a, C>> {
142+
self.parent
143+
.render_token(token)
144+
.and_then(|t| self.object.render_token(t))
145+
}
146+
147+
fn post_render<T, D>(
148+
&mut self,
149+
draw_target: &mut D,
150+
character_style: &T,
151+
text: &str,
152+
bounds: Rectangle,
153+
) -> Result<(), D::Error>
154+
where
155+
T: TextRenderer<Color = C>,
156+
D: DrawTarget<Color = C>,
157+
{
158+
self.parent
159+
.post_render(draw_target, character_style, text, bounds)?;
160+
self.object
161+
.post_render(draw_target, character_style, text, bounds)
162+
}
163+
164+
fn on_start_render<S: CharacterStyle>(
165+
&mut self,
166+
cursor: &mut Cursor,
167+
props: TextBoxProperties<'_, S>,
168+
) {
169+
self.parent.on_start_render(cursor, props.clone());
170+
self.object.on_start_render(cursor, props);
171+
}
172+
}

src/rendering/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use line_iter::LineEndType;
3030
/// Text box properties.
3131
///
3232
/// This struct holds information about the text box.
33+
#[derive(Clone)]
3334
pub struct TextBoxProperties<'a, S> {
3435
/// The used text box style.
3536
pub box_style: &'a TextBoxStyle,

0 commit comments

Comments
 (0)