Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make text wrap configurable #158

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions core/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub struct Text<'a, Font> {

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

/// The [`Wrap`] mode of the [`Text`].
pub wrap: Wrap,
}

/// The shaping strategy of some text.
Expand All @@ -66,6 +69,22 @@ pub enum Shaping {
Advanced,
}

/// The wrap mode of some text.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Wrap {
/// No wraping
None,
/// Wraps at a glyph level
Glyph,
/// Wraps at a word level
Word,
/// Wraps at the word level, or fallback to glyph level if a word can't fit on a line by itself
///
/// This is the default
#[default]
WordOrGlyph,
}

/// The height of a line of text in a paragraph.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LineHeight {
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, Wrap};

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

Expand All @@ -51,6 +52,7 @@ where
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
shaping: Shaping::Advanced,
wrap: Default::default(),
style: Default::default(),
}
}
Expand Down Expand Up @@ -116,6 +118,12 @@ where
self.shaping = shaping;
self
}

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

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

Expand Down Expand Up @@ -246,6 +255,7 @@ pub fn layout<Renderer>(
horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical,
shaping: Shaping,
wrap: Wrap,
) -> layout::Node
where
Renderer: text::Renderer,
Expand All @@ -267,6 +277,7 @@ where
horizontal_alignment,
vertical_alignment,
shaping,
wrap,
});

paragraph.min_bounds()
Expand Down Expand Up @@ -347,6 +358,7 @@ where
font: self.font,
style: self.style.clone(),
shaping: self.shaping,
wrap: self.wrap,
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion graphics/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use paragraph::Paragraph;
pub use cosmic_text;

use crate::core::font::{self, Font};
use crate::core::text::Shaping;
use crate::core::text::{Shaping, Wrap};
use crate::core::{Color, Point, Rectangle, Size};

use once_cell::sync::OnceCell;
Expand Down Expand Up @@ -171,6 +171,16 @@ pub fn to_shaping(shaping: Shaping) -> cosmic_text::Shaping {
}
}

/// Converts some [`Wrap`] mode to a [`cosmic_text::Wrap`] strategy.
pub fn to_wrap(wrap: Wrap) -> cosmic_text::Wrap {
match wrap {
Wrap::None => cosmic_text::Wrap::None,
Wrap::Glyph => cosmic_text::Wrap::Glyph,
Wrap::Word => cosmic_text::Wrap::Word,
Wrap::WordOrGlyph => cosmic_text::Wrap::WordOrGlyph,
}
}

/// Converts some [`Color`] to a [`cosmic_text::Color`].
pub fn to_color(color: Color) -> cosmic_text::Color {
let [r, g, b, a] = color.into_rgba8();
Expand Down
8 changes: 7 additions & 1 deletion graphics/src/text/paragraph.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Draw paragraphs.
use crate::core;
use crate::core::alignment;
use crate::core::text::{Hit, LineHeight, Shaping, Text};
use crate::core::text::{Hit, LineHeight, Shaping, Text, Wrap};
use crate::core::{Font, Pixels, Point, Size};
use crate::text;

Expand All @@ -17,6 +17,7 @@ struct Internal {
content: String, // TODO: Reuse from `buffer` (?)
font: Font,
shaping: Shaping,
wrap: Wrap,
horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical,
bounds: Size,
Expand Down Expand Up @@ -81,6 +82,8 @@ impl core::text::Paragraph for Paragraph {
Some(text.bounds.height),
);

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

buffer.set_text(
font_system.raw(),
text.content,
Expand All @@ -97,6 +100,7 @@ impl core::text::Paragraph for Paragraph {
horizontal_alignment: text.horizontal_alignment,
vertical_alignment: text.vertical_alignment,
shaping: text.shaping,
wrap: text.wrap,
bounds: text.bounds,
min_bounds,
version: font_system.version(),
Expand Down Expand Up @@ -141,6 +145,7 @@ impl core::text::Paragraph for Paragraph {
horizontal_alignment: internal.horizontal_alignment,
vertical_alignment: internal.vertical_alignment,
shaping: internal.shaping,
wrap: internal.wrap,
});
}
}
Expand Down Expand Up @@ -274,6 +279,7 @@ impl Default for Internal {
content: String::new(),
font: Font::default(),
shaping: Shaping::default(),
wrap: Wrap::default(),
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
bounds: Size::ZERO,
Expand Down
14 changes: 14 additions & 0 deletions widget/src/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub struct Checkbox<
text_size: Option<Pixels>,
text_line_height: text::LineHeight,
text_shaping: text::Shaping,
text_wrap: text::Wrap,
font: Option<Renderer::Font>,
icon: Icon<Renderer::Font>,
style: <Theme as StyleSheet>::Style,
Expand Down Expand Up @@ -107,13 +108,15 @@ where
text_size: None,
text_line_height: text::LineHeight::default(),
text_shaping: text::Shaping::Advanced,
text_wrap: text::Wrap::default(),
font: None,
icon: Icon {
font: Renderer::ICON_FONT,
code_point: Renderer::CHECKMARK_ICON,
size: None,
line_height: text::LineHeight::default(),
shaping: text::Shaping::Advanced,
wrap: text::Wrap::default(),
},
style: Default::default(),
}
Expand Down Expand Up @@ -158,6 +161,12 @@ where
self
}

