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

Add start end end to the TextHorizontalAlignment #4550

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 24 additions & 8 deletions internal/backends/qt/qt_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,13 @@ impl ItemRenderer for QtItemRenderer<'_> {
let mut string: qttypes::QString = text.text().as_str().into();
let font: QFont = get_font(text.font_request(WindowInner::from_pub(self.window)));
let flags = match text.horizontal_alignment() {
TextHorizontalAlignment::Left => key_generated::Qt_AlignmentFlag_AlignLeft,
TextHorizontalAlignment::Start | TextHorizontalAlignment::Left => {
key_generated::Qt_AlignmentFlag_AlignLeft
}
TextHorizontalAlignment::Center => key_generated::Qt_AlignmentFlag_AlignHCenter,
TextHorizontalAlignment::Right => key_generated::Qt_AlignmentFlag_AlignRight,
TextHorizontalAlignment::End | TextHorizontalAlignment::Right => {
key_generated::Qt_AlignmentFlag_AlignRight
}
} | match text.vertical_alignment() {
TextVerticalAlignment::Top => key_generated::Qt_AlignmentFlag_AlignTop,
TextVerticalAlignment::Center => key_generated::Qt_AlignmentFlag_AlignVCenter,
Expand Down Expand Up @@ -708,9 +712,13 @@ impl ItemRenderer for QtItemRenderer<'_> {
let font: QFont =
get_font(text_input.font_request(&WindowInner::from_pub(self.window).window_adapter()));
let flags = match text_input.horizontal_alignment() {
TextHorizontalAlignment::Left => key_generated::Qt_AlignmentFlag_AlignLeft,
TextHorizontalAlignment::Start | TextHorizontalAlignment::Left => {
key_generated::Qt_AlignmentFlag_AlignLeft
}
TextHorizontalAlignment::Center => key_generated::Qt_AlignmentFlag_AlignHCenter,
TextHorizontalAlignment::Right => key_generated::Qt_AlignmentFlag_AlignRight,
TextHorizontalAlignment::End | TextHorizontalAlignment::Right => {
key_generated::Qt_AlignmentFlag_AlignRight
}
} | match text_input.vertical_alignment() {
TextVerticalAlignment::Top => key_generated::Qt_AlignmentFlag_AlignTop,
TextVerticalAlignment::Center => key_generated::Qt_AlignmentFlag_AlignVCenter,
Expand Down Expand Up @@ -1931,9 +1939,13 @@ impl i_slint_core::renderer::RendererSealed for QtWindow {
let string = qttypes::QString::from(visual_representation.text.as_str());

let flags = match text_input.horizontal_alignment() {
TextHorizontalAlignment::Left => key_generated::Qt_AlignmentFlag_AlignLeft,
TextHorizontalAlignment::Start | TextHorizontalAlignment::Left => {
key_generated::Qt_AlignmentFlag_AlignLeft
}
TextHorizontalAlignment::Center => key_generated::Qt_AlignmentFlag_AlignHCenter,
TextHorizontalAlignment::Right => key_generated::Qt_AlignmentFlag_AlignRight,
TextHorizontalAlignment::End | TextHorizontalAlignment::Right => {
key_generated::Qt_AlignmentFlag_AlignRight
}
} | match text_input.vertical_alignment() {
TextVerticalAlignment::Top => key_generated::Qt_AlignmentFlag_AlignTop,
TextVerticalAlignment::Center => key_generated::Qt_AlignmentFlag_AlignVCenter,
Expand Down Expand Up @@ -1988,9 +2000,13 @@ impl i_slint_core::renderer::RendererSealed for QtWindow {
let mut string = qttypes::QString::from(text.as_str());
let offset: u32 = utf8_byte_offset_to_utf16_units(text.as_str(), byte_offset) as _;
let flags = match text_input.horizontal_alignment() {
TextHorizontalAlignment::Left => key_generated::Qt_AlignmentFlag_AlignLeft,
TextHorizontalAlignment::Start | TextHorizontalAlignment::Left => {
key_generated::Qt_AlignmentFlag_AlignLeft
}
TextHorizontalAlignment::Center => key_generated::Qt_AlignmentFlag_AlignHCenter,
TextHorizontalAlignment::Right => key_generated::Qt_AlignmentFlag_AlignRight,
TextHorizontalAlignment::End | TextHorizontalAlignment::Right => {
key_generated::Qt_AlignmentFlag_AlignRight
}
} | match text_input.vertical_alignment() {
TextVerticalAlignment::Top => key_generated::Qt_AlignmentFlag_AlignTop,
TextVerticalAlignment::Center => key_generated::Qt_AlignmentFlag_AlignVCenter,
Expand Down
6 changes: 6 additions & 0 deletions internal/common/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ macro_rules! for_each_enums {
$macro![
/// This enum describes the different types of alignment of text along the horizontal axis of a [`Text`](elements.md#text) element.
enum TextHorizontalAlignment {
/// The text will be aligned with the start edge of the containing box.
/// This could be left or right depending on the direction of the text in the language.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// This could be left or right depending on the direction of the text in the language.
/// This could be left or right depending on the direction of the text.

Start,
/// The text will be aligned with the end edge of the containing box.
/// This could be left or right depending on the direction of the text in the language.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// This could be left or right depending on the direction of the text in the language.
/// This could be left or right depending on the direction of the text.

End,
/// The text will be aligned with the left edge of the containing box.
Left,
/// The text will be horizontally centered within the containing box.
Expand Down
8 changes: 6 additions & 2 deletions internal/core/textlayout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,13 @@ impl<'a, Font: AbstractFont> TextParagraphLayout<'a, Font> {
};

let x = match self.horizontal_alignment {
TextHorizontalAlignment::Left => Font::Length::zero(),
TextHorizontalAlignment::Start | TextHorizontalAlignment::Left => {
Font::Length::zero()
}
TextHorizontalAlignment::Center => self.max_width / two - text_width() / two,
TextHorizontalAlignment::Right => self.max_width - text_width(),
TextHorizontalAlignment::End | TextHorizontalAlignment::Right => {
self.max_width - text_width()
}
};

let mut elide_glyph = elide_glyph.as_ref();
Expand Down
6 changes: 4 additions & 2 deletions internal/renderers/femtovg/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,11 +596,13 @@ pub(crate) fn layout_text_lines(
let mut process_line =
|text: &str, y: PhysicalLength, start: usize, line_metrics: &femtovg::TextMetrics| {
let x = match horizontal_alignment {
TextHorizontalAlignment::Left => PhysicalLength::default(),
TextHorizontalAlignment::Start | TextHorizontalAlignment::Left => {
PhysicalLength::default()
}
TextHorizontalAlignment::Center => {
max_width / 2. - max_width.min(PhysicalLength::new(line_metrics.width())) / 2.
}
TextHorizontalAlignment::Right => {
TextHorizontalAlignment::End | TextHorizontalAlignment::Right => {
max_width - max_width.min(PhysicalLength::new(line_metrics.width()))
}
};
Expand Down
6 changes: 4 additions & 2 deletions internal/renderers/femtovg/itemrenderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,11 @@ impl<'a> ItemRenderer for GLItemRenderer<'a> {
if let Some(cursor_point) = cursor_point.or_else(|| {
cursor_visible.then(|| {
let x = match text_input.horizontal_alignment() {
TextHorizontalAlignment::Left => PhysicalLength::default(),
TextHorizontalAlignment::Start | TextHorizontalAlignment::Left => {
PhysicalLength::default()
}
TextHorizontalAlignment::Center => width / 2.,
TextHorizontalAlignment::Right => width,
TextHorizontalAlignment::End | TextHorizontalAlignment::Right => width,
};
PhysicalPoint::from_lengths(x, next_y)
})
Expand Down
16 changes: 12 additions & 4 deletions internal/renderers/skia/textlayout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ pub fn create_layout(
}

style.set_text_align(match h_align {
items::TextHorizontalAlignment::Left => skia_safe::textlayout::TextAlign::Left,
TextHorizontalAlignment::Start | items::TextHorizontalAlignment::Left => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think start should be mapped to skia_safe::textlayout::TextAlign::Start; same for end.

skia_safe::textlayout::TextAlign::Left
}
items::TextHorizontalAlignment::Center => skia_safe::textlayout::TextAlign::Center,
items::TextHorizontalAlignment::Right => skia_safe::textlayout::TextAlign::Right,
TextHorizontalAlignment::End | items::TextHorizontalAlignment::Right => {
skia_safe::textlayout::TextAlign::Right
}
});

style.set_text_style(&text_style);
Expand Down Expand Up @@ -210,9 +214,13 @@ pub fn cursor_rect(
) -> PhysicalRect {
if string.is_empty() {
let x = match h_align {
TextHorizontalAlignment::Left => PhysicalLength::default(),
TextHorizontalAlignment::Start | TextHorizontalAlignment::Left => {
PhysicalLength::default()
}
TextHorizontalAlignment::Center => PhysicalLength::new(layout.max_width() / 2.),
TextHorizontalAlignment::Right => PhysicalLength::new(layout.max_width()),
TextHorizontalAlignment::End | TextHorizontalAlignment::Right => {
PhysicalLength::new(layout.max_width())
}
};
return PhysicalRect::new(
PhysicalPoint::from_lengths(x, PhysicalLength::default()),
Expand Down
Loading