dymokomi/luce-ui
Source of release 0.19.2
0.19.2 / src / docking / state / floating.lucb
#==============================================================================================
#
# docking/state/floating - Floating windows
#
# DESCRIPTION:
# A floating panel leaves the model and sits in a window inside the stack, over the
# panes: these State methods place, raise, move, resize, dock and draw those windows;
# the frame widget in frame.lucb hands them the presses on a window's chrome.
#
#==============================================================================================
extend State:
## Give the state its own reference, for the frames it makes.
pub func set_owner(owner: interop.Reference[State]):
if let previous = self.me: previous.release()
self.me = interop.WeakReference[State](owner)
## A floating panel's window, title bar included.
pub func float_bounds(index: usize) -> gpu.Rect:
return self.floats[index]
pub func floating_count() -> usize:
return self.float_count
pub func floating(index: usize) -> bool:
if index == 0: return false
for order in 0..<self.float_count:
if self.stacking[order] == index: return true
return false
## A floating window's title bar, close button and content, in stack coordinates.
pub func float_title(index: usize) -> gpu.Rect:
let window = self.floats[index]
return gpu.Rect(window.x, window.y, window.width, self.height() + 1.0)
pub func float_close(index: usize) -> gpu.Rect:
let title = self.float_title(index)
let extent = math.min(title.width, title.height)
return gpu.Rect(title.x + title.width - extent, title.y, extent, extent)
pub func float_content(index: usize) -> gpu.Rect:
let window = self.floats[index]
let top = self.height() + 1.0
return gpu.Rect(window.x + float_border, window.y + top, math.max(0.0, window.width - float_border * 2.0),
math.max(0.0, window.height - top - float_border))
## The smallest window that holds the panel's content.
func float_minimum(index: usize) -> Size:
var minimum = Size(120.0, 60.0)
if let panel = self.panels[index]:
let box = panel.value().layout()
defer box.release()
let content = box.value().minimum_size()
minimum = Size(math.max(minimum.width, content.width), math.max(minimum.height, content.height))
return Size(minimum.width + float_border * 2.0, minimum.height + self.height() + 1.0 + float_border)
## `window` no smaller than its panel needs, no larger than the stack, and inside it.
func fitted(index: usize, window: gpu.Rect) -> gpu.Rect:
let size = self.size()
let minimum = self.float_minimum(index)
let width = math.min(math.max(window.width, minimum.width), math.max(minimum.width, size.width))
let height = math.min(math.max(window.height, minimum.height), math.max(minimum.height, size.height))
return gpu.Rect(math.clamp(window.x, 0.0, math.max(0.0, size.width - width)),
math.clamp(window.y, 0.0, math.max(0.0, size.height - height)), width, height)
## The window a tab floats into when let go at `pointer`: about its pane's
## size, with the title under the pointer.
func float_window(index: usize, pointer: Point) -> gpu.Rect:
let size = self.size()
var width = size.width * 0.35
var height = size.height * 0.5
let group = self.model.tabs[index].group
if group != 0:
let pane = self.geometry_for(self.model, size).panes[group]
width = math.min(pane.width, size.width * 0.5)
height = math.min(pane.height, size.height * 0.6)
return self.fitted(index, gpu.Rect(pointer.x - math.min(60.0, width * 0.5), pointer.y - self.height() * 0.5, width, height))
## A window for a panel that has none yet: centered, tall, as wide as it asks.
pub func default_window(index: usize) -> gpu.Rect:
let size = self.size()
let width = math.max(self.float_minimum(index).width, 360.0)
let height = size.height * 0.7
return self.fitted(index, gpu.Rect((size.width - width) * 0.5, (size.height - height) * 0.5, width, height))
func unstack(index: usize):
var kept: usize = 0
for order in 0..<self.float_count:
if self.stacking[order] != index:
self.stacking[kept] = self.stacking[order]
kept += 1
self.float_count = kept
func stack_on_top(index: usize):
self.unstack(index)
self.stacking[self.float_count] = index
self.float_count += 1
## Bring a floating window to the front.
func raise_window(index: usize) -> !:
if self.float_count > 0 and self.stacking[self.float_count - 1] == index: return
self.stack_on_top(index)
try self.children()
self.invalidate()
func ensure_frame(index: usize) -> !:
if self.frames[index] != none: return
let me = self.me else error(result.invalid, "dynamic stack state has no owner")
let owner = me.get() else error(result.invalid, "dynamic stack is closed")
defer owner.release()
self.frames[index] = try make_frame(owner, index)
## Float a docked panel in `window`, or move a floating one there.
pub func make_floating(index: usize, window: gpu.Rect) -> !:
self.cancel()
if self.floating(index):
self.floats[index] = self.fitted(index, window)
try self.select(index)
return
var next = self.model
try next.remove(index)
try self.ensure_frame(index)
self.floats[index] = self.fitted(index, window)
self.stack_on_top(index)
errdefer self.unstack(index)
try self.children()
self.publish(next)
try self.select(index)
## Add a panel that is not in the stack yet as a floating window: in
## `window`, or centered when none.
pub func add_floating(panel: interop.Reference[Panel], window: gpu.Rect?) -> !:
self.cancel()
var slot: usize = 0
for index in 1..<self.panels.length:
if self.panels[index] == none:
slot = index
break
if slot == 0: error(result.limit, "a dynamic stack supports at most 128 panels")
try self.ensure_frame(slot)
self.stack_on_top(slot)
errdefer self.unstack(slot)
try self.children(slot, panel)
self.panels[slot] = panel.clone()
self.floats[slot] = self.default_window(slot)
if let chosen = window: self.floats[slot] = self.fitted(slot, chosen)
self.show_visible()
try self.select(slot)
## Dock a floating panel: `next` is the model with it already added.
func dock(index: usize, next: Model) -> !:
self.unstack(index)
errdefer self.stack_on_top(index)
try self.children()
self.publish(next)
try self.select(index)
## The window edges at a point: 1 left, 2 right, 4 top, 8 bottom. A corner
## grip at the bottom right is wider than the border.
func edges_at(index: usize, x: f64, y: f64) -> u8:
let window = self.floats[index]
let right = window.x + window.width
let bottom = window.y + window.height
if x >= right - 12.0 and y >= bottom - 12.0: return 10
var edges: u8 = 0
if x < window.x + float_border: edges |= 1
elif x >= right - float_border: edges |= 2
if y < window.y + 3.0: edges |= 4
elif y >= bottom - float_border: edges |= 8
return edges
## A pointer event on floating window `index`'s frame, at stack coordinates:
## the title moves the window and, let go over a header or a pane's edge,
## docks it there; a double press on the title docks it with the active
## group; the border resizes it.
pub func float_event(index: usize, event: Event, x: f64, y: f64) -> !:
if event.cancelled:
if self.gesture == Gesture.moving and self.float_pressed == index: self.floats[index] = self.float_origin
self.gesture = Gesture.idle
self.float_pressed = 0
self.invalidate()
return
if not self.floating(index): return
if event.kind == input.EventKind.pointer_down and event.button == 0:
self.cancel()
try self.select(index)
self.float_pressed = index
self.start = Point(x, y)
self.float_origin = self.floats[index]
self.float_moved = false
self.edges = self.edges_at(index, x, y)
if self.edges != 0:
self.gesture = Gesture.resizing
return
let panel = try self.panel(index)
defer panel.release()
if panel.value().is_closable() and strip.contains(self.float_close(index), x, y):
self.gesture = Gesture.closing
return
if strip.contains(self.float_title(index), x, y):
# A second click on the title soon after a first, neither of them a drag.
if self.last_pressed == index and time.now() - self.last_press < double_press:
self.last_press = 0
self.cancel()
var next = self.model
try next.add(index, 0 if next.root == 0 else next.active, DockPosition.tab)
try self.dock(index, next)
return
self.gesture = Gesture.moving
self.last_press = 0
return
if self.float_pressed != index: return
let dx = x - self.start.x
let dy = y - self.start.y
let origin = self.float_origin
if event.kind == input.EventKind.pointer_moved:
if self.gesture == Gesture.moving:
self.floats[index] = self.fitted(index, gpu.Rect(origin.x + dx, origin.y + dy, origin.width, origin.height))
self.float_moved = self.float_moved or dx * dx + dy * dy >= 16.0
self.drag_pointer = Point(x, y)
self.invalidate()
elif self.gesture == Gesture.resizing:
let minimum = self.float_minimum(index)
var window = origin
if (self.edges & 1) != 0:
let shift = math.min(dx, origin.width - minimum.width)
window.x = origin.x + shift
window.width = origin.width - shift
if (self.edges & 2) != 0: window.width = origin.width + dx
if (self.edges & 4) != 0:
let shift = math.min(dy, origin.height - minimum.height)
window.y = origin.y + shift
window.height = origin.height - shift
if (self.edges & 8) != 0: window.height = origin.height + dy
self.floats[index] = self.fitted(index, window)
self.invalidate()
elif event.kind == input.EventKind.pointer_up and event.button == 0:
let gesture = self.gesture
let moved = self.float_moved
self.last_press = time.now() if gesture == Gesture.moving and not moved else 0
self.last_pressed = index
self.gesture = Gesture.idle
self.float_pressed = 0
self.float_moved = false
if gesture == Gesture.closing and strip.contains(self.float_close(index), x, y):
try self.request_close(index)
elif gesture == Gesture.moving and moved:
let drop = self.drop_for(index, self.model, x, y, false)
if drop.accepted:
try self.dock(index, try self.inserted(index, self.model, drop, x, y))
self.invalidate()
## The pointer over floating window `index` at stack coordinates.
pub func float_cursor(index: usize, x: f64, y: f64) -> input.Cursor:
if self.gesture == Gesture.moving and self.float_pressed == index: return input.Cursor.grabbing
let edges = self.edges if self.gesture == Gesture.resizing and self.float_pressed == index else self.edges_at(index, x, y)
if (edges & 12) != 0 and (edges & 3) == 0: return input.Cursor.resize_vertical
if edges != 0: return input.Cursor.resize_horizontal
if let panel = self.panels[index]:
if panel.value().is_closable() and strip.contains(self.float_close(index), x, y): return input.Cursor.pointer
if strip.contains(self.float_title(index), x, y): return input.Cursor.grab
return input.Cursor.arrow
## A floating window's chrome, in its own coordinates: border, title bar
## with the panel's name and close button, and the surface under the content.
pub func draw_float(index: usize, target: interop.View[gpu.RenderTarget], pointer: Point, hovered: bool) -> !:
let panel = try self.panel(index)
defer panel.release()
let content = panel.value().layout()
defer content.release()
var painter = try Painter(target)
defer painter.close()
let theme = self.theme()
let window = self.floats[index]
let title = self.height()
try painter.rectangle(gpu.Rect(width = window.width, height = window.height), theme.active_border if content.value().contains_focus() else theme.strip)
try painter.rectangle(gpu.Rect(1.0, 1.0, math.max(0.0, window.width - 2.0), math.max(0.0, window.height - 2.0)), theme.panel)
try painter.rectangle(gpu.Rect(1.0, 1.0, math.max(0.0, window.width - 2.0), title - 1.0), theme.strip)
try painter.rectangle(gpu.Rect(1.0, title, math.max(0.0, window.width - 2.0), 1.0), theme.background)
let close = self.float_close(index)
let closing = panel.value().is_closable()
let label_end = close.x - window.x if closing else window.width
let inset = theme.inset(self.font)
let label = panel.value().text()
defer label.release()
let clipped = try target.get().clipped(gpu.Rect(inset, 0.0, math.max(0.0, label_end - inset * 2.0), title))
defer clipped.release()
var labels = try Painter(clipped)
defer labels.close()
try labels.text(label.value, inset, (title - self.font.value().height()) * 0.5, theme.foreground, self.font)
if closing:
let button = gpu.Rect(close.x - window.x, close.y - window.y, close.width, close.height)
if hovered and strip.contains(button, pointer.x, pointer.y):
try painter.rectangle(button, theme.hover())
let middle = Point(button.x + button.width * 0.5, button.y + button.height * 0.5)
let extent = self.font.value().advance() * 0.3
try painter.line(Point(middle.x - extent, middle.y - extent), Point(middle.x + extent, middle.y + extent), 1.0, theme.muted)
try painter.line(Point(middle.x - extent, middle.y + extent), Point(middle.x + extent, middle.y - extent), 1.0, theme.muted)