15 Replies Latest reply on Jul 8, 2009 3:26 PM by jsteinme

    Closing dialogs

    john beardsworth Adobe Community Professional & MVP

      Can't quite figure out how to get stopModalWithResult working.

       

      I'm calling a dialog using MainDialog = LrDialogs.presentModalDialog.... This dialog has a number of buttons which "do something" and it would be cool if they also dismissed the dialog. So I'm including a stopModalWithResult in the button's action. However, the dialog isn't closing.

       

      f:push_button {
        title = 'do owt',
        enabled = true,
        action = function()
         DoSomething(editEditor1.value)
         LrDialogs.stopModalWithResult( MainDialog, true )                                              
        end,
      },

       

       

       

      I suspect it's a matter of getting the correct name for the dialog. I've tried a range from MainDialog, 'MainDialog '.

       

      Anyone done this? Clues?

       

      John

        • 1. Re: Closing dialogs
          jsteinme

          The second parameter to stopModalWithResult() needs to be a string and not the value true. Try replacing true with string like "foo". Hopefully that should fix this issue.

          • 2. Re: Closing dialogs
            john beardsworth Adobe Community Professional & MVP

            Hm, still no go. This is a cut down test script (attached or via link)

             

            John

            • 3. Re: Closing dialogs
              DawMatt Level 3

              Hi,

               

              Haven't used this function before, but the reference doco on the first parameter is:

              (LrView) The dialog to dismiss, or any element of that dialog.

               

              So try changing the first parameter to c (your LrView local variable) rather than a text string.  Failing that try aligning your first parameter with the dialog title, or the context's name, but these last two suggestions are unlikely to work because they don't align with the reference doco.

               

              The current value ('MainDialog') doesn't match up with anything suitable.  The local variable MainDialog will hold a string when it eventually gets set (after the stopModalwithResult does its thing), so couldn't help identify the dialog to close.

               

              Matt

              • 4. Re: Closing dialogs
                john beardsworth Adobe Community Professional & MVP

                Thanks Matt

                 

                I had read the same passage (seems to make sense now) and had tried 'c', c. 'f', f. I've tried them all.... As the second parameter I've tried things like 'ok'. Man, this one has me stumped.

                 

                John

                • 5. Re: Closing dialogs
                  Mark J M Wilson

                  I think Jeffrey uses it in his geotagging plug-in (there is a check-box option to close the dialog when you press the main button), so may be worth dropping him an email.

                  • 6. Re: Closing dialogs
                    john beardsworth Adobe Community Professional & MVP

                    Thanks to Jeffrey. Hopefully you will love this one as much as I do....

                     

                    Apparently clairvoyance is helpful:

                     

                          action = function(magic_sauce)
                              LrDialogs.stopModalWithResult(magic_sauce, 'FOO' )
                          end,

                     

                    In this snippet, this would set local MainDialog to 'FOO' and closes the dialog.

                     

                    I think this really emphasises a point I've made before, that for every documented function, specimen code needs to be available.

                     

                    John

                    • 7. Re: Closing dialogs
                      Mark J M Wilson Level 1

                      Thanks for sharing that John.  The second parameter behaves as I expected from the documentation (i.e. it sets the return value for the dialog, just as if you pressed the typical OK or Cancel buttons) - it was the first parameter that didn't make any sense to me.  In your example, where do you get the magic_sauce to pass it to the function?

                      • 8. Re: Closing dialogs
                        john beardsworth Adobe Community Professional & MVP

                        That's the clairvoyance, as mediated by Gypsy Jeffrey. It can be anything you want.

                         

                        John

                        • 9. Re: Closing dialogs
                          Mark J M Wilson Level 1

                          Oh...of course.  Why didn't I realise that from the docs?

                          • 10. Re: Closing dialogs
                            jsteinme Level 1

                            Sorry, I did not notice until you posted the whole code snippet that it looks like MainDialog is probably nil since it looks like it is declared after its use as an up value. You could move the definition up:

                             

                            LrFunctionContext.callWithContext("MyDialog", function(context)
                                 
                            local f = LrView.osFactory()
                            
                            local MainDialog
                            
                            local c = f:column {                                                       
                                 f:push_button {
                                         title = 'I should close the dialog too!',
                                      action = function()
                                           LrDialogs.message( MainDialog, 'The button is doing something....' )     
                                           LrDialogs.stopModalWithResult( MainDialog, 'ok' )     
                                      end,
                                 },                                                                           
                            }
                                 
                            MainDialog = LrDialogs.presentModalDialog(
                                 {
                                      contents = c
                                 }
                            )
                            
                            end) -- end main function
                            
                            (Hopefully that formatting works)
                            Or you could also do as suggested by using the first parameter to the action() method which is a reference to the button LrView. This is only briefly mentioned in the docs.
                            The first parameter to stopModalWithResult() can be the dialog or any view within the dialog.

                            • 11. Re: Closing dialogs
                              john beardsworth Adobe Community Professional & MVP

                              You sure about that? I wouldn't be surprised if it is lurking there in an unsuspected form, but AFAICS stopModalWithResult isn't even mentioned in the SDK PDF and in only one spot in the API reference HTML content.

                               

                              Equally, declaring MainDialog early doesn't seem to work. I still have to put the first parameter in the function.

                               

                              But I've got what I need working now (and had my whinge) !

                               

                              John

                              • 12. Re: Closing dialogs
                                Mark J M Wilson Level 1

                                Not sure I follow that - I thought presentModalDialog returned the value of the button pressed (e.g. "ok").  How does defining MainDialog earlier in the function make it work as the LrView dialog to pass to stopModalWithResult?

                                 

                                jsteinme wrote:

                                 

                                The first parameter to stopModalWithResult() can be the dialog or any view within the dialog.

                                 

                                Have you got any examples of how to pass a dialog or other view within the dialog?  I have tried many different things and failed to get anything to work - the only thing I have seen working is the method passed on by John above.

                                • 13. Re: Closing dialogs
                                  jsteinme Level 1

                                  Sorry, you are correct. Our internal versions of these act a little different. You could use the upvalue c instead of MainDialog but using the first parameter to action is probably the most flexible.

                                   

                                  The current versions of the docs in the internal source tree mention button as the first parameter to the action method, but that does not mean it is clear in any way. There should be some documentation improvements coming with the next version.

                                   

                                  Glad things are working now.

                                  • 14. Re: Closing dialogs
                                    DawMatt Level 3

                                    jsteinme wrote:

                                     

                                    You could use the upvalue c instead of MainDialog but using the first parameter to action is probably the most flexible.

                                    The more I think about it, the more I think Jeffrey's magic_sauce is the only way to deal with this issue.

                                     

                                    Given in lua you can do things like:

                                    a, b = b, a

                                    to swap values between variables, I'm pretty sure John's c variable will still be nil if we use that as the input to the stopDialog... function.  That is because it is being accessed as part of setting the c variable.

                                     

                                    magic_sauce is guaranteed to be set with a valid, non-nil value at the time the button is pressed.  So you will have a correct reference to an element in the correct LrView dialog.  I can't really think of any other way to do that, short of explicitly setting one of the elements as a separate variable (e.g. set d = a static_text element) then reference d as an entry in table c.  That seems a bit of a kludge.  magic_sauce seems more natural way to achieve this.

                                     

                                    So I think "correct answer" needs to go to Jeffrey.  How to we assign those forum points to someone in their absence?

                                    • 15. Re: Closing dialogs
                                      jsteinme Level 1

                                      Sorry, in case it is not clear I am a member of the Lightroom team. So I can say that by looking at the internal source code that the first parameter can be either the dialog or a view in the dialog. I would recommend changing the name of the action parameter to 'button' from 'secret_sauce'.

                                       

                                      For 'c' to be a valid up value you would probably need to declare it earlier on but that is academic. I agree that the better answer is to use the button parameter.

                                       

                                      Taking a quick look through LR source reveals that that is what we use most commonly as well.