Skip to content

Commit

Permalink
feat(eventbox-keypress-command): First implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrePicavet committed Jan 15, 2025
1 parent 593a4f4 commit 3199825
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ All notable changes to eww will be listed here, starting at changes since versio
- Add keyboard support for button presses (By: julianschuler)
- Support empty string for safe access operator (By: ModProg)
- Add `log` function calls to simplexpr (By: topongo)
- Add support for `:keypress` for eventbox (By: AlexandrePicavet)

## [0.6.0] (21.04.2024)

Expand Down
47 changes: 46 additions & 1 deletion crates/eww/src/widgets/widget_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,16 @@ fn build_gtk_event_box(bargs: &mut BuilderArgs) -> Result<gtk::EventBox> {
}
glib::Propagation::Proceed
}));
}
},
// @prop keypress - The key to press followed by the separator '|>' then followed by the
// command to execute.
// @prop timeout - timeout of the command. Default "200ms"
prop(
timeout: as_duration = Duration::from_millis(200),
keypress: as_string
) {
on_keypress(gtk_widget.as_ref(), timeout, keypress);
},
});
Ok(gtk_widget)
}
Expand Down Expand Up @@ -1398,3 +1407,39 @@ fn connect_first_map<W: IsA<gtk::Widget>, F: Fn(&W) + 'static>(widget: &W, func:
}
}));
}

const KEYPRESS_COMMAND_SEPARATOR: &str = "|>";
fn on_keypress(gtk_widget: &gtk::Widget, timeout: Duration, keypress: String) -> () {
let parsed_keypress = keypress
.split_once(KEYPRESS_COMMAND_SEPARATOR)
.map(|(key_name, command)| (key_name.trim().to_owned(), command.trim().to_owned()));

match parsed_keypress {
Some((wanted_key_name, command)) => handle_keypress(gtk_widget, timeout, wanted_key_name, command),
None => log::error!(
"The 'keypress' property must be in the format: '[KEY] {} [COMMAND]', but got: {}",
KEYPRESS_COMMAND_SEPARATOR,
keypress
),
};
}

fn handle_keypress(gtk_widget: &gtk::Widget, timeout: Duration, wanted_key_name: String, command: String) -> () {
connect_signal_handler!(
gtk_widget,
gtk_widget.connect_key_press_event(move |gtk_widget, event_key| {
match event_key.keyval().name().map(|pressed_key_name| pressed_key_name.to_string()) {
Some(pressed_key_name) => {
log::debug!("Key '{}' pressed on widget: {}", pressed_key_name, gtk_widget.path());

if wanted_key_name == pressed_key_name {
run_command(timeout, &command, &[] as &[&str]);
};
}
None => (),
};

glib::Propagation::Proceed
})
);
}

0 comments on commit 3199825

Please sign in to comment.