Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy entry content on Ctrl+C #244

Merged
merged 5 commits into from
Mar 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow {
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 ActionEntry[] ACTION_ENTRIES = {
{ ACTION_INSERT, action_insert, "s"},
{ ACTION_FUNCTION, action_function, "s"},
{ ACTION_CLEAR, action_clear },
{ ACTION_UNDO, undo }
{ ACTION_UNDO, undo },
{ ACTION_COPY, copy }
};

static construct {
Expand All @@ -74,6 +76,7 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow {
var application_instance = (Gtk.Application) GLib.Application.get_default ();
application_instance.set_accels_for_action (ACTION_PREFIX + ACTION_CLEAR, {"Escape"});
application_instance.set_accels_for_action (ACTION_PREFIX + ACTION_UNDO, {"<Control>z"});
application_instance.set_accels_for_action (ACTION_PREFIX + ACTION_COPY, {"<Control>c"});

resizable = false;
title = _("Calculator");
Expand Down Expand Up @@ -503,6 +506,20 @@ public class PantheonCalculator.MainWindow : Gtk.ApplicationWindow {
}
}

public void copy () {
int start, end;
entry.get_selection_bounds (out start, out end);
var text_selected = end - start != 0;

// we have to copy text in both cases
// because seems like application action blocks entry's action
if (!text_selected) {
entry.get_clipboard ().set_text (entry.text);
} else {
entry.get_clipboard ().set_text (entry.text.slice (start, end));
}
}

private void action_insert (SimpleAction action, Variant? variant) {
var token = variant.get_string ();
int new_position = entry.get_position ();
Expand Down