44 lines
1.4 KiB
Rust
Executable File
44 lines
1.4 KiB
Rust
Executable File
use std::marker::PhantomData;
|
|
|
|
use druid::{BoxConstraints, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx, Size, UpdateCtx, Widget, WidgetPod};
|
|
use druid::text::RichText;
|
|
use druid::widget::{LineBreaking, RawLabel};
|
|
|
|
pub struct RichTextDisplay<T> {
|
|
text: RichText,
|
|
label: WidgetPod<RichText, RawLabel<RichText>>,
|
|
_phantom: PhantomData<T>,
|
|
}
|
|
|
|
impl<T> RichTextDisplay<T> {
|
|
pub fn new(text: RichText) -> Self {
|
|
Self {
|
|
text,
|
|
label: WidgetPod::new(RawLabel::new().with_line_break_mode(LineBreaking::WordWrap)),
|
|
_phantom: PhantomData,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T> Widget<T> for RichTextDisplay<T> {
|
|
fn event(&mut self, ctx: &mut EventCtx, event: &Event, _data: &mut T, env: &Env) {
|
|
self.label.event(ctx, event, &mut self.text, env);
|
|
}
|
|
|
|
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, _data: &T, env: &Env) {
|
|
self.label.lifecycle(ctx, event, &self.text, env);
|
|
}
|
|
|
|
fn update(&mut self, ctx: &mut UpdateCtx, _old_data: &T, _data: &T, env: &Env) {
|
|
self.label.update(ctx, &self.text, env);
|
|
}
|
|
|
|
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, _data: &T, env: &Env) -> Size {
|
|
self.label.layout(ctx, bc, &self.text, env)
|
|
}
|
|
|
|
fn paint(&mut self, ctx: &mut PaintCtx, _data: &T, env: &Env) {
|
|
self.label.paint(ctx, &self.text, env);
|
|
}
|
|
}
|