Why doesn't my NinePatchRect update its size correctly?

Godot Version

v4.6.1.stable.official [14d19694e]

Question

I’m making a key node for my game’s controls menu. It’s simple enough: a label with the key’s name and a 9-patch background, yet something goes wrong.

If I input a bigger key name first and then change it to something smaller, the background doesn’t update correctly (when it’s a single letter, it should be square):

If I delete the key name string entirely and write it anew, it displays as intended:

Relevant code:

@export var key_name: String:
	set(value):
		key_name = value
		print_key_name(value)

func print_key_name(key: String) -> void:
	match key.to_lower():
		"up":
			$KeyLabel.text = "↑"
		"down":
			$KeyLabel.text = "↓"
		"left":
			$KeyLabel.text = "←"
		"right":
			$KeyLabel.text = "→"
		_:
			$KeyLabel.text = key
	
	var text_length: int = $KeyLabel.get_text_length_pixels()
	var nearest_multiple: int = 8 * ((text_length / 8) + 1)
	
	# my attempt to make it look pretty since my font isn't monospaced
	if text_length % 8 < 3:
		size.x = nearest_multiple
		$KeyLabel.size.x = nearest_multiple
	else:
		size.x = nearest_multiple + 8
		$KeyLabel.size.x = nearest_multiple + 8
	
	# container-related stuff
	custom_minimum_size.x = 16 if size.x < 16 else nearest_multiple
	$KeyLabel.custom_minimum_size.x = 16 if size.x < 16 else nearest_multiple

Where the hell is something going wrong? I can’t figure it out and it’s driving me crazy.

It’s hard to tell without seeing your properties, but have you set up things like Patch Margins and the Axis Stretch properties?

Yes I did, they seem to work fine

As far as I can tell, it’s something related to my multiplication bit and probably the % symbol because I still don’t quite understand how it works

basically I put the minimum size bit before the prettifying bit and it somehow worked :exploding_head: