Skip to content

Instantly share code, notes, and snippets.

View stanch's full-sized avatar

Nick stanch

  • Product Manager at @snowplow
  • Lisbon, Portugal
View GitHub Profile
new SVerticalLayout {
STextView("ID")
val userId = SEditText()
SButton("Sign in", signin(userId.text))
}.padding(20 dip)
[info] [running phase parser on 9 compilation units]
[info] [running phase macroparadise on 9 compilation units]
[info] [running phase namer on 9 compilation units]
[info] [running phase packageobjects on 9 compilation units]
[info] [running phase typer on 9 compilation units]
[warn] issue error: type mismatch;
[warn] found : c.WeakTypeTag[X&0]
[warn] (which expands to) c.universe.WeakTypeTag[X&0]
[warn] required: c.universe.WeakTypeTag[needs.this.Need[_$1] forSome { type _$1 }]
[warn] issue error: type mismatch;
// deep search inside myLayout
myLayout ~~> {
// make all buttons have medium-sized text
case b: Button ⇒ b ~> TextSize.medium
// find layouts consisting of just ImageView and TextView
case Layout(m: ImageView, t: TextView) ⇒
t ~> text("I’m next to an image")
// hide everything else
case otherStuff ⇒ otherStuff ~> hide
}
// apply optional tweak: Option[Tweak[_]]
def large(implicit ctx: Context): Option[Tweak[TextView]] = hdpi ? TextSize.large
myTextView ~> large
// apply a list of tweaks: List[Tweak[_]]
myButton ~> List(text("The red button"), id(Id.redButton))
// functional reactive programming: Rx[Tweak[_]]
val caption = rx.Var("Olá")
myTextView ~> caption.map(c ⇒ text(c)) // sets text to “Olá”
myTextView ~>
text("Voilà") ~@>
fadeIn(400) ~@>
delay(500) ~>
text("Bye!") ~@>
fadeOut(400) ~>
text("Nobody will see this text")
trait MyStyles extends LayoutDsl with MediaQueries with ExtraTweaks {
// sets text, large font size and a long click handler
def caption(cap: String)(implicit ctx: Context): Tweak[TextView] =
text(cap) + TextSize.large + On.longClick {
Toast.makeText(ctx, "I’m a caption", Toast.LENGTH_SHORT).show()
true
}
// sets large font size for high-density screens, medium font size for medium-density screens
// does nothing in other cases
l[LinearLayout](
// It.stuff generates a new id
// Tag.stuff just returns "stuff", but boy! isn’t it fancy?
f[MyAwesomeFragment](Id.stuff, Tag.stuff, "items" → 4, "title" → "buzz"),
f[SupportMapFragment](Id.map, Tag.mainMap)
)
// if screen width > 1000 dip, make layout horizontal, else make it vertical
l[LinearLayout](...) ~> (minWidth(1000 dp) ? horizontal | vertical)
// use different widget depending on screen width
(widerThan(1000 dp) ? w[BigTextView] |
widerThan(600 dp) ? w[MediumTextView] |
w[TextView]) ~> text("Gibberish")
// tweak a widget only if a condition is met
w[TextView] ~> text("Balderdash!") ~> (hdpi ? TextSize.large)
var greeting = slot[TextView]
l[LinearLayout](
w[Button] ~>
text("Greet me!") ~>
On.click {
greeting ~> text("Hello there") ~> show
},
w[TextView] ~> hide ~> wire(greeting)
var status = slot[TextView]
setContentView {
l[LinearLayout](
w[TextView] ~> text("Downloading") ~> wire(status),
w[TextView] ~> text("the internet...") ~> id(Id.inet)
)
}
// after some time...