Skip to content

Text : Added EllipsizeMode for tail and clip alignment #14660

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "updated ellipsis to respect tail and clip behaviour , for head , middle follow defaulty tail behaviour",
"packageName": "react-native-windows",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ void ParagraphComponentView::updateProps(
updateTextAlignment(newViewProps.textAttributes.alignment);
}


// Reset m_textLayout when ellipsizeMode changes
if (oldViewProps.paragraphAttributes.ellipsizeMode != newViewProps.paragraphAttributes.ellipsizeMode) {
m_textLayout = nullptr;
}
if (oldViewProps.paragraphAttributes.adjustsFontSizeToFit != newViewProps.paragraphAttributes.adjustsFontSizeToFit) {
m_requireRedraw = true;
}
Expand Down Expand Up @@ -140,16 +145,16 @@ void ParagraphComponentView::updateTextAlignment(
case facebook::react::TextAlignment::Right:
alignment = DWRITE_TEXT_ALIGNMENT_TRAILING;
break;
// TODO use LTR values
case facebook::react::TextAlignment::Natural:
alignment = DWRITE_TEXT_ALIGNMENT_LEADING;
break;
default:
assert(false);
}
}
// TODO
// m_textFormat->SetTextAlignment(alignment);

// Apply the alignment to the text layout
winrt::check_hresult(m_textLayout->SetTextAlignment(alignment));
}

void ParagraphComponentView::OnRenderingDeviceLost() noexcept {
Expand Down Expand Up @@ -276,6 +281,48 @@ void ParagraphComponentView::DrawText() noexcept {

const auto &props = paragraphProps();

if (m_textLayout) {
DWRITE_TEXT_METRICS metrics;
winrt::check_hresult(m_textLayout->GetMetrics(&metrics));

float maxWidth = m_layoutMetrics.frame.size.width - m_layoutMetrics.contentInsets.left -
m_layoutMetrics.contentInsets.right;

if (metrics.width > maxWidth) {
m_textLayout->SetMaxWidth(maxWidth);
}

// Ensure alignment is applied correctly
updateTextAlignment(props.textAttributes.alignment);

// Apply DWRITE_TRIMMING for ellipsizeMode
DWRITE_TRIMMING trimming = {};
winrt::com_ptr<IDWriteInlineObject> ellipsisSign;

switch (props.paragraphAttributes.ellipsizeMode) {
case facebook::react::EllipsizeMode::Tail:
trimming.granularity = DWRITE_TRIMMING_GRANULARITY_CHARACTER;
break;
case facebook::react::EllipsizeMode::Clip:
trimming.granularity = DWRITE_TRIMMING_GRANULARITY_NONE;
break;
default:
trimming.granularity = DWRITE_TRIMMING_GRANULARITY_CHARACTER; // Default to tail behavior
break;
}

// Use IDWriteFactory to create the ellipsis trimming sign
winrt::com_ptr<IDWriteFactory> dwriteFactory;
HRESULT hr = DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown **>(dwriteFactory.put()));
if (SUCCEEDED(hr)) {
hr = dwriteFactory->CreateEllipsisTrimmingSign(m_textLayout.get(), ellipsisSign.put());
if (SUCCEEDED(hr)) {
m_textLayout->SetTrimming(&trimming, ellipsisSign.get());
}
}
}

RenderText(
*d2dDeviceContext,
*m_textLayout,
Expand Down
Loading