The Green Shoes Manual 1.1.362

The App Object

An App is a single window running code at a URL. When you switch URLs, a new App object is created and filled up with stacks, flows and other Green Shoes elements.

The App is the window itself. Which may be closed or cleared and filled with new elements.

The App itself, in slot/box terminology, is a flow. See the Slots section for more, but this just means that any elements placed directly at the top-level will flow.

Starts up a Green Shoes app window. This is the starting place for making a Green Shoes program. Inside the block, you fill the window with various Green Shoes elements (buttons, artwork, etc.) and, outside the block, you use the styles to describe how big the window is. Perhaps also the name of the app.

 Shoes.app title: "White Circle",
   width: 200, height: 200 do
   background black
   fill white
   oval top: 20, left: 20, radius: 160
 end

In the case above, a small window is built. 200 pixels by 200 pixels. And, inside the window, two elements: a black background and a white circle.

Once an app is created, it is added to the Shoes.APPS list. If you want an app to spawn more windows, see the window method and the dialog method.

Shoes.APPS() » An array of Shoes::App objects

Builds a complete list of all the Green Shoes apps that are open right now. Once an app is closed, it is removed from the list. Yes, you can run many apps at once in Green Shoes. It's completely encouraged.

 Shoes.app do
   button('Open a new app'){Shoes.app{}}
   button('Print Shoes.APPS'){p Shoes.APPS}
 end
clipboard() » a string

Returns a string containing all of the text that's on the system clipboard. This is the global clipboard that every program on the computer cuts and pastes into.

Stores a string of text in the system clipboard.

Closes the app window. If multiple windows are open and you want to close the entire application, use the built-in method exit.

 Shoes.app do
  para 'hello'
  button 'spawn' do
   Shoes.app do
    para 'hello'
    button('close: close this window only'){close}
    button('exit: quit Green Shoes'){exit}
   end
  end
  button('close: close this window only'){close}
  button('exit: quit Green Shoes'){exit}
 end

Starts a download thread (much like XMLHttpRequest, if you're familiar with JavaScript.) This method returns immediately and runs the download in the background. Each download thread also fires start, progress and finish events. You can send the download to a file or just get back a string (in the finish event.)

If you attach a block to a download, it'll get called as the finish event.

 Shoes.app do
   stack do
     title "Searching Google", size: 16
     @status = para "One moment..."
     download "http://is.gd/bXTVY7" do |goog|
       @status.text = 
         "Headers: #{goog.meta["date"]}"
     end
   end
 end

This example is truly the simplest form of download: pulling some web data down into memory and handling it once it's done.

Another simple use of download is to save some web data to a file, using the :save style.

 Shoes.app do
   stack do
     title "Downloading Google image", size: 16
     @status = para "One moment..."
     download "http://is.gd/GVAGF7",
       :save => "nasa50th.gif" do
	@status.text = "Okay, is downloaded."
	image "nasa50th.gif", top: 100
     end
   end
 end

If you need to send certain headers or actions to the web server, you can use the :method, :headers and :body styles to customize the HTTP request. (And, if you need to go beyond these, you can always break out Ruby's OpenURI class.)

 # Not yet available
 Shoes.app do
   stack do
     title "GET Google", size: 16
     @status = para "One moment..."
     download "http://is.gd/bXTVY7", 
         :method => "GET" do |dump|
       @status.text = dump.response.body
     end
   end
 end

As you can see from the above example, Shoes makes use of the "GET" method to query google's search engine.

Note: Green Shoes doesn't support the :method, :headers and :body styles.

 include Hpricot
 Shoes.app do
   status = para "One moment..."
   download 'http://is.gd/BatiRt' do |dl|
     samples = []
     Hpricot(dl).inner_text.each_line do |line|
       samples.push($1) if line =~ /(sample.*\.rb)/
     end
     status.text = samples.join(', ')
     flush
   end
 end

As you can see from the above example, Green Shoes includes the Hpricot library for parsing HTML.

Note: Windows platform only so far.

location() » a string

Gets a string containing the URL of the current app.

mouse() » an array of numbers: button, left, top

Identifies the mouse cursor's location, along with which button is being pressed.

 Shoes.app do
   @p = para
   animate do
     button, left, top = self.mouse
     @p.replace "mouse: #{button}, #{left}, #{top}"
   end
 end
owner() » Shoes::App

Gets the app which launched this app. In most cases, this will be nil. But if this app was launched using the window method, the owner will be the app which called window.

started?() » true or false

Has the window been fully constructed and displayed? This is useful for threaded code which may try to use the window before it is completely built. (Also see the start event which fires once the window is open.)

Note: Green Shoes doesn't support started? method so far.

Changes the location, in order to view a different Shoes URL.

Absolute URLs (such as http://google.com) are okay, but Green Shoes will be expecting a Green Shoes application to be at that address. (So, google.com won't work, as it's an HTML app.)

Next: The Styles Master List