wip
This commit is contained in:
@@ -3,15 +3,11 @@ import gleam/float
|
||||
import gleam/int
|
||||
import gleam/list
|
||||
import gleam/string
|
||||
import gleam/string_tree.{type StringTree}
|
||||
import gleam/string_tree
|
||||
|
||||
import musicplayer/logging/logging
|
||||
import musicplayer/ui/internal
|
||||
|
||||
pub type Layout {
|
||||
Layout(width: Int, height: Int, nodes: dict.Dict(Section, Node))
|
||||
}
|
||||
|
||||
pub type Section {
|
||||
Section(String)
|
||||
|
||||
@@ -21,22 +17,6 @@ pub type Section {
|
||||
PlaybackTime
|
||||
}
|
||||
|
||||
// pub type Section {
|
||||
// Root
|
||||
// Header
|
||||
// Search
|
||||
// PlaybackTime
|
||||
// Test
|
||||
|
||||
// Row1
|
||||
// A
|
||||
// B
|
||||
|
||||
// Row2
|
||||
// C
|
||||
// D
|
||||
// }
|
||||
|
||||
pub type NodeType {
|
||||
Container
|
||||
Row
|
||||
@@ -54,6 +34,10 @@ pub type Node {
|
||||
)
|
||||
}
|
||||
|
||||
pub type Layout {
|
||||
Layout(width: Int, height: Int, nodes: dict.Dict(Section, Node))
|
||||
}
|
||||
|
||||
pub fn new() -> Layout {
|
||||
let nodes =
|
||||
dict.from_list([
|
||||
@@ -154,8 +138,18 @@ pub fn render(layout: Layout) -> Nil {
|
||||
let container_top_left_x = 1
|
||||
let container_top_left_y = 1
|
||||
|
||||
let ansi_ops =
|
||||
RenderOps(
|
||||
draw_box: fn(tree, x, y, w, h) {
|
||||
string_tree.append(tree, draw_box(x, y, w, h))
|
||||
},
|
||||
draw_text: fn(tree, text, x, y) {
|
||||
string_tree.append(tree, internal.chars_at(text, x, y))
|
||||
},
|
||||
)
|
||||
|
||||
string_tree.new()
|
||||
|> render_loop(
|
||||
|> render_generic(
|
||||
layout,
|
||||
container_width,
|
||||
container_height,
|
||||
@@ -164,53 +158,39 @@ pub fn render(layout: Layout) -> Nil {
|
||||
0,
|
||||
Section("Root"),
|
||||
_,
|
||||
ansi_ops,
|
||||
)
|
||||
|> string_tree.to_string
|
||||
|> internal.print
|
||||
}
|
||||
|
||||
pub fn render_loop(
|
||||
pub type RenderOps(ctx) {
|
||||
RenderOps(
|
||||
draw_box: fn(ctx, Int, Int, Int, Int) -> ctx,
|
||||
draw_text: fn(ctx, String, Int, Int) -> ctx,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn render_generic(
|
||||
layout: Layout,
|
||||
// Dimensions
|
||||
container_width: Float,
|
||||
container_height: Float,
|
||||
container_top_left_x: Int,
|
||||
container_top_left_y: Int,
|
||||
container_tl_x: Int,
|
||||
container_tl_y: Int,
|
||||
// State
|
||||
index: Int,
|
||||
from: Section,
|
||||
tree: StringTree,
|
||||
) -> StringTree {
|
||||
let margin = 2.0
|
||||
|
||||
current_ctx: ctx,
|
||||
// <--- Generic State
|
||||
ops: RenderOps(ctx),
|
||||
// <--- The Strategy
|
||||
) -> ctx {
|
||||
case dict.get(layout.nodes, from) {
|
||||
Error(_) -> tree
|
||||
Error(_) -> current_ctx
|
||||
Ok(node) -> {
|
||||
let final_tree =
|
||||
list.index_map(node.children, fn(child, i) { #(i, child) })
|
||||
|> list.fold(tree, fn(updated_tree: StringTree, ic: #(Int, Section)) {
|
||||
let #(i, child) = ic
|
||||
|
||||
let cw =
|
||||
container_width
|
||||
*. { int.to_float(node.width_percent) /. 100.0 }
|
||||
-. margin
|
||||
|> float.floor
|
||||
|
||||
let ch =
|
||||
container_height
|
||||
*. { int.to_float(node.height_percent) /. 100.0 }
|
||||
-. margin
|
||||
|> float.floor
|
||||
|
||||
let cx = container_top_left_x + 1
|
||||
let cy = container_top_left_y + 1
|
||||
render_loop(layout, cw, ch, cx, cy, i, child, updated_tree)
|
||||
})
|
||||
|
||||
logging.log("section: " <> string.inspect(from))
|
||||
logging.log("section type: " <> string.inspect(node.t))
|
||||
logging.log("index: " <> string.inspect(index))
|
||||
logging.log("container width: " <> float.to_string(container_width))
|
||||
logging.log("container height: " <> float.to_string(container_height))
|
||||
// --- 1. MATH (Shared Logic) ---
|
||||
let margin = 2.0
|
||||
|
||||
let width =
|
||||
container_width *. { int.to_float(node.width_percent) /. 100.0 }
|
||||
@@ -222,28 +202,51 @@ pub fn render_loop(
|
||||
|> float.floor
|
||||
|> float.truncate
|
||||
|
||||
logging.log("section width: " <> int.to_string(width))
|
||||
logging.log("section height: " <> int.to_string(height))
|
||||
|
||||
let #(cx, cy) = case node.t {
|
||||
Container -> #(container_top_left_x, container_top_left_y)
|
||||
Row -> #(
|
||||
container_top_left_x,
|
||||
container_top_left_y + { index * height },
|
||||
)
|
||||
Cell -> #(
|
||||
container_top_left_x + { index * width },
|
||||
container_top_left_y,
|
||||
)
|
||||
Container -> #(container_tl_x, container_tl_y)
|
||||
Row -> #(container_tl_x, container_tl_y + { index * height })
|
||||
Cell -> #(container_tl_x + { index * width }, container_tl_y)
|
||||
}
|
||||
|
||||
logging.log("cx: " <> int.to_string(cx))
|
||||
logging.log("cy: " <> int.to_string(cy))
|
||||
// --- 2. RENDER PARENT (Using Generic Ops) ---
|
||||
// We modify the context using the provided functions
|
||||
let ctx_with_parent =
|
||||
current_ctx
|
||||
|> ops.draw_box(cx, cy, width, height)
|
||||
|> ops.draw_text(node.content, cx, cy)
|
||||
|
||||
final_tree
|
||||
|> string_tree.append(draw_box(cx, cy, width, height))
|
||||
// Box heading
|
||||
|> string_tree.append(internal.chars_at(node.content, cx, cy))
|
||||
// --- 3. RECURSE CHILDREN ---
|
||||
list.index_map(node.children, fn(child, i) { #(i, child) })
|
||||
|> list.fold(ctx_with_parent, fn(acc_ctx, ic) {
|
||||
let #(i, child) = ic
|
||||
|
||||
let cw =
|
||||
container_width
|
||||
*. { int.to_float(node.width_percent) /. 100.0 }
|
||||
-. margin
|
||||
|> float.floor
|
||||
|
||||
let ch =
|
||||
container_height
|
||||
*. { int.to_float(node.height_percent) /. 100.0 }
|
||||
-. margin
|
||||
|> float.floor
|
||||
|
||||
let child_origin_x = container_tl_x + 1
|
||||
let child_origin_y = container_tl_y + 1
|
||||
|
||||
render_generic(
|
||||
layout,
|
||||
cw,
|
||||
ch,
|
||||
child_origin_x,
|
||||
child_origin_y,
|
||||
i,
|
||||
child,
|
||||
acc_ctx,
|
||||
ops,
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
3
src/musicplayer/ui/layout_examples/one.gleam
Normal file
3
src/musicplayer/ui/layout_examples/one.gleam
Normal file
@@ -0,0 +1,3 @@
|
||||
pub fn main() {
|
||||
echo "hello"
|
||||
}
|
||||
Reference in New Issue
Block a user