mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:08:10 +00:00
LibGUI: Port the drag&drop code to Core::MimeData
This commit is contained in:
parent
3cba9c3c25
commit
814d59f462
10 changed files with 59 additions and 53 deletions
|
@ -31,6 +31,7 @@
|
|||
#include <AK/StringBuilder.h>
|
||||
#include <AK/URL.h>
|
||||
#include <LibCore/ConfigFile.h>
|
||||
#include <LibCore/MimeData.h>
|
||||
#include <LibCore/UserInfo.h>
|
||||
#include <LibGUI/AboutDialog.h>
|
||||
#include <LibGUI/Action.h>
|
||||
|
@ -575,10 +576,10 @@ int main(int argc, char** argv)
|
|||
directory_view->on_drop = [&](const GUI::AbstractView&, const GUI::ModelIndex& index, const GUI::DropEvent& event) {
|
||||
if (!index.is_valid())
|
||||
return;
|
||||
if (event.data_type() != "url-list")
|
||||
if (!event.mime_data().has_urls())
|
||||
return;
|
||||
auto paths_to_copy = event.data().split('\n');
|
||||
if (paths_to_copy.is_empty()) {
|
||||
auto urls = event.mime_data().urls();
|
||||
if (urls.is_empty()) {
|
||||
dbg() << "No files to drop";
|
||||
return;
|
||||
}
|
||||
|
@ -587,8 +588,7 @@ int main(int argc, char** argv)
|
|||
if (!target_node.is_directory())
|
||||
return;
|
||||
|
||||
for (auto& path_to_copy : paths_to_copy) {
|
||||
auto url_to_copy = URL(path_to_copy);
|
||||
for (auto& url_to_copy : urls) {
|
||||
if (!url_to_copy.is_valid())
|
||||
continue;
|
||||
auto new_path = String::format("%s/%s",
|
||||
|
@ -597,7 +597,7 @@ int main(int argc, char** argv)
|
|||
|
||||
if (!FileUtils::copy_file_or_directory(url_to_copy.path(), new_path)) {
|
||||
auto error_message = String::format("Could not copy %s into %s.",
|
||||
path_to_copy.characters(),
|
||||
url_to_copy.to_string().characters(),
|
||||
new_path.characters());
|
||||
GUI::MessageBox::show(error_message, "File Manager", GUI::MessageBox::Type::Error);
|
||||
} else {
|
||||
|
|
|
@ -26,10 +26,11 @@
|
|||
|
||||
#include "QSWidget.h"
|
||||
#include <AK/URL.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibCore/MimeData.h>
|
||||
#include <LibGUI/MessageBox.h>
|
||||
#include <LibGUI/Painter.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
|
||||
QSWidget::QSWidget(GUI::Widget* parent)
|
||||
: GUI::Frame(parent)
|
||||
|
@ -130,15 +131,15 @@ void QSWidget::drop_event(GUI::DropEvent& event)
|
|||
event.accept();
|
||||
window()->move_to_front();
|
||||
|
||||
if (event.data_type() == "url-list") {
|
||||
auto lines = event.data().split_view('\n');
|
||||
if (lines.is_empty())
|
||||
if (event.mime_data().has_urls()) {
|
||||
auto urls = event.mime_data().urls();
|
||||
if (urls.is_empty())
|
||||
return;
|
||||
if (lines.size() > 1) {
|
||||
if (urls.size() > 1) {
|
||||
GUI::MessageBox::show("QuickShow can only open one file at a time!", "One at a time please!", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
|
||||
return;
|
||||
}
|
||||
URL url(lines[0]);
|
||||
auto url = urls.first();
|
||||
auto bitmap = Gfx::Bitmap::load_from_file(url.path());
|
||||
if (!bitmap) {
|
||||
GUI::MessageBox::show(String::format("Failed to open %s", url.to_string().characters()), "Cannot open image", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#include <AK/StringBuilder.h>
|
||||
#include <AK/URL.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/MimeData.h>
|
||||
#include <LibGUI/AboutDialog.h>
|
||||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
|
@ -191,7 +192,6 @@ TextEditorWidget::TextEditorWidget()
|
|||
if (needle.is_empty())
|
||||
return;
|
||||
|
||||
|
||||
auto found_range = m_editor->document().find_next(needle);
|
||||
while (found_range.is_valid()) {
|
||||
m_editor->set_selection(found_range);
|
||||
|
@ -463,15 +463,14 @@ void TextEditorWidget::drop_event(GUI::DropEvent& event)
|
|||
event.accept();
|
||||
window()->move_to_front();
|
||||
|
||||
if (event.data_type() == "url-list") {
|
||||
auto lines = event.data().split_view('\n');
|
||||
if (lines.is_empty())
|
||||
if (event.mime_data().has_urls()) {
|
||||
auto urls = event.mime_data().urls();
|
||||
if (urls.is_empty() )
|
||||
return;
|
||||
if (lines.size() > 1) {
|
||||
if (urls.size() > 1) {
|
||||
GUI::MessageBox::show("TextEditor can only open one file at a time!", "One at a time please!", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
|
||||
return;
|
||||
}
|
||||
URL url(lines[0]);
|
||||
open_sesame(url.path());
|
||||
open_sesame(urls.first().path());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,6 +32,10 @@
|
|||
#include <LibGfx/Rect.h>
|
||||
#include <LibGUI/WindowType.h>
|
||||
|
||||
namespace Core {
|
||||
class MimeData;
|
||||
}
|
||||
|
||||
namespace GUI {
|
||||
|
||||
class Event : public Core::Event {
|
||||
|
@ -326,25 +330,18 @@ private:
|
|||
|
||||
class DropEvent final : public Event {
|
||||
public:
|
||||
DropEvent(const Gfx::Point& position, const String& text, const String& data_type, const String& data)
|
||||
: Event(Event::Drop)
|
||||
, m_position(position)
|
||||
, m_text(text)
|
||||
, m_data_type(data_type)
|
||||
, m_data(data)
|
||||
{
|
||||
}
|
||||
DropEvent(const Gfx::Point&, const String& text, NonnullRefPtr<Core::MimeData> mime_data);
|
||||
|
||||
~DropEvent();
|
||||
|
||||
const Gfx::Point& position() const { return m_position; }
|
||||
const String& text() const { return m_text; }
|
||||
const String& data_type() const { return m_data_type; }
|
||||
const String& data() const { return m_data; }
|
||||
const Core::MimeData& mime_data() const { return m_mime_data; }
|
||||
|
||||
private:
|
||||
Gfx::Point m_position;
|
||||
String m_text;
|
||||
String m_data_type;
|
||||
String m_data;
|
||||
NonnullRefPtr<Core::MimeData> m_mime_data;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -561,7 +561,7 @@ bool FileSystemModel::accepts_drag(const ModelIndex& index, const StringView& da
|
|||
{
|
||||
if (!index.is_valid())
|
||||
return false;
|
||||
if (data_type != "url-list")
|
||||
if (data_type != "text/uri-list")
|
||||
return false;
|
||||
auto& node = this->node(index);
|
||||
return node.is_directory();
|
||||
|
|
|
@ -123,7 +123,7 @@ public:
|
|||
virtual void update() override;
|
||||
virtual ModelIndex parent_index(const ModelIndex&) const override;
|
||||
virtual ModelIndex index(int row, int column = 0, const ModelIndex& parent = ModelIndex()) const override;
|
||||
virtual StringView drag_data_type() const override { return "url-list"; }
|
||||
virtual StringView drag_data_type() const override { return "text/uri-list"; }
|
||||
virtual bool accepts_drag(const ModelIndex&, const StringView& data_type) override;
|
||||
|
||||
static String timestamp_string(time_t timestamp)
|
||||
|
|
|
@ -19,6 +19,7 @@ OBJS = \
|
|||
Desktop.o \
|
||||
Dialog.o \
|
||||
DragOperation.o \
|
||||
Event.o \
|
||||
FilePicker.o \
|
||||
FileSystemModel.o \
|
||||
FontDatabase.o \
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
#include <AK/JsonObject.h>
|
||||
#include <AK/NeverDestroyed.h>
|
||||
#include <AK/SharedBuffer.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibCore/MimeData.h>
|
||||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/Event.h>
|
||||
|
@ -36,6 +36,7 @@
|
|||
#include <LibGUI/Widget.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <LibGUI/WindowServerConnection.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
@ -188,7 +189,7 @@ void Window::event(Core::Event& event)
|
|||
if (!m_main_widget)
|
||||
return;
|
||||
auto result = m_main_widget->hit_test(drop_event.position());
|
||||
auto local_event = make<DropEvent>(result.local_position, drop_event.text(), drop_event.data_type(), drop_event.data());
|
||||
auto local_event = make<DropEvent>(result.local_position, drop_event.text(), drop_event.mime_data());
|
||||
ASSERT(result.widget);
|
||||
return result.widget->dispatch_event(*local_event, this);
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <LibCore/MimeData.h>
|
||||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/Clipboard.h>
|
||||
|
@ -309,8 +310,11 @@ void WindowServerConnection::handle(const Messages::WindowClient::AsyncSetWallpa
|
|||
|
||||
void WindowServerConnection::handle(const Messages::WindowClient::DragDropped& message)
|
||||
{
|
||||
if (auto* window = Window::from_window_id(message.window_id()))
|
||||
Core::EventLoop::current().post_event(*window, make<DropEvent>(message.mouse_position(), message.text(), message.data_type(), message.data()));
|
||||
if (auto* window = Window::from_window_id(message.window_id())) {
|
||||
auto mime_data = Core::MimeData::construct();
|
||||
mime_data->set_data(message.data_type(), message.data().to_byte_buffer());
|
||||
Core::EventLoop::current().post_event(*window, make<DropEvent>(message.mouse_position(), message.text(), mime_data));
|
||||
}
|
||||
}
|
||||
|
||||
void WindowServerConnection::handle(const Messages::WindowClient::DragAccepted&)
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
#include <AK/String.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <Kernel/KeyCode.h>
|
||||
#include <LibGfx/Font.h>
|
||||
#include <LibCore/MimeData.h>
|
||||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/Clipboard.h>
|
||||
|
@ -38,6 +38,7 @@
|
|||
#include <LibGUI/Painter.h>
|
||||
#include <LibGUI/ScrollBar.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <LibGfx/Font.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -698,21 +699,23 @@ void TerminalWidget::context_menu_event(GUI::ContextMenuEvent& event)
|
|||
|
||||
void TerminalWidget::drop_event(GUI::DropEvent& event)
|
||||
{
|
||||
if (event.data_type() == "text") {
|
||||
if (event.mime_data().has_text()) {
|
||||
event.accept();
|
||||
write(m_ptm_fd, event.data().characters(), event.data().length());
|
||||
} else if (event.data_type() == "url-list") {
|
||||
auto text = event.mime_data().text();
|
||||
write(m_ptm_fd, text.characters(), text.length());
|
||||
} else if (event.mime_data().has_urls()) {
|
||||
event.accept();
|
||||
auto lines = event.data().split('\n');
|
||||
auto urls = event.mime_data().urls();
|
||||
bool first = true;
|
||||
for (auto& line : lines) {
|
||||
if (!first)
|
||||
for (auto& url : event.mime_data().urls()) {
|
||||
if (!first) {
|
||||
write(m_ptm_fd, " ", 1);
|
||||
first = false;
|
||||
|
||||
if (line.starts_with("file://"))
|
||||
line = line.substring(7, line.length() - 7);
|
||||
write(m_ptm_fd, line.characters(), line.length());
|
||||
}
|
||||
if (url.protocol() == "file")
|
||||
write(m_ptm_fd, url.path().characters(), url.path().length());
|
||||
else
|
||||
write(m_ptm_fd, url.to_string().characters(), url.to_string().length());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue