Skip to content

Commit 973b555

Browse files
author
Fixerer
committed
Add mouse action and binds instead
1 parent 872f8d0 commit 973b555

File tree

4 files changed

+120
-32
lines changed

4 files changed

+120
-32
lines changed

anyrun/src/config.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ pub struct Config {
4040
#[config_args(skip)]
4141
#[serde(default = "Config::default_keybinds")]
4242
pub keybinds: Vec<Keybind>,
43+
44+
#[config_args(skip)]
45+
#[serde(default = "Config::default_mousebinds")]
46+
pub mousebinds: Vec<Mousebind>,
4347
}
4448
impl Config {
4549
fn default_x() -> RelativeNum {
@@ -103,6 +107,19 @@ impl Config {
103107
},
104108
]
105109
}
110+
111+
fn default_mousebinds() -> Vec<Mousebind> {
112+
vec![
113+
Mousebind {
114+
button: MouseButton::Primary,
115+
action: Action::Select,
116+
},
117+
Mousebind {
118+
button: MouseButton::Secondary,
119+
action: Action::Nop,
120+
},
121+
]
122+
}
106123
}
107124
impl Default for Config {
108125
fn default() -> Self {
@@ -121,6 +138,7 @@ impl Default for Config {
121138
layer: Self::default_layer(),
122139
keyboard_mode: Self::default_keyboard_mode(),
123140
keybinds: Self::default_keybinds(),
141+
mousebinds: Self::default_mousebinds(),
124142
}
125143
}
126144
}
@@ -173,6 +191,21 @@ pub enum Action {
173191
Select,
174192
Up,
175193
Down,
194+
Nop,
195+
}
196+
197+
#[derive(Deserialize, Debug, Clone, Copy, PartialEq)]
198+
pub enum MouseButton {
199+
Primary,
200+
Secondary,
201+
Middle,
202+
Unknown,
203+
}
204+
205+
#[derive(Deserialize, Debug, Clone, Copy)]
206+
pub struct Mousebind {
207+
pub button: MouseButton,
208+
pub action: Action,
176209
}
177210

178211
#[derive(Deserialize, Clone)]

anyrun/src/main.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use relm4::prelude::*;
1616
use wl_clipboard_rs::copy;
1717

1818
use crate::{
19-
config::{Action, Config, ConfigArgs, Keybind},
19+
config::{Action, Config, ConfigArgs, Keybind, Mousebind},
2020
plugin_box::{PluginBox, PluginBoxInput, PluginBoxOutput, PluginMatch},
2121
};
2222

@@ -106,6 +106,22 @@ impl App {
106106
(i, plugin, plugin_match)
107107
})
108108
}
109+
110+
fn select_row(&self, plugin_ind: DynamicIndex, match_ind: DynamicIndex) {
111+
// Select match row
112+
for (i, plugin) in self.plugins.iter().enumerate() {
113+
if i == plugin_ind.current_index() {
114+
for (j, plugin_match) in plugin.matches.iter().enumerate() {
115+
if j == match_ind.current_index() {
116+
plugin
117+
.matches
118+
.widget()
119+
.select_row(Option::<&gtk::ListBoxRow>::Some(&plugin_match.row));
120+
}
121+
}
122+
}
123+
}
124+
}
109125
}
110126

111127
#[relm4::component]
@@ -377,6 +393,7 @@ impl Component for App {
377393
}
378394
}
379395
AppMsg::Action(action) => match action {
396+
Action::Nop => {}
380397
Action::Close => {
381398
root.close();
382399
relm4::main_application().quit();
@@ -472,8 +489,25 @@ impl Component for App {
472489
}
473490
}
474491
}
475-
AppMsg::PluginOutput(PluginBoxOutput::MatchClicked) => {
476-
sender.input(AppMsg::Action(Action::Select));
492+
AppMsg::PluginOutput(PluginBoxOutput::MouseAction(button, match_ind, plugin_ind)) => {
493+
// Handle binding
494+
if let Some(Mousebind { action, .. }) = self
495+
.config
496+
.mousebinds
497+
.iter()
498+
.find(|mousebind| mousebind.button == button)
499+
{
500+
// Potentially select row
501+
match action {
502+
Action::Select | Action::Nop => self.select_row(plugin_ind, match_ind),
503+
_ => {
504+
// Don't select row for other actions
505+
}
506+
};
507+
508+
// Perform action
509+
sender.input(AppMsg::Action(*action));
510+
}
477511
}
478512
}
479513
self.update_view(widgets, sender);

anyrun/src/plugin_box.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use gtk::{glib, pango, prelude::*};
66
use gtk4 as gtk;
77
use relm4::prelude::*;
88

9-
use crate::Config;
9+
use crate::{config::MouseButton, Config};
1010

