LibGUI+Everywhere: Make sync requests to Clipboard server more obvious

This commit is contained in:
Ben Wiederhake 2021-11-20 15:22:01 +01:00 committed by Linus Groh
parent 06f140a025
commit f22c0ffe0c
Notes: sideshowbarker 2024-07-18 00:55:17 +09:00
13 changed files with 22 additions and 23 deletions

View File

@ -44,7 +44,7 @@ private:
virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override; virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
// ^GUI::Clipboard::ClipboardClient // ^GUI::Clipboard::ClipboardClient
virtual void clipboard_content_did_change(const String&) override { add_item(GUI::Clipboard::the().data_and_type()); } virtual void clipboard_content_did_change(const String&) override { add_item(GUI::Clipboard::the().fetch_data_and_type()); }
Vector<GUI::Clipboard::DataAndType> m_history_items; Vector<GUI::Clipboard::DataAndType> m_history_items;
size_t m_history_limit; size_t m_history_limit;

View File

@ -136,7 +136,7 @@ Tab::Tab(BrowserWindow& window)
}; };
m_location_box->add_custom_context_menu_action(GUI::Action::create("Paste && Go", [this](auto&) { m_location_box->add_custom_context_menu_action(GUI::Action::create("Paste && Go", [this](auto&) {
auto [data, mime_type, _] = GUI::Clipboard::the().data_and_type(); auto [data, mime_type, _] = GUI::Clipboard::the().fetch_data_and_type();
if (!mime_type.starts_with("text/")) if (!mime_type.starts_with("text/"))
return; return;
auto const& paste_text = data; auto const& paste_text = data;

View File

@ -58,7 +58,7 @@ int main(int argc, char** argv)
GUI::Clipboard::the().set_plain_text(widget.get_entry()); GUI::Clipboard::the().set_plain_text(widget.get_entry());
})); }));
edit_menu.add_action(GUI::CommonActions::make_paste_action([&](auto&) { edit_menu.add_action(GUI::CommonActions::make_paste_action([&](auto&) {
auto clipboard = GUI::Clipboard::the().data_and_type(); auto clipboard = GUI::Clipboard::the().fetch_data_and_type();
if (clipboard.mime_type == "text/plain") { if (clipboard.mime_type == "text/plain") {
if (!clipboard.data.is_empty()) { if (!clipboard.data.is_empty()) {
auto data = atof(StringView(clipboard.data).to_string().characters()); auto data = atof(StringView(clipboard.data).to_string().characters());

View File

@ -151,7 +151,7 @@ void do_copy(Vector<String> const& selected_file_paths, FileOperation file_opera
void do_paste(String const& target_directory, GUI::Window* window) void do_paste(String const& target_directory, GUI::Window* window)
{ {
auto data_and_type = GUI::Clipboard::the().data_and_type(); auto data_and_type = GUI::Clipboard::the().fetch_data_and_type();
if (data_and_type.mime_type != "text/uri-list") { if (data_and_type.mime_type != "text/uri-list") {
dbgln("Cannot paste clipboard type {}", data_and_type.mime_type); dbgln("Cannot paste clipboard type {}", data_and_type.mime_type);
return; return;
@ -345,7 +345,7 @@ int run_in_desktop_mode()
do_paste(directory_view.path(), directory_view.window()); do_paste(directory_view.path(), directory_view.window());
}, },
window); window);
paste_action->set_enabled(GUI::Clipboard::the().mime_type() == "text/uri-list" && access(directory_view.path().characters(), W_OK) == 0); paste_action->set_enabled(GUI::Clipboard::the().fetch_mime_type() == "text/uri-list" && access(directory_view.path().characters(), W_OK) == 0);
GUI::Clipboard::the().on_change = [&](String const& data_type) { GUI::Clipboard::the().on_change = [&](String const& data_type) {
paste_action->set_enabled(data_type == "text/uri-list" && access(directory_view.path().characters(), W_OK) == 0); paste_action->set_enabled(data_type == "text/uri-list" && access(directory_view.path().characters(), W_OK) == 0);
@ -1007,7 +1007,7 @@ int run_in_windowed_mode(String initial_location, String entry_focused_on_init)
mkdir_action->set_enabled(can_write_in_path); mkdir_action->set_enabled(can_write_in_path);
touch_action->set_enabled(can_write_in_path); touch_action->set_enabled(can_write_in_path);
paste_action->set_enabled(can_write_in_path && GUI::Clipboard::the().mime_type() == "text/uri-list"); paste_action->set_enabled(can_write_in_path && GUI::Clipboard::the().fetch_mime_type() == "text/uri-list");
go_forward_action->set_enabled(directory_view.path_history_position() < directory_view.path_history_size() - 1); go_forward_action->set_enabled(directory_view.path_history_position() < directory_view.path_history_size() - 1);
go_back_action->set_enabled(directory_view.path_history_position() > 0); go_back_action->set_enabled(directory_view.path_history_position() > 0);
open_parent_directory_action->set_enabled(new_path != "/"); open_parent_directory_action->set_enabled(new_path != "/");
@ -1089,7 +1089,7 @@ int run_in_windowed_mode(String initial_location, String entry_focused_on_init)
auto& node = directory_view.node(index); auto& node = directory_view.node(index);
if (node.is_directory()) { if (node.is_directory()) {
auto should_get_enabled = access(node.full_path().characters(), W_OK) == 0 && GUI::Clipboard::the().mime_type() == "text/uri-list"; auto should_get_enabled = access(node.full_path().characters(), W_OK) == 0 && GUI::Clipboard::the().fetch_mime_type() == "text/uri-list";
folder_specific_paste_action->set_enabled(should_get_enabled); folder_specific_paste_action->set_enabled(should_get_enabled);
directory_context_menu->popup(event.screen_position(), directory_open_action); directory_context_menu->popup(event.screen_position(), directory_open_action);
} else { } else {
@ -1207,7 +1207,7 @@ int run_in_windowed_mode(String initial_location, String entry_focused_on_init)
directory_view.open(initial_location); directory_view.open(initial_location);
directory_view.set_focus(true); directory_view.set_focus(true);
paste_action->set_enabled(GUI::Clipboard::the().mime_type() == "text/uri-list" && access(initial_location.characters(), W_OK) == 0); paste_action->set_enabled(GUI::Clipboard::the().fetch_mime_type() == "text/uri-list" && access(initial_location.characters(), W_OK) == 0);
window->set_rect({ left, top, width, height }); window->set_rect({ left, top, width, height });
if (was_maximized) if (was_maximized)

View File

@ -202,7 +202,7 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::BitmapFont>&&
m_glyph_editor_widget->paste_glyph(); m_glyph_editor_widget->paste_glyph();
m_glyph_map_widget->update_glyph(m_glyph_map_widget->selected_glyph()); m_glyph_map_widget->update_glyph(m_glyph_map_widget->selected_glyph());
}); });
m_paste_action->set_enabled(GUI::Clipboard::the().mime_type() == "glyph/x-fonteditor"); m_paste_action->set_enabled(GUI::Clipboard::the().fetch_mime_type() == "glyph/x-fonteditor");
m_delete_action = GUI::CommonActions::make_delete_action([this](auto&) { m_delete_action = GUI::CommonActions::make_delete_action([this](auto&) {
if (m_glyph_editor_widget->is_glyph_empty() && m_edited_font->raw_glyph_width(m_glyph_map_widget->selected_glyph()) == 0) if (m_glyph_editor_widget->is_glyph_empty() && m_edited_font->raw_glyph_width(m_glyph_map_widget->selected_glyph()) == 0)
return; return;

View File

@ -84,7 +84,7 @@ void GlyphEditorWidget::copy_glyph()
void GlyphEditorWidget::paste_glyph() void GlyphEditorWidget::paste_glyph()
{ {
auto [data, mime_type, metadata] = GUI::Clipboard::the().data_and_type(); auto [data, mime_type, metadata] = GUI::Clipboard::the().fetch_data_and_type();
if (!mime_type.starts_with("glyph/")) if (!mime_type.starts_with("glyph/"))
return; return;

View File

@ -214,7 +214,7 @@ void MainWidget::initialize_menubar(GUI::Window& window)
auto* editor = current_image_editor(); auto* editor = current_image_editor();
if (!editor) if (!editor)
return; return;
auto bitmap = GUI::Clipboard::the().data_and_type().as_bitmap(); auto bitmap = GUI::Clipboard::the().fetch_data_and_type().as_bitmap();
if (!bitmap) if (!bitmap)
return; return;
@ -226,7 +226,7 @@ void MainWidget::initialize_menubar(GUI::Window& window)
GUI::Clipboard::the().on_change = [&](auto& mime_type) { GUI::Clipboard::the().on_change = [&](auto& mime_type) {
m_paste_action->set_enabled(mime_type == "image/x-serenityos"); m_paste_action->set_enabled(mime_type == "image/x-serenityos");
}; };
m_paste_action->set_enabled(GUI::Clipboard::the().mime_type() == "image/x-serenityos"); m_paste_action->set_enabled(GUI::Clipboard::the().fetch_mime_type() == "image/x-serenityos");
m_undo_action = GUI::CommonActions::make_undo_action([&](auto&) { m_undo_action = GUI::CommonActions::make_undo_action([&](auto&) {
if (auto* editor = current_image_editor()) if (auto* editor = current_image_editor())

View File

@ -170,7 +170,7 @@ SpreadsheetWidget::SpreadsheetWidget(NonnullRefPtrVector<Sheet>&& sheets, bool s
auto& sheet = *worksheet_ptr; auto& sheet = *worksheet_ptr;
auto& cells = sheet.selected_cells(); auto& cells = sheet.selected_cells();
VERIFY(!cells.is_empty()); VERIFY(!cells.is_empty());
const auto& data = GUI::Clipboard::the().data_and_type(); const auto& data = GUI::Clipboard::the().fetch_data_and_type();
if (auto spreadsheet_data = data.metadata.get("text/x-spreadsheet-data"); spreadsheet_data.has_value()) { if (auto spreadsheet_data = data.metadata.get("text/x-spreadsheet-data"); spreadsheet_data.has_value()) {
Vector<Spreadsheet::Position> source_positions, target_positions; Vector<Spreadsheet::Position> source_positions, target_positions;
auto lines = spreadsheet_data.value().split_view('\n'); auto lines = spreadsheet_data.value().split_view('\n');

View File

@ -50,7 +50,7 @@ Clipboard& Clipboard::the()
return *s_the; return *s_the;
} }
Clipboard::DataAndType Clipboard::data_and_type() const Clipboard::DataAndType Clipboard::fetch_data_and_type() const
{ {
auto response = connection().get_clipboard_data(); auto response = connection().get_clipboard_data();
if (!response.data().is_valid()) if (!response.data().is_valid())

View File

@ -39,9 +39,8 @@ public:
static void initialize(Badge<Application>); static void initialize(Badge<Application>);
static Clipboard& the(); static Clipboard& the();
DataAndType data_and_type() const; DataAndType fetch_data_and_type() const;
ByteBuffer data() const { return data_and_type().data; } String fetch_mime_type() const { return fetch_data_and_type().mime_type; }
String mime_type() const { return data_and_type().mime_type; }
void set_data(ReadonlyBytes data, String const& mime_type = "text/plain", HashMap<String, String> const& metadata = {}); void set_data(ReadonlyBytes data, String const& mime_type = "text/plain", HashMap<String, String> const& metadata = {});
void set_plain_text(String const& text) { set_data(text.bytes()); } void set_plain_text(String const& text) { set_data(text.bytes()); }

View File

@ -83,7 +83,7 @@ void TextEditor::create_actions()
m_cut_action->set_enabled(false); m_cut_action->set_enabled(false);
m_copy_action->set_enabled(false); m_copy_action->set_enabled(false);
m_paste_action = CommonActions::make_paste_action([&](auto&) { paste(); }, this); m_paste_action = CommonActions::make_paste_action([&](auto&) { paste(); }, this);
m_paste_action->set_enabled(is_editable() && Clipboard::the().mime_type().starts_with("text/")); m_paste_action->set_enabled(is_editable() && Clipboard::the().fetch_mime_type().starts_with("text/"));
if (is_multi_line()) { if (is_multi_line()) {
m_go_to_line_action = Action::create( m_go_to_line_action = Action::create(
"Go to line...", { Mod_Ctrl, Key_L }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) { "Go to line...", { Mod_Ctrl, Key_L }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
@ -1453,7 +1453,7 @@ void TextEditor::paste()
if (!is_editable()) if (!is_editable())
return; return;
auto [data, mime_type, _] = GUI::Clipboard::the().data_and_type(); auto [data, mime_type, _] = GUI::Clipboard::the().fetch_data_and_type();
if (!mime_type.starts_with("text/")) if (!mime_type.starts_with("text/"))
return; return;

View File

@ -752,7 +752,7 @@ void TerminalWidget::paste()
if (m_ptm_fd == -1) if (m_ptm_fd == -1)
return; return;
auto [data, mime_type, _] = GUI::Clipboard::the().data_and_type(); auto [data, mime_type, _] = GUI::Clipboard::the().fetch_data_and_type();
if (!mime_type.starts_with("text/")) if (!mime_type.starts_with("text/"))
return; return;
if (data.is_empty()) if (data.is_empty())
@ -1166,7 +1166,7 @@ void TerminalWidget::update_copy_action()
void TerminalWidget::update_paste_action() void TerminalWidget::update_paste_action()
{ {
auto [data, mime_type, _] = GUI::Clipboard::the().data_and_type(); auto [data, mime_type, _] = GUI::Clipboard::the().fetch_data_and_type();
m_paste_action->set_enabled(mime_type.starts_with("text/") && !data.is_empty()); m_paste_action->set_enabled(mime_type.starts_with("text/") && !data.is_empty());
} }

View File

@ -74,7 +74,7 @@ int main(int argc, char* argv[])
clipboard.on_change = [&](const String&) { clipboard.on_change = [&](const String&) {
// Technically there's a race here... // Technically there's a race here...
auto data_and_type = clipboard.data_and_type(); auto data_and_type = clipboard.fetch_data_and_type();
if (data_and_type.mime_type.is_null()) { if (data_and_type.mime_type.is_null()) {
spawn_command(watch_command, {}, "clear"); spawn_command(watch_command, {}, "clear");
} else { } else {
@ -88,7 +88,7 @@ int main(int argc, char* argv[])
return app->exec(); return app->exec();
} }
auto data_and_type = clipboard.data_and_type(); auto data_and_type = clipboard.fetch_data_and_type();
if (data_and_type.mime_type.is_null()) { if (data_and_type.mime_type.is_null()) {
warnln("Nothing copied"); warnln("Nothing copied");