Skip to content

Commit

Permalink
Add wrapping strategy to text widget
Browse files Browse the repository at this point in the history
  • Loading branch information
nrjais committed Feb 21, 2024
1 parent 56ac21c commit a037706
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 1 deletion.
17 changes: 17 additions & 0 deletions core/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ pub struct Text<'a, Font> {

/// The [`Shaping`] strategy of the [`Text`].
pub shaping: Shaping,

/// The [`Wrapping`] strategy of the [`Text`].
pub wrapping: Wrapping,
}

/// The wrapping strategy of some text.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Wrapping {
/// No wrapping.
///
/// This is the default.
None,
/// Word wrapping.
#[default]
Word,
/// Glyph wrapping.
Glyph,
}

/// The shaping strategy of some text.
Expand Down
14 changes: 13 additions & 1 deletion core/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{

use std::borrow::Cow;

pub use text::{LineHeight, Shaping};
pub use text::{LineHeight, Shaping, Wrapping};

/// A paragraph of text.
#[allow(missing_debug_implementations)]
Expand All @@ -30,6 +30,7 @@ where
font: Option<Renderer::Font>,
shaping: Shaping,
style: Theme::Style,
wrapping: Wrapping,
}

impl<'a, Theme, Renderer> Text<'a, Theme, Renderer>
Expand All @@ -50,6 +51,7 @@ where
vertical_alignment: alignment::Vertical::Top,
shaping: Shaping::Basic,
style: Default::default(),
wrapping: Wrapping::Word,
}
}

Expand Down Expand Up @@ -114,6 +116,12 @@ where
self.shaping = shaping;
self
}

/// Sets the [`Wrapping`] strategy of the [`Text`].
pub fn wrapping(mut self, wrapping: Wrapping) -> Self {
self.wrapping = wrapping;
self
}
}

/// The internal state of a [`Text`] widget.
Expand Down Expand Up @@ -160,6 +168,7 @@ where
self.horizontal_alignment,
self.vertical_alignment,
self.shaping,
self.wrapping,
)
}

Expand Down Expand Up @@ -200,6 +209,7 @@ pub fn layout<Renderer>(
horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical,
shaping: Shaping,
wrapping: Wrapping,
) -> layout::Node
where
Renderer: text::Renderer,
Expand All @@ -221,6 +231,7 @@ where
horizontal_alignment,
vertical_alignment,
shaping,
wrapping,
});

paragraph.min_bounds()
Expand Down Expand Up @@ -300,6 +311,7 @@ where
font: self.font,
style: self.style.clone(),
shaping: self.shaping,
wrapping: self.wrapping,
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions graphics/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::core::font::{self, Font};
use crate::core::text::Shaping;
use crate::core::{Color, Point, Rectangle, Size};

use iced_core::text::Wrapping;
use once_cell::sync::OnceCell;
use std::borrow::Cow;
use std::sync::{Arc, RwLock, Weak};
Expand Down Expand Up @@ -123,6 +124,14 @@ pub fn to_attributes(font: Font) -> cosmic_text::Attrs<'static> {
.style(to_style(font.style))
}

fn to_wrap(wrapping: Wrapping) -> cosmic_text::Wrap {
match wrapping {
Wrapping::None => cosmic_text::Wrap::None,
Wrapping::Word => cosmic_text::Wrap::Word,
Wrapping::Glyph => cosmic_text::Wrap::Glyph,
}
}

fn to_family(family: font::Family) -> cosmic_text::Family<'static> {
match family {
font::Family::Name(name) => cosmic_text::Family::Name(name),
Expand Down
10 changes: 10 additions & 0 deletions graphics/src/text/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::core::text::{Hit, LineHeight, Shaping, Text};
use crate::core::{Font, Pixels, Point, Size};
use crate::text;

use iced_core::text::Wrapping;
use std::fmt;
use std::sync::{self, Arc};

Expand All @@ -22,6 +23,7 @@ struct Internal {
bounds: Size,
min_bounds: Size,
version: text::Version,
wrapping: Wrapping,
}

impl Paragraph {
Expand Down Expand Up @@ -88,6 +90,8 @@ impl core::text::Paragraph for Paragraph {
text::to_shaping(text.shaping),
);

buffer.set_wrap(font_system.raw(), text::to_wrap(text.wrapping));

let min_bounds = text::measure(&buffer);

Self(Some(Arc::new(Internal {
Expand All @@ -100,6 +104,7 @@ impl core::text::Paragraph for Paragraph {
bounds: text.bounds,
min_bounds,
version: font_system.version(),
wrapping: text.wrapping,
})))
}

Expand Down Expand Up @@ -141,6 +146,7 @@ impl core::text::Paragraph for Paragraph {
horizontal_alignment: internal.horizontal_alignment,
vertical_alignment: internal.vertical_alignment,
shaping: internal.shaping,
wrapping: internal.wrapping,
});
}
}
Expand All @@ -159,6 +165,7 @@ impl core::text::Paragraph for Paragraph {
|| paragraph.shaping != text.shaping
|| paragraph.horizontal_alignment != text.horizontal_alignment
|| paragraph.vertical_alignment != text.vertical_alignment
|| paragraph.wrapping != text.wrapping
{
core::text::Difference::Shape
} else if paragraph.bounds != text.bounds {
Expand Down Expand Up @@ -247,6 +254,7 @@ impl fmt::Debug for Paragraph {
.field("vertical_alignment", &paragraph.vertical_alignment)
.field("bounds", &paragraph.bounds)
.field("min_bounds", &paragraph.min_bounds)
.field("wrapping", &paragraph.wrapping)
.finish()
}
}
Expand All @@ -261,6 +269,7 @@ impl PartialEq for Internal {
&& self.bounds == other.bounds
&& self.min_bounds == other.min_bounds
&& self.buffer.metrics() == other.buffer.metrics()
&& self.wrapping == other.wrapping
}
}

