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

FileDialog FILE_MODE_OPEN_ANY mode do not select right dir path. #102058

Closed
shutaozhenzhen opened this issue Jan 26, 2025 · 0 comments · Fixed by #102080
Closed

FileDialog FILE_MODE_OPEN_ANY mode do not select right dir path. #102058

shutaozhenzhen opened this issue Jan 26, 2025 · 0 comments · Fixed by #102080
Assignees
Milestone

Comments

@shutaozhenzhen
Copy link

Tested versions

v4.3.stable.official [77dcf97]

System information

Godot v4.3.stable - Windows 10.0.19045 - GLES3 (Compatibility) - NVIDIA GeForce MX250 (NVIDIA; 31.0.15.3209) - Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz (8 Threads)

Issue description

using FileDialog with FILE_MODE_OPEN_ANY mode, print selected files. If I select a file first, then select a dir in the same dir, then click Open, this editor return the path of the file, not the dir.

After checking the code, I think this is because when I select a file, FileDialog::_tree_selected set select file name into file

void FileDialog::_tree_selected() {
	TreeItem *ti = tree->get_selected();
	if (!ti) {
		return;
	}
	Dictionary d = ti->get_metadata(0);

	if (!d["dir"]) {
		file->set_text(d["name"]);
	} else if (mode == FILE_MODE_OPEN_DIR) {
		set_ok_button_text(ETR("Select This Folder"));
	}

	get_ok_button()->set_disabled(_is_open_should_be_disabled());
}

then in FileDialog::_action_pressed, file->get_text() return a exist file, cause select dir is ignored.

void FileDialog::_action_pressed() {
	if (mode == FILE_MODE_OPEN_FILES) {
		TreeItem *ti = tree->get_next_selected(nullptr);
		String fbase = dir_access->get_current_dir();

		Vector<String> files;
		while (ti) {
			files.push_back(fbase.path_join(ti->get_text(0)));
			ti = tree->get_next_selected(ti);
		}

		if (files.size()) {
			emit_signal(SNAME("files_selected"), files);
			hide();
		}

		return;
	}

	String file_text = file->get_text();
	String f = file_text.is_absolute_path() ? file_text : dir_access->get_current_dir().path_join(file_text);

	if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && (dir_access->file_exists(f) || dir_access->is_bundle(f))) {
		emit_signal(SNAME("file_selected"), f);
		hide();
	} else if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_DIR) {
		String path = dir_access->get_current_dir();

		path = path.replace("\\", "/");
		TreeItem *item = tree->get_selected();
		if (item) {
			Dictionary d = item->get_metadata(0);
			if (d["dir"] && d["name"] != "..") {
				path = path.path_join(d["name"]);
			}
		}

		emit_signal(SNAME("dir_selected"), path);
		hide();
	}
//...
}

Steps to reproduce

using FileDialog with FILE_MODE_OPEN_ANY mode
select a file first
then select a dir in the same dir
click Open

Minimal reproduction project (MRP)

MRP.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants