Skip to content

Commit 6eaedd1

Browse files
committed
Merge branch 'master' of https://github.com/yglukhov/nimx
2 parents 292cb97 + 6e98eae commit 6eaedd1

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

nimx/formatted_text.nim

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -645,15 +645,22 @@ void compose()
645645
""", false, "mediump")
646646

647647
type ForEachLineAttributeCallback = proc(c: GraphicsContext, t: FormattedText, p: var Point, curLine, endIndex: int, str: string) {.nimcall.}
648-
proc forEachLineAttribute(c: GraphicsContext, origP: Point, t: FormattedText, cb: ForEachLineAttributeCallback) =
648+
proc forEachLineAttribute(c: GraphicsContext, inRect: Rect, origP: Point, t: FormattedText, cb: ForEachLineAttributeCallback) =
649649
var p = origP
650650
let numLines = t.lines.len
651651
var curLine = 0
652652
let top = t.topOffset() + origP.y
653+
let inRectValid = (inRect.y + inRect.height) > 0.01
653654

654655
while curLine < numLines:
655656
p.x = origP.x + t.lineLeft(curLine)
656657
p.y = t.lines[curLine].top + t.lines[curLine].baseline + top
658+
if inRectValid:
659+
if p.y < inRect.y:
660+
curLine.inc
661+
continue
662+
elif p.y > inRect.height:
663+
break
657664

658665
var lastCurAttrIndex: int
659666
var lastAttrStartIndex: int
@@ -719,9 +726,9 @@ proc forEachLineAttribute(c: GraphicsContext, origP: Point, t: FormattedText, cb
719726
curLine.inc
720727

721728

722-
proc drawShadow(c: GraphicsContext, origP: Point, t: FormattedText) =
729+
proc drawShadow(c: GraphicsContext, inRect: Rect, origP: Point, t: FormattedText) =
723730
# TODO: Optimize heavily
724-
forEachLineAttribute(c, origP, t) do(c: GraphicsContext, t: FormattedText, p: var Point, curLine, curAttrIndex: int, str: string):
731+
forEachLineAttribute(c, inRect, origP, t) do(c: GraphicsContext, t: FormattedText, p: var Point, curLine, curAttrIndex: int, str: string):
725732
c.fillColor = t.mAttributes[curAttrIndex].shadowColor
726733
let font = t.mAttributes[curAttrIndex].font
727734
let oldBaseline = font.baseline
@@ -762,9 +769,9 @@ proc drawShadow(c: GraphicsContext, origP: Point, t: FormattedText) =
762769
font.baseline = oldBaseline
763770
p.x += pp.x - ppp.x
764771

765-
proc drawStroke(c: GraphicsContext, origP: Point, t: FormattedText) =
772+
proc drawStroke(c: GraphicsContext, inRect: Rect, origP: Point, t: FormattedText) =
766773
# TODO: Optimize heavily
767-
forEachLineAttribute(c, origP, t) do(c: GraphicsContext, t: FormattedText, p: var Point, curLine, curAttrIndex: int, str: string):
774+
forEachLineAttribute(c, inRect, origP, t) do(c: GraphicsContext, t: FormattedText, p: var Point, curLine, curAttrIndex: int, str: string):
768775
const magicStrokeMaxSizeCoof = 0.46
769776
let font = t.mAttributes[curAttrIndex].font
770777

@@ -806,14 +813,14 @@ proc drawStroke(c: GraphicsContext, origP: Point, t: FormattedText) =
806813
# Dirty hack to advance x position. Should be optimized, of course.
807814
c.drawText(font, p, str)
808815

809-
proc drawText*(c: GraphicsContext, origP: Point, t: FormattedText) =
816+
proc drawText*(c: GraphicsContext, origP: Point, t: FormattedText, inRect: Rect = zeroRect) =
810817
t.updateCacheIfNeeded()
811818

812819
if t.overrideColor.a == 0:
813-
if t.shadowAttrs.len > 0: c.drawShadow(origP, t)
814-
if t.strokeAttrs.len > 0: c.drawStroke(origP, t)
820+
if t.shadowAttrs.len > 0: c.drawShadow(inRect, origP, t)
821+
if t.strokeAttrs.len > 0: c.drawStroke(inRect, origP, t)
815822

816-
forEachLineAttribute(c, origP, t) do(c: GraphicsContext, t: FormattedText, p: var Point, curLine, curAttrIndex: int, str: string):
823+
forEachLineAttribute(c, inRect, origP, t) do(c: GraphicsContext, t: FormattedText, p: var Point, curLine, curAttrIndex: int, str: string):
817824
let font = t.mAttributes[curAttrIndex].font
818825
let oldBaseline = font.baseline
819826
font.baseline = bAlphabetic

nimx/scroll_bar.nim

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@ type ScrollBar* = ref object of Slider
1010
# document is 0.5.
1111
trackingPos: Coord # Position of mouse coordinate (x or y depending on orientation) within knob
1212

13+
const minKnobSize = 0.05
1314
method init*(s: ScrollBar, r: Rect) =
1415
procCall s.Slider.init(r)
1516
s.mKnobSize = 0.2
1617

1718
proc knobRect(s: ScrollBar): Rect =
1819
result = s.bounds
1920
if s.isHorizontal:
20-
result.size.width *= s.mKnobSize
21+
result.size.width *= max(s.mKnobSize, minKnobSize)
2122
result.origin.x = max((s.bounds.width - result.width) * s.value, 3)
2223
if result.maxX > s.bounds.width - 3:
2324
result.size.width = s.bounds.width - result.x - 3
2425
result = inset(result, 0, 2)
2526
else:
26-
result.size.height *= s.mKnobSize
27+
result.size.height *= max(s.mKnobSize, minKnobSize)
2728
result.origin.y = max((s.bounds.height - result.height) * s.value, 3)
2829
if result.maxY > s.bounds.height - 3:
2930
result.size.height = s.bounds.height - result.y - 3

nimx/text_field.nim

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ proc drawSelection(t: TextField) {.inline.} =
246246
r.size.width = endOff
247247
c.drawRect(r)
248248

249+
#todo: replace by generic visibleRect which should be implemented in future
250+
proc visibleRect(t: TextField): Rect =
251+
let wndRect = t.convertRectToWindow(t.bounds)
252+
let wndBounds = t.window.bounds
253+
254+
result.origin.y = if wndRect.y < 0.0: abs(wndRect.y) else: 0.0
255+
result.size.width = t.bounds.width
256+
result.size.height = min(t.bounds.height, wndBounds.height) + result.y - max(wndRect.y, 0.0)
257+
249258
method draw*(t: TextField, r: Rect) =
250259
procCall t.View.draw(r)
251260

@@ -267,7 +276,11 @@ method draw*(t: TextField, r: Rect) =
267276
t.mText.overrideColor = whiteColor()
268277
else:
269278
t.mText.overrideColor.a = 0
270-
c.drawText(pt, t.mText)
279+
280+
if t.bounds.height > t.window.bounds.height:
281+
c.drawText(pt, t.mText, t.visibleRect())
282+
else:
283+
c.drawText(pt, t.mText)
271284

272285
if t.isEditing:
273286
if t.hasBezel:

0 commit comments

Comments
 (0)