Dynamic layouts #9
@@ -1,4 +1,7 @@
|
||||
import gleam/dict
|
||||
import gleam/list
|
||||
|
||||
import musicplayer/ui/internal
|
||||
|
||||
pub type Layout {
|
||||
Layout(width: Int, height: Int, nodes: dict.Dict(Section, Node))
|
||||
@@ -56,3 +59,14 @@ pub fn update_section(
|
||||
pub fn update_dimensions(layout: Layout, width: Int, height: Int) -> Layout {
|
||||
Layout(..layout, width:, height:)
|
||||
}
|
||||
|
||||
pub fn render(layout: Layout, from: Section) -> Nil {
|
||||
case dict.get(layout.nodes, from) {
|
||||
Error(_) -> Nil
|
||||
Ok(node) -> {
|
||||
list.each(node.children, fn(child) { render(layout, child) })
|
||||
|
||||
internal.print_at(node.content, node.x, node.y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import gleam/dict
|
||||
import gleam/erlang/process.{type Subject}
|
||||
import gleam/int
|
||||
import gleam/list
|
||||
@@ -8,7 +7,7 @@ import gleam/string
|
||||
import musicplayer/logging/logging
|
||||
import musicplayer/ui/control.{type Control}
|
||||
import musicplayer/ui/internal
|
||||
import musicplayer/ui/layout.{type Layout, type Section}
|
||||
import musicplayer/ui/layout.{type Layout}
|
||||
|
||||
pub type State(redraw, content) {
|
||||
State(redraw: Subject(Layout), layout: Layout)
|
||||
@@ -92,8 +91,8 @@ fn handle_message(
|
||||
fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil {
|
||||
let layout = process.receive_forever(redraw)
|
||||
|
||||
render_layout(layout, layout.Root)
|
||||
internal.clear_screen()
|
||||
layout.render(layout, layout.Root)
|
||||
|
||||
redraw_on_update_loop(redraw)
|
||||
}
|
||||
@@ -109,13 +108,3 @@ fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) {
|
||||
process.sleep(interval_ms)
|
||||
update_dimensions_on_interval(ui, interval_ms)
|
||||
}
|
||||
|
||||
fn render_layout(layout: Layout, from: Section) -> Nil {
|
||||
case dict.get(layout.nodes, from) {
|
||||
Error(_) -> Nil
|
||||
Ok(node) -> {
|
||||
list.each(node.children, fn(child) { render_layout(layout, child) })
|
||||
internal.print_at(node.content, node.x, node.y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user