Expand All @@ -279,6 +288,7 @@ impl Default for Internal {
bounds: Size::ZERO,
min_bounds: Size::ZERO,
version: text::Version::default(),
wrapping: Wrapping::default(),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions widget/src/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::core::layout;
use crate::core::mouse;
use crate::core::renderer;
use crate::core::text;
use crate::core::text::Wrapping;
use crate::core::touch;
use crate::core::widget;
use crate::core::widget::tree::{self, Tree};
Expand Down Expand Up @@ -232,6 +233,7 @@ where
alignment::Horizontal::Left,
alignment::Vertical::Top,
self.text_shaping,
Wrapping::default(),
)
},
)
Expand Down Expand Up @@ -337,6 +339,7 @@ where
horizontal_alignment: alignment::Horizontal::Center,
vertical_alignment: alignment::Vertical::Center,
shaping: *shaping,
wrapping: Wrapping::default(),
},
bounds.center(),
custom_style.icon_color,
Expand Down
1 change: 1 addition & 0 deletions widget/src/overlay/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ where
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Center,
shaping: self.text_shaping,
wrapping: text::Wrapping::default(),
},
Point::new(bounds.x + self.padding.left, bounds.center_y()),
if is_selected {
Expand Down
3 changes: 3 additions & 0 deletions widget/src/pick_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ where
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Center,
shaping: text_shaping,
wrapping: text::Wrapping::default(),
};

for (option, paragraph) in options.iter().zip(state.options.iter_mut()) {
Expand Down Expand Up @@ -759,6 +760,7 @@ pub fn draw<'a, T, Theme, Renderer>(
horizontal_alignment: alignment::Horizontal::Right,
vertical_alignment: alignment::Vertical::Center,
shaping,
wrapping: text::Wrapping::default(),
},
Point::new(
bounds.x + bounds.width - padding.horizontal(),
Expand Down Expand Up @@ -787,6 +789,7 @@ pub fn draw<'a, T, Theme, Renderer>(
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Center,
shaping: text_shaping,
wrapping: text::Wrapping::default(),
},
Point::new(bounds.x + padding.left, bounds.center_y()),
if is_selected {
Expand Down
1 change: 1 addition & 0 deletions widget/src/radio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ where
alignment::Horizontal::Left,
alignment::Vertical::Top,
self.text_shaping,
text::Wrapping::default(),
)
},
)
Expand Down
3 changes: 3 additions & 0 deletions widget/src/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ where
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Center,
shaping: text::Shaping::Advanced,
wrapping: text::Wrapping::default(),
};

state.placeholder.update(placeholder_text);
Expand All @@ -545,6 +546,7 @@ where
horizontal_alignment: alignment::Horizontal::Center,
vertical_alignment: alignment::Vertical::Center,
shaping: text::Shaping::Advanced,
wrapping: text::Wrapping::default(),
};

state.icon.update(icon_text);
Expand Down Expand Up @@ -1464,6 +1466,7 @@ fn replace_paragraph<Renderer>(
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
shaping: text::Shaping::Advanced,
wrapping: text::Wrapping::default(),
});
}

Expand Down
1 change: 1 addition & 0 deletions widget/src/toggler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ where
self.text_alignment,
alignment::Vertical::Top,
self.text_shaping,
text::Wrapping::default(),
)
} else {
layout::Node::new(Size::ZERO)
Expand Down

0 comments on commit a037706

Please sign in to comment.