2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-05-28 20:40:53 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-05-26 14:52:44 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
2019-07-13 19:58:04 -05:00
|
|
|
#include <AK/Optional.h>
|
2023-01-11 22:42:45 +01:00
|
|
|
#include <AK/String.h>
|
2020-04-19 19:57:05 +02:00
|
|
|
#include <LibCore/StandardPaths.h>
|
2023-01-11 22:42:45 +01:00
|
|
|
#include <LibGUI/ComboBox.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Dialog.h>
|
2023-01-11 22:42:45 +01:00
|
|
|
#include <LibGUI/FileTypeFilter.h>
|
2020-07-22 15:29:51 +02:00
|
|
|
#include <LibGUI/ImageWidget.h>
|
2020-07-11 06:47:26 -06:00
|
|
|
#include <LibGUI/Model.h>
|
2019-05-09 01:24:37 +02:00
|
|
|
|
2023-05-13 05:07:41 -04:00
|
|
|
namespace FileSystemAccessServer {
|
|
|
|
|
|
|
|
|
|
class ConnectionFromClient;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
2019-03-09 21:38:13 +01:00
|
|
|
|
2020-08-13 20:06:14 +02:00
|
|
|
class FilePicker final
|
|
|
|
|
: public Dialog
|
|
|
|
|
, private ModelClient {
|
|
|
|
|
C_OBJECT(FilePicker);
|
|
|
|
|
|
2019-03-09 21:38:13 +01:00
|
|
|
public:
|
2019-07-13 19:58:04 -05:00
|
|
|
enum class Mode {
|
|
|
|
|
Open,
|
2020-07-11 23:13:12 -06:00
|
|
|
OpenMultiple,
|
2021-04-11 19:34:10 -04:00
|
|
|
OpenFolder,
|
2019-07-13 19:58:04 -05:00
|
|
|
Save
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
static Optional<ByteString> get_open_filepath(Window* parent_window, ByteString const& window_title = {}, StringView path = Core::StandardPaths::home_directory(), bool folder = false, ScreenPosition screen_position = Dialog::ScreenPosition::CenterWithinParent, Optional<Vector<FileTypeFilter>> allowed_file_types = {});
|
|
|
|
|
static Optional<ByteString> get_save_filepath(Window* parent_window, ByteString const& title, ByteString const& extension, StringView path = Core::StandardPaths::home_directory(), ScreenPosition screen_position = Dialog::ScreenPosition::CenterWithinParent);
|
2019-07-13 19:58:04 -05:00
|
|
|
|
2023-05-13 05:07:41 -04:00
|
|
|
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>> = {});
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual ~FilePicker() override;
|
2019-03-09 21:38:13 +01:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
Optional<ByteString> const& selected_file() const { return m_selected_file; }
|
2019-05-09 04:56:52 +02:00
|
|
|
|
2019-03-16 12:57:04 +01:00
|
|
|
private:
|
2019-07-29 15:50:06 -05:00
|
|
|
void on_file_return();
|
2019-05-26 22:33:54 +02:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
void set_path(ByteString const&);
|
2020-07-19 21:42:00 +02:00
|
|
|
|
2020-08-13 20:06:14 +02:00
|
|
|
// ^GUI::ModelClient
|
|
|
|
|
virtual void model_did_update(unsigned) override;
|
2020-07-11 06:47:26 -06:00
|
|
|
|
2023-01-11 22:42:45 +01:00
|
|
|
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 = {});
|
2019-10-01 00:18:20 -05:00
|
|
|
|
2023-02-11 20:51:04 +01:00
|
|
|
static String ok_button_name(Mode mode)
|
2019-07-13 19:58:04 -05:00
|
|
|
{
|
|
|
|
|
switch (mode) {
|
|
|
|
|
case Mode::Open:
|
2020-07-11 23:13:12 -06:00
|
|
|
case Mode::OpenMultiple:
|
2021-04-11 19:34:10 -04:00
|
|
|
case Mode::OpenFolder:
|
2023-08-07 22:26:17 -04:00
|
|
|
return "Open"_string;
|
2019-07-13 19:58:04 -05:00
|
|
|
case Mode::Save:
|
2023-08-07 22:26:17 -04:00
|
|
|
return "Save"_string;
|
2019-07-13 19:58:04 -05:00
|
|
|
default:
|
2023-08-07 22:26:17 -04:00
|
|
|
return "OK"_string;
|
2019-07-13 19:58:04 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 18:18:57 +02:00
|
|
|
struct CommonLocationButton {
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString path;
|
2021-10-21 19:33:08 +02:00
|
|
|
size_t tray_item_index { 0 };
|
2021-04-26 18:18:57 +02:00
|
|
|
};
|
|
|
|
|
|
2020-02-24 20:50:21 +01:00
|
|
|
RefPtr<MultiView> m_view;
|
2020-02-02 15:07:41 +01:00
|
|
|
NonnullRefPtr<FileSystemModel> m_model;
|
2023-12-16 17:49:34 +03:30
|
|
|
Optional<ByteString> m_selected_file;
|
2019-05-26 22:33:54 +02:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
Vector<ByteString> m_allowed_file_types_names;
|
2023-01-11 22:42:45 +01:00
|
|
|
Optional<Vector<FileTypeFilter>> m_allowed_file_types;
|
|
|
|
|
|
2021-07-27 13:52:28 +02:00
|
|
|
RefPtr<GUI::Label> m_error_label;
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
RefPtr<TextBox> m_filename_textbox;
|
2020-07-11 06:47:26 -06:00
|
|
|
RefPtr<TextBox> m_location_textbox;
|
2021-04-26 18:18:57 +02:00
|
|
|
Vector<CommonLocationButton> m_common_location_buttons;
|
2021-03-29 13:26:28 -04:00
|
|
|
RefPtr<Menu> m_context_menu;
|
2019-07-13 19:58:04 -05:00
|
|
|
Mode m_mode { Mode::Open };
|
2019-07-25 19:49:28 +02:00
|
|
|
};
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|