use std::any::TypeId;
use accesskit::{Node, Role};
use include_doc_path::include_doc_path;
use masonry_core::core::UsesProperty;
use tracing::{Span, trace_span};
use crate::core::{
AccessCtx, AccessEvent, ChildrenIds, EventCtx, LayoutCtx, MeasureCtx, NewWidget, NoAction,
PaintCtx, PointerEvent, PropertiesMut, PropertiesRef, RegisterCtx, TextEvent, Update,
UpdateCtx, Widget, WidgetId, WidgetMut, WidgetPod,
};
use crate::imaging::Painter;
use crate::kurbo::{Axis, Point, Size};
use crate::layout::{LenReq, Length};
use crate::properties::{CaretColor, ContentColor, SelectionColor};
use crate::widgets::TextArea;
#[doc = concat!(
",
")",
)]
pub struct Prose {
text: WidgetPod<TextArea<false>>,
clip: bool,
}
impl Prose {
pub fn new(text: &str) -> Self {
Self::from_text_area(TextArea::new_immutable(text).prepare())
}
pub fn from_text_area(text: NewWidget<TextArea<false>>) -> Self {
Self {
text: text.to_pod(),
clip: false,
}
}
pub fn with_clip(mut self, clip: bool) -> Self {
self.clip = clip;
self
}
}
impl Prose {
pub fn text_area_pod(&self) -> &WidgetPod<TextArea<false>> {
&self.text
}
}
impl Prose {
pub fn text_mut<'t>(this: &'t mut WidgetMut<'_, Self>) -> WidgetMut<'t, TextArea<false>> {
this.ctx.get_mut(&mut this.widget.text)
}
pub fn set_clip(this: &mut WidgetMut<'_, Self>, clip: bool) {
this.widget.clip = clip;
this.ctx.request_layout();
}
}
impl UsesProperty<CaretColor> for Prose {}
impl UsesProperty<ContentColor> for Prose {}
impl UsesProperty<SelectionColor> for Prose {}
impl Widget for Prose {
type Action = NoAction;
fn on_pointer_event(
&mut self,
_: &mut EventCtx<'_>,
_props: &mut PropertiesMut<'_>,
_: &PointerEvent,
) {
}
fn on_text_event(
&mut self,
ctx: &mut EventCtx<'_>,
_props: &mut PropertiesMut<'_>,
event: &TextEvent,
) {
match event {
TextEvent::WindowFocusChange(focused) => {
if *focused {
ctx.remove_class("#unfocused");
} else {
ctx.add_class("#unfocused");
}
}
_ => {}
}
}
fn on_access_event(
&mut self,
_ctx: &mut EventCtx<'_>,
_props: &mut PropertiesMut<'_>,
_event: &AccessEvent,
) {
}
fn register_children(&mut self, ctx: &mut RegisterCtx<'_>) {
ctx.register_child(&mut self.text);
}
fn property_changed(&mut self, ctx: &mut UpdateCtx<'_>, property_type: TypeId) {
if property_type == TypeId::of::<CaretColor>() {
ctx.mutate_self_later(|mut input| {
let mut input = input.downcast::<Self>();
let color = *input.get_prop::<CaretColor>();
let mut text_area = Self::text_mut(&mut input);
text_area.insert_prop(color);
});
} else if property_type == TypeId::of::<ContentColor>() {
ctx.mutate_self_later(|mut input| {
let mut input = input.downcast::<Self>();
let color = *input.get_prop::<ContentColor>();
let mut text_area = Self::text_mut(&mut input);
text_area.insert_prop(color);
});
} else if property_type == TypeId::of::<SelectionColor>() {
ctx.mutate_self_later(|mut input| {
let mut input = input.downcast::<Self>();
let color = *input.get_prop::<SelectionColor>();
let mut text_area = Self::text_mut(&mut input);
text_area.insert_prop(color);
});
}
}
fn update(&mut self, ctx: &mut UpdateCtx<'_>, _props: &mut PropertiesMut<'_>, event: &Update) {
match event {
Update::WidgetAdded => {
ctx.mutate_self_later(|mut input| {
let mut input = input.downcast::<Self>();
let color = *input.get_prop::<CaretColor>();
let mut text_area = Self::text_mut(&mut input);
text_area.insert_prop(color);
});
ctx.mutate_self_later(|mut input| {
let mut input = input.downcast::<Self>();
let color = *input.get_prop::<ContentColor>();
let mut text_area = Self::text_mut(&mut input);
text_area.insert_prop(color);
});
ctx.mutate_self_later(|mut input| {
let mut input = input.downcast::<Self>();
let color = *input.get_prop::<SelectionColor>();
let mut text_area = Self::text_mut(&mut input);
text_area.insert_prop(color);
});
}
_ => {}
}
}
fn measure(
&mut self,
ctx: &mut MeasureCtx<'_>,
_props: &PropertiesRef<'_>,
axis: Axis,
_len_req: LenReq,
cross_length: Option<Length>,
) -> Length {
ctx.redirect_measurement(&mut self.text, axis, cross_length)
}
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, _props: &PropertiesRef<'_>, size: Size) {
ctx.run_layout(&mut self.text, size);
ctx.place_child(&mut self.text, Point::ORIGIN);
ctx.derive_baselines(&self.text);
if self.clip {
let border_box = size.to_rect() + ctx.border_box_insets();
ctx.set_clip_path(border_box);
} else {
ctx.clear_clip_path();
}
}
fn paint(
&mut self,
_ctx: &mut PaintCtx<'_>,
_props: &PropertiesRef<'_>,
_painter: &mut Painter<'_>,
) {
}
fn accessibility_role(&self) -> Role {
Role::GenericContainer
}
fn accessibility(
&mut self,
_ctx: &mut AccessCtx<'_>,
_props: &PropertiesRef<'_>,
_node: &mut Node,
) {
}
fn children_ids(&self) -> ChildrenIds {
ChildrenIds::from_slice(&[self.text.id()])
}
fn make_trace_span(&self, id: WidgetId) -> Span {
trace_span!("Prose", id = id.trace())
}
fn get_debug_text(&self) -> Option<String> {
self.clip.then(|| "(clip)".into())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::TextAlign;
use crate::core::PropertySet;
use crate::layout::AsUnit;
use crate::parley::StyleProperty;
use crate::properties::Gap;
use crate::properties::types::CrossAxisAlignment;
use crate::testing::{TestHarness, assert_render_snapshot};
use crate::theme::{ACCENT_COLOR, test_property_set};
use crate::util::Duration;
use crate::widgets::{Flex, SizedBox, TextArea};
#[test]
fn prose_clipping() {
let prose = Prose::from_text_area(
TextArea::new_immutable("Truncated text - you should not see this")
.with_style(StyleProperty::FontSize(14.0))
.with_word_wrap(false)
.prepare(),
)
.with_clip(true)
.prepare();
let root_widget = Flex::row()
.with_fixed(SizedBox::new(prose).width(60.px()).prepare())
.prepare();
let mut harness =
TestHarness::create_with_size(test_property_set(), root_widget, (200, 40));
assert_render_snapshot!(harness, "prose_clipping");
}
#[test]
fn prose_alignment_flex() {
fn base_prose(text_alignment: TextAlign) -> Prose {
Prose::from_text_area(
TextArea::new_immutable("Hello ")
.with_style(StyleProperty::FontSize(14.0))
.with_text_alignment(text_alignment)
.with_word_wrap(true)
.prepare(),
)
}
let prose1 = base_prose(TextAlign::Start);
let prose2 = base_prose(TextAlign::Center);
let prose3 = base_prose(TextAlign::End);
let prose4 = base_prose(TextAlign::Start);
let prose5 = base_prose(TextAlign::Center);
let prose6 = base_prose(TextAlign::End);
let flex = Flex::column()
.with(prose1.prepare(), CrossAxisAlignment::Start)
.with(prose2.prepare(), CrossAxisAlignment::Start)
.with(prose3.prepare(), CrossAxisAlignment::Start)
.with(prose4.prepare(), CrossAxisAlignment::Center)
.with(prose5.prepare(), CrossAxisAlignment::Center)
.with(prose6.prepare(), CrossAxisAlignment::Center);
let flex = NewWidget::new(flex).with_props(PropertySet::one(Gap::ZERO));
let mut harness = TestHarness::create_with_size(test_property_set(), flex, (200, 120));
assert_render_snapshot!(harness, "prose_alignment_flex");
}
#[test]
fn prose_outline() {
let prose = Prose::new("TextInput contents").prepare();
let mut harness = TestHarness::create_with_size(test_property_set(), prose, (150, 40));
assert_render_snapshot!(harness, "prose_outline");
let mut text_area_id = None;
harness.edit_root_widget(|mut prose| {
let mut prose = Prose::text_mut(&mut prose);
text_area_id = Some(prose.ctx.widget_id());
TextArea::select_text(&mut prose, "contents");
});
let text_area_tag = harness.make_dyn_tag_for_widget(text_area_id.unwrap());
harness.focus_on(text_area_tag);
assert_render_snapshot!(harness, "prose_selection");
harness.process_text_event(TextEvent::WindowFocusChange(false));
assert_render_snapshot!(harness, "prose_selection_unfocused");
harness.process_text_event(TextEvent::WindowFocusChange(true));
harness.advance_time(Duration::from_millis(500));
assert_render_snapshot!(harness, "prose_cursor_blink");
}
#[test]
fn edit_text() {
let image_1 = {
let prose = Prose::new("The quick brown fox jumps over the lazy dog")
.prepare()
.with_props(PropertySet::new().with(ContentColor::new(ACCENT_COLOR)));
let mut harness = TestHarness::create_with_size(test_property_set(), prose, (50, 50));
harness.render()
};
let image_2 = {
let prose = Prose::new("Hello world").prepare();
let mut harness = TestHarness::create_with_size(test_property_set(), prose, (50, 50));
harness.edit_root_widget(|mut prose| {
prose.insert_prop(ContentColor::new(ACCENT_COLOR));
TextArea::reset_text(
&mut Prose::text_mut(&mut prose),
"The quick brown fox jumps over the lazy dog",
);
});
harness.render()
};
assert!(image_1 == image_2);
}
}