diff options
author | Yuqian Yang <crupest@outlook.com> | 2018-10-01 17:11:11 +0000 |
---|---|---|
committer | Yuqian Yang <crupest@outlook.com> | 2018-10-01 17:11:11 +0000 |
commit | 30ecda8bb354d5982978af97aa90b5f49d9ea195 (patch) | |
tree | a271bddb244fa2041f14f8d46d249457cee09e5f /src/ui/controls/text_control.cpp | |
parent | 398b8f3ba535bb43c4b8593e3027c14894a7a211 (diff) | |
parent | 040a6c18f18100b825a56443a73aa1de64e4518c (diff) | |
download | cru-30ecda8bb354d5982978af97aa90b5f49d9ea195.tar.gz cru-30ecda8bb354d5982978af97aa90b5f49d9ea195.tar.bz2 cru-30ecda8bb354d5982978af97aa90b5f49d9ea195.zip |
Merge branch '9-border' into 'master'
Resolve "Abstract out border control of button and border."
Closes #9
See merge request crupest/CruUI!11
Diffstat (limited to 'src/ui/controls/text_control.cpp')
-rw-r--r-- | src/ui/controls/text_control.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/ui/controls/text_control.cpp b/src/ui/controls/text_control.cpp index 692c4451..0b730500 100644 --- a/src/ui/controls/text_control.cpp +++ b/src/ui/controls/text_control.cpp @@ -259,6 +259,76 @@ namespace cru::ui::controls return result_size; } + inline Size ThicknessToSize(const Thickness& thickness) + { + return Size(thickness.left + thickness.right, thickness.top + thickness.bottom); + } + + inline float AtLeast0(const float value) + { + return value < 0 ? 0 : value; + } + + inline Size AtLeast0(const Size& size) + { + return Size(AtLeast0(size.width), AtLeast0(size.height)); + } + + Size TextControl::TextMeasureWithPadding(const Size& available_size, const Thickness& padding) + { + const auto layout_params = GetLayoutParams(); + const auto padding_size = ThicknessToSize(padding); + + auto&& get_measure_length = [](const LayoutSideParams& layout_length, const float available_length) -> float + { + switch (layout_length.mode) + { + case MeasureMode::Exactly: + { + return std::min(layout_length.length, available_length); + } + case MeasureMode::Stretch: + case MeasureMode::Content: + { + return available_length; + } + default: + UnreachableCode(); + } + }; + + Size measure_size(get_measure_length(layout_params->width, available_size.width), + get_measure_length(layout_params->height, available_size.height)); + + measure_size = AtLeast0(measure_size - padding_size); + + ThrowIfFailed(text_layout_->SetMaxWidth(measure_size.width)); + ThrowIfFailed(text_layout_->SetMaxHeight(measure_size.height)); + + DWRITE_TEXT_METRICS metrics{}; + + ThrowIfFailed(text_layout_->GetMetrics(&metrics)); + + const Size measure_result(metrics.width, metrics.height); + + auto&& calculate_final_length = [](const LayoutSideParams& layout_length, const float measure_length, const float measure_result_length) -> float + { + if ((layout_length.mode == MeasureMode::Stretch || + layout_length.mode == MeasureMode::Exactly) + && measure_result_length < measure_length) + return measure_length; + else + return measure_result_length; + }; + + const Size result_size( + calculate_final_length(layout_params->width, measure_size.width, measure_result.width), + calculate_final_length(layout_params->height, measure_size.height, measure_result.height) + ); + + return result_size + padding_size; + } + void TextControl::RequestChangeCaretPosition(unsigned position) { |