Shoes,Ruby GUI!

今天逛InfoQ 的时候居然发现了Shoes,一个Ruby GUI 客户端。试了一下,比Ruby/Tk 好用。比如这个Tutorial 里面的例子。假设下面的代码保存在background.rb 里面。

background

 

  1. Shoes.app do  
  2.   background "#F3F".."#F90"  
  3.   title "Shoooes":top => 60,  
  4.     :align => "center",  
  5.     :font => "Trebuchet MS",  
  6.     :stroke => white  
  7. end  

 

安装Shoes 之后,用Shoes 打开这个.rb 文件就可以看到:

可能是由于刚出现的原因,响应还很慢(有人用"两百来行"编了个扫雷,鼠标点下去半天没反应)。但我相信它会引起又一轮Ruby 浪潮。

Shoes 并不是针对严谨的大规模GUI 应用,Shoes 应用一般都很灵巧。Shoes 的目标是让GUI 编程愉快、有趣。

Shoes 有本漫画教程NOBODY KNOWS SHOES,是why 大叔亲自写的。

一个GUI 应该包括显示和响应两方面元素。

Shoes 有10种元素用于显示:

  • para

para 是paragraph 的简写。你不必给它任何坐标或者尺寸参数。它会自动填充它所在的box。但可以为文本指定格式。比如:strong() 就是加粗,em() 是强调等。paragraph

 

  1. Shoes.app do  
  2.   para "Testing test test. ",  
  3.   strong("Breadsticks. "),  
  4.   em("Breadsticks. "),  
  5.   code("Breadsticks. "),  
  6.   strong(ins("Very good."))  
  7. end  

  

还可以指定字体大小:title 什么的或者直接指定(如:para "Oh, to fling and be flung.", :size => 48)title

 

  1. Shoes.app do  
  2.   title "Title/n"  
  3.   subtitle "Subtitle/n"  
  4.   tagline "Tagline/n"  
  5.   caption "Caption/n"  
  6.   para "Para/n"  
  7.   inscription "Inscription/n"  
  8. end  
  • stacks & flows

stack 和flow 就是Shoes 里面的布局管理器。stack 把部件垂直摞在一起。flow 也是一种容器,只不过它在水平方向上摆放部件。如果一行摆不下了,它会另起一行。主窗口本身也是一个flow。flow 和stack 组合可以形成不同的布局。如果你是一个Web 开发者,你会发现它们很像CSS布局引擎。也不奇怪,Shoes 的作者why the lucky stiff说他深受Web 开发的影响。比如:后面你会看到的Shoes 的一种虚拟部件link。why the lucky stiff

 

  1. Shoes.app do  
  2.   @o = oval :top => 0, :left => 0,  
  3.   :radius => 40  
  4.   stack :margin => 40 do  
  5.     title "Dancing With a Circle"  
  6.     subtitle "How graceful and round."  
  7.   end  
  8.   motion do |x, y|  
  9.     @o.move width - x, height - y  
  10.   end  
  11. end  

当鼠标移动时,motion 会拿到你鼠标的坐标重新绘制圆。

  • button

一旦你点击button,就会激活它之后的block。

button

 

  1. Shoes.app {  
  2.   button("Trurl?") {  
  3.     alert("Klapaucius!")  
  4.   }  
  5. }  

还可以这么写:

 

  1. Shoes.app {  
  2.   @b = button("Trurl?")  
  3.   @b.click { alert("Klapaucius!") }  
  4. }  

button 还有个方法focus()。如果用户敲了回车,按钮就会按下(手册上这么说,但我还没试成)。

  • image

图片可以是PNG, JPEG 或者GIF;可以从本地文件系统,也可以从Web 上载入。还可以通过:height 和:width调整图片大小。image

 

  1. Shoes.app do  
  2.   image("f:/Ruby.jpg":click=>"http://google.com")  
  3. end  
  • edit-line

text 把敲进编辑框的内容当作字符串返回。

  • edit-line

 

  1. Shoes.app do  
  2.   @e = edit_line :width => 400  
  3.   button "O.K." do  
  4.     alert @e.text  
  5.   end  
  6. end  

 

如果是多行,可以用edit-box。edit-box

 

  1. Shoes.app do  
  2.   @e = edit_box "Would that I...":width => 400, :height => 240  
  3.   button "O.K." do  
  4.     alert @e.text  
  5.   end  
  6. end  
  • linklink

 

  1. Shoes.app do  
  2.   para(link("Google":click=>"http://google.com"))  
  3. end  
  • background

一幅背景包括三个要素:颜色、明暗变化和图片。你可以用内建的颜色,也可以用rgb 方法。查看这里

  • url

 

  1. class BookList < Shoes  
  2.   url '/':index  
  3.   url '/twain':twain  
  4.   url '/kv':vonnegut  
  5.   def index  
  6.     para "Books I've read: ",  
  7.       link("by Mark Twain/n":click => "/twain"),  
  8.       link("by Kurt Vonnegut/n":click => "/kv")  
  9.   end  
  10.   def twain  
  11.     para "Just Huck Finn./n",  
  12.       link("Go back.":click => "/")  
  13.   end  
  14.   def vonnegut  
  15.     para "Cat's Cradle. Sirens of Titan. ",  
  16.       "Breakfast of Champions./n",  
  17.       link("Go back.":click => "/")  
  18.   end  
  19. end  
  20. Shoes.app :width => 400, :height => 500  

url 还可以是正则表达式:

 

  1. class Dictionary < Shoes  
  2.   url '/':index  
  3.   url '/(/w+)':word  
  4.   def index  
  5.     stack do  
  6.       title "Enter a Word"  
  7.       @word = edit_line  
  8.       button "OK" do  
  9.         visit "/#{@word.text}"  
  10.       end  
  11.     end  
  12.   end  
  13.   def word(string)  
  14.     stack do  
  15.       para "No definition found for #{string}. ",  
  16.         "Sorry this doesn't actually work."  
  17.     end  
  18.   end  
  19. end  
  20. Shoes.app  
  • clearclear

 

  1. Shoes.app {  
  2.   @poem = stack {  
  3.     para "   
  4.     My eyes have blinked again  
  5.     And I have just realized  
  6.     This upright world   
  7.     I have been in.  
  8.       
  9.     My eyelids wipe  
  10.     My eyes hundreds of times  
  11.     Reseting and renovating  
  12.     The scenery."  
  13.   }  
  14.   para(link("Clear").  
  15.     click { @poem.clear })  
  16. }  

 

此外,Shoes 还提供了click,animate等。clock

 

  1. Shoes.app :width => 200, :height => 120 do  
  2.   @seconds = 0  
  3.   @paused = false  
  4.   def display_time  
  5.     @display.clear do  
  6.       title "%02d:%02d:%02d" % [  
  7.         @seconds / (60*60),  
  8.         @seconds / 60 % 60,  
  9.         @seconds % 60  
  10.       ], :stroke => @paused ? gray : black  
  11.     end  
  12.   end  
  13.   @display = stack :margin => 10  
  14.   display_time  
  15.   button "Pause":width => '50%' do  
  16.     @paused = !@paused  
  17.     display_time  
  18.   end  
  19.     
  20.   button "Reset":width => '50%' do  
  21.     @seconds = 0  
  22.     display_time  
  23.   end  
  24.   animate(1) do  
  25.     @seconds += 1 unless @paused  
  26.     display_time  
  27.   end  
  28. end  

如果你感兴趣,应该去Shoebox 逛逛,N多牛人啊!

原文地址:https://www.cnblogs.com/zhouwenwu/p/2352553.html