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