Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile...

25
Scala for GUI Aurelijus Banelis

Transcript of Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile...

Page 1: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

Scala for GUI

Aurelijus Banelis

Page 2: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

About me

Aurelijus Banelis [email protected] Using Scalafor personal project

2About

Page 3: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

Scala for GUI? Really?

3Preface

https://typesafe.com/blog/eight-hot-technologies-that-were-built-in-scala

GUInot listed

Page 4: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

You will learn

4In short

WHYScala

HOWfor GUI

DesktopScalaFx

WEBScala.js

MobileScaloid

Page 5: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

Context: Knowledge management tool

5Context

Page 6: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

Context: When GUI matters

6

GUI = Added valuePrevent cognitive overheadBoost visual memoryFaster perception

Context

Page 7: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

Context: When GUI matters

7

GUI = Added valuePrevent cognitive overheadBoost visual memoryFaster perception

Personal use: more like prototype

Java 6 + SwingJust get things done

Context

Page 8: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

WHY: Java → Scala

8

Common Async Zoom

Java → Scala

Page 9: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

WHY: Java → Scala

9

Common Async Zoom

Traits Immutability @tailrec

Java → Scala

Page 10: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

WHY: Swing → JavaFx

10Desktop

Page 11: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

Context: JavaFx example

11

https://youtu.be/ZZ6CZLcWnTE

Desktop

Page 12: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

HOW: JavaFx ↔ ScalaFx

12

class Label(val _text: String)extends RichJPanewith ViewableNodewith HaveOperationswith DragableNode[jp]with ZoomableNode[jp]with ScalableElement[jp]with Datawith Transformable[Label]with EditableNode {

class Imageextends RichImageViewwith ViewableNodewith HaveOperations ...

mousePressed += beginDrag

mouseReleased += endDrag

mouseDragged += { (e: MouseEvent) => if (beingDragged) { endDrag(e) beginDrag(e) }}

Desktop

Page 13: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

HOW: Multi module project

13

WHYScala

HOWfor GUI

DesktopScalaFx

WEBScala.js

MobileScaloid

Desktop

Page 14: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

HOW: Multi module project

14

WHYScala

HOWfor GUI

DesktopScalaFx

WEBScala.js

MobileScaloid

WEB

Page 15: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

HOW: Scala.js + React

15

Scala.jsCompile Scala → JavaScriptType safety for complex GUIAccess to native JavaScript

ReactForces immutabilityDirect data flowVirtual DOM

https://github.com/japgolly/scalajs-react

WEB

Page 16: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

HOW: Scala.js + React

16

Flow

State

Render

Events

Logic

React

Page 17: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

HOW: Scala.js + React

17

Flow

State

Render

Events

Logic

case class State(camera: Camera) { def inCamera(converter: Camera => Camera) = copy(camera = converter(camera))}

Immutable

Deeper structure

React

Page 18: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

HOW: Scala.js + React

18

Flow

State

Render

Events

Logic

.render { (P, S, B) => <.span( P.element.text, ^.`class` := "dragable noselect", ^.left := (P.element.x - P.camera.x) / P.camera.scale, ^.fontSize := s"${1.0 / P.camera.scale}em",

^.onMouseDown ==> P.receive, ^.onTouchStart ==> P.receive, )}

Parameters

Callbacks

React

Page 19: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

HOW: Scala.js + React

19

Flow

State

Render

Events

Logic

def beginDrag(e: PointerEvent): Unit = preventDefault(e) { selectedElement(e) match { case Some(element) => elements.Dragging.begin(element, e) case None => view.Dragging.begin(e) }}

def touch(reactEvent: ReactMouseEvent): Unit = event(reactEvent) match { case e: TouchStart if e.touchEvent.touches.length == 1 => preventDefault(reactEvent) { elements.Dragging.begin(element, e.touchEvent.touches(0)) }... Different parameters

Eventpropagation

React

Page 20: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

HOW: Scala.js + React

20

Flow

State

Render

Events

Logic

def drag(event: ScreenPosition): T = moveElement(event) andThen savePosition(event)

private def savePosition(position: ScreenPosition): T = { State:State => state inSelected (_ inElements ( _ withPosition inCamera(state, position) ))}

All of same type

Easy combine

Returns functionReact

Page 21: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

Rendering techniques

21

Flow

State

Render

Events

LogicFunctional style

Transformation based

ObserversJavaFx

Swing

Threadupdates

Eventdispatch Force update

From OS/callback

Rendering

Page 22: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

HOW: Multi module project

22

WHYScala

HOWfor GUI

DesktopScalaFx

WEBScala.js

MobileScaloid

Mobile

Page 23: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

Reuse in Multi module project

23

Use tools Copy withsymlinks

crossProject scaloid

Scala.js Android

Not user-friendly

SBT vs IDEA

Dirty, but works

Code completion

Cross

Page 24: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

Questions?

?

WHYScala

HOWfor GUI

DesktopScalaFx

WEBScala.js

MobileScaloid

Page 25: Scala for GUI - Aurelijus Banelis · WHY Scala HOW for GUI Desktop ScalaFx WEB Scala.js Mobile Scaloid. Context: Knowledge management tool Context 5. Context: When GUI matters 6 GUI

References and useful links

25i

●http://auginte.com/●http://www.scala-lang.org/●http://www.oracle.com/technetwork/java/javase/overview/javafx-overview-2158620.html●https://github.com/scalafx/scalafx●http://www.scala-js.org/●https://github.com/japgolly/scalajs-react●https://www.youtube.com/watch?v=KVZ-P-ZI6W4●http://www.scala-js.org/doc/sbt/cross-building.html