Skip to content

When the cell content exceeds the column width, the text will be automatically truncated and "..." will be displayed. Ellipsis #6

Description

@Caesar454905970

✅ text will not exceed the column width to be displayed

✅ overly long content will be truncated and an ellipsis will be shown

✅ the tooltip when the mouse hovers over will still display the full content (original function)

✅ when editing cells, you can see and modify the full content (original function).

I have made two key modifications: 1. Disable automatic column width adjustment (lines 251-270) Comment out the code for automatically expanding the column width in set data(). Now the column width you manually set will be fully retained. If automatic adjustment is needed in the future, you can uncomment 2. Add the text ellipsis function (lines 664-700). Automatically truncate when the text exceeds the column width. Add "..." at the end. When the ellipsis mouse hovers over the tooltip, the full content is still displayed

	# 自动调整列宽已禁用,以保留手动设置的列宽
	# 如果需要自动调整,请取消下面代码的注释
	# for r in range(_total_rows):
	# 	for col in range (_total_columns):
	# 		var header_size = font.get_string_size(str(_get_header_text(col)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size)
	# 		var data_s = Vector2.ZERO
	# 		
	# 		if _is_progress_column(col):
	# 			data_s = Vector2(default_minimum_column_width + 20, font_size)
	# 		elif _is_checkbox_column(col):
	# 			data_s = Vector2(default_minimum_column_width - 50, font_size)
	# 		elif _is_image_column(col):
	# 			data_s = Vector2(row_height, row_height)
	# 		else:
	# 			if r < _data.size() and col < _data[r].size():
	# 				data_s = font.get_string_size(str(_data[r][col]), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size)
	# 		
	# 		if (_column_widths[col] < max(header_size.x, data_s.x)):
	# 			_column_widths[col] = max(header_size.x, data_s.x) + font_size * 4
	# 			_min_column_widths[col] = _column_widths[col]
func _draw_cell_text(cell_x: float, row_y: float, col: int, r_idx: int): # `row` rinominato a `r_idx`
	var cell_val = "" # Rinominato `cell_value`
	if r_idx >=0 and r_idx < _data.size() and col >=0 and col < _data[r_idx].size(): # Aggiunto check limiti
		cell_val = str(_data[r_idx][col])
	
	var align_info = _align_text_in_cell(col)
	var h_align_val = align_info[1]
	var x_margin_val = align_info[2]
	
	# 计算可用宽度
	var available_width = _column_widths[col] - abs(x_margin_val) * 2
	
	# 获取文本实际宽度
	var text_full_size = font.get_string_size(cell_val, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size)
	
	# 如果文本超出可用宽度,添加省略号
	var display_text = cell_val
	if text_full_size.x > available_width:
		var ellipsis = "..."
		var ellipsis_width = font.get_string_size(ellipsis, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x
		var max_text_width = available_width - ellipsis_width
		
		# 逐字符截断直到文本适合宽度
		if max_text_width > 0:
			var truncated_text = ""
			for i in range(cell_val.length()):
				var test_text = cell_val.substr(0, i + 1)
				var test_width = font.get_string_size(test_text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x
				if test_width > max_text_width:
					break
				truncated_text = test_text
			display_text = truncated_text + ellipsis
		else:
			display_text = ellipsis
	
	var text_y_pos = row_y + row_height/2.0 + font.get_height(font_size)/2.0 - (font_size/2.0 - 2.0)
	draw_string(font, Vector2(cell_x + x_margin_val, text_y_pos), display_text, h_align_val, available_width, font_size, default_font_color)
		

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions