From 2c5023e9c7f30e00ee29f8a84f45bd6a97de7b20 Mon Sep 17 00:00:00 2001 From: Leo Date: Mon, 13 Mar 2023 17:48:14 +0900 Subject: [PATCH] Paste text into entry on Ctrl+V (#245) --- src/MainWindow.vala | 48 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 10ff6928..12cd0bbc 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -47,19 +47,21 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { public struct History { string exp; string output; } private double memory_value = 0; - public const string ACTION_PREFIX = "win."; - public const string ACTION_CLEAR = "action-clear"; - public const string ACTION_INSERT = "action-insert"; - public const string ACTION_FUNCTION = "action-function"; - public const string ACTION_UNDO = "action-undo"; - public const string ACTION_COPY = "action-copy"; + private const string ACTION_PREFIX = "win."; + private const string ACTION_CLEAR = "action-clear"; + private const string ACTION_INSERT = "action-insert"; + private const string ACTION_FUNCTION = "action-function"; + private const string ACTION_UNDO = "action-undo"; + private const string ACTION_COPY = "action-copy"; + private const string ACTION_PASTE = "action-paste"; private const ActionEntry[] ACTION_ENTRIES = { { ACTION_INSERT, action_insert, "s"}, { ACTION_FUNCTION, action_function, "s"}, { ACTION_CLEAR, action_clear }, { ACTION_UNDO, undo }, - { ACTION_COPY, copy } + { ACTION_COPY, copy }, + { ACTION_PASTE, paste } }; static construct { @@ -77,6 +79,7 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { application_instance.set_accels_for_action (ACTION_PREFIX + ACTION_CLEAR, {"Escape"}); application_instance.set_accels_for_action (ACTION_PREFIX + ACTION_UNDO, {"z"}); application_instance.set_accels_for_action (ACTION_PREFIX + ACTION_COPY, {"c"}); + application_instance.set_accels_for_action (ACTION_PREFIX + ACTION_PASTE, {"v"}); resizable = false; title = _("Calculator"); @@ -498,7 +501,7 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { } } - public void undo () { + private void undo () { unowned List previous_entry = history.last (); if (previous_entry != null) { entry.set_text (previous_entry.data.exp); @@ -506,7 +509,7 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { } } - public void copy () { + private void copy () { int start, end; entry.get_selection_bounds (out start, out end); var text_selected = end - start != 0; @@ -520,6 +523,33 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow { } } + private void paste () { + get_clipboard_text.begin ((obj, res) => { + var text = get_clipboard_text.end (res); + if (text == null) { + return; + } + + int start, end; + entry.get_selection_bounds (out start, out end); + + var before = entry.text.slice (0, start); + var after = entry.text.slice (end, entry.text_length); + + entry.text = before + text + after; + entry.set_position (before.char_count () + text.char_count ()); + }); + } + + private async string? get_clipboard_text () { + try { + return yield entry.get_clipboard ().read_text_async (null); + } catch (Error e) { + warning (e.message); + return null; + } + } + private void action_insert (SimpleAction action, Variant? variant) { var token = variant.get_string (); int new_position = entry.get_position ();