dymokomi/luce-ui
Source of release 0.19.2
0.19.2 / src / widgets / text_editor / view.lucb
#==============================================================================================
#
# widgets/text_editor/view - Layout and drawing: the projection, scrolling, wrap and painting
#
#==============================================================================================
extend TextEditor:
## The visual rows shown: lines left after folding, split further by soft wrap.
pub func visible_line_count() -> i64:
return (i64)self.projection.visible_count()
pub func wrapping() -> bool:
return self.wrap
## Soft wrap long lines to the viewport width, or restore horizontal scrolling.
## The caret keeps its place and is revealed on its new visual row.
pub func set_wrap(enabled: bool) -> !:
if self.wrap == enabled:
return
self.wrap = enabled
self.scroll_x = 0.0
try self.refresh_view()
try self.reveal_caret()
self.box.value().invalidate()
## The horizontal and vertical scroll in points, clamped to the content.
pub func scroll_offset() -> Size:
return Size(math.clamp(self.scroll_x, 0.0, self.maximum_scroll_x()), math.clamp(self.scroll_y, 0.0, self.maximum_scroll()))
## Columns per visual row when wrapping, or 0 to disable it. One column is
## reserved so the caret past the last glyph never sits under the scrollbar.
func wrap_columns() -> usize:
if not self.wrap:
return 0
let columns = (usize)(self.content_width() / self.font.value().advance())
return columns if columns > 1 else 1
func refresh_view() -> !:
try self.projection.refresh(&self.document, &self.folds, self.wrap_columns())
try self.refresh_diff()
self.clamp_scroll()
## Recompute change bars only when the text changed since the last diff.
func refresh_diff() -> !:
if let baseline = self.baseline:
if self.diff_version != (i64)self.document.version():
let current = try self.document.text()
defer current.release()
try self.diff.compute(baseline.value, current.value, (i64)self.document.version())
self.diff_version = (i64)self.document.version()
func finish_folding() -> !:
try self.refresh_view()
if self.projection.is_hidden(self.document.caret()):
let row = self.projection.physical_row(self.projection.visual_row(self.document.caret()))
let offset = self.projection.line(row).end
try self.document.select(offset, offset)
self.clamp_scroll()
self.box.value().invalidate()
pub func measure(available: Constraints) -> interop.Outcome[Size]:
return result.success[Size](Size(640.0, 360.0))
pub func draw(target: interop.View[gpu.RenderTarget]) -> interop.Outcome[unit]:
self.paint(target) catch failure:
return result.failure[unit](failure.code, failure.message)
return result.success[unit](())
func paint(target: interop.View[gpu.RenderTarget]) -> !:
try self.poll_completion()
try self.refresh_view()
try self.show_due_tip()
var painter = try Painter(target)
defer painter.close()
let size = try target.get().size()
var hovered_row: usize? = none
let pointer = self.box.value().pointer_position()
if self.box.value().contains_pointer() and pointer.x < self.gutter_width():
hovered_row = (usize)((pointer.y + self.scroll_y) / self.font.value().line_height())
# The caret shows 600 ms of each second; the frame loop wakes for the next change.
let phase = time.since(self.active_since) % 1000000000
if self.box.value().is_focused():
layout.wake_at(time.now() + (600000000 - phase if phase < 600000000 else 1000000000 - phase))
try drawing.paint(&self.document, &painter, size.width, size.height, self.scroll_x, self.scroll_y,
self.line_numbers, self.box.value().is_focused(), phase < 600000000,
self.font, (self.theme else self.box.value().theme().editor()), self.highlights.values(), &self.projection, &self.folds, &self.diff, hovered_row,
(usize)self.ai_first, (usize)self.ai_last, self.swatches.values(), not self.wrap,
&self.diagnostics)
func maximum_scroll() -> f64:
return math.max(0.0, (f64)self.projection.visible_count() * self.font.value().line_height() + drawing.vertical_padding(self.font) * 2.0 - self.box.value().bounds().height)
func scroll_to_pointer(y: f64):
let height = self.box.value().bounds().height
self.scroll_y = math.clamp(y / math.max(1.0, height), 0.0, 1.0) * self.maximum_scroll()
func gutter_width() -> f64:
return drawing.gutter_width(self.line_numbers, self.font, self.projection.line_count(), self.folds.values().length > 0)
func content_width() -> f64:
return math.max(1.0, self.box.value().bounds().width - self.gutter_width() - self.font.value().advance() - 8.0)
func maximum_scroll_x() -> f64:
if self.wrap:
return 0.0
return math.max(0.0, (f64)(self.projection.maximum_columns() + 1) * self.font.value().advance() - self.content_width())
func clamp_scroll():
self.scroll_x = math.clamp(self.scroll_x, 0.0, self.maximum_scroll_x())
self.scroll_y = math.clamp(self.scroll_y, 0.0, self.maximum_scroll())
func reveal_caret() -> !:
self.folds.reveal(self.document.caret())
try self.refresh_view()
if self.projection.is_hidden(self.document.caret()):
self.folds.reveal(self.document.caret(), include_end = true)
try self.refresh_view()
let bounds = self.box.value().bounds()
let y = (f64)self.projection.visual_row(self.document.caret()) * self.font.value().line_height()
let height = math.max(self.font.value().line_height(), bounds.height - drawing.vertical_padding(self.font) * 2.0)
if y < self.scroll_y:
self.scroll_y = y
if y + self.font.value().line_height() > self.scroll_y + height:
self.scroll_y = y + self.font.value().line_height() - height
self.scroll_y = math.clamp(self.scroll_y, 0.0, self.maximum_scroll())
if not self.wrap:
let x = (f64)editing.column(&self.document, self.document.caret(), self.active_swatches()) * self.font.value().advance()
let width = self.content_width()
if x < self.scroll_x:
self.scroll_x = x
if x + self.font.value().advance() > self.scroll_x + width:
self.scroll_x = x + self.font.value().advance() - width
self.clamp_scroll()