Slide 1

Slide 1 text

Desktop Application using JRuby + SWT 呂旺燊

Slide 2

Slide 2 text

專家講座 C : Desktop Application using JRuby + SWT

Slide 3

Slide 3 text

個人簡介  呂旺燊  Twitter: @tkalu  RoR programmer  任職於和多 handlino.com

Slide 4

Slide 4 text

Fire.app http://fireapp.handlino.com/

Slide 5

Slide 5 text

HTML prototyping tool 網頁原型設計工具 HTML + CSS + JavaScript

Slide 6

Slide 6 text

HTML ● ERB ● HAML ● Slim ● Markdown 支援多種 Template language

Slide 7

Slide 7 text

CSS http://sass-lang.com/ http://compass-style.org/

Slide 8

Slide 8 text

JavaScript http://coffeescript.org/

Slide 9

Slide 9 text

Server Simple Web Server 可以在瀏覽器裏面透過 http://127.0.0.1:24681的網址 瀏覽專案內容 Support LiveReload protocol 當檔案修改之後 , 瀏覽器會自動更新 Ref. ● http://livereload.com/ ● LiveReload Protocol

Slide 10

Slide 10 text

跨平台支援 Linux OS X Windows

Slide 11

Slide 11 text

簡潔的介面 - 1

Slide 12

Slide 12 text

簡潔的介面 - 2

Slide 13

Slide 13 text

為什麼打造 Fire.app 故事要從遇見 Compass 說起了 ......

Slide 14

Slide 14 text

Compass Awesome!!! from http://sonspring.com/journal/sass-for-designers

Slide 15

Slide 15 text

痛恨 command line....

Slide 16

Slide 16 text

需要跨平台

Slide 17

Slide 17 text

需要 GUI - 1 ● Tk ● wxRuby ● qtRuby ● Ruby/GTK ● Shoes ● FXRuby ● MacRuby Ruby GUI Library Cross-Platform

Slide 18

Slide 18 text

Eclipse Java + SWT JRuby + SWT 需要 GUI - 2

Slide 19

Slide 19 text

成果 2010.12 推出 Compass.app 2012.04 推出 Fire.app

Slide 20

Slide 20 text

為什麼選擇 JRuby+SWT? 誰需要 JRuby + SWT ? 或是

Slide 21

Slide 21 text

誰需要 JRuby + SWT ? 1. 熟悉 Ruby 想寫 GUIa 2. 想使用的 Library 只有 Ruby 的版本 3. 很熟 SWT 想改用 Ruby 的語法寫程式

Slide 22

Slide 22 text

回到正題 如何用 JRuby 搭配 SWT 開發程式

Slide 23

Slide 23 text

JRuby 中使用 Java 程式 # 使用 Java 的環境 require 'java' # 載入指定的 jar 檔 require 'path/to/mycode.jar' # 取得目前的 Java 版號 java.lang.System.getProperties["java.runtime. version"] 更多資料請參考 https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby

Slide 24

Slide 24 text

載入 SWT 前往 SWT 官網下載檔案後解壓縮後可以取 得 swt.jar 後 , 透過下列兩行程式碼即可 載入 SWT require "java" require "download/path/swt"

Slide 25

Slide 25 text

Hellow Word! display = org.eclipse.swt.widgets.Display.new shell = org.eclipse.swt.widgets.Shell.new(display) row_layout= org.eclipse.swt.layout.RowLayout.new(org.eclipse.swt.SWT::HO RIZONTAL) shell.setLayout( row_layout) label = org.eclipse.swt.widgets.Label.new(shell, org.eclipse.swt.SWT::HORIZONTAL ) label.setText( 'Hello World!' ) shell.open while(!shell.is_disposed) do display.sleep if(!display.read_and_dispatch) end display.dispose

Slide 26

Slide 26 text

更加的 Ruby 風格 module Swt import org.eclipse.swt.SWT import org.eclipse.swt.program.Program module Widgets import org.eclipse.swt.widgets.Display import org.eclipse.swt.widgets.Label import org.eclipse.swt.widgets.Shell end module Layout import org.eclipse.swt.layout.RowLayout end end

Slide 27

Slide 27 text

Hello World! 加強版 display = Swt::Widgets::Display.get_current shell = Swt::Widgets::Shell.new(display) shell.layout = Swt::Layout::RowLayout.new(Swt::SWT::HORIZONTAL) label = Swt::Widgets::Label.new(shell, Swt::SWT::HORIZONTAL ) label.text = 'Hello World!' shell.open while(!shell.is_disposed) do display.sleep if(!display.read_and_dispatch) end display.dispose

Slide 28

Slide 28 text

其他開發心得 ● SWT 跨平台問題 ● 調整啟動速度 ● 打包程式 ● Mac 上隱藏 Dock 的圖示

Slide 29

Slide 29 text

SWT 跨平台問題 SWT_LIB_PATH ="#{LIB_PATH}/swt" if org.jruby.platform.Platform::IS_MAC os="osx" elsif org.jruby.platform.Platform::IS_LINUX os="linux" elsif org.jruby.platform.Platform::IS_WINDOWS os="win" end if org.jruby.platform.Platform::ARCH =~ /64/ arch="64" else arch="32" end require "#{SWT_LIB_PATH}/swt_#{os}#{arch}"

Slide 30

Slide 30 text

調整啟動速度 1. 不要使用 rubygem 管理 library require "rubygems" 會花上約 0.7 秒 建議自己將需要的 library 的路徑加到 $LOAD_PATH 中 2. 需要使用時才 require library JRuby require 的成本十分龐大 , 建議要用到的時候才進 行 require 的動作 , 而不是程式一啟動就將需要 library 全部載入 其他技巧還可參考 http://headius.blogspot.com/2010/03/jruby-startup-time-tips.html

Slide 31

Slide 31 text

打包程式 使用 Rawr http://github.com/rawr/rawr Rawr 包裝了 launch4j提供 JRuby 程式更加方便的打包方式 安裝 rawr 到系統 : gem install rawr 建立專案 : rawr install {project_path} 打包 Mac 程式: rake rawr:bundle:app 打包 Windows 程式: rake rawr:bundle:exe

Slide 32

Slide 32 text

Mac 上隱藏 Dock 的圖示 在 OSX 中使用 SWT 的程式會預設在 Dock 中顯示 圖示 , 如果想要做出 只顯示在 system tray 上面 的效果 , 需會要修改 Info.plist, 必須在 中 添加下列內容 : NSUIElement 1

Slide 33

Slide 33 text

範例程式 請前往下方 Github 網址下載 https://github.com/tka/jruby-rawr-swt-helloworld

Slide 34

Slide 34 text

Live Demo 如果有時間的話 .....

Slide 35

Slide 35 text

Thank You

Slide 36

Slide 36 text

如果還有時間 .... JRuby on Android http://ruboto.org/

Slide 37

Slide 37 text

Thank You