Updating form field contents multiple times with only one button press

1,016 views
Skip to first unread message

Glutanimate

unread,
May 26, 2014, 7:09:32 AM5/26/14
to yad-c...@googlegroups.com
I am in the process of writing a script that assigns unique properties to multiple items. Right now I am figuring out how to iterate over all items with yad. 

Originally I planned to write a yad form dialog with a 'next' and 'previous' button that launches a new interface each time the item is switched. While writing this implementation I discovered the 'BTN' field that allows updating the contents of a form dialog without launching a new one. This, I think, would be a far more elegant way to process multiple items.

What I am trying to figure out right now, is how to make the fields update several times with only one button press. This is essential to my script because it takes quite a while for the backend to process the next item in line and pass the data to the interface. Ideally I would like to replace the fields of my form dialog with a placeholder while the backend process is doing its work - e.g. 'loading' - and replace it with the actual values as soon as they become available.

I wrote a minimal working example to make this clearer:

#! /bin/bash

# YAD form/btn example
# update form field contents via button 

function on_click () {
    for FIELDNR in {1..3}; do
      echo "$FIELDNR:Loading next item..."
    done
    sleep 2
    for FIELDNR in {1..3}; do
      echo "$FIELDNR:Updated Field $FIELDNR"
    done
}
export -f on_click

yad \
--form \
--field="Field 1:" \
--field="Field 2:" \
--field="Field 3:" \
--field "Next:BTN" \
"Field 1 content" \
"Field 2 content" \
"Field 3 content" \
"@bash -c on_click"

The "slow backend process" is represented by 'sleep' in this example. 

Unfortunately this example doesn't work the way I would like it to. Instead of going the way 'original contents' → 'Loading next item...' (for 2 seconds) → 'updated contents', the dialog jumps right to the updated contents after 2 seconds.

I was wondering if there was any way to change this behaviour. If not, is there any chance you could implement something like this into the next release of yad?  (only if you have the time and you deem this a worthy addition, of course)

Anyway, thanks for reading this!

-- Glutanimate
Message has been deleted

Johnny Rosenberg

unread,
May 26, 2014, 3:42:45 PM5/26/14
to yad-c...@googlegroups.com
Thank YOU for writing this! I didn't know you could do something like this with Yad! I tried to do it with GTK-dialog, but I failed even there (though it should be possible)…
Unfortunately I can't answer your question, just wanted to thank you for answering a question that I thought about a lot but just never asked… :P


Johnny Rosenberg 

Victor Ananjevsky

unread,
May 27, 2014, 12:57:09 PM5/27/14
to yad-c...@googlegroups.com
В Mon, 26 May 2014 04:09:32 -0700 (PDT)
Glutanimate <gluta...@gmail.com> писал:

> What I am trying to figure out right now, is how to make the fields update
> several times with only one button press.

sorry, i can't see the way how you can do this now. you can change a field
values several times, but you cannot get a user's changed values between
them.

i think about using dbus or something for doing such things, but
currently there are no progress in this way, sorry

maybe a notebook dialog with classical ipc between child dialogues will
help or be more useful for you


--
Victor Ananjevsky <anan...@gmail.com>

Victor Ananjevsky

unread,
May 27, 2014, 11:47:25 PM5/27/14
to yad-c...@googlegroups.com
On Mon, 26 May 2014 07:41:24 -0700 (PDT)
Glutanimate <gluta...@gmail.com> wrote:

> Somewhat unrelated to my first post but I just discovered an issue with the
> BTN field.
>
> This script works:
>
> #! /bin/bash
>
> yad \
> --form \
> --field="Field 1:" \
> --field="Field 2:" \
> --field="Field 3:" \
> --field "Next:FBTN" \
> "Field 1 content" \
> "Field 2 content" \
> "Field 3 content" \
> 'echo "%1 %2 %3"'
>
> But this one doesn't:
>
> #! /bin/bash
>
> function on_click () {
> echo $0
> echo $1
> echo $2
> echo $3
> }
> export -f on_click
>
> yad \
> --form \
> --field="Field 1:" \
> --field="Field 2:" \
> --field="Field 3:" \
> --field "Next:FBTN" \
> "Field 1 content" \
> "Field 2 content" \
> "Field 3 content" \
> 'bash -c on_click "%1" "%2" "%3"'

bash -c gets only one string as argument.
so, it must be 'bash -c "on_click %1 %2 %3"'

>
> It seems as though only the first argument gets passed to on_click(). I get
> the same results if I replace 'bash -c on_click "%1" "%2" "%3"' with 'bash
> -c on_click a b c'.
>
> I am not quite sure if I am doing something wrong or if this is a bug.
>
> -- Glutanimate
>
> --
> You received this message because you are subscribed to the Google Groups "yad-common" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to yad-common+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


--
wbr,
Victor Ananjevsky <anan...@gmail.com>

Glutanimate

unread,
May 31, 2014, 6:21:27 PM5/31/14
to yad-c...@googlegroups.com

On Monday, May 26, 2014 9:42:45 PM UTC+2, Johnny Rosenberg wrote:

Thank YOU for writing this! I didn't know you could do something like this with Yad! I tried to do it with GTK-dialog, but I failed even there (though it should be possible)…
Unfortunately I can't answer your question, just wanted to thank you for answering a question that I thought about a lot but just never asked… :P


