Skip to content

Commit 3199825

Browse files
feat(eventbox-keypress-command): First implementation
1 parent 593a4f4 commit 3199825

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ All notable changes to eww will be listed here, starting at changes since versio
3232
- Add keyboard support for button presses (By: julianschuler)
3333
- Support empty string for safe access operator (By: ModProg)
3434
- Add `log` function calls to simplexpr (By: topongo)
35+
- Add support for `:keypress` for eventbox (By: AlexandrePicavet)
3536

3637
## [0.6.0] (21.04.2024)
3738

crates/eww/src/widgets/widget_definitions.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,16 @@ fn build_gtk_event_box(bargs: &mut BuilderArgs) -> Result<gtk::EventBox> {
952952
}
953953
glib::Propagation::Proceed
954954
}));
955-
}
955+
},
956+
// @prop keypress - The key to press followed by the separator '|>' then followed by the
957+
// command to execute.
958+
// @prop timeout - timeout of the command. Default "200ms"
959+
prop(
960+
timeout: as_duration = Duration::from_millis(200),
961+
keypress: as_string
962+
) {
963+
on_keypress(gtk_widget.as_ref(), timeout, keypress);
964+
},
956965
});
957966
Ok(gtk_widget)
958967
}
@@ -1398,3 +1407,39 @@ fn connect_first_map<W: IsA<gtk::Widget>, F: Fn(&W) + 'static>(widget: &W, func:
13981407
}
13991408
}));
14001409
}
1410+
1411+
const KEYPRESS_COMMAND_SEPARATOR: &str = "|>";
1412+
fn on_keypress(gtk_widget: &gtk::Widget, timeout: Duration, keypress: String) -> () {
1413+
let parsed_keypress = keypress
1414+
.split_once(KEYPRESS_COMMAND_SEPARATOR)
1415+
.map(|(key_name, command)| (key_name.trim().to_owned(), command.trim().to_owned()));
1416+
1417+
match parsed_keypress {
1418+
Some((wanted_key_name, command)) => handle_keypress(gtk_widget, timeout, wanted_key_name, command),
1419+
None => log::error!(
1420+
"The 'keypress' property must be in the format: '[KEY] {} [COMMAND]', but got: {}",
1421+
KEYPRESS_COMMAND_SEPARATOR,
1422+
keypress
1423+
),
1424+
};
1425+
}
1426+
1427+
fn handle_keypress(gtk_widget: &gtk::Widget, timeout: Duration, wanted_key_name: String, command: String) -> () {
1428+
connect_signal_handler!(
1429+
gtk_widget,
1430+
gtk_widget.connect_key_press_event(move |gtk_widget, event_key| {
1431+
match event_key.keyval().name().map(|pressed_key_name| pressed_key_name.to_string()) {
1432+
Some(pressed_key_name) => {
1433+
log::debug!("Key '{}' pressed on widget: {}", pressed_key_name, gtk_widget.path());
1434+
1435+
if wanted_key_name == pressed_key_name {
1436+
run_command(timeout, &command, &[] as &[&str]);
1437+
};
1438+
}
1439+
None => (),
1440+
};
1441+
1442+
glib::Propagation::Proceed
1443+
})
1444+
);
1445+
}

0 commit comments

Comments
 (0)