FilePicker.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/LexicalPath.h>
  8. #include <AK/Optional.h>
  9. #include <AK/String.h>
  10. #include <LibCore/StandardPaths.h>
  11. #include <LibGUI/ComboBox.h>
  12. #include <LibGUI/Dialog.h>
  13. #include <LibGUI/FileTypeFilter.h>
  14. #include <LibGUI/ImageWidget.h>
  15. #include <LibGUI/Model.h>
  16. namespace FileSystemAccessServer {
  17. class ConnectionFromClient;
  18. }
  19. namespace GUI {
  20. class FilePicker final
  21. : public Dialog
  22. , private ModelClient {
  23. C_OBJECT(FilePicker);
  24. public:
  25. enum class Mode {
  26. Open,
  27. OpenMultiple,
  28. OpenFolder,
  29. Save
  30. };
  31. static Optional<DeprecatedString> get_open_filepath(Window* parent_window, DeprecatedString const& window_title = {}, StringView path = Core::StandardPaths::home_directory(), bool folder = false, ScreenPosition screen_position = Dialog::ScreenPosition::CenterWithinParent, Optional<Vector<FileTypeFilter>> allowed_file_types = {});
  32. static Optional<DeprecatedString> get_save_filepath(Window* parent_window, DeprecatedString const& title, DeprecatedString const& extension, StringView path = Core::StandardPaths::home_directory(), ScreenPosition screen_position = Dialog::ScreenPosition::CenterWithinParent);
  33. static ErrorOr<Optional<String>> get_filepath(Badge<FileSystemAccessServer::ConnectionFromClient>, i32 window_server_client_id, i32 parent_window_id, Mode, StringView window_title, StringView file_basename = {}, StringView path = Core::StandardPaths::home_directory(), Optional<Vector<FileTypeFilter>> = {});
  34. virtual ~FilePicker() override;
  35. Optional<DeprecatedString> const& selected_file() const { return m_selected_file; }
  36. private:
  37. void on_file_return();
  38. void set_path(DeprecatedString const&);
  39. // ^GUI::ModelClient
  40. virtual void model_did_update(unsigned) override;
  41. FilePicker(Window* parent_window, Mode type = Mode::Open, StringView filename = "Untitled"sv, StringView path = Core::StandardPaths::home_directory(), ScreenPosition screen_position = Dialog::ScreenPosition::CenterWithinParent, Optional<Vector<FileTypeFilter>> allowed_file_types = {});
  42. static String ok_button_name(Mode mode)
  43. {
  44. switch (mode) {
  45. case Mode::Open:
  46. case Mode::OpenMultiple:
  47. case Mode::OpenFolder:
  48. return "Open"_string;
  49. case Mode::Save:
  50. return "Save"_string;
  51. default:
  52. return "OK"_string;
  53. }
  54. }
  55. struct CommonLocationButton {
  56. DeprecatedString path;
  57. size_t tray_item_index { 0 };
  58. };
  59. RefPtr<MultiView> m_view;
  60. NonnullRefPtr<FileSystemModel> m_model;
  61. Optional<DeprecatedString> m_selected_file;
  62. Vector<DeprecatedString> m_allowed_file_types_names;
  63. Optional<Vector<FileTypeFilter>> m_allowed_file_types;
  64. RefPtr<GUI::Label> m_error_label;
  65. RefPtr<TextBox> m_filename_textbox;
  66. RefPtr<TextBox> m_location_textbox;
  67. Vector<CommonLocationButton> m_common_location_buttons;
  68. RefPtr<Menu> m_context_menu;
  69. Mode m_mode { Mode::Open };
  70. };
  71. }