FilePicker.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 GUI {
  17. class FilePicker final
  18. : public Dialog
  19. , private ModelClient {
  20. C_OBJECT(FilePicker);
  21. public:
  22. enum class Mode {
  23. Open,
  24. OpenMultiple,
  25. OpenFolder,
  26. Save
  27. };
  28. 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 = {});
  29. 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);
  30. virtual ~FilePicker() override;
  31. DeprecatedString const& selected_file() const { return m_selected_file; }
  32. private:
  33. void on_file_return();
  34. void set_path(DeprecatedString const&);
  35. // ^GUI::ModelClient
  36. virtual void model_did_update(unsigned) override;
  37. 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 = {});
  38. static DeprecatedString ok_button_name(Mode mode)
  39. {
  40. switch (mode) {
  41. case Mode::Open:
  42. case Mode::OpenMultiple:
  43. case Mode::OpenFolder:
  44. return "Open";
  45. case Mode::Save:
  46. return "Save";
  47. default:
  48. return "OK";
  49. }
  50. }
  51. struct CommonLocationButton {
  52. DeprecatedString path;
  53. size_t tray_item_index { 0 };
  54. };
  55. RefPtr<MultiView> m_view;
  56. NonnullRefPtr<FileSystemModel> m_model;
  57. DeprecatedString m_selected_file;
  58. Vector<DeprecatedString> m_allowed_file_types_names;
  59. Optional<Vector<FileTypeFilter>> m_allowed_file_types;
  60. RefPtr<GUI::Label> m_error_label;
  61. RefPtr<TextBox> m_filename_textbox;
  62. RefPtr<TextBox> m_location_textbox;
  63. Vector<CommonLocationButton> m_common_location_buttons;
  64. RefPtr<Menu> m_context_menu;
  65. Mode m_mode { Mode::Open };
  66. };
  67. }