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

Improve Event Controllers #181

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/Event Controllers/main.blp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Adw.StatusPage {
}

Gtk.Button ctrl_button {
label: _("Ctrl + Click to Activate");
label: _("Ctrl Key");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the label here should indicate that it represents the Control key state, and probably shouldn't be a button anymore since the signal has been removed.

width-request: 200;
halign: center;
}
Expand Down
32 changes: 14 additions & 18 deletions src/Event Controllers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,17 @@ const secondary_button = workbench.builder.get_object("secondary_button");
stack_picture_1.file = Gio.File.new_for_uri(workbench.resolve("image1.png"));
stack_picture_2.file = Gio.File.new_for_uri(workbench.resolve("image2.png"));

let ctrl_pressed = false;

// Key controller to detect when the Ctrl key is pressed and released
const key_controller = new Gtk.EventControllerKey();
window.add_controller(key_controller);
key_controller.connect("key-pressed", (_self, keyval, _keycode, _state) => {
if (keyval === Gdk.KEY_Control_L || keyval === Gdk.KEY_Control_R) {
ctrl_pressed = true;
ctrl_button.add_css_class("suggested-action");
}
});

key_controller.connect("key-released", (_self, keyval, _keycode, _state) => {
if (keyval === Gdk.KEY_Control_L || keyval === Gdk.KEY_Control_R) {
ctrl_pressed = false;
}
});

ctrl_button.connect("clicked", () => {
if (ctrl_pressed) {
ctrl_button.label = _("Click to Deactivate");
ctrl_button.add_css_class("suggested-action");
} else {
ctrl_button.label = _("Ctrl + Click to Activate");
ctrl_button.remove_css_class("suggested-action");
}
});
Expand All @@ -48,33 +36,41 @@ const gesture_click = new Gtk.GestureClick({ button: 0 });
window.add_controller(gesture_click);

gesture_click.connect("pressed", (_self, _n_press, _x, _y) => {
switch (gesture_click.get_current_button()) {
let css_class = "suggested-action";
if (_self.get_current_event_state() & Gdk.ModifierType.CONTROL_MASK) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The _self variable should have the leading underscore removed, if it's being used now.

css_class = "destructive-action";
}

switch (_self.get_current_button()) {
case Gdk.BUTTON_PRIMARY:
primary_button.add_css_class("suggested-action");
primary_button.add_css_class(css_class);
break;

case Gdk.BUTTON_MIDDLE:
middle_button.add_css_class("suggested-action");
middle_button.add_css_class(css_class);
break;

case Gdk.BUTTON_SECONDARY:
secondary_button.add_css_class("suggested-action");
secondary_button.add_css_class(css_class);
break;
}
});

