JRubyFXでWebブラウザを作ってみる

JavaFxになってSwingと比べてWebViewのエンジンが強化されているようです。
SwingでJavascriptとかHTML5とかがうまく動かなかったのがJavaFXではちゃんと動くっぽいです。

まずは簡単なWeb表示

require 'jrubyfx'

class App < JRubyFX::Application
  def start(stage)
    with(stage, width: 800, height: 600, title: 'sample') do
      layout_scene do
        web_view do |v| 
          v.engine.load "http://google.com/"
        end 
      end 
      show
    end 
  end 
end

App.launch

BackボタンとURLバーを作ってみる

デザインにこだわっていないので、見た目がよくないですが。。

require 'jrubyfx'

class App < JRubyFX::Application

  DefaultURL = "http://google.com/"

  def start(stage)
    with(stage, width: 800, height: 600, title: 'sample') do
      layout_scene do
        vbox do
          hbox do
            button(id:"back", text:"back", min_width: 80) 
            text_field(id:"url", pref_width: 800)
          end 
          web_view(id:"browser") do |v| 
            v.engine.load DefaultURL
          end 
        end 
      end 
      show
    end 

    #戻るボタン
    stage['#back'].set_on_action do
      stage["#browser"].engine.history.go(-1)
    end 

    #入力したURLへ移動する
    stage['#url'].set_on_action do
      stage["#browser"].engine.load stage['#url'].text
    end 

    # URLを表示する
    stage['#browser'].engine.get_load_worker.state_property.add_change_listener do |ov, os, new_state|
      if new_state == Worker::State::SUCCEEDED
        stage['#url'].text = stage['#browser'].engine.get_location
      end
    end
   end 
end

App.launch


まとめ

JRubyFXということもあって記述は見よう見まねで書いたので適当です。
それでも意外とあっさり作れました。

googleマップも普通に見れてびっくりしました。
YoutubeはさすがにFlashがないと怒られました。
動画はhtml5でのh.264エンコードされたファイルなら見れたりするかもです。。

なんだかいろいろできそうな感じです。