Update Layout.{width, height} on interval

This commit is contained in:
Alexander Heldt
2025-11-29 16:49:02 +01:00
parent 82a8800362
commit 661b8f5e82
4 changed files with 59 additions and 7 deletions

View File

@@ -3,6 +3,7 @@ import gleam/erlang/process.{type Subject}
import musicplayer/ui/layout.{type Section}
pub type Control {
UpdateDimensions(width: Int, height: Int)
UpdateState(section: Section, content: String)
Exit(reply_to: Subject(Nil))

View File

@@ -19,3 +19,13 @@ pub fn hide_cursor() -> Nil {
pub fn show_cursor() -> Nil {
io.print("\u{001B}[?25h")
}
pub type Enotsup
// https://www.erlang.org/doc/apps/stdlib/io.html#rows/0
@external(erlang, "io", "rows")
pub fn io_get_rows() -> Result(Int, Enotsup)
// https://www.erlang.org/doc/apps/stdlib/io.html#columns/0
@external(erlang, "io", "columns")
pub fn io_get_columns() -> Result(Int, Enotsup)

View File

@@ -1,7 +1,7 @@
import gleam/dict
pub type Layout {
Layout(nodes: dict.Dict(Section, Node))
Layout(width: Int, height: Int, nodes: dict.Dict(Section, Node))
}
pub type Section {
@@ -31,7 +31,7 @@ pub fn new() -> Layout {
#(PlaybackTime, Node(content: "00:00", x: 1, y: 2, children: [])),
])
Layout(nodes: nodes)
Layout(0, 0, nodes: nodes)
}
pub fn update_section(
@@ -42,10 +42,17 @@ pub fn update_section(
case dict.get(layout.nodes, section) {
Error(_) -> layout
Ok(node) ->
Layout(nodes: dict.insert(
Layout(
..layout,
nodes: dict.insert(
layout.nodes,
section,
Node(..node, content: content),
))
),
)
}
}
pub fn update_dimensions(layout: Layout, width: Int, height: Int) -> Layout {
Layout(..layout, width:, height:)
}

View File

@@ -1,5 +1,6 @@
import gleam/dict
import gleam/erlang/process.{type Subject}
import gleam/int
import gleam/list
import gleam/otp/actor
import gleam/string
@@ -26,6 +27,11 @@ pub fn new() -> Result(Subject(Control), String) {
Error(start_error) ->
Error("Could not start actor: " <> string.inspect(start_error))
Ok(actor.Started(data: ui, ..)) -> {
process.spawn(fn() {
let update_dimensions_interval_ms = 300
update_dimensions_on_interval(ui, update_dimensions_interval_ms)
})
process.spawn(fn() {
let assert Ok(_) = process.register(process.self(), redraw_name)
@@ -45,6 +51,22 @@ fn handle_message(
control: Control,
) -> actor.Next(State(redraw, layout), Control) {
case control {
control.UpdateDimensions(width, height) -> {
let current_dimensions = #(state.layout.width, state.layout.height)
case #(width, height) == current_dimensions {
True -> actor.continue(state)
False -> {
actor.continue(
State(
..state,
layout: layout.update_dimensions(state.layout, width, height),
),
)
}
}
}
control.UpdateState(section, content) -> {
let layout = layout.update_section(state.layout, section, content)
let state = State(..state, layout:)
@@ -70,6 +92,18 @@ fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil {
redraw_on_update_loop(redraw)
}
fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) {
case ui_internal.io_get_columns(), ui_internal.io_get_rows() {
Ok(width), Ok(height) -> {
process.send(ui, control.UpdateDimensions(width, height))
}
_, _ -> Nil
}
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