Johnny Rosenberg 

Haha, no problem. yad is always good for a surprise. I feel like I've only started to explore all the possible options. It really is a marvelous piece of software. 

Glutanimate

unread,
May 31, 2014, 7:07:46 PM5/31/14
to yad-c...@googlegroups.com
Thank you very much for the response!

On Tuesday, May 27, 2014 6:57:09 PM UTC+2, Victor Ananjevsky wrote:
В Mon, 26 May 2014 04:09:32 -0700 (PDT) 
Glutanimate <gluta...@gmail.com> писал: 
sorry, i can't see the way how you can do this now. you can change a field 
values several times, but you cannot get a user's changed values between 
them. 
i think about using dbus or something for doing such things, but 
currently there are no progress in this way, sorry 
maybe a notebook dialog with classical ipc between child dialogues will 
help or be more useful for you 


-- 
Victor Ananjevsky <anan...@gmail.com

Ok, absolutely no problem. I've settled for using different dialogs for each item and showing a progress bar while the backend loads the data for the next item. It's a good solution, I think, and works pretty well so far. A tabbed dialog probably wouldn't be the right choice in this case because I am going through dozens of files and need a dialog for each. Thanks for the suggestion, though! I am sure I will find a good use for the notebook dialog in the future.

Now that I have the chance, may I ask for a somewhat related feature? One downside to having multiple dialogs spawn one after another in one workflow is that their position gets reset each time. I know that I can set the window geometry or simply set dialogs to spawn in the screen center but sometimes you just want the layout to be more dynamic. 

It really would be nice if there was an option to retain the window position across all yad instances spawned from the same script. I could think of a global variable like YAD_GEOMETRY as a possible solution to this.

I did manage to achieve persistent geometry by monitoring yad in the background, but I fear it's not very elegant and probably quite resource-intensive:

    #!/bin/bash

    WMCLASS="YadTestClass"
    TMPDIR="/run/shm"
    GEOMETRYOPTS="--geometry=960x1020+1366+26"
    GEOMETRYPIPE="$(mktemp -u --tmpdir="$TMPDIR" ${0##*/}.XXXXXXXX)"

    trap "rm "$GEOMETRYPIPE"; exit" EXIT

    gui_form(){
        YADOPTS="$(yad --form $GEOMETRYOPTS --field=text --class="$WMCLASS")"
    }

    gui_monitor_geometry(){
        sleep 2
        while true; do
          GEOMETRY="$(xwininfo -id $(wmctrl -lx | grep "$WMCLASS" | cut -d " " -f1 ) 2> /dev/null \
          | sed -n 's/  -geometry //p')"
          if [[ -z "$GEOMETRY" ]]
            then
                break
            else
                echo "--geometry=$GEOMETRY" > "$GEOMETRYPIPE"
          fi
          sleep 1
        done
    }


    get_geo&
    gui_form
    GEOMETRYOPTS="$(cat "$GEOMETRYPIPE")"
    echo "geo: $GEOMETRYOPTS"
    echo "opts: $YADOPTS"
    gui_form

I'd really love to see something like integrated into yad. It certainly would make working with multiple consecutive much easier.


On Wednesday, May 28, 2014 5:47:25 AM UTC+2, Victor Ananjevsky wrote:

bash -c  gets only one string as argument. 
so, it must be 'bash -c "on_click %1 %2 %3"' 

Thank you! That was it. Just for posterity's sake: If the fields contain whitespace you can quote the arguments by escaping the double quotes, e.g.: 

    'bash -c "on_click \"%1\" \"%2\" \"%3\""'

One thing, though: I am having a bit of trouble replicating this with normal variables. Take this code for instance:

    #! /bin/bash

    function on_click () {
        echo "$0"
        echo "$1"
        echo "$2"
    }
    export -f on_click

    Var1="a b"
    Var2="c d"

    bash -c "on_click \"$Var1\" \"$Var2\""

    yad \
    --form \
    --field="Field 1:" \
    --field="Field 2:" \
    --field="Field 3:" \
    --field "Next:FBTN" \
    "Field 1 content" \
    "Field 2 content" \
    "Field 3 content" \
    "bash -c "on_click \"$Var1\" \"$Var2\"""

The first bash call works perfectly fine, the second one doesn't. If I replace the latter with "bash -c 'on_click $Var1 $Var2'" it does work, but the whitespace isn't handled correctly and the wrong variables are passed on.

I guess my main question is: Is there any way to pass on arguments with whitespace/other special characters?

Thank you very much in advance!

-- Glutanimate
Message has been deleted
Message has been deleted

Glutanimate

unread,
Jun 1, 2014, 5:12:23 AM6/1/14
to yad-c...@googlegroups.com
For some reason most of my replies are getting deleted. I think they might be getting stuck in a spam filter. I would appreciate it if a moderator could look into that.

Thank you

-- Glutanimate

Johnny Rosenberg

unread,
Jun 1, 2014, 12:36:13 PM6/1/14
to yad-c...@googlegroups.com
There are still a few things I need to figure out before being able to do what I want to do, though.

When a certain button is pressed (”Add”), I want the currently selected lines in four combo boxes to be added as a new line in a (multi line) text field in the same dialogue. But I guess I need to open a new thread for that.


Johnny Rosenberg
Reply all
Reply to author
Forward
Message has been deleted
0 new messages