How to prevent FileDialog confirm under certain conditions

Godot Version

4.2.1

Question

Here is a simple example of my code, which opens up a load file dialog in Godot editor plugin.

func _enter_tree():
        //bla bla bla other code setting up UI for Editor Plugin

        //showing the dialog:
        var fDialog = FileDialog.new()
	fDialog.file_mode = FileDialog.FILE_MODE_OPEN_FILE
	fDialog.set_filters(["*png, *jpg"])
	fDialog.file_selected.connect( load_file ) 
        fDialog.show()

func load_file (path):
        var loadedTexture = load( path )
        if loadedTexture.get_height() % 96 == 0 and loadedTexture.get_width() % 64 == 0:
              print( "file size is incorrect" );

my issue is that when the user pushes “open” button, the fileDialog closes. if the size of the file being opened is not what I want, I print a debug message… but I want to PREVENT the dialog from closing. I am ok with printing the error message, but want to only close the dialog if the conditions are met. is this possible?
fileDialog inherits from AcceptDialog… so I imagine I can disable the OK button with a flag… but still get if it is clicked?? will I still load the file if conditions are met? anyways not sure how to do this or if possible…

There may be another way to do this. It’s kinda possible in a hacky way.

extends Node


@onready var file_dialog = $FileDialog


func _ready():
	# Get the already connected confirmed signal Callable (the internal one)
	var original_callable = file_dialog.confirmed.get_connections()[0].get('callable')
	# Disconnect it
	file_dialog.confirmed.disconnect(original_callable)
	# Connect our confirmed signal one
	file_dialog.confirmed.connect(func():
		var filepath = file_dialog.current_path
		var file = file_dialog.current_file
		print("Button pressed with %s" % filepath)
		if not file.begins_with('clipped_'):
			# If the file does not begins with 'clipped_' do nothing
			print('Not a clipped one')
		else:
			# Else, call the original Callable so it can do the FileDialog validations
			original_callable.call()
	)
	file_dialog.file_selected.connect(func(file:String):
		print('Selected %s' % file)
	)
	file_dialog.popup_centered_ratio()

Basically, get the internal confirmed signal connection Callable that the engine has, disconnect it, and connect our own which will do some validation before calling that Callable if the validation passes.

2 Likes

thanks I will try this. its seems to be a clever fix. its not a huge issue for me because I am ok with the dialog closing and print a message.

something that is more troublesome for me is the inability to get the where in the editor I clicked.
i made a post about it here if you could help me:

basically when I click anywhere in the editor it registers the click but i only want it to do so in the 3D scene view.