gesture_click.connect("released", (_self, _n_press, _x, _y) => {
switch (gesture_click.get_current_button()) {
switch (_self.get_current_button()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto for _self

case Gdk.BUTTON_PRIMARY:
primary_button.remove_css_class("suggested-action");
primary_button.remove_css_class("destructive-action");
break;

case Gdk.BUTTON_MIDDLE:
middle_button.remove_css_class("suggested-action");
middle_button.remove_css_class("destructive-action");
break;

case Gdk.BUTTON_SECONDARY:
secondary_button.remove_css_class("suggested-action");
secondary_button.remove_css_class("destructive-action");
break;
}
});
Expand Down
36 changes: 14 additions & 22 deletions src/Event Controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
gi.require_version("Gdk", "4.0")

from gi.repository import Gdk, Gio, Gtk
from gettext import gettext as _
import workbench

window = workbench.window
Expand All @@ -19,52 +18,47 @@
stack_picture_1.set_file(Gio.File.new_for_uri(workbench.resolve("image1.png")))
stack_picture_2.set_file(Gio.File.new_for_uri(workbench.resolve("image2.png")))

ctrl_pressed = False


def on_key_pressed(_self, keyval, _keycode, _state):
if keyval == Gdk.KEY_Control_L or keyval == Gdk.KEY_Control_R:
global ctrl_pressed
ctrl_pressed = True
ctrl_button.add_css_class("suggested-action")


def on_key_released(_self, keyval, _keycode, _state):
if keyval == Gdk.KEY_Control_L or keyval == Gdk.KEY_Control_R:
global ctrl_pressed
ctrl_pressed = False


def on_clicked(_self):
if ctrl_pressed:
ctrl_button.set_label(_("Click to Deactivate"))
ctrl_button.add_css_class("suggested-action")
else:
ctrl_button.set_label(_("Ctrl + Click to Activate"))
ctrl_button.remove_css_class("suggested-action")


def on_pressed(_self, _n_press, _x, _y):
match gesture_click.get_current_button():
if _self.get_current_event_state() & Gdk.ModifierType.CONTROL_MASK:
css_class = "destructive-action"
else:
css_class = "suggested-action"

match _self.get_current_button():
case Gdk.BUTTON_PRIMARY:
primary_button.add_css_class("suggested-action")
primary_button.add_css_class(css_class)

case Gdk.BUTTON_MIDDLE:
middle_button.add_css_class("suggested-action")
middle_button.add_css_class(css_class)

case Gdk.BUTTON_SECONDARY:
secondary_button.add_css_class("suggested-action")
secondary_button.add_css_class(css_class)


def on_released(_self, _n_press, _x, _y):
match gesture_click.get_current_button():
match _self.get_current_button():
case Gdk.BUTTON_PRIMARY:
primary_button.remove_css_class("suggested-action")
primary_button.remove_css_class("destructive-action")

case Gdk.BUTTON_MIDDLE:
middle_button.remove_css_class("suggested-action")
middle_button.remove_css_class("destructive-action")

case Gdk.BUTTON_SECONDARY:
secondary_button.remove_css_class("suggested-action")
secondary_button.remove_css_class("destructive-action")


def on_swipe(_self, vel_x, _vel_y):
Expand All @@ -80,8 +74,6 @@ def on_swipe(_self, vel_x, _vel_y):
key_controller.connect("key-pressed", on_key_pressed)
key_controller.connect("key-released", on_key_released)

ctrl_button.connect("clicked", on_clicked)

# Detect pointer button press and release events
gesture_click = Gtk.GestureClick(button=0)
window.add_controller(gesture_click)
Expand Down
27 changes: 11 additions & 16 deletions src/Event Controllers/main.vala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#! /usr/bin/env -S vala workbench.vala --pkg libadwaita-1

private bool ctrl_pressed = false;

public void main () {
Gtk.Window window = workbench.window;

Expand All @@ -23,23 +21,13 @@ public void main () {

key_controller.key_pressed.connect ((keyval, keycode, state) => {
if (keyval == Gdk.Key.Control_L || keyval == Gdk.Key.Control_R) {
ctrl_pressed = true;
ctrl_button.add_css_class ("suggested-action");
}
return true;
});

key_controller.key_released.connect ((keyval, keycode, state) => {
if (keyval == Gdk.Key.Control_L || keyval == Gdk.Key.Control_R) {
ctrl_pressed = false;
}
});

ctrl_button.clicked.connect (() => {
if (ctrl_pressed) {
ctrl_button.label = "Click to Deactivate";
ctrl_button.add_css_class ("suggested-action");
} else {
ctrl_button.label = "Ctrl + Click to Activate";
ctrl_button.remove_css_class ("suggested-action");
}
});
Expand All @@ -50,15 +38,19 @@ public void main () {
((Gtk.Widget) window).add_controller (gesture_click);

gesture_click.pressed.connect ((gesture, n_press, x, y) => {
var css_class = "suggested-action";
if ((gesture.get_current_event_state () & Gdk.ModifierType.CONTROL_MASK) > 0) {
css_class = "destructive-action";
}
switch (gesture.get_current_button ()) {
case Gdk.BUTTON_PRIMARY:
primary_button.add_css_class ("suggested-action");
primary_button.add_css_class (css_class);
break;
case Gdk.BUTTON_MIDDLE:
middle_button.add_css_class ("suggested-action");
middle_button.add_css_class (css_class);
break;
case Gdk.BUTTON_SECONDARY:
secondary_button.add_css_class ("suggested-action");
secondary_button.add_css_class (css_class);
break;
}
});
Expand All @@ -67,12 +59,15 @@ public void main () {
switch (gesture.get_current_button ()) {
case Gdk.BUTTON_PRIMARY:
primary_button.remove_css_class ("suggested-action");
primary_button.remove_css_class ("destructive-action");
break;
case Gdk.BUTTON_MIDDLE:
middle_button.remove_css_class ("suggested-action");
middle_button.remove_css_class ("destructive-action");
break;
case Gdk.BUTTON_SECONDARY:
secondary_button.remove_css_class ("suggested-action");
secondary_button.remove_css_class ("destructive-action");
break;
}
});
Expand Down
Loading