Skip to content

Commit

Permalink
feat: gui file selector on click
Browse files Browse the repository at this point in the history
  • Loading branch information
f0e committed Nov 24, 2024
1 parent 4c6b753 commit 053c42f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,28 @@ static os::WindowRef create_window(os::DragTarget& dragTarget) {

bool processEvent(const os::Event& ev) {
switch (ev.type()) {
case os::Event::KeyDown:
case os::Event::KeyDown: {
if (ev.scancode() == os::kKeyEsc)
return false;
break;
}

case os::Event::CloseApp:
case os::Event::CloseWindow:
case os::Event::CloseWindow: {
closing = true;
return false;
}

case os::Event::ResizeWindow:
break;

case os::Event::MouseDown: {
base::paths paths;
utils::show_file_selector("Blur input", ".", { "mp4", "mkv" }, os::FileDialog::Type::OpenFiles, paths);
tasks::add_files(paths);
break;
}

default:
break;
}
Expand Down
72 changes: 72 additions & 0 deletions src/gui/ui/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,75 @@ double utils::get_display_refresh_rate(int screenNumber)
return static_cast<double>(rate);
#endif
}

bool utils::show_file_selector( // aseprite
const std::string& title,
const std::string& initialPath,
const base::paths& extensions,
os::FileDialog::Type type,
base::paths& output
) {
const std::string defExtension = ""; //

if (os::instance()->nativeDialogs()) {
os::FileDialogRef dlg =
os::instance()->nativeDialogs()->makeFileDialog();

if (dlg) {
dlg->setTitle(title);

// Must be set before setFileName() as the Linux impl might
// require the default extension to fix the initial file name
// with the default extension.
if (!defExtension.empty()) {
dlg->setDefaultExtension(defExtension);
}

#if LAF_LINUX // As the X11 version doesn't store the default path to
// start navigating, we use our own
// get_initial_path_to_select_filename()
dlg->setFileName(get_initial_path_to_select_filename(initialPath));
#else // !LAF_LINUX
dlg->setFileName(initialPath);
#endif

dlg->setType(type);

for (const auto& ext : extensions)
dlg->addFilter(ext, ext + " files (*." + ext + ")");

auto res = dlg->show(os::instance()->defaultWindow());
if (res != os::FileDialog::Result::Error) {
if (res == os::FileDialog::Result::OK) {
if (type == os::FileDialog::Type::OpenFiles)
dlg->getMultipleFileNames(output);
else
output.push_back(dlg->fileName());

#if LAF_LINUX // Save the path in the configuration file
if (!output.empty()) {
set_current_dir_for_file_selector(base::get_file_path(output[0]));
}
#endif

return true;
}
else {
return false;
}
}
else {
// Fallback to default file selector if we weren't able to
// open the native dialog...
}
}
}

// FileSelector fileSelector(type);

// if (!defExtension.empty())
// fileSelector.setDefaultExtension(defExtension);

// return fileSelector.show(title, initialPath, extensions, output);
return false;
}
8 changes: 8 additions & 0 deletions src/gui/ui/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ namespace utils {
#else
double get_display_refresh_rate(int screenNumber);
#endif

bool show_file_selector(
const std::string& title,
const std::string& initialPath,
const base::paths& extensions,
os::FileDialog::Type type,
base::paths& output
);
}

0 comments on commit 053c42f

Please sign in to comment.