Skip to content

Commit 9fca3a3

Browse files
committed
WIP
1 parent 0390d0d commit 9fca3a3

File tree

1 file changed

+64
-48
lines changed

1 file changed

+64
-48
lines changed

lib/ui/core.jsx

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,21 @@ class Platform {
2323
}
2424

2525
class Util {
26-
static function format(fmt : string, list : int[]) : string {
27-
return fmt.replace(/%/g, (s) -> {
28-
return list[s as int] as string;
26+
static function format(fmt : string, list : variant[]) : string {
27+
return fmt.replace(/%\d+/g, (s) -> {
28+
var index = s.slice(1) as int - 1;
29+
return list[index] as string;
2930
});
3031
}
32+
33+
static function format(fmt : string, list : int[]) : string {
34+
return Util.format(fmt, list as __noconvert__ variant[]);
35+
}
3136
static function format(fmt : string, list : number[]) : string {
32-
return fmt.replace(/%/g, (s) -> {
33-
return list[s as int] as string;
34-
});
37+
return Util.format(fmt, list as __noconvert__ variant[]);
3538
}
3639
static function format(fmt : string, list : string[]) : string {
37-
return fmt.replace(/%/g, (s) -> {
38-
return list[s as int] as string;
39-
});
40+
return Util.format(fmt, list as __noconvert__ variant[]);
4041
}
4142

4243
static function createTextNode(s : string) : web.Node {
@@ -65,7 +66,14 @@ class Util {
6566
}
6667

6768
static function borderWithColor(color : Color) : string {
68-
return "solid 1px " + color.toStyle();
69+
return "solid 1px " + color.toString();
70+
}
71+
72+
73+
static function applyGradient(style : web.CSSStyleDeclaration, type : string, begin : string, end : string, fromColor : Color, toColor : Color) : void {
74+
var s = Util.format("-webkit-gradient(%1, %2, %3, from(%4), to(%5))",
75+
[type, begin, end, fromColor.toString(), toColor.toString()]);
76+
style.background = s;
6977
}
7078
}
7179

@@ -103,7 +111,7 @@ class Util {
103111
}
104112

105113
mixin Responder {
106-
114+
// TODO
107115
}
108116

109117
class Application implements Responder {
@@ -112,7 +120,6 @@ class Application implements Responder {
112120

113121
function constructor() {
114122
this._view = new View();
115-
this._view.setFrame(new Rectangle(0, 0, Platform.getWidth(), Platform.getHeight()));
116123
Application.resetStyles();
117124
}
118125

@@ -274,21 +281,11 @@ mixin Appearance {
274281
}
275282

276283
class View implements Responder, Appearance {
277-
var _frame : Rectangle = new Rectangle(0, 0, 0, 0);
278-
var _autoSized : boolean = true;
279-
280284
var _backgroundColor : Color = Color.WHITE;
281285

282286
var _parent : View = null;
283287
var _subviews = new Array.<View>();
284288

285-
function getFrame() : Rectangle {
286-
return this._frame;
287-
}
288-
function setFrame(rect : Rectangle) : void {
289-
this._frame = rect;
290-
}
291-
292289
function getBackgroundColor() : Color {
293290
return this._backgroundColor;
294291
}
@@ -311,16 +308,6 @@ class View implements Responder, Appearance {
311308
view._parent = this;
312309
}
313310

314-
function calculateFrame() : Rectangle {
315-
var frame = new Rectangle(0, 0, this._frame.size.width, this._frame.size.height);
316-
for (var v = this; v != null; v = v.getParent()) {
317-
var origin = v.getFrame().origin;
318-
frame.origin.x += origin.x;
319-
frame.origin.y += origin.y;
320-
}
321-
return frame;
322-
}
323-
324311
function onClick(cb : function(:MouseEvent):void) : void {
325312
var listener = function (e : web.Event) : void {
326313
cb(new MouseEvent(e));
@@ -332,21 +319,11 @@ class View implements Responder, Appearance {
332319
var block = Util.createDiv();
333320
var style = block.style;
334321

335-
style.backgroundColor = this._backgroundColor.toStyle();
336-
style.position = "absolute";
337-
338-
var frame = this.calculateFrame();
322+
style.backgroundColor = this._backgroundColor.toString();
323+
style.width = "100%";
339324

340325
if (Platform.DEBUG) {
341-
style.border = Util.borderWithColor(Color.BLUE);
342-
}
343-
344-
style.left = frame.origin.x as string + "px";
345-
style.top = frame.origin.y as string + "px";
346-
347-
if (! this._autoSized) {
348-
style.width = frame.size.width as string + "px";
349-
style.height = frame.size.height as string + "px";
326+
//style.border = Util.borderWithColor(Color.BLUE);
350327
}
351328

352329
this._subviews.forEach( (view) -> {
@@ -497,6 +474,7 @@ class Control extends View {
497474
class Label extends View {
498475
var _content : web.Node = null;
499476
var _color : Color = Color.DARK_TEXT;
477+
var _align : string;
500478

501479
function constructor() {
502480
}
@@ -513,14 +491,39 @@ class Label extends View {
513491
this._content = content;
514492
}
515493

494+
function setAlign(align : string) : void {
495+
this._align = align;
496+
}
497+
498+
function toCenter() : Label {
499+
this.setAlign("center");
500+
return this;
501+
}
502+
function toLeft() : Label {
503+
this.setAlign("left");
504+
return this;
505+
}
506+
function toRight() : Label {
507+
this.setAlign("right");
508+
return this;
509+
}
510+
516511
override function _toElement() : web.HTMLElement {
517512
assert this._content != null;
518513

519514
var element = super._toElement(); // <div>
520515
element.appendChild(this._content);
521516

522517
var style = element.style;
523-
style.color = this._color.toStyle();
518+
style.color = this._color.toString();
519+
style.textAlign = this._align;
520+
style.paddingTop = "5px";
521+
style.paddingBottom = "5px";
522+
style.margin = "2px";
523+
524+
style.borderRadius = "8px";
525+
Util.applyGradient(style, "linear", "left top", "left bottom", Color.WHITE, Color.LIGHT_GRAY);
526+
524527

525528
return element;
526529
}
@@ -563,16 +566,29 @@ class TextField extends Control {
563566
this._a = a;
564567
}
565568

566-
function toStyle() : string {
569+
function toRGBAStyle() : string {
567570
return "rgba("
568571
+ this._r as string + ", "
569572
+ this._g as string + ", "
570573
+ this._b as string + ", "
571574
+ this._a as string + ")";
572575
}
573576

577+
578+
function _hex02(c : int) : string {
579+
var s = c.toString(16);
580+
return s.length > 1 ? s : "0" + s;
581+
}
582+
583+
function toHexStyle() : string {
584+
return "#"
585+
+ this._hex02(this._r)
586+
+ this._hex02(this._g)
587+
+ this._hex02(this._b);
588+
}
589+
574590
override function toString() : string {
575-
return this.toStyle();
591+
return this.toRGBAStyle();
576592
}
577593
}
578594

0 commit comments

Comments
 (0)