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

Implement dynamic scaling of the LineEdit right icon based on control size and scale factor #95817

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nongvantinh
Copy link
Contributor

Close: godotengine/godot-proposals#10493

The implementation allows the LineEdit node to scale the right icon to match the font size first. Then, when the expand_icon option is enabled, the icon will expand to the full height of the node. The scale of the icon can then be controlled using the scale factor.

Screen.Recording.2024-08-20.020245.mp4

@nongvantinh nongvantinh requested review from a team as code owners August 19, 2024 19:26
@KoBeWi KoBeWi added this to the 4.x milestone Aug 19, 2024
@nongvantinh nongvantinh force-pushed the implement-10493 branch 2 times, most recently from 965d970 to 28bbc19 Compare October 14, 2024 14:17
@KoBeWi
Copy link
Member

KoBeWi commented Nov 20, 2024

Right Icon Scale has no effect when Expand Icon is off. It should be disabled in that case.

@KoBeWi
Copy link
Member

KoBeWi commented Nov 20, 2024

This changes the default behavior of the right icon. It will scale to text, while previously it would use original size.
I think the behavior should be preserved. You can change expand_icon to expand_mode enum and allow all 3 behaviors (original size, fit to text and fit to LineEdit).

@nongvantinh nongvantinh force-pushed the implement-10493 branch 3 times, most recently from d067c5d to 533d0b9 Compare November 22, 2024 14:49
@nongvantinh nongvantinh requested a review from KoBeWi December 3, 2024 12:45
@nongvantinh nongvantinh force-pushed the implement-10493 branch 2 times, most recently from b53077d to 6bd198e Compare January 4, 2025 12:03
icon_max_width = MAX(icon_max_width, theme_cache.clear_icon->get_width());
Point2 right_icon_size = _get_right_icon_size(theme_cache.clear_icon);
min_size.height = MAX(min_size.height, right_icon_size.height);
icon_max_width = MAX(icon_max_width, right_icon_size.width);
Copy link
Member

Choose a reason for hiding this comment

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

If right icon is bigger, the minimum size will fit to the right icon, even if it's not used when clear button is enabled.
It's a pre-existing bug, so you don't need to fix it now.

@@ -2523,6 +2566,31 @@ Ref<Texture2D> LineEdit::get_right_icon() {
return right_icon;
}

void LineEdit::set_icon_expand_mode(ExpandMode p_mode) {
if (icon_expand_mode != p_mode) {
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
if (icon_expand_mode != p_mode) {
if (icon_expand_mode = p_mode) {
return;
}

Early returns are preferred. Same below.

Copy link
Member

@KoBeWi KoBeWi left a comment

Choose a reason for hiding this comment

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

Overall looks good now.

@KoBeWi KoBeWi modified the milestones: 4.x, 4.5 Jan 17, 2025
enum ExpandMode {
EXPAND_MODE_ORIGINAL_SIZE,
EXPAND_MODE_FIT_TO_TEXT,
EXPAND_MODE_FIT_TO_LINE_EDIT
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
EXPAND_MODE_FIT_TO_LINE_EDIT
EXPAND_MODE_FIT_TO_LINE_EDIT,

@@ -2962,6 +3040,11 @@ void LineEdit::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::INT, "structured_text_bidi_override", PROPERTY_HINT_ENUM, "Default,URI,File,Email,List,None,Custom"), "set_structured_text_bidi_override", "get_structured_text_bidi_override");
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");

ADD_GROUP("Icon", "");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_right_icon", "get_right_icon");
ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_expand_mode", PROPERTY_HINT_ENUM, "Original, Fit to Text, Fit to LineEdit"), "set_icon_expand_mode", "get_icon_expand_mode");
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
ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_expand_mode", PROPERTY_HINT_ENUM, "Original, Fit to Text, Fit to LineEdit"), "set_icon_expand_mode", "get_icon_expand_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_expand_mode", PROPERTY_HINT_ENUM, "Original,Fit to Text,Fit to LineEdit"), "set_icon_expand_mode", "get_icon_expand_mode");

@@ -2873,6 +2944,10 @@ void LineEdit::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_drag_and_drop_selection_enabled"), &LineEdit::is_drag_and_drop_selection_enabled);
ClassDB::bind_method(D_METHOD("set_right_icon", "icon"), &LineEdit::set_right_icon);
ClassDB::bind_method(D_METHOD("get_right_icon"), &LineEdit::get_right_icon);
ClassDB::bind_method(D_METHOD("set_icon_expand_mode", "mode"), &LineEdit::set_icon_expand_mode);
ClassDB::bind_method(D_METHOD("get_icon_expand_mode"), &LineEdit::get_icon_expand_mode);
ClassDB::bind_method(D_METHOD("set_right_icon_scale", "p_ratio"), &LineEdit::set_right_icon_scale);
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
ClassDB::bind_method(D_METHOD("set_right_icon_scale", "p_ratio"), &LineEdit::set_right_icon_scale);
ClassDB::bind_method(D_METHOD("set_right_icon_scale", "scale"), &LineEdit::set_right_icon_scale);

void set_icon_expand_mode(ExpandMode p_mode);
ExpandMode get_icon_expand_mode() const;

void set_right_icon_scale(float p_ratio);
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
void set_right_icon_scale(float p_ratio);
void set_right_icon_scale(float p_scale);

return icon_expand_mode;
}

void LineEdit::set_right_icon_scale(float p_ratio) {
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
void LineEdit::set_right_icon_scale(float p_ratio) {
void LineEdit::set_right_icon_scale(float p_scale) {

… size and scale factor

    The implementation allows the LineEdit node to scale the right icon to match the font size first. Then, when the `expand_icon` option is enabled, the icon will expand to the full height of the node. The scale of the icon can then be controlled using the scale factor.

Co-authored-by: Tomasz Chabora <[email protected]>
Co-authored-by: A Thousand Ships <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dynamic Scaling of LineEdit Icons Based on Control Size
3 participants