Skip to content

Instantly share code, notes, and snippets.

@migueldeicaza
Created March 15, 2021 02:21
Show Gist options
  • Save migueldeicaza/954c2c64be08d8bb7849f2374759e25c to your computer and use it in GitHub Desktop.
Save migueldeicaza/954c2c64be08d8bb7849f2374759e25c to your computer and use it in GitHub Desktop.
diff --git a/Sources/Example/main.swift b/Sources/Example/main.swift
index c6bf5fa..dcf37ae 100644
--- a/Sources/Example/main.swift
+++ b/Sources/Example/main.swift
@@ -18,8 +18,7 @@ class Filler: View {
public static let h = 50
public override init () { super.init () }
- public override func redraw(region: Rect) {
- let p = getPainter()
+ public override func redraw(region: Rect, painter p: Painter) {
p.clear()
let f = frame
for y in 0..<f.height {
diff --git a/Sources/TermKit/Core/Application.swift b/Sources/TermKit/Core/Application.swift
index 5bf6bbb..f1616b7 100644
--- a/Sources/TermKit/Core/Application.swift
+++ b/Sources/TermKit/Core/Application.swift
@@ -144,7 +144,7 @@ public class Application {
var last : View? = nil
for v in toplevels {
v.setNeedsDisplay()
- v.redraw(region: v.bounds)
+ v.redraw(region: v.bounds, painter: Painter (from: v))
last = v
}
last?.positionCursor()
@@ -368,7 +368,8 @@ public class Application {
static func redrawView (_ view: View)
{
- view.redraw(region: view.bounds)
+ let painter = Painter (from: view)
+ view.redraw(region: view.bounds, painter: painter)
driver.refresh()
}
@@ -405,7 +406,7 @@ public class Application {
{
if let c = current {
if !c.needDisplay.isEmpty || c._childNeedsDisplay {
- c.redraw (region: c.bounds)
+ c.redraw (region: c.bounds, painter: Painter (from: c))
if debugDrawBounds {
drawBounds (c)
}
diff --git a/Sources/TermKit/Core/Painter.swift b/Sources/TermKit/Core/Painter.swift
index 2e40143..bfe1a8a 100644
--- a/Sources/TermKit/Core/Painter.swift
+++ b/Sources/TermKit/Core/Painter.swift
@@ -19,6 +19,11 @@ public class Painter {
/// The current drawing row
public private(set) var row: Int
+ /// Offset that this content is from the origin
+ public private(set) var colOffset: Int
+ /// Offset that this content is from the origin
+ public private(set) var rowOffset: Int
+
/// The attribute used to draw
public var attribute: Attribute? {
didSet {
diff --git a/Sources/TermKit/Core/Rect.swift b/Sources/TermKit/Core/Rect.swift
index de2ffa1..4041e51 100644
--- a/Sources/TermKit/Core/Rect.swift
+++ b/Sources/TermKit/Core/Rect.swift
@@ -123,7 +123,7 @@ public struct Rect: CustomDebugStringConvertible, Codable, Equatable {
public func contains (x: Int, y: Int) -> Bool
{
- return x >= left && x <= right && y >= top && y <= bottom
+ return x >= left && x < right && y >= top && y < bottom
}
public func contains (_ point: Point) -> Bool
diff --git a/Sources/TermKit/Core/View.swift b/Sources/TermKit/Core/View.swift
index 5f1ed11..6cc31c9 100644
--- a/Sources/TermKit/Core/View.swift
+++ b/Sources/TermKit/Core/View.swift
@@ -716,14 +716,15 @@ open class View: Responder, Hashable, CustomDebugStringConvertible {
*
* - Parameter region: The region to redraw, this is relative to the view itself.
*/
- open func redraw(region: Rect)
+ open func redraw(region: Rect, painter: Painter)
{
let clipRect = Rect (origin: Point.zero, size: frame.size)
for view in subviews {
if !view.needDisplay.isEmpty || view._childNeedsDisplay {
if view.frame.intersects(clipRect) && view.frame.intersects(region){
// TODO: optimize this by computing the intersection of region and view.Bounds
- view.redraw (region: view.bounds)
+ let childPainter = Painter (from: view)
+ view.redraw (region: view.bounds, painter: childPainter)
}
view.needDisplay = Rect.zero
view._childNeedsDisplay = false
diff --git a/Sources/TermKit/Core/Window.swift b/Sources/TermKit/Core/Window.swift
index b12ee29..6de6659 100644
--- a/Sources/TermKit/Core/Window.swift
+++ b/Sources/TermKit/Core/Window.swift
@@ -64,12 +64,10 @@ public class Window : Toplevel {
// TODO: removeAll
- public override func redraw(region: Rect) {
+ public override func redraw(region: Rect, painter p: Painter) {
//log ("Window.redraw: \(frame) and region to redraw is: \(region)")
if !needDisplay.isEmpty {
-
- let p = getPainter ()
p.attribute = colorScheme!.normal
p.drawFrame (bounds, padding: padding, fill: true)
@@ -86,7 +84,7 @@ public class Window : Toplevel {
}
p.attribute = colorScheme!.normal
}
- contentView.redraw(region: contentView.bounds)
+ contentView.redraw(region: contentView.bounds, painter: Painter (from: contentView))
clearNeedsDisplay()
}
diff --git a/Sources/TermKit/Views/Button.swift b/Sources/TermKit/Views/Button.swift
index b0d6573..d9fe803 100644
--- a/Sources/TermKit/Views/Button.swift
+++ b/Sources/TermKit/Views/Button.swift
@@ -106,8 +106,7 @@ public class Button : View {
setNeedsDisplay()
}
- public override func redraw(region: Rect) {
- let painter = getPainter()
+ public override func redraw(region: Rect, painter: Painter) {
painter.attribute = hasFocus ? colorScheme!.focus : colorScheme!.normal
painter.goto(col: 0, row: 0)
painter.add(str: shownText)
diff --git a/Sources/TermKit/Views/Checkbox.swift b/Sources/TermKit/Views/Checkbox.swift
index 0ee8e80..1f28a41 100644
--- a/Sources/TermKit/Views/Checkbox.swift
+++ b/Sources/TermKit/Views/Checkbox.swift
@@ -102,8 +102,7 @@ public class Checkbox : View {
updateHotkeySettings()
}
- public override func redraw(region: Rect) {
- let painter = getPainter()
+ public override func redraw(region: Rect, painter: Painter) {
painter.attribute = hasFocus ? colorScheme!.focus : colorScheme!.normal
painter.goto(col: 0, row: 0)
painter.add(str: checked ? "[x]" : "[ ]")
diff --git a/Sources/TermKit/Views/Frame.swift b/Sources/TermKit/Views/Frame.swift
index d07e4f8..379785d 100644
--- a/Sources/TermKit/Views/Frame.swift
+++ b/Sources/TermKit/Views/Frame.swift
@@ -50,9 +50,8 @@ public class FrameView : View {
// TODO: implement remove
// TODO: implement removeAll
- public override func redraw(region: Rect) {
+ public override func redraw(region: Rect, painter: Painter) {
if !needDisplay.isEmpty {
- let painter = getPainter ()
painter.attribute = colorScheme!.normal
painter.drawFrame (bounds, padding: 0, fill: true)
if hasFocus {
@@ -68,7 +67,7 @@ public class FrameView : View {
}
painter.attribute = colorScheme!.normal
}
- contentView.redraw(region: contentView.bounds)
+ contentView.redraw(region: contentView.bounds, painter: Painter (from: contentView))
clearNeedsDisplay()
}
diff --git a/Sources/TermKit/Views/Label.swift b/Sources/TermKit/Views/Label.swift
index a0453a5..97910b2 100644
--- a/Sources/TermKit/Views/Label.swift
+++ b/Sources/TermKit/Views/Label.swift
@@ -147,12 +147,11 @@ public class Label : View {
Label.recalc (text, lineResult: &lines, width: frame.width, align: textAlignment)
}
- public override func redraw(region: Rect) {
+ public override func redraw(region: Rect, painter: Painter) {
if recalcPending {
recalc ()
}
- let painter = getPainter()
painter.attribute = textAttribute ?? colorScheme.normal
painter.clear ()
painter.goto(col: 0, row: 0)
diff --git a/Sources/TermKit/Views/ListView.swift b/Sources/TermKit/Views/ListView.swift
index e697fe9..924259e 100644
--- a/Sources/TermKit/Views/ListView.swift
+++ b/Sources/TermKit/Views/ListView.swift
@@ -194,9 +194,7 @@ public class ListView : View {
}
}
- public override func redraw(region: Rect) {
- let painter = getPainter()
-
+ public override func redraw(region: Rect, painter: Painter) {
let n = dataSource.getCount (listView: self)
let b = bounds
let lines = bounds.height
diff --git a/Sources/TermKit/Views/Menu.swift b/Sources/TermKit/Views/Menu.swift
index 0742922..1928336 100644
--- a/Sources/TermKit/Views/Menu.swift
+++ b/Sources/TermKit/Views/Menu.swift
@@ -146,7 +146,7 @@ public class Menu : View {
canFocus = true
}
- public override func redraw(region: Rect) {
+ public override func redraw(region: Rect, painter: Painter) {
driver.setAttribute(colorScheme!.normal)
drawFrame(region, padding: 0, fill: true)
@@ -272,8 +272,7 @@ public class MenuBar: View {
colorScheme = Colors.menu
}
- public override func redraw(region: Rect) {
- let p = getPainter()
+ public override func redraw(region: Rect, painter p: Painter) {
p.goto(col: 0, row: 0)
p.attribute = Colors.base.focus
diff --git a/Sources/TermKit/Views/ProgressBar.swift b/Sources/TermKit/Views/ProgressBar.swift
index c24e0aa..cc4ca46 100644
--- a/Sources/TermKit/Views/ProgressBar.swift
+++ b/Sources/TermKit/Views/ProgressBar.swift
@@ -66,24 +66,23 @@ public class ProgressBar : View {
setNeedsDisplay()
}
- public override func redraw(region: Rect) {
- let paint = getPainter ()
- paint.attribute = colorScheme!.normal
+ public override func redraw(region: Rect, painter: Painter) {
+ painter.attribute = colorScheme!.normal
let top = frame.width
if isActivity {
- paint.goto(col: 0, row: 0)
+ painter.goto(col: 0, row: 0)
for i in 0..<top {
- paint.add (rune: i == activityPos ? driver.stipple : driver.space)
+ painter.add (rune: i == activityPos ? driver.stipple : driver.space)
}
} else {
- paint.goto(col: 0, row: 0)
+ painter.goto(col: 0, row: 0)
let mid = Int (fraction * Float (top))
for _ in 0..<mid {
- paint.add(rune: driver.stipple)
+ painter.add(rune: driver.stipple)
}
for _ in mid..<top {
- paint.add (rune: driver.space)
+ painter.add (rune: driver.space)
}
}
}
diff --git a/Sources/TermKit/Views/RadioGroup.swift b/Sources/TermKit/Views/RadioGroup.swift
index 6d67e50..a01f41b 100644
--- a/Sources/TermKit/Views/RadioGroup.swift
+++ b/Sources/TermKit/Views/RadioGroup.swift
@@ -98,10 +98,9 @@ public class RadioGroup: View {
canFocus = true
}
- public override func redraw(region: Rect) {
- let paint = getPainter ()
- paint.attribute = colorScheme!.normal
- paint.clear()
+ public override func redraw(region: Rect, painter: Painter) {
+ painter.attribute = colorScheme!.normal
+ painter.clear()
switch orientation {
case .horizontal:
@@ -110,11 +109,11 @@ public class RadioGroup: View {
break
case .vertical:
for line in 0..<radioLabels.count {
- paint.goto(col: 0, row: line)
+ painter.goto(col: 0, row: line)
- paint.attribute = hasFocus && (line == cursor) ? colorScheme.focus : colorScheme.normal
- paint.add(str: line == selected ? "(x) " : "( ) ")
- paint.drawHotString(
+ painter.attribute = hasFocus && (line == cursor) ? colorScheme.focus : colorScheme.normal
+ painter.add(str: line == selected ? "(x) " : "( ) ")
+ painter.drawHotString(
text: radioLabels[line],
focused: hasFocus && line == cursor,
scheme: colorScheme!)
diff --git a/Sources/TermKit/Views/ScrollView.swift b/Sources/TermKit/Views/ScrollView.swift
index b2da38e..142ad2b 100644
--- a/Sources/TermKit/Views/ScrollView.swift
+++ b/Sources/TermKit/Views/ScrollView.swift
@@ -71,8 +71,7 @@ public class ScrollBarView : View {
}
}
- public override func redraw(region: Rect) {
- let paint = getPainter ()
+ public override func redraw(region: Rect, painter paint: Painter) {
paint.attribute = colorScheme!.normal
if isVertical {
if region.right < bounds.width - 1 {
@@ -216,6 +215,19 @@ public class ScrollBarView : View {
}
}
+class _ContentView: View {
+ var scrollView: ScrollView
+ init (scrollView: ScrollView)
+ {
+ self.scrollView = scrollView
+ super.init()
+ }
+
+ public override func redraw(region: Rect, painter: Painter) {
+
+ super.redraw(region: region, painter: painter)
+ }
+}
/// Scrollviews are views that present a window into a virtual space where
/// subviews are added. Similar to the iOS UIScrollView.
@@ -233,21 +245,24 @@ public class ScrollView : View {
public override init ()
{
contentView = View ()
+ contentView.canFocus = true
vertical = ScrollBarView (size: 0, position: 0, isVertical: true)
horizontal = ScrollBarView (size: 0, position: 0, isVertical: false)
super.init()
horizontal.changedPosition = { sender, old, new in
-
+ self.contentOffset = Point(x: new, y: self.contentOffset.y)
}
vertical.changedPosition = { sender, old, new in
-
+ self.contentOffset = Point(x: self.contentOffset.x, y: new)
}
super.addSubview(contentView)
canFocus = true
}
- public override func redraw(region: Rect) {
- super.redraw(region: region)
+ public override func redraw(region: Rect, painter: Painter) {
+ let oldClip = clipToBounds()
+ super.redraw(region: region, painter: painter)
+ driver.clip = oldClip
}
public override func layoutSubviews () {
diff --git a/Sources/TermKit/Views/TextField.swift b/Sources/TermKit/Views/TextField.swift
index f30055a..8f019dd 100644
--- a/Sources/TermKit/Views/TextField.swift
+++ b/Sources/TermKit/Views/TextField.swift
@@ -165,9 +165,7 @@ public class TextField : View {
}
}
- public override func redraw(region: Rect) {
- let p = getPainter ()
-
+ public override func redraw(region: Rect, painter p: Painter) {
p.attribute = colorScheme!.focus
p.goto(col:0, row: 0)
diff --git a/Sources/TermKit/Views/TextView.swift b/Sources/TermKit/Views/TextView.swift
index 431f70f..d88bb41 100644
--- a/Sources/TermKit/Views/TextView.swift
+++ b/Sources/TermKit/Views/TextView.swift
@@ -284,8 +284,7 @@ public class TextView : View {
setNeedsDisplay()
}
- public override func redraw(region: Rect) {
- let p = getPainter ()
+ public override func redraw(region: Rect, painter p: Painter) {
p.colorNormal()
let bottom = region.bottom
let right = region.right
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment