Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JRubyFX For Web Developers

JRubyFX For Web Developers

JRubyFX is a DSL and set of wrappers for the Java UI toolkit JavaFX

E0fdc1036537c1308400fc8ba6e987b0?s=128

Neil Matatall

April 30, 2015
Tweet

Transcript

  1. JRubyFX For Web Developers

  2. When I was a little boy I wanted to build

    cool apps
  3. None
  4. None
  5. None
  6. None
  7. None
  8. A programmer’s best friend

  9. The minimum standard is that the software has to work

    for me
  10. JavaFX

  11. Why not shoes? or GTK? Or X?

  12. 1.0 1.1 1.2 1.3 1.3.1 2.0-2.2 8

  13. What’s cool

  14. SceneBuilder

  15. CSS .default-color0.chart-series-line { -fx-stroke: #CC0000; } .default-color1.chart-series-line { -fx-stroke: #FF6600;

    } .default-color2.chart-series-line { -fx-stroke: #FFCC00; } .default-color0.chart-line-symbol { -fx-background-color: #CC0000, white; } .default-color1.chart-line-symbol { -fx-background-color: #FF6600, white; } .default-color2.chart-line-symbol { -fx-background-color: #FFCC00, white; }
  16. ScenicView

  17. ControlsFX

  18. java 8u40 / OpenJFX dialogs

  19. AquaFX for that native look and feel

  20. Automaton require_relative '../lib/brakeman_pro' require 'lib/automaton.jar' java_import 'com.athaydes.automaton.FXer' java_import 'com.athaydes.automaton.FXApp' FXApp.start_app(

    BrakemanPro::Main.new ); fxer = FXer.get_user_with( FXApp.scene.root ) fxer.click_on("text:Add Group").wait_for_fx_events fxer.type("Yo mama").click_on("text:Save")
  21. Polish You can distribute apps as a jar or as

    a native application with various installers • deb/rpm • OSX Applications (bundles) / dmg / pkg • exe / msi for windows The JVM is included in the package.
  22. JRubyFX

  23. For web developers

  24. None
  25. DAFUQ

  26. None
  27. None
  28. None
  29. None
  30. Using the DSL

  31. Using FXML

  32. Contols Buttons Labels VBox HBox SplitPane BorderPane AnchorPane ListView TableView

    TreeView TreeTableView
  33. Property binding

  34. The not so ideal way

  35. The more better way

  36. Binding to events

  37. <button fx:id=”b” onAction=”#handle_click” /> def handle_click event # do stuff

    # event.target == @b end
  38. @b.set_on_action do |event| # do stuff end

  39. But inline event handlers are bad Prefer binding in controller

    code over on* attributes in fxml unless your component is tiny
  40. The selection model API is much more powerful

  41. don’t bind to “click” events, bind to the selected_item property

  42. None
  43. Custom Elements module BrakemanPro module Controls class CodeViewer < Java::javafx::scene::control::VBox

    include JRubyFX::Controller fxml "result_category_pane.fxml" … def update_code code @webview.get_engine.load_content(content, "text/html") end
  44. Controller class MainWindow include JRubyFX::Controller fxml "main.fxml" def some_function @main_pane.center

    = CodeViewer.new @split_pane.items.add(@code_viewer) @code_viewer.update_code(warning.location) end end
  45. Starting the app class Main < JRubyFX::Application def start(stage) title

    = "Brakeman Pro" with(stage, title: title, resizable: true) do fxml MainWindow show end end end
  46. Design Tip (follow at your own risk) • Make components

    as small as possible • Have each component belong to an object, or prefer composition over inheritance. depends. • If the object won’t change, bind properties • If the object will change, use a “repaint” strategy
  47. Performance Tips • Store “pages” in memory • Track “dirty

    regions” • Update on change (or bind) • The fewer elements, the faster the process • Perform (most) code in event handlers in a run_later
  48. Threads Possibly the best part of JRuby

  49. Don’t block the main thread

  50. run_later { update_ui }

  51. A simple, performant pattern

  52. FXCollections and Java 8

  53. (filtering demo)

  54. What isn’t cool Exception in thread "JavaFX Application Thread" org.jruby.exceptions.RaiseException:

    (NoMethodError) undefined method `int' for RubyWrapperBeanAdapter:Class at RUBY.getConstantValue(.../jrubyfx-fxmlloader-0.4-java/lib/fxmlloader/rrba.rb:607) at RUBY.constructValue(.../jrubyfx-fxmlloader-0.4-java/lib/fxmlloader/real_elts.rb:85) at RUBY.processStartElement(.../jrubyfx-fxmlloader-0.4-java/lib/fxmlloader/value_elts.rb:62) at FxmlLoader.processStartElement(.../jrubyfx-fxmlloader-0.4-java/lib/jrubyfx-fxmlloader.rb:440) at RUBY.load(.../jrubyfx-fxmlloader-0.4-java/lib/jrubyfx-fxmlloader.rb:351) at RUBY.load_fxml(.../jrubyfx-1.1.1-java/lib/jrubyfx/controller.rb:266) at RUBY.initialize_controller(.../jrubyfx-1.1.1-java/lib/jrubyfx/controller.rb:248) at RUBY.new(.../jrubyfx-1.1.1-java/lib/jrubyfx/controller.rb:157) at RUBY.initialize(...brakeman-pro/lib/brakeman_pro/controller/details_controller.rb:12) <columnResizePolicy> <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /> </columnResizePolicy>
  55. What isn’t cool I’ve yet to be able to use

    a release, always pointing to master Project activity is… bursty Java
  56. Where I’ve been wrong Composition vs Inheritance

  57. None
  58. Resources http://code.makery.ch/java/javafx-8-tutorial-intro www.GuiGarage.com Mastering javafx 8 controls