Skip to content

Commit 3e6af63

Browse files
committed
cleanup
1 parent 10e9036 commit 3e6af63

File tree

4 files changed

+70
-71
lines changed

4 files changed

+70
-71
lines changed

samples/drawtext/main.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,12 @@ fun main(args: Array<String>) = appWindow(
6969
val area = Area {
7070
val astr = makeAttributedString()
7171
draw {
72-
val context = it.Context!!
73-
context.draw(astr, defaultFont.value, it.AreaWidth, alignment.value, 0.0, 0.0)
72+
text(astr, defaultFont.value, it.AreaWidth, alignment.value, 0.0, 0.0)
7473
}
7574
}
7675

77-
defaultFont.action { area.queueRedrawAll() }
78-
alignment.action { area.queueRedrawAll() }
76+
defaultFont.action { area.redraw() }
77+
alignment.action { area.redraw() }
7978

8079
add(widget = HorizontalBox {
8180
padded = true

samples/histogram/main.kt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ fun main(args: Array<String>) = appWindow(
5050
}
5151

5252
draw {
53-
val context = it.Context!!
5453
val graphWidth = graphWidth(it.AreaWidth)
5554
val graphHeight = graphHeight(it.AreaHeight)
5655
val graphColor = colorButton.value
@@ -59,24 +58,24 @@ fun main(args: Array<String>) = appWindow(
5958
pointLocations(graphWidth, graphHeight, xs, ys)
6059

6160
// fill the area with white
62-
context.fill(uiDrawFillModeWinding, brush.solid(colorWhite)) {
61+
fill(brush.solid(colorWhite)) {
6362
rectangle(0.0, 0.0, it.AreaWidth, it.AreaHeight)
6463
}
6564

6665
// draw the axes
67-
context.stroke(uiDrawFillModeWinding, brush.solid(colorBlack), stroke) {
66+
stroke(brush.solid(colorBlack), stroke) {
6867
figure(xoffLeft, yoffTop)
6968
lineTo(xoffLeft, yoffTop + graphHeight)
7069
lineTo(xoffLeft + graphWidth, yoffTop + graphHeight)
7170
}
7271

73-
// now transform the coordinate space so (0, 0) is the top-left corner of the graph
74-
context.transform {
72+
// transform the coordinate space so (0, 0) is the top-left corner of the graph
73+
transform {
7574
translate(xoffLeft, yoffTop)
7675
}
7776

78-
// now create the fill for the graph below the graph line
79-
context.fill(uiDrawFillModeWinding, brush.solid(graphColor, opacity = 0.5)) {
77+
// create the fill for the graph below the graph line
78+
fill(brush.solid(graphColor, opacity = 0.5)) {
8079
figure(xs[0], ys[0])
8180
for (i in 1 until 10)
8281
lineTo(xs[i], ys[i])
@@ -85,16 +84,16 @@ fun main(args: Array<String>) = appWindow(
8584
closeFigure()
8685
}
8786

88-
// now draw the histogram line
89-
context.stroke(uiDrawFillModeWinding, brush.solid(graphColor), stroke) {
87+
// draw the histogram line
88+
stroke(brush.solid(graphColor), stroke) {
9089
figure(xs[0], ys[0])
9190
for (i in 1 until 10)
9291
lineTo(xs[i], ys[i])
9392
}
9493

95-
// now draw the point being hovered over
94+
// draw the point being hovered over
9695
if (currentPoint != -1) {
97-
context.fill(uiDrawFillModeWinding, brush) {
96+
fill(brush) {
9897
figureWithArc(xs[currentPoint], ys[currentPoint], pointRadius,
9998
startAngle = 0.0, sweep = 6.23)
10099
}
@@ -121,17 +120,17 @@ fun main(args: Array<String>) = appWindow(
121120
}
122121
}
123122

124-
queueRedrawAll()
123+
redraw()
125124
}
126125
}
127126

128127
add(widget = VerticalBox {
129128
padded = true
130129
datapoints.forEach {
131-
it.action { histogram.queueRedrawAll() }
130+
it.action { histogram.redraw() }
132131
add(widget = it)
133132
}
134-
colorButton.action { histogram.queueRedrawAll() }
133+
colorButton.action { histogram.redraw() }
135134
add(widget = colorButton)
136135
})
137136

samples/logo/main.kt

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,65 +17,60 @@ fun main(args: Array<String>) = appWindow(
1717
mouseEvent {
1818
if (it.Down != 0) {
1919
angle += PI / 2.0
20-
queueRedrawAll()
20+
redraw()
2121
}
2222
}
2323

2424
draw {
25-
with (it.Context!!) {
26-
27-
fill(uiDrawFillModeWinding, brush.solid(0xFFFFFF)) {
28-
rectangle(0.0, 0.0, it.AreaWidth, it.AreaHeight)
29-
}
25+
fill(uiDrawFillModeWinding, brush.solid(0xFFFFFF)) {
26+
rectangle(0.0, 0.0, it.AreaWidth, it.AreaHeight)
27+
}
3028

31-
transform {
32-
scale(0.0, 0.0, it.AreaWidth / 60.0, it.AreaHeight / 60.0)
33-
rotate(it.AreaWidth / 2.0, it.AreaHeight / 2.0, angle)
34-
}
29+
transform { scale(0.0, 0.0, it.AreaWidth / 60.0, it.AreaHeight / 60.0) }
30+
transform { rotate(30.0, 30.0, angle) }
3531

36-
fill(uiDrawFillModeWinding, brush.linear(
37-
Point(0.0, 60.0), Point(30.0, 30.0),
38-
0.0968 to Color(0x0095D5),
39-
0.3007 to Color(0x238AD9),
40-
0.6211 to Color(0x557BDE),
41-
0.8643 to Color(0x7472E2),
42-
1.0000 to Color(0x806EE3))) {
32+
fill(uiDrawFillModeWinding, brush.linear(
33+
Point(0.0, 60.0), Point(30.0, 30.0),
34+
0.0968 to Color(0x0095D5),
35+
0.3007 to Color(0x238AD9),
36+
0.6211 to Color(0x557BDE),
37+
0.8643 to Color(0x7472E2),
38+
1.0000 to Color(0x806EE3))) {
4339

44-
figure( 0.0, 60.0)
45-
lineTo(30.1, 29.9)
46-
lineTo(60.0, 60.0)
47-
closeFigure()
48-
}
40+
figure( 0.0, 60.0)
41+
lineTo(30.1, 29.9)
42+
lineTo(60.0, 60.0)
43+
closeFigure()
44+
}
4945

50-
fill(uiDrawFillModeWinding, brush.linear(
51-
Point(10.0, 20.0), Point(30.0, 0.0),
52-
0.1183 to Color(0x0095D5),
53-
0.4178 to Color(0x3C83DC),
54-
0.6962 to Color(0x6D74E1),
55-
0.8333 to Color(0x806EE3))) {
46+
fill(uiDrawFillModeWinding, brush.linear(
47+
Point(10.0, 20.0), Point(30.0, 0.0),
48+
0.1183 to Color(0x0095D5),
49+
0.4178 to Color(0x3C83DC),
50+
0.6962 to Color(0x6D74E1),
51+
0.8333 to Color(0x806EE3))) {
5652

57-
figure( 0.0, 0.0)
58-
lineTo(30.1, 0.0)
59-
lineTo( 0.0, 32.5)
60-
closeFigure()
61-
}
53+
figure( 0.0, 0.0)
54+
lineTo(30.1, 0.0)
55+
lineTo( 0.0, 32.5)
56+
closeFigure()
57+
}
6258

63-
fill(uiDrawFillModeWinding, brush.linear(
64-
Point(0.0, 60.0), Point(50.0, 10.0),
65-
0.1075 to Color(0xC757BC),
66-
0.2138 to Color(0xD0609A),
67-
0.4254 to Color(0xE1725C),
68-
0.6048 to Color(0xEE7E2F),
69-
0.7430 to Color(0xF58613),
70-
0.8232 to Color(0xF88909))) {
59+
fill(uiDrawFillModeWinding, brush.linear(
60+
Point(0.0, 60.0), Point(50.0, 10.0),
61+
0.1075 to Color(0xC757BC),
62+
0.2138 to Color(0xD0609A),
63+
0.4254 to Color(0xE1725C),
64+
0.6048 to Color(0xEE7E2F),
65+
0.7430 to Color(0xF58613),
66+
0.8232 to Color(0xF88909))) {
7167

72-
figure(30.1, 0.0)
73-
lineTo( 0.0, 31.7)
74-
lineTo( 0.0, 60.0)
75-
lineTo(30.1, 29.9)
76-
lineTo(60.0, 0.0)
77-
closeFigure()
78-
}
68+
figure(30.1, 0.0)
69+
lineTo( 0.0, 31.7)
70+
lineTo( 0.0, 60.0)
71+
lineTo(30.1, 29.9)
72+
lineTo(60.0, 0.0)
73+
closeFigure()
7974
}
8075
}
8176
})

src/main/kotlin/canvas.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ open class Area internal constructor(
4141
val handler: CPointer<ktAreaHandler>
4242
) : Control<uiArea>(alloc) {
4343

44-
internal var draw: (Area.(params: uiAreaDrawParams) -> Unit)? = null
44+
internal var draw: (DrawContext.(params: uiAreaDrawParams) -> Unit)? = null
4545
internal var mouseEvent: (Area.(event: uiAreaMouseEvent) -> Unit)? = null
4646
internal var mouseCrossed: (Area.(left: Boolean) -> Unit)? = null
4747
internal var dragBroken: (Area.() -> Unit)? = null
@@ -51,7 +51,7 @@ open class Area internal constructor(
5151
handler.pointed.ui.Draw = staticCFunction { _handler, _, params ->
5252
val handler: CPointer<ktAreaHandler> = _handler!!.reinterpret()
5353
with (handler.pointed.ref!!.asStableRef<Area>().get()) {
54-
draw?.invoke(this, params!!.pointed)
54+
draw?.invoke(params!!.pointed.Context!!, params.pointed)
5555
}
5656
}
5757

@@ -102,7 +102,7 @@ class ScrollingArea internal constructor(
102102

103103
/** Queues the entire Area for redraw.
104104
* The Area is not redrawn before this function returns; it is redrawn when next possible. */
105-
fun Area.queueRedrawAll() = uiAreaQueueRedrawAll(ptr)
105+
fun Area.redraw() = uiAreaQueueRedrawAll(ptr)
106106

107107
/** Sets the size of a ScrollingArea to the given size, in points. */
108108
fun ScrollingArea.setSize(width: Int, height: Int) =
@@ -122,7 +122,7 @@ fun ScrollingArea.scrollTo(x: Double, y: Double, width: Double, height: Double)
122122

123123
/** Funcion to be run when the area was created or got resized with [uiAreaDrawParams] as parameter.
124124
* Only one function can be registered at a time. */
125-
fun Area.draw(block: Area.(params: uiAreaDrawParams) -> Unit) {
125+
fun Area.draw(block: DrawContext.(params: uiAreaDrawParams) -> Unit) {
126126
draw = block
127127
}
128128

@@ -624,6 +624,9 @@ fun DrawContext.fill(
624624
path.dispose()
625625
}
626626

627+
fun DrawContext.fill(brush: Brush, block: Path.() -> Unit) =
628+
fill(uiDrawFillModeWinding, brush, block)
629+
627630
/** Draw a path in the context. */
628631
fun DrawContext.stroke(
629632
mode: uiDrawFillMode,
@@ -638,6 +641,9 @@ fun DrawContext.stroke(
638641
path.dispose()
639642
}
640643

644+
fun DrawContext.stroke(brush: Brush, stroke: Stroke, block: Path.() -> Unit) =
645+
stroke(uiDrawFillModeWinding, brush, stroke, block)
646+
641647
/** Apply a different transform matrix to the context. */
642648
fun DrawContext.transform(block: Matrix.() -> Unit) {
643649
val matrix = Matrix()
@@ -648,7 +654,7 @@ fun DrawContext.transform(block: Matrix.() -> Unit) {
648654
}
649655

650656
/** draws formatted text with the top-left point at ([x], [y]). */
651-
fun DrawContext.draw(
657+
fun DrawContext.text(
652658
string: AttributedString,
653659
defaultFont: Font,
654660
width: Double,

0 commit comments

Comments
 (0)