wip-working-ish

This commit is contained in:
Alexander Heldt
2025-11-29 20:46:35 +01:00
parent e524a865fe
commit 6ef3e9734d
3 changed files with 98 additions and 26 deletions

View File

@@ -155,6 +155,7 @@ fn update_playback_time_loop(
interval_ms: Int, interval_ms: Int,
) { ) {
process.sleep(interval_ms) process.sleep(interval_ms)
// TODO only update if state is playing
update_playback_time(mpv, ui) update_playback_time(mpv, ui)
update_playback_time_loop(mpv, ui, interval_ms) update_playback_time_loop(mpv, ui, interval_ms)

View File

@@ -1,6 +1,11 @@
import gleam/dict import gleam/dict
import gleam/erlang/process.{type Subject}
import gleam/int
import gleam/list import gleam/list
import gleam/string
import musicplayer/logging/control as logging_control
import musicplayer/logging/logging
import musicplayer/ui/internal import musicplayer/ui/internal
pub type Layout { pub type Layout {
@@ -14,8 +19,16 @@ pub type Section {
PlaybackTime PlaybackTime
} }
/// A Nodes width and height is in percentage (of the available width/height of its parent Node)
pub type Node { pub type Node {
Node(content: String, x: Int, y: Int, children: List(Section)) Node(
content: String,
x: Int,
y: Int,
width_percent: Int,
height_percent: Int,
children: List(Section),
)
} }
pub fn new() -> Layout { pub fn new() -> Layout {
@@ -23,15 +36,35 @@ pub fn new() -> Layout {
dict.from_list([ dict.from_list([
#( #(
Root, Root,
Node(content: "", x: 0, y: 0, children: [ Node(
Header, content: "",
Search, x: 1,
PlaybackTime, y: 1,
]), width_percent: 100,
height_percent: 100,
children: [
// Header,
// Search,
PlaybackTime,
],
),
),
// #(
// Header,
// Node(content: "Music Player", width: 50, height: 10, children: []),
// ),
// #(Search, Node(content: "", width: 50, height: 10, children: [])),
#(
PlaybackTime,
Node(
content: "00:00",
x: 1,
y: 2,
width_percent: 100,
height_percent: 100,
children: [],
),
), ),
#(Header, Node(content: "Music Player", x: 1, y: 1, children: [])),
#(Search, Node(content: "", x: 30, y: 1, children: [])),
#(PlaybackTime, Node(content: "00:00", x: 1, y: 2, children: [])),
]) ])
Layout(0, 0, nodes: nodes) Layout(0, 0, nodes: nodes)
@@ -60,13 +93,53 @@ pub fn update_dimensions(layout: Layout, width: Int, height: Int) -> Layout {
Layout(..layout, width:, height:) Layout(..layout, width:, height:)
} }
pub fn render(layout: Layout, from: Section) -> Nil { pub fn render(logger: Subject(logging_control.Control), layout: Layout) -> Nil {
internal.clear_screen()
[layout.width, layout.height]
|> list.map(int.to_string)
|> string.join(" ")
|> string.append("layout - render: ", _)
|> logging.log(logger, _)
render_loop(layout, layout.width, layout.height, Root)
}
pub fn render_loop(
layout: Layout,
container_width: Int,
container_height: Int,
from: Section,
) -> Nil {
case dict.get(layout.nodes, from) { case dict.get(layout.nodes, from) {
Error(_) -> Nil Error(_) -> Nil
Ok(node) -> { Ok(node) -> {
list.each(node.children, fn(child) { render(layout, child) }) list.each(node.children, fn(child) {
let cw = container_width * { node.width_percent / 100 }
let ch = container_height * { node.height_percent / 100 }
render_loop(layout, cw, ch, child)
})
let width = container_width * { node.width_percent / 100 }
let height = container_height * { node.height_percent / 100 }
draw_box(node.x, node.y, width, height)
internal.print_at(node.content, node.x, node.y) internal.print_at(node.content, node.x, node.y)
} }
} }
} }
fn draw_box(x: Int, y: Int, width: Int, height: Int) -> Nil {
let box_chars = #("", "", "", "", "", "")
let #(tl, tr, bl, br, h, v) = box_chars
internal.print_at(tl <> string.repeat(h, width - 2) <> tr, x, y)
list.range(1, height - 2)
|> list.each(fn(row) {
internal.print_at(v, x, y + row)
internal.print_at(v, x + width - 1, y + row)
})
internal.print_at(bl <> string.repeat(h, width - 2) <> br, x, y + height - 1)
}

View File

@@ -45,7 +45,7 @@ pub fn new(
internal.clear_screen() internal.clear_screen()
internal.hide_cursor() internal.hide_cursor()
redraw_on_update_loop(redraw) redraw_loop(logger, redraw)
}) })
Ok(ui) Ok(ui)
@@ -70,21 +70,18 @@ fn handle_message(
|> string.append("ui - updating dimensions: ", _) |> string.append("ui - updating dimensions: ", _)
|> logging.log(state.logger, _) |> logging.log(state.logger, _)
actor.continue( let layout = layout.update_dimensions(state.layout, width, height)
State(
..state, process.send(state.redraw, layout)
layout: layout.update_dimensions(state.layout, width, height), actor.continue(State(..state, layout:))
),
)
} }
} }
} }
control.UpdateState(section, content) -> { control.UpdateState(section, content) -> {
let layout = layout.update_section(state.layout, section, content) let layout = layout.update_section(state.layout, section, content)
let state = State(..state, layout:)
actor.send(state.redraw, layout) actor.send(state.redraw, layout)
actor.continue(state) actor.continue(State(..state, layout:))
} }
control.Exit(reply_to) -> { control.Exit(reply_to) -> {
@@ -95,13 +92,14 @@ fn handle_message(
} }
} }
fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil { fn redraw_loop(
let layout = process.receive_forever(redraw) logger: Subject(logging_control.Control),
redraw: Subject(Layout),
) -> Nil {
process.receive_forever(redraw)
|> layout.render(logger, _)
internal.clear_screen() redraw_loop(logger, redraw)
layout.render(layout, layout.Root)
redraw_on_update_loop(redraw)
} }
fn update_dimensions_on_interval( fn update_dimensions_on_interval(