TimUntersberger / nog

A tiling window manager for Windows
MIT License
696 stars 20 forks source link

Keybinding type for custom callback #126

Closed TimUntersberger closed 4 years ago

TimUntersberger commented 4 years ago

Introduce a new keybinding type that enables the user to bind a key combination to a custom function.

Example

fn f(){
  print("Hello World");
}

bind "<key-combo>" callback(f);
ramirezmike commented 4 years ago

I was going to create this issue today. You might want to include some examples in the documentation of how to do this with rhai. I wrote something like this

let swap_value = false;
fn swap_split() {
   if (this) {
     split("Vertical");
   } else {
     split("Horizontal");
   }
   this = !this;
}

bind "Alt+T" swap_value.swap_split();

But then realized that isn't implemented.. I think that would be how to write something like that in rhai, if there's a simpler way let me know, but yeah: being able to write a function in the config that switches the behavior of keybinding so in this case the user can convert the two swap() bindings into one toggle binding.

TimUntersberger commented 4 years ago

The way you want to use the keybindings won't work. The bind "Alt+T" swap_value.swap_split(); gets called once at the beginning when parsing the file and then gets ignored. That is why we need the callback(f) this tells us that this is a function that needs to be executed everytime.

ramirezmike commented 4 years ago

I understand the problem you're describing where the function gets called when the script loads rather than set to call on each key bind, but is there a way to set it up so it parses the function and realizes it's referencing a function in the scope of the config script as opposed to one you have in the engine?

the callback( ) would work for sure I'm just curious if that's actually necessary

TimUntersberger commented 4 years ago

I understand the problem you're describing where the function gets called when the script loads rather than set to call on each key bind, but is there a way to set it up so it parses the function and realizes it's referencing a function in the scope of the config script as opposed to one you have in the engine

This could be possible, but I am not sure whether I'm a fan of that idea.

ramirezmike commented 4 years ago

That's fair, I could see a user getting confused if they copy a line from another config that calls a custom function vs a built in one and not immediately know why one works vs the other.

TimUntersberger commented 4 years ago

This is now working in the popup branch, i just have to push it.

Example

bind "Alt+T" callback(||{
    popup_new(#{
        text: ["Hello World"],
        padding: 5
    });
});