# Execute UI.messagebox when a tool is deactivated or on 'onReturn'

**URL:** <https://forums.sketchup.com/t/execute-ui-messagebox-when-a-tool-is-deactivated-or-on-onreturn/67591>\
**Category:** Ruby API\
**Tags:** tools\
**Created:** [May 1, 2018, 6:25am UTC](https://forums.sketchup.com/t/execute-ui-messagebox-when-a-tool-is-deactivated-or-on-onreturn/67591 "2018-05-01T06:25:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rojj](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@Rojj](https://forums.sketchup.com/u/Rojj)\
**Post date:** [May 1, 2018, 6:25am UTC](https://forums.sketchup.com/t/execute-ui-messagebox-when-a-tool-is-deactivated-or-on-onreturn/67591/1 "2018-05-01T06:25:40Z")

</div>

I have developed a `tool` that allows users to select a few surfaces and then perform some calculations on them.

When the user either deactivate the tool or hits Return, I would like to show a message with the results of these calculations.

```
tool = RG::Aoi::PickSurface.new
Sketchup.active_model.select_tool(tool)
UI.messagebox(tool.results)

```

The tool is defined as follows

```
module RG::Aoi
  class PickSurface
    attr_reader :results

    def activate
    .....
    end
    
   other events
 end
end

```

The problem that I have is that `UI.messagebox` is executed **immediately** after the tool activation and the tool cannot be used until I click OK. This means that `tool.results` is of course `nil`. The tool is activated within a callback from `webdialog` if that counts.

I guess I could save the results of the calculation in a file and read the file later, but this would require the user to perform another action.

Is there a better solution? Can tools be used in this way?

---

<div class="post-metadata">

**Author:** ![Aerilius](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Aerilius](https://forums.sketchup.com/u/Aerilius)\
**Post date:** [May 1, 2018, 7:27am UTC](https://forums.sketchup.com/t/execute-ui-messagebox-when-a-tool-is-deactivated-or-on-onreturn/67591/2 "2018-05-01T07:27:10Z")

</div>

The first example is synchronous, that means a line of code **blocks** execution until it is finished and only then the next line can be executed.

However the lower example is asynchronous. A tool does not freeze SketchUp, instead SketchUp calls event handlers whenever an event happens, and inbetween the tool does nothing.

Selecting a tool waits until the tool is marked as selected, it does not wait until the tool is deselected. What you need to do is to pass to the tool instance an (optional) callback that the tool will call in its deactivate event handler.

```auto
class MyTool
  def initialize(&callback)
    @callback = callback # Store it in an instance variable for later use.
  end
  def deactivate(view)
    @callback.call(@results) if @callback # You have saved results in an instance variable in another method. If no callback given, it may be nil.
  end
end

MyTool.new{ |results|
  UI.messagebox(results.inspect)
}

```

---

<div class="post-metadata">

**Author:** ![eneroth3](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/eneroth3/32/1222_2.png) [@eneroth3](https://forums.sketchup.com/u/eneroth3)\
**Post date:** [May 1, 2018, 7:27am UTC](https://forums.sketchup.com/t/execute-ui-messagebox-when-a-tool-is-deactivated-or-on-onreturn/67591/3 "2018-05-01T07:27:48Z")

</div>

Why a custom tool? Why not just perform the action on whatever was selected using the select tool?

---

<div class="post-metadata">

**Author:** ![Rojj](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@Rojj](https://forums.sketchup.com/u/Rojj)\
**Post date:** [May 1, 2018, 8:21am UTC](https://forums.sketchup.com/t/execute-ui-messagebox-when-a-tool-is-deactivated-or-on-onreturn/67591/4 "2018-05-01T08:21:45Z")

</div>

> [@eneroth3](#):
>
> Why a custom tool? Why not just perform the action on whatever was selected using the select tool?

Because the calculations are handled by separate classes and modules and I would like to keep them separate. The purpose of the tool is just to extract data from some surfaces selected by the user. This is then passed to the ‘calculator’.

---

<div class="post-metadata">

**Author:** ![Aerilius](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Aerilius](https://forums.sketchup.com/u/Aerilius)\
**Post date:** [May 1, 2018, 8:43am UTC](https://forums.sketchup.com/t/execute-ui-messagebox-when-a-tool-is-deactivated-or-on-onreturn/67591/5 "2018-05-01T08:43:39Z")

</div>

That does not exclude the possibility of keeping aspects/responsibilities separated. Your technical code should be given e.g. entities or attributes, and you could have UI code that cares about extracting these from the current user selection. I agree, selection is better not directly used in technical code.

In any case don’t try to rebuild something that already exists (if your tool really does just selection), because you will loose focus on what is important and a self-made Select Tool will likely never be as powerful and well-tested (bugfree) as SketchUp’s native tool.

---

<div class="post-metadata">

**Author:** ![Rojj](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@Rojj](https://forums.sketchup.com/u/Rojj)\
**Post date:** [May 1, 2018, 6:22pm UTC](https://forums.sketchup.com/t/execute-ui-messagebox-when-a-tool-is-deactivated-or-on-onreturn/67591/6 "2018-05-01T18:22:19Z")

</div>

It works. Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sketchup/original/3X/f/2/f2a121500b085ed60806607a82720b092d800bb3.png) [@system](https://forums.sketchup.com/u/system)\
**Post date:** [August 1, 2018, 10:22am UTC](https://forums.sketchup.com/t/execute-ui-messagebox-when-a-tool-is-deactivated-or-on-onreturn/67591/7 "2018-08-01T10:22:26Z")

</div>

Closed on August 1, 2018, 10:22am UTC

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.
