Skip to content

thbar/magic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Magic is a gem making it easier to develop UI:
- Windows Forms, WPF/Silverlight with IronRuby
- Swing with JRuby

USAGE

Here are quick starters (some of these are under samples/):

Windows Forms

form = Magic.build do
  @menu = main_menu do
    menu_item("&File") do
      menu_item("&New")
      menu_item("&Quit").click { Application.Exit }
      menu_item("&Other Quit", :click => lambda { Application.Exit })
    end
  end
  form(:text => "Title", :menu => @menu) do
    flow_layout_panel(:dock => :fill) do
      button(:text => "Click me!").click do
        MessageBox.Show("Hello from button!")
      end
    end
  end
end

Application.Run(form)

WPF

window = Magic.build do
  window(:width => 600, :height => 480, :title => 'Hello world!') do
    stack_panel(:margin => thickness(30)) do
      button(:content => 'Click me!', :font_size => 22).click do
        MessageBox.show("Ok!")
      end
    end
  end
end

app = Application.new
app.run(window)

Silverlight

You’ll need to run “rake compress” to create magic-compressed.rb, which gathers all the magic files into one. Once you have magic-compressed.rb, you can use it like that:

require "silverlight"
require "magic-compressed"

class App < SilverlightApplication

  def initialize
    application.root_visual = Magic.build do
      stack_panel do
        10.times { |i| text_block(:text => i.to_s, :width => 30,:height => 30) }
      end
    end
  end
end

$app = App.new

It makes it easy to create XAML-free applications, or to reduce the amount of XAML to be created.

Swing

Preliminary support for Swing has just been added. Here’s an example:

import 'javax.swing.JFrame'
import 'javax.swing.JButton'

frame = Magic.build do
  JFrame do
    title 'Hello!'
    size 400,500
    JButton('Press me') do |b|
      b.addActionListener do
        b.setText 'Pressed!'
      end
    end
  end
end

frame.set_default_close_operation(JFrame::EXIT_ON_CLOSE)
frame.show

UNDER THE COVER

A few points (see spec/magic_spec.rb for details):

  • classes to be built are inferred from method calls (converted from snake_case to CamelCase)
    • menu_item creates an instance of MenuItem
    • button becomes Button
    • flow_layout_panel becomes FlowLayoutPanel
  • if a method call matches an existing method of the parent, the method is called
  • the instanciated control is passed as an optional param to the block
  • method calls automatically add the object to its parent children collection (if the object is a Control, a MenuItem or a UIElement – this will become configurable)
  • if the parent responds to :content (eg: WPF Window), then parent.content will use the (unique) child
  • if a Hash is passed, corresponding properties are set after instanciation (:text => “This is the text”)
  • if the property is an enum, snake_case symbol value is allowed (:dock => :fill is the same as :dock => DockStyle.Fill)
  • handlers are registered if a lambda is passed (:click => lambda)
  • handlers can also be registered by calling the block directly (button.lambda do … end)
  • instance variables can be reused (form.menu = @menu)
  • non-control (like BackgroundWorker for long-running operations) instances can be created (worker = background_worker)
  • the last evaluted expression is returned.

TESTING

Magic comes with its set of specs (based on MSpec). You’ll currently have to tweak Rakefile (see spec task) to match your environment.

Then you can run (either on Windows or Mac OS):

rake spec

to see all the (m)specs running.

Note that I currently use a few nasty mocks (see mocks.rb) that I’ll need to clean-up.

TODO

  • remove obsolete .Net flagged enum support (IronRuby now supports X | Y)
  • split the core into toolkit-specific builders

COPYRIGHT

Copyright © 2008 Thibaut Barrère. See LICENSE for details.

About

IronRuby sugar for WPF, Silverlight and Windows Forms.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages