summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorKarol Kosek <krkk@serenityos.org>2022-03-13 13:29:31 +0100
committerAndreas Kling <kling@serenityos.org>2022-04-06 18:32:49 +0200
commit35934acbd3316ed5025cb0b46492ab9fc1955a29 (patch)
treed15da132e015aaebc0cbb0cb961f59e106b5a609 /Userland
parent7ba2e5e3e74e42d9cadee0aaec317c20e431dc2d (diff)
downloadserenity-35934acbd3316ed5025cb0b46492ab9fc1955a29.zip
Spreadsheet: Change paste action's enabled state on clipboard change
Previously, the paste action was always enabled and always assumed that anything was selected, which led to a crash by clicking the paste action right after the application startup. This patch will automatically enable/disable the paste action depending on whether a selection exists (it usually does, except on the app launch and after adding a new tab) and if the clipboard mime type is a text/ group. So no, you can't paste an image into the app anymore, even though this mostly froze the app before...
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp9
-rw-r--r--Userland/Applications/Spreadsheet/SpreadsheetWidget.h9
2 files changed, 17 insertions, 1 deletions
diff --git a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp
index 6b0af1f5a7..470ad77892 100644
--- a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp
+++ b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp
@@ -245,6 +245,7 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, NonnullRefPtrVe
m_cut_action->set_enabled(false);
m_copy_action->set_enabled(false);
+ m_paste_action->set_enabled(false);
m_tab_widget->on_change = [this](auto& selected_widget) {
// for keyboard shortcuts and command palette
@@ -269,6 +270,12 @@ void SpreadsheetWidget::resize_event(GUI::ResizeEvent& event)
m_inline_documentation_window->set_rect(m_cell_value_editor->screen_relative_rect().translated(0, m_cell_value_editor->height() + 7).inflated(6, 6));
}
+void SpreadsheetWidget::clipboard_content_did_change(String const& mime_type)
+{
+ if (auto* sheet = current_worksheet_if_available())
+ m_paste_action->set_enabled(!sheet->selected_cells().is_empty() && mime_type.starts_with("text/"));
+}
+
void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
{
for (auto& sheet : new_sheets) {
@@ -286,6 +293,7 @@ void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
VERIFY(!selection.is_empty());
m_cut_action->set_enabled(true);
m_copy_action->set_enabled(true);
+ m_paste_action->set_enabled(GUI::Clipboard::the().fetch_mime_type().starts_with("text/"));
m_current_cell_label->set_enabled(true);
m_cell_value_editor->set_enabled(true);
@@ -353,6 +361,7 @@ void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
m_cut_action->set_enabled(false);
m_copy_action->set_enabled(false);
+ m_paste_action->set_enabled(false);
static_cast<CellSyntaxHighlighter*>(const_cast<Syntax::Highlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(nullptr);
};
diff --git a/Userland/Applications/Spreadsheet/SpreadsheetWidget.h b/Userland/Applications/Spreadsheet/SpreadsheetWidget.h
index ef1c5dcfa0..ef9aa1442b 100644
--- a/Userland/Applications/Spreadsheet/SpreadsheetWidget.h
+++ b/Userland/Applications/Spreadsheet/SpreadsheetWidget.h
@@ -9,12 +9,15 @@
#include "SpreadsheetView.h"
#include "Workbook.h"
#include <AK/NonnullRefPtrVector.h>
+#include <LibGUI/Clipboard.h>
#include <LibGUI/TabWidget.h>
#include <LibGUI/Widget.h>
namespace Spreadsheet {
-class SpreadsheetWidget final : public GUI::Widget {
+class SpreadsheetWidget final
+ : public GUI::Widget
+ , public GUI::Clipboard::ClipboardClient {
C_OBJECT(SpreadsheetWidget);
public:
@@ -49,8 +52,12 @@ public:
auto& undo_stack() { return m_undo_stack; }
private:
+ // ^GUI::Widget
virtual void resize_event(GUI::ResizeEvent&) override;
+ // ^GUI::Clipboard::ClipboardClient
+ virtual void clipboard_content_did_change(String const& mime_type) override;
+
explicit SpreadsheetWidget(GUI::Window& window, NonnullRefPtrVector<Sheet>&& sheets = {}, bool should_add_sheet_if_empty = true);
void setup_tabs(NonnullRefPtrVector<Sheet> new_sheets);