This may be the craziest code I've ever written.
The challenge: Place a thin, moveable line on the screen. Use as simple and little code as possible.
Such a line was requested here and I can see the usefulness when superimposed on line charts. I occasionally could use them myself for easier alignment of windows.
What we need for a horizontal line is a window with a colored background, no text, no decoration, 1px high and as wide as the screen.
First idea: urxvt window. Colored background OK, 1px height may be tricky but possible, but unfortunately the borderLess argument is buggy and doesn't work.
Second idea:Yad window. Would not work. Yad's WM_NORMAL_HINTS directive specifies a minimum height of 15px.
Third idea: Gtkdialog window. Bingo!
An orthodox solution would create a temporary file with a SVG line and then load this file into gtkdialog as <pixmap>. Not a big deal, but it requires a tmp file, and I just don't like tmp files.
So here comes the crazy part. This code creates a gtkdialog window with a <text> widget. The text uses pango markup and has a colored background. The text itself contains of only 2 spaces - with an insane amount of letter_spacing, which stretches the text background to roughly 9000px, Now that we have enough width to fill a cinema screen we need to decrease the height to 1px. This is done by decreasing the font size to the lowest possible value. The lowest value is zero, and this even works, but it removes the background (and its color) and leaves a thin 1px gray line (not so bad if we need a gray line ). For a color line the minimum is 1. Font sizes are not measured in pixel and a font size of 1 is not guaranteed to result in a 1px line (in my case it does, but tested in other distros it resulted in a 3px line), but it's still a fairly thin line.
Using busybox ash to increase speed and fun:
Code: Select all
#!/bin/ash
linecolor=red
thickness=1
echo '<window title="'$linecolor'" decorated="false" resizable="false" margin="0" tooltip-text="'$linecolor' line">
<text use-markup="true"><label>"<span letter_spacing='\''9999999'\'' bgcolor='\'$linecolor\'' font='\'$thickness\''> </span>"</label></text>
</window>'| gtkdialog -cs
linecolor can be any valid X11 color name (e.g. red, lavender or PeachPuff ) or a hex triplet (e.g. #A52A2A). In case of invalid value the line defaults to black.
Focussing and subsequentially moving/deleting such a line can be tricky. Two feature can help:
1) The window appears in the taskbar with its color name. Right clicking on the taskbar tab allows to minimize, move and close the window. Here it is also possible to set the layer to "Above" which will keep the line on top of other windows.
2) The window has a tooltip. When the mouse cursor hovers over the line and the tooltip appears it means that a left click will focus the window. The easiest way to move the line: Press Alt key and keep it pressed, then slowly position the mouse and find the "sweet spot" that triggers the tooltip. When the tooltip appears, press the left mouse button and drag the line to the desired position.
Shortcomings: Above code works only for horizontal lines. For vertical lines we would need the SVG <pixmap> approach, which also would allow to set the line height to a precise value in pixel.
Have fun!
[ EDIT ]────────────────────────────────────────────────────────────────
Above code places the line always at the center. Normally that requires a subsequent move to the intended position.
It might be more useful to create the line where you click on the screen.
Change
gtkdialog -cs
to
gtkdialog -sG +0+$(sleep .5; xwininfo > /dev/null; getcurpos | cut -d' ' -f2) &
The script will start with a cross-hair cursor. Position the cursor to the intended line position and click.