FilePicker.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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, String const& window_title = {}, StringView path = Core::StandardPaths::home_directory(), bool folder = false, ScreenPosition screen_position = Dialog::ScreenPosition::CenterWithinParent);
  26. static Optional<String> get_save_filepath(Window* parent_window, String const& title, String const& extension, StringView path = Core::StandardPaths::home_directory(), ScreenPosition screen_position = Dialog::ScreenPosition::CenterWithinParent);
  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(String const&);
  32. // ^GUI::ModelClient
  33. virtual void model_did_update(unsigned) override;
  34. FilePicker(Window* parent_window, Mode type = Mode::Open, StringView filename = "Untitled", StringView path = Core::StandardPaths::home_directory(), ScreenPosition screen_position = Dialog::ScreenPosition::CenterWithinParent);
  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. size_t tray_item_index { 0 };
  51. };
  52. RefPtr<MultiView> m_view;
  53. NonnullRefPtr<FileSystemModel> m_model;
  54. String m_selected_file;
  55. RefPtr<GUI::Label> m_error_label;
  56. RefPtr<TextBox> m_filename_textbox;
  57. RefPtr<TextBox> m_location_textbox;
  58. Vector<CommonLocationButton> m_common_location_buttons;
  59. RefPtr<Menu> m_context_menu;
  60. Mode m_mode { Mode::Open };
  61. };
  62. }