Bspwm presel and resize current window

Hi. Is it possible to presel current window (Crtl+Alt+1-9) and resize this window to preselected size?

Without spawning a window in the leftover space?

Yes but balancing another to fill leftover space.

That doesn't seem to be feasible, and in many layouts it would be pretty much nonsensical.

Are you sure you want to preselected the window as a step of this process? Wouldn't it be better to just resize the window to a percentage of its current size with ctrl+alt+1-9? That could be scripted. You could also preselect the window like that, resize it to that size and then cancel the presel, but that doesn't really make sense from usage perspective. You could add a delay to use it animation of course...

The script would take numbers 1-9 as arguments. 1=10% and so on. It would detect the direction of the resize from internal bspwm data and resize the window in relation to its brother node. You could detect window dimensions with wmutils, xdotool or xwininfo. Determine the resize amount in pixels with simple calculation, probably use bc because bash doesn't do floats and speed is not very important (it isn't executed multiple times per second). The resize with bspc command.

I mean the same behavior as super+mouse2 but for keyboard.

Is there something wrong with super+ctrl+direction?

No but it's a slow a little.

I mean it exactly as you said. But I don't know how to resize window after ctrl+alt+1-9

It takes some scripting.

One option would be to do as I suggested earlier. Here is another option:

  1. bspc subscribe node_presel

  2. Preselect the window like normal

    • bspc subscribe command gives the presel percentage
  3. Resize the window to percentage provided by the subscription

  4. Stop subscription

However, if you don't like scripting, there are easier options too. The resize script in the sxhkdrc can be sped up in two ways:

  1. Give it a bigger pixel count as an argument. By default bspwm-resize.sh resizes windows by 10 pixels per tick. To resize in bigger increments, you could put this in your sxhkdrc:
super + ctrl + {a,s,w,d}
	bspwm_resize.sh {west,south,north,east} 20
  1. Don't use that script, use bspc directly. That script provides an abstraction that let's you resize tiled, pseudo tiled and floating windows with one keybinding. But if you only care about tiled windows, you can get more speed.
1 Like

Thanks for your answers. I will try to make script doing this. For now I need to read bspwm manpage :slight_smile:

It's super rewarding when it succeeds :slightly_smiling_face:

Another option came to mind: make a separate keybinding for resize action. You can get the presel direction and ratio with this command:

bspc query -T -n focused

{"id":20971525,"splitType":"vertical","splitRatio":0.520000,"birthRotation":0,"vacant":false,"hidden":false,"sticky":false,"private":false,"locked":false,"marked":false,"presel":{"splitDir":"east","splitRatio":0.200000},"rectangle":{"x":783,"y":25,"width":1147,"height":1065},"constraints":{"min_width":32,"min_height":32},"firstChild":null,"secondChild"0

You would need to parse that output somehow, jshon is probably a good option:

bspc query -T -n focused | jshon -e presel
{
 "splitDir": "north",
 "splitRatio": 0.52000000000000002
}

So to get direction and ratio:

dir=$(bspc query -T -n focused | jshon -e presel | jshon -e splitDir)
ratio=$(bspc query -T -n focused | jshon -e presel | jshon -e splitRatio)
1 Like

Can you please review my script https://pastebin.com/iQXJtuQ8. For me it works exactly what I want (binded to Ctrl+alt+return).

I can't run using bash because
./resize.sh: line 9: 1248-1248*0.40000000000000002: syntax error: invalid arithmetic operator (error token is ".40000000000000002")

bash does not handle floats, so any time your arithmetic expansion returns someting like 10.4 or 0.4, you should get an error... Oh wait, you are using zsh too...

Here is my current script, very much work in progress:

#!/bin/zsh

# If the focused window is not preselected, exit
[[ "$(bspc query -T -n focused | jshon -e presel)" == null ]] && exit 0

# Focused window is preselected

dir=$(bspc query -T -n focused | jshon -e presel -e splitDir | tr -d \")
ratio=$(bspc query -T -n focused | jshon -e presel -e splitRatio)

# In what direction should the resize happen?

case $dir in
"north")
    dimension=$(bspc query -T -n focused | jshon -e client -e tiledRectangle -e height)
    # How many pixels?
    reduction=$(( ratio * dimension ))
    # round the reduction
    reduction=$( printf "%.0f" $reduction )
    bspc node focused -z top 0 $reduction
    ;;
"south")
    dimension=$(bspc query -T -n focused | jshon -e client -e tiledRectangle -e height)
    # How many pixels?
    reduction=$(( ratio * dimension ))
    # round the reduction
    reduction=$( printf "%.0f" $reduction )
    bspc node focused -z bottom 0 -$reduction
    ;;
"east")
    dimension=$(bspc query -T -n focused | jshon -e client -e tiledRectangle -e width)
    reduction=$(( ratio * dimension ))
    # round the reduction
    reduction=$( printf "%.0f" $reduction )
    bspc node focused -z right 0 -$reduction
    ;;
*)
    dimension=$(bspc query -T -n focused | jshon -e client -e tiledRectangle -e width)
    reduction=$(( ratio * dimension ))
    # round the reduction
    reduction=$( printf "%.0f" $reduction )
    bspc node focused -z left 0 $reduction
    ;;
esac

# cancel the presel

It seems that you arrived at very similar conclusion :smile:

it seems that bspc node @north -r is more suitable for this than bspc node -z. I'll make a few tweaks to your script...

#!/bin/zsh

# If the focused window is not preselected, exit
[[ "$(bspc query -T -n focused | jshon -e presel)" == null ]] && exit 0

dir=$(bspc query -T -n focused | jshon -e presel -e splitDir | tr -d \")
ratio=$(bspc query -T -n focused | jshon -e presel -e splitRatio)
 
case "$dir" in
        west|east)
        cur=$(bspc query -T -n focused | jshon -e rectangle | jshon -e width)
        bspc node @east -r -$((${cur}-${cur}*${ratio})) || bspc node @west -r +$((${cur}-${cur}*${ratio}))
        ;;
 
        north|south)
        cur=$(bspc query -T -n focused | jshon -e rectangle | jshon -e height)
        bspc node @south -r -$((${cur}-${cur}*${ratio})) || bspc node @north -r +$((${cur}-${cur}*${ratio}))
esac
 
cancel_presels

Now it exits cleanly if there is no presel and is tiny bit more efficient with json query. Very well done!

Do you mind if I include this in bspwm-scripts?

1 Like

I just did the same tweaks :smiley:

I completely agree!

I wonder if we can further improve it to work with receptacles in addition to preselections...

I have almost finished the receptacle support. I also noticed some little bugs, I'll see if I get them fixed too.

Good to hear it! I'll test it when you are done.

Forum kindly sponsored by