Browse Source

room-history: Add key bindings to open context menu of reactions

pipelines/786320
Kévin Commaille 1 year ago
parent
commit
7e40957a5e
No known key found for this signature in database
GPG Key ID: C971D9DBC9D678D
  1. 13
      src/components/context_menu_bin.rs
  2. 7
      src/session/view/content/room_history/message_row/reaction/mod.rs
  3. 20
      src/utils/key_bindings.rs

13
src/components/context_menu_bin.rs

@ -1,7 +1,7 @@
use adw::subclass::prelude::*;
use gtk::{gdk, glib, glib::clone, prelude::*, CompositeTemplate};
use crate::utils::BoundObject;
use crate::utils::{key_bindings, BoundObject};
mod imp {
use std::cell::{Cell, RefCell};
@ -62,16 +62,7 @@ mod imp {
klass.install_action("context-menu.activate", None, |obj, _, _| {
obj.open_menu_at(0, 0);
});
klass.add_binding_action(
gdk::Key::F10,
gdk::ModifierType::SHIFT_MASK,
"context-menu.activate",
);
klass.add_binding_action(
gdk::Key::Menu,
gdk::ModifierType::empty(),
"context-menu.activate",
);
key_bindings::add_context_menu_bindings(klass, "context-menu.activate");
klass.install_action("context-menu.close", None, |obj, _, _| {
if let Some(popover) = obj.popover() {

7
src/session/view/content/room_history/message_row/reaction/mod.rs

@ -11,7 +11,7 @@ use crate::{
model::{Member, MemberList, ReactionData, ReactionGroup},
view::content::room_history::member_timestamp::MemberTimestamp,
},
utils::{BoundObjectWeakRef, EMOJI_REGEX},
utils::{key_bindings, BoundObjectWeakRef, EMOJI_REGEX},
};
mod imp {
@ -69,6 +69,11 @@ mod imp {
fn class_init(klass: &mut Self::Class) {
Self::bind_template(klass);
Self::bind_template_callbacks(klass);
klass.install_action("reaction.show-popover", None, |obj, _, _| {
obj.imp().show_popover();
});
key_bindings::add_context_menu_bindings(klass, "reaction.show-popover");
}
fn instance_init(obj: &InitializingObject<Self>) {

20
src/utils/key_bindings.rs

@ -4,7 +4,7 @@ use gtk::{gdk, subclass::prelude::*};
/// List of keys that activate a widget.
// Copied from GtkButton's source code.
pub(crate) const ACTIVATE_KEYS: &[gdk::Key] = &[
const ACTIVATE_KEYS: &[gdk::Key] = &[
gdk::Key::space,
gdk::Key::KP_Space,
gdk::Key::Return,
@ -12,10 +12,24 @@ pub(crate) const ACTIVATE_KEYS: &[gdk::Key] = &[
gdk::Key::KP_Enter,
];
/// Activate the given action when one of the [`ACTIVATE_KEYS`] binding is
/// triggered.
/// Add key bindings to the given class to trigger the given action to activate
/// a widget.
pub(crate) fn add_activate_bindings<T: WidgetClassExt>(klass: &mut T, action: &str) {
for key in ACTIVATE_KEYS {
klass.add_binding_action(*key, gdk::ModifierType::empty(), action);
}
}
/// List of key and modifier combos that trigger a context menu to appear.
const CONTEXT_MENU_BINDINGS: &[(gdk::Key, gdk::ModifierType)] = &[
(gdk::Key::F10, gdk::ModifierType::SHIFT_MASK),
(gdk::Key::Menu, gdk::ModifierType::empty()),
];
/// Add key bindings to the given class to trigger the given action to show a
/// context menu.
pub(crate) fn add_context_menu_bindings<T: WidgetClassExt>(klass: &mut T, action: &str) {
for (key, modifier) in CONTEXT_MENU_BINDINGS {
klass.add_binding_action(*key, *modifier, action);
}
}

Loading…
Cancel
Save