FilePicker.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <LibCore/StandardPaths.h>
  10. #include <LibGUI/Dialog.h>
  11. #include <LibGUI/ImageWidget.h>
  12. #include <LibGUI/Model.h>
  13. namespace GUI {
  14. class FilePicker final
  15. : public Dialog
  16. , private ModelClient {
  17. C_OBJECT(FilePicker);
  18. public:
  19. enum class Mode {
  20. Open,
  21. OpenMultiple,
  22. OpenFolder,
  23. Save
  24. };
  25. static Optional<String> get_open_filepath(Window* parent_window, const String& window_title = {}, const StringView& path = Core::StandardPaths::home_directory(), bool folder = false);
  26. static Optional<String> get_save_filepath(Window* parent_window, const String& title, const String& extension, const StringView& path = Core::StandardPaths::home_directory());
  27. virtual ~FilePicker() override;
  28. String const& selected_file() const { return m_selected_file; }
  29. private:
  30. void on_file_return();
  31. void set_path(const String&);
  32. // ^GUI::ModelClient
  33. virtual void model_did_update(unsigned) override;
  34. FilePicker(Window* parent_window, Mode type = Mode::Open, const StringView& filename = "Untitled", const StringView& path = Core::StandardPaths::home_directory());
  35. static String ok_button_name(Mode mode)
  36. {
  37. switch (mode) {
  38. case Mode::Open:
  39. case Mode::OpenMultiple:
  40. case Mode::OpenFolder:
  41. return "Open";
  42. case Mode::Save:
  43. return "Save";
  44. default:
  45. return "OK";
  46. }
  47. }
  48. struct CommonLocationButton {
  49. String path;
  50. Button& button;
  51. };
  52. RefPtr<MultiView> m_view;
  53. NonnullRefPtr<FileSystemModel> m_model;
  54. String m_selected_file;
  55. RefPtr<TextBox> m_filename_textbox;
  56. RefPtr<TextBox> m_location_textbox;
  57. Vector<CommonLocationButton> m_common_location_buttons;
  58. RefPtr<Menu> m_context_menu;
  59. Mode m_mode { Mode::Open };
  60. };
  61. }