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

Rework control shift press handling - Gtk4 prep #788

Closed
wants to merge 2 commits into from

Conversation

jeremypw
Copy link
Collaborator

@jeremypw jeremypw commented Sep 8, 2024

Extracted from #780

Clarify how this works by having explicit booleans control-pressed and shift-pressed.

Note: In Gtk4 the work around of explicitly passing events to the key controller is not needed but must be kept here.

The copy-last-output' seems to work the same as in master`

Comment on lines +151 to +152
private bool control_pressed = false;
private bool shift_pressed = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like these are only used inside the key press handler. Why are we setting these variables outside of that function at all?

Copy link
Collaborator Author

@jeremypw jeremypw Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because they need to maintain their state between key presses when multiple keys are held down sequentially.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what you mean. Testing locally, replacing all of this with checking for CONTROL and SHIFT mask in modifiers seems to work as expected. Can you provide steps where it doesn't work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's what I'm testing

diff --git a/src/Widgets/TerminalWidget.vala b/src/Widgets/TerminalWidget.vala
index cc779d30..eca5e5c4 100644
--- a/src/Widgets/TerminalWidget.vala
+++ b/src/Widgets/TerminalWidget.vala
@@ -148,8 +148,6 @@ namespace Terminal {
         private Gtk.GestureMultiPress primary_gesture;
         private Gtk.GestureMultiPress secondary_gesture;
 
-        private bool control_pressed = false;
-        private bool shift_pressed = false;
         private double scroll_delta = 0.0;
 
         public signal void cwd_changed (string cwd);
@@ -196,7 +194,7 @@ namespace Terminal {
                 propagation_phase = TARGET
             };
             key_controller.key_pressed.connect (key_pressed);
-            key_controller.key_released.connect (key_released);
+            key_controller.key_released.connect (() => scroll_controller.flags = NONE);
             key_controller.focus_out.connect (() => scroll_controller.flags = NONE);
 
             primary_gesture = new Gtk.GestureMultiPress (this) {
@@ -337,13 +335,9 @@ namespace Terminal {
             switch (keyval) {
                 case Gdk.Key.Control_R:
                 case Gdk.Key.Control_L:
-                    scroll_controller.flags = VERTICAL;
-                    control_pressed = true;
-                    break;
                 case Gdk.Key.Shift_R:
                 case Gdk.Key.Shift_L:
                     scroll_controller.flags = VERTICAL;
-                    shift_pressed = true;
                     break;
                 case Gdk.Key.Alt_L:
                 case Gdk.Key.Alt_R:
@@ -381,7 +375,7 @@ namespace Terminal {
                     popup_context_menu (rect);
                     break;
                 default:
-                    if (!(control_pressed || shift_pressed) || !(Gtk.accelerator_get_default_mod_mask () in modifiers)) {
+                    if (!(CONTROL_MASK in modifiers || SHIFT_MASK in modifiers) || !(Gtk.accelerator_get_default_mod_mask () in modifiers)) {
                         remember_command_start_position ();
                     }
                     break;
@@ -403,11 +397,11 @@ namespace Terminal {
                 return false;
             }
 
-            if (control_pressed && Application.settings.get_boolean ("natural-copy-paste")) {
+            if (CONTROL_MASK in modifiers && Application.settings.get_boolean ("natural-copy-paste")) {
                 if (match_keycode (Gdk.Key.c, keycode)) {
                     if (get_has_selection ()) {
                         copy_clipboard ();
-                        if (!shift_pressed) { // Shift not pressed
+                        if (!(SHIFT_MASK in modifiers)) { // Shift not pressed
                             unselect_all ();
                         }
                         return true;
@@ -427,23 +421,6 @@ namespace Terminal {
             return false;
         }
 
-        private void key_released (uint keyval, uint keycode, Gdk.ModifierType modifiers) {
-            switch (keyval) {
-                case Gdk.Key.Control_R:
-                case Gdk.Key.Control_L:
-                    control_pressed = false;
-                    break;
-                case Gdk.Key.Shift_R:
-                case Gdk.Key.Shift_L:
-                    shift_pressed = true;
-                    break;
-                default:
-                    break;
-            }
-
-            scroll_controller.flags = NONE;
-        }
-
         private void setup_menu () {
             // Update the "Paste" menu option
             clipboard.request_targets ((clipboard, atoms) => {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This had to be done in the Gtk4 version because

Gtk4 seems to still have a bug in the "state" parameter of the key controller signals in that if multiple modifiers are held down this parameter only includes one of them so we still have to track the state ourselves.

I'll check again whether this fix is needed.

@jeremypw
Copy link
Collaborator Author

Confirmed that the changes made in the Gtk4 branch are not needed here and not worth backporting so closing. Sorry for the confusion.

@jeremypw jeremypw closed this Sep 12, 2024
@jeremypw jeremypw deleted the jeremypw/rework-controlshifthandling branch September 12, 2024 10:10
@jeremypw
Copy link
Collaborator Author

Looks like I misdiagnosed the problem with the Gtk4 port - we do not need to maintain control_pressed and shift_pressed variables there either. But we do have to handle the native mode of copy-paste as well. Just leaving it to the Vte.Terminal like we do here does not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants