Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thbar/magic
IronRuby sugar for WPF, Silverlight and Windows Forms.
https://github.com/thbar/magic
Last synced: 2 months ago
JSON representation
IronRuby sugar for WPF, Silverlight and Windows Forms.
- Host: GitHub
- URL: https://github.com/thbar/magic
- Owner: thbar
- License: mit
- Created: 2009-03-01T21:08:10.000Z (almost 16 years ago)
- Default Branch: master
- Last Pushed: 2010-04-17T08:53:14.000Z (over 14 years ago)
- Last Synced: 2024-05-01T22:41:36.382Z (8 months ago)
- Language: Ruby
- Homepage:
- Size: 266 KB
- Stars: 19
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.textile
- License: LICENSE
Awesome Lists containing this project
README
Magic is a gem making it easier to develop UI:
- Windows Forms, WPF/Silverlight with IronRuby
- Swing with JRubyh1. USAGE
Here are quick starters (some of these are under samples/):
h2. 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
endApplication.Run(form)
h2. 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
endapp = Application.new
app.run(window)h2. 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.
h2. 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
endframe.set_default_close_operation(JFrame::EXIT_ON_CLOSE)
frame.showh1. 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.h1. 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.
h1. TODO
* remove obsolete .Net flagged enum support (IronRuby now supports X | Y)
* split the core into toolkit-specific buildersh1. COPYRIGHT
Copyright (c) 2008 Thibaut Barrère. See LICENSE for details.