1111
pub struct PluginMatch {
1212
pub content: Match,
@@ -16,7 +16,7 @@ pub struct PluginMatch {
1616

1717
#[derive(Debug)]
1818
pub enum MatchOutput {
19-
Clicked,
19+
MouseAction(MouseButton, <PluginMatch as FactoryComponent>::Index),
2020
}
2121

2222
#[relm4::factory(pub)]
@@ -31,15 +31,18 @@ impl FactoryComponent for PluginMatch {
3131
set_css_classes: &["match"],
3232
set_height_request: 32,
3333
gtk::Box {
34+
3435
add_controller = gtk::GestureClick {
35-
connect_pressed[sender, root] => move |gesture, _, _, _| {
36-
if let Some(binding) = root.parent() {
37-
if let Some(list_box) = binding.downcast_ref::<gtk::ListBox>() {
38-
list_box.select_row(Some(&root));
39-
}
40-
}
36+
set_button: 0,
37+
connect_pressed[sender, index] => move |gesture, _, _, _| {
4138
gesture.set_state(gtk::EventSequenceState::Claimed);
42-
sender.output(MatchOutput::Clicked).unwrap();
39+
let button: MouseButton = match gesture.current_button() {
40+
gtk::gdk::BUTTON_PRIMARY => MouseButton::Primary,
41+
gtk::gdk::BUTTON_SECONDARY => MouseButton::Secondary,
42+
gtk::gdk::BUTTON_MIDDLE => MouseButton::Middle,
43+
_ => MouseButton::Unknown,
44+
};
45+
sender.output(MatchOutput::MouseAction(button, index.clone())).unwrap();
4346
}
4447
},
4548

@@ -91,7 +94,7 @@ impl FactoryComponent for PluginMatch {
9194

9295
fn init_widgets(
9396
&mut self,
94-
_index: &Self::Index,
97+
index: &Self::Index,
9598
root: Self::Root,
9699
_returned_widget: &<Self::ParentWidget as relm4::factory::FactoryView>::ReturnedWidget,
97100
sender: FactorySender<Self>,
@@ -157,7 +160,11 @@ pub enum PluginBoxInput {
157160
pub enum PluginBoxOutput {
158161
MatchesLoaded,
159162
RowSelected(<PluginBox as FactoryComponent>::Index),
160-
MatchClicked,
163+
MouseAction(
164+
MouseButton,
165+
<PluginMatch as FactoryComponent>::Index,
166+
<PluginBox as FactoryComponent>::Index,
167+
),
161168
}
162169

163170
#[relm4::factory(pub)]
@@ -230,13 +237,16 @@ impl FactoryComponent for PluginBox {
230237

231238
fn init_model(
232239
(plugin, config): Self::Init,
233-
_index: &Self::Index,
240+
index: &Self::Index,
234241
sender: FactorySender<Self>,
235242
) -> Self {
243+
let ind = index.clone();
236244
let matches = FactoryVecDeque::<PluginMatch>::builder()
237245
.launch(gtk::ListBox::default())
238-
.forward(sender.output_sender(), |output| match output {
239-
MatchOutput::Clicked => PluginBoxOutput::MatchClicked,
246+
.forward(sender.output_sender(), move |output| match output {
247+
MatchOutput::MouseAction(button, row) => {
248+
PluginBoxOutput::MouseAction(button, row, ind.clone())
249+
}
240250
});
241251

242252
Self {

examples/config.ron

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Config(
22
// Position/size fields use an enum for the value, it can be either:
33
// Absolute(n): The absolute value in pixels
44
// Fraction(n): A fraction of the width or height of the full screen (depends on exclusive zones and the settings related to them) window respectively
5-
5+
66
// The horizontal position, adjusted so that Relative(0.5) always centers the runner
77
x: Fraction(0.5),
88

@@ -15,18 +15,18 @@ Config(
1515
// The minimum height of the runner, the runner will expand to fit all the entries
1616
// NOTE: If this is set to 0, the window will never shrink after being expanded
1717
height: Absolute(1),
18-
19-
// Hide match and plugin info icons
20-
hide_icons: false,
2118

22-
// ignore exclusive zones, f.e. Waybar
23-
ignore_exclusive_zones: false,
19+
// Hide match and plugin info icons
20+
hide_icons: false,
21+
22+
// ignore exclusive zones, f.e. Waybar
23+
ignore_exclusive_zones: false,
24+
25+
// Layer shell layer: Background, Bottom, Top, Overlay
26+
layer: Overlay,
2427

25-
// Layer shell layer: Background, Bottom, Top, Overlay
26-
layer: Overlay,
27-
2828
// Hide the plugin info panel
29-
hide_plugin_info: false,
29+
hide_plugin_info: false,
3030

3131
// Close window when a click outside the main box is received
3232
close_on_click: false,
@@ -36,7 +36,7 @@ Config(
3636

3737
// Limit amount of entries shown in total
3838
max_entries: None,
39-
39+
4040
// List of plugins to be loaded by default, can be specified with a relative path to be loaded from the
4141
// `<anyrun config dir>/plugins` directory or with an absolute path to just load the file the path points to.
4242
//
@@ -52,19 +52,30 @@ Config(
5252
keybinds: [
5353
Keybind(
5454
key: "Return",
55-
action: Select,
55+
action: Select,
5656
),
5757
Keybind(
5858
key: "Up",
59-
action: Up,
59+
action: Up,
6060
),
6161
Keybind(
6262
key: "Down",
63-
action: Down,
63+
action: Down,
6464
),
6565
Keybind(
6666
key: "Escape",
67-
action: Close,
67+
action: Close,
68+
),
69+
],
70+
71+
mousebinds: [
72+
Mousebind(
73+
button: Primary,
74+
action: Select,
75+
),
76+
Mousebind(
77+
button: Secondary,
78+
action: Nop,
6879
),
6980
],
7081
)

0 commit comments

Comments
 (0)