This commit is contained in:
Alexander Heldt
2025-12-05 20:00:36 +01:00
parent 6a4486c407
commit fff9e32895
3 changed files with 61 additions and 81 deletions

View File

@@ -18,19 +18,14 @@ pub type Section {
PlaybackTime PlaybackTime
} }
pub type NodeType {
Container
Row
Cell
}
pub type Style { pub type Style {
Style(width_percent: Int, height_percent: Int) Style(width_percent: Int, height_percent: Int)
} }
/// A Nodes width and height is in percentage (of the available width/height of its parent Node) /// A Nodes width and height is in percentage (of the available width/height of its parent Node)
pub type Node { pub type Node {
Node(t: NodeType, content: String, style: Style, children: List(Section)) Row(content: String, style: Style, children: List(Section))
Cell(content: String, style: Style)
} }
pub type Layout { pub type Layout {
@@ -42,8 +37,7 @@ pub fn new() -> Layout {
dict.from_list([ dict.from_list([
#( #(
Root, Root,
Node( Row(
t: Container,
content: "Music Player", content: "Music Player",
style: Style(width_percent: 100, height_percent: 100), style: Style(width_percent: 100, height_percent: 100),
children: [Header, Search, PlaybackTime], children: [Header, Search, PlaybackTime],
@@ -51,8 +45,7 @@ pub fn new() -> Layout {
), ),
#( #(
Header, Header,
Node( Row(
t: Row,
content: "Foo (1) | Bar (2) | Baz (3)", content: "Foo (1) | Bar (2) | Baz (3)",
style: Style(width_percent: 100, height_percent: 33), style: Style(width_percent: 100, height_percent: 33),
children: [], children: [],
@@ -60,8 +53,7 @@ pub fn new() -> Layout {
), ),
#( #(
Search, Search,
Node( Row(
t: Row,
content: "", content: "",
style: Style(width_percent: 100, height_percent: 33), style: Style(width_percent: 100, height_percent: 33),
children: [], children: [],
@@ -69,8 +61,7 @@ pub fn new() -> Layout {
), ),
#( #(
PlaybackTime, PlaybackTime,
Node( Row(
t: Row,
content: "00:00", content: "00:00",
style: Style(width_percent: 100, height_percent: 33), style: Style(width_percent: 100, height_percent: 33),
children: [], children: [],
@@ -87,15 +78,14 @@ pub fn update_section(
) -> Layout { ) -> Layout {
case dict.get(layout.nodes, section) { case dict.get(layout.nodes, section) {
Error(_) -> layout Error(_) -> layout
Ok(node) -> Ok(node) -> {
Layout( let updated = case node {
..layout, Cell(..) -> Cell(..node, content: content)
nodes: dict.insert( Row(..) -> Row(..node, content: content)
layout.nodes, }
section,
Node(..node, content: content), Layout(..layout, nodes: dict.insert(layout.nodes, section, updated))
), }
)
} }
} }
@@ -177,10 +167,9 @@ pub fn render_generic(
|> float.floor |> float.floor
|> float.truncate |> float.truncate
let #(cx, cy) = case node.t { let #(cx, cy) = case node {
Container -> #(container_tl_x, container_tl_y) Row(..) -> #(container_tl_x, container_tl_y + { index * height })
Row -> #(container_tl_x, container_tl_y + { index * height }) Cell(..) -> #(container_tl_x + { index * width }, container_tl_y)
Cell -> #(container_tl_x + { index * width }, container_tl_y)
} }
let parent = let parent =
@@ -189,7 +178,10 @@ pub fn render_generic(
// + 2 for header margin // + 2 for header margin
|> renders.text(node.content, cx, cy) |> renders.text(node.content, cx, cy)
list.index_map(node.children, fn(child, i) { #(i, child) }) case node {
Cell(..) -> parent
Row(children:, ..) -> {
list.index_map(children, fn(child, i) { #(i, child) })
|> list.fold(parent, fn(acc_into, ic) { |> list.fold(parent, fn(acc_into, ic) {
let #(i, child) = ic let #(i, child) = ic
@@ -223,3 +215,5 @@ pub fn render_generic(
} }
} }
} }
}
}

View File

@@ -1,7 +1,7 @@
import gleam/dict import gleam/dict
import musicplayer/ui/internal import musicplayer/ui/internal
import musicplayer/ui/layout.{Container, Layout, Node, Section, Style} import musicplayer/ui/layout.{Layout, Section, Style}
import musicplayer/ui/layout_examples/wait_for_input.{wait_for_input} import musicplayer/ui/layout_examples/wait_for_input.{wait_for_input}
pub fn main() { pub fn main() {
@@ -22,8 +22,7 @@ fn two_rows_with_cells(width: Int, height: Int) -> layout.Layout {
dict.from_list([ dict.from_list([
#( #(
Section("Root"), Section("Root"),
Node( layout.Row(
t: Container,
content: "container", content: "container",
style: Style(width_percent: 100, height_percent: 100), style: Style(width_percent: 100, height_percent: 100),
children: [ children: [
@@ -34,8 +33,7 @@ fn two_rows_with_cells(width: Int, height: Int) -> layout.Layout {
), ),
#( #(
Section("Row1"), Section("Row1"),
Node( layout.Row(
t: layout.Row,
content: "row 1", content: "row 1",
style: Style(width_percent: 100, height_percent: 50), style: Style(width_percent: 100, height_percent: 50),
children: [ children: [
@@ -46,26 +44,21 @@ fn two_rows_with_cells(width: Int, height: Int) -> layout.Layout {
), ),
#( #(
Section("A"), Section("A"),
Node( layout.Cell(
t: layout.Cell,
content: "cell 1", content: "cell 1",
style: Style(width_percent: 50, height_percent: 100), style: Style(width_percent: 50, height_percent: 100),
children: [],
), ),
), ),
#( #(
Section("B"), Section("B"),
Node( layout.Cell(
t: layout.Cell,
content: "cell 2", content: "cell 2",
style: Style(width_percent: 50, height_percent: 100), style: Style(width_percent: 50, height_percent: 100),
children: [],
), ),
), ),
#( #(
Section("Row2"), Section("Row2"),
Node( layout.Row(
t: layout.Row,
content: "row 2", content: "row 2",
style: Style(width_percent: 100, height_percent: 50), style: Style(width_percent: 100, height_percent: 50),
children: [], children: [],

View File

@@ -5,7 +5,7 @@ import gleeunit
import gleeunit/should import gleeunit/should
import musicplayer/ui/virtual_ansi import musicplayer/ui/virtual_ansi
import musicplayer/ui/layout.{Layout, Node, Section, Style} import musicplayer/ui/layout.{Layout, Section, Style}
pub fn main() -> Nil { pub fn main() -> Nil {
gleeunit.main() gleeunit.main()
@@ -19,8 +19,7 @@ pub fn foo_test() {
nodes: dict.from_list([ nodes: dict.from_list([
#( #(
Section("Root"), Section("Root"),
Node( layout.Row(
t: layout.Container,
content: "container", content: "container",
style: Style(width_percent: 100, height_percent: 100), style: Style(width_percent: 100, height_percent: 100),
children: [ children: [
@@ -31,8 +30,7 @@ pub fn foo_test() {
), ),
#( #(
Section("Row1"), Section("Row1"),
Node( layout.Row(
t: layout.Row,
content: "row 1", content: "row 1",
style: Style(width_percent: 100, height_percent: 50), style: Style(width_percent: 100, height_percent: 50),
children: [ children: [
@@ -43,26 +41,21 @@ pub fn foo_test() {
), ),
#( #(
Section("A"), Section("A"),
Node( layout.Cell(
t: layout.Cell,
content: "cell 1", content: "cell 1",
style: Style(width_percent: 50, height_percent: 100), style: Style(width_percent: 50, height_percent: 100),
children: [],
), ),
), ),
#( #(
Section("B"), Section("B"),
Node( layout.Cell(
t: layout.Cell,
content: "cell 2", content: "cell 2",
style: Style(width_percent: 50, height_percent: 100), style: Style(width_percent: 50, height_percent: 100),
children: [],
), ),
), ),
#( #(
Section("Row2"), Section("Row2"),
Node( layout.Row(
t: layout.Row,
content: "row 1", content: "row 1",
style: Style(width_percent: 100, height_percent: 50), style: Style(width_percent: 100, height_percent: 50),
children: [], children: [],