/// Sets the [`text::Wrap`] mode of the [`Checkbox`].
pub fn text_wrap(mut self, wrap: text::Wrap) -> Self {
self.text_wrap = wrap;
self
}

/// Sets the [`Renderer::Font`] of the text of the [`Checkbox`].
///
/// [`Renderer::Font`]: crate::core::text::Renderer
Expand Down Expand Up @@ -258,6 +267,7 @@ where
alignment::Horizontal::Left,
alignment::Vertical::Top,
self.text_shaping,
self.text_wrap,
)
},
)
Expand Down Expand Up @@ -345,6 +355,7 @@ where
size,
line_height,
shaping,
wrap,
} = &self.icon;
let size = size.unwrap_or(Pixels(bounds.height * 0.7));

Expand All @@ -359,6 +370,7 @@ where
horizontal_alignment: alignment::Horizontal::Center,
vertical_alignment: alignment::Vertical::Center,
shaping: *shaping,
wrap: *wrap,
},
bounds.center(),
custom_style.icon_color,
Expand Down Expand Up @@ -497,4 +509,6 @@ pub struct Icon<Font> {
pub line_height: text::LineHeight,
/// The shaping strategy of the icon.
pub shaping: text::Shaping,
/// The wrap mode of the icon.
pub wrap: text::Wrap,
}
12 changes: 12 additions & 0 deletions widget/src/overlay/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct Menu<
text_size: Option<Pixels>,
text_line_height: text::LineHeight,
text_shaping: text::Shaping,
text_wrap: text::Wrap,
font: Option<Renderer::Font>,
style: Theme::Style,
}
Expand Down Expand Up @@ -70,6 +71,7 @@ where
text_size: None,
text_line_height: text::LineHeight::default(),
text_shaping: text::Shaping::Advanced,
text_wrap: text::Wrap::default(),
font: None,
style: Default::default(),
}
Expand Down Expand Up @@ -108,6 +110,12 @@ where
self
}

/// Sets the [`text::Wrap`] mode of the [`Menu`].
pub fn text_wrap(mut self, wrap: text::Wrap) -> Self {
self.text_wrap = wrap;
self
}

/// Sets the font of the [`Menu`].
pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
self.font = Some(font.into());
Expand Down Expand Up @@ -199,6 +207,7 @@ where
text_size,
text_line_height,
text_shaping,
text_wrap,
style,
} = menu;

Expand All @@ -211,6 +220,7 @@ where
text_size,
text_line_height,
text_shaping,
text_wrap,
padding,
style: style.clone(),
}));
Expand Down Expand Up @@ -333,6 +343,7 @@ where
text_size: Option<Pixels>,
text_line_height: text::LineHeight,
text_shaping: text::Shaping,
text_wrap: text::Wrap,
font: Option<Renderer::Font>,
style: Theme::Style,
}
Expand Down Expand Up @@ -535,6 +546,7 @@ where
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Center,
shaping: self.text_shaping,
wrap: self.text_wrap,
},
Point::new(bounds.x + self.padding.left, bounds.center_y()),
if is_selected {
Expand Down
Loading
Loading