Compare commits
2 Commits
main
...
2332710235
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2332710235 | ||
|
|
18c4793872 |
@@ -146,6 +146,7 @@ fn update_playback_time_loop(
|
||||
interval_ms: Int,
|
||||
) {
|
||||
process.sleep(interval_ms)
|
||||
// TODO only update if state is playing
|
||||
update_playback_time(mpv, ui)
|
||||
|
||||
update_playback_time_loop(mpv, ui, interval_ms)
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import gleam/dict
|
||||
import gleam/float
|
||||
import gleam/int
|
||||
import gleam/list
|
||||
import gleam/string
|
||||
|
||||
import musicplayer/logging/logging
|
||||
import musicplayer/ui/internal
|
||||
|
||||
pub type Layout {
|
||||
@@ -14,8 +18,14 @@ pub type Section {
|
||||
PlaybackTime
|
||||
}
|
||||
|
||||
/// A Nodes width and height is in percentage (of the available width/height of its parent Node)
|
||||
pub type Node {
|
||||
Node(content: String, x: Int, y: Int, children: List(Section))
|
||||
Node(
|
||||
content: String,
|
||||
width_percent: Int,
|
||||
height_percent: Int,
|
||||
children: List(Section),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new() -> Layout {
|
||||
@@ -23,15 +33,26 @@ pub fn new() -> Layout {
|
||||
dict.from_list([
|
||||
#(
|
||||
Root,
|
||||
Node(content: "", x: 0, y: 0, children: [
|
||||
Header,
|
||||
Search,
|
||||
Node(content: "", width_percent: 100, height_percent: 100, children: [
|
||||
// Header,
|
||||
// Search,
|
||||
PlaybackTime,
|
||||
]),
|
||||
),
|
||||
#(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: [])),
|
||||
// #(
|
||||
// Header,
|
||||
// Node(content: "Music Player", width: 50, height: 10, children: []),
|
||||
// ),
|
||||
// #(Search, Node(content: "", width: 50, height: 10, children: [])),
|
||||
#(
|
||||
PlaybackTime,
|
||||
Node(
|
||||
content: "00:00",
|
||||
width_percent: 50,
|
||||
height_percent: 100,
|
||||
children: [],
|
||||
),
|
||||
),
|
||||
])
|
||||
|
||||
Layout(0, 0, nodes: nodes)
|
||||
@@ -60,13 +81,100 @@ pub fn update_dimensions(layout: Layout, width: Int, height: Int) -> Layout {
|
||||
Layout(..layout, width:, height:)
|
||||
}
|
||||
|
||||
pub fn render(layout: Layout, from: Section) -> Nil {
|
||||
pub fn render(layout: Layout) -> Nil {
|
||||
internal.clear_screen()
|
||||
[layout.width, layout.height]
|
||||
|> list.map(int.to_string)
|
||||
|> string.join(" ")
|
||||
|> string.append("layout - render: ", _)
|
||||
|> logging.log
|
||||
|
||||
let container_width = int.to_float(layout.width)
|
||||
let container_height = int.to_float(layout.height)
|
||||
let container_top_left_x = 1
|
||||
let container_top_left_y = 1
|
||||
|
||||
render_loop(
|
||||
layout,
|
||||
container_width,
|
||||
container_height,
|
||||
container_top_left_x,
|
||||
container_top_left_y,
|
||||
Root,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn render_loop(
|
||||
layout: Layout,
|
||||
container_width: Float,
|
||||
container_height: Float,
|
||||
container_top_left_x: Int,
|
||||
container_top_left_y: Int,
|
||||
from: Section,
|
||||
) -> Nil {
|
||||
let margin = 2.0
|
||||
|
||||
case dict.get(layout.nodes, from) {
|
||||
Error(_) -> Nil
|
||||
Ok(node) -> {
|
||||
list.each(node.children, fn(child) { render(layout, child) })
|
||||
list.each(node.children, fn(child) {
|
||||
let cw =
|
||||
container_width
|
||||
*. { int.to_float(node.width_percent) /. 100.0 }
|
||||
-. margin
|
||||
|> float.floor
|
||||
|
||||
internal.print_at(node.content, node.x, node.y)
|
||||
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, child)
|
||||
})
|
||||
|
||||
logging.log("section: " <> string.inspect(from))
|
||||
logging.log("container width: " <> float.to_string(container_width))
|
||||
logging.log("container height: " <> float.to_string(container_height))
|
||||
|
||||
let width =
|
||||
container_width *. { int.to_float(node.width_percent) /. 100.0 }
|
||||
|> float.floor
|
||||
|> float.truncate
|
||||
|
||||
let height =
|
||||
container_height *. { int.to_float(node.height_percent) /. 100.0 }
|
||||
|> float.floor
|
||||
|> float.truncate
|
||||
|
||||
logging.log("width: " <> int.to_string(width))
|
||||
logging.log("height: " <> int.to_string(height))
|
||||
|
||||
let cx = container_top_left_x
|
||||
let cy = container_top_left_y
|
||||
logging.log("cx: " <> int.to_string(cx))
|
||||
logging.log("cy: " <> int.to_string(cy))
|
||||
|
||||
draw_box(cx, cy, width, height)
|
||||
// Box heading
|
||||
internal.print_at(node.content, cx, cy)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ pub fn new() -> Result(Subject(Control), String) {
|
||||
internal.clear_screen()
|
||||
internal.hide_cursor()
|
||||
|
||||
redraw_on_update_loop(redraw)
|
||||
redraw_loop(redraw)
|
||||
})
|
||||
|
||||
Ok(ui)
|
||||
@@ -63,21 +63,18 @@ fn handle_message(
|
||||
|> string.append("ui - updating dimensions: ", _)
|
||||
|> logging.log
|
||||
|
||||
actor.continue(
|
||||
State(
|
||||
..state,
|
||||
layout: layout.update_dimensions(state.layout, width, height),
|
||||
),
|
||||
)
|
||||
let layout = layout.update_dimensions(state.layout, width, height)
|
||||
|
||||
process.send(state.redraw, layout)
|
||||
actor.continue(State(..state, layout:))
|
||||
}
|
||||
}
|
||||
}
|
||||
control.UpdateState(section, content) -> {
|
||||
let layout = layout.update_section(state.layout, section, content)
|
||||
let state = State(..state, layout:)
|
||||
|
||||
actor.send(state.redraw, layout)
|
||||
actor.continue(state)
|
||||
actor.continue(State(..state, layout:))
|
||||
}
|
||||
|
||||
control.Exit(reply_to) -> {
|
||||
@@ -88,13 +85,11 @@ fn handle_message(
|
||||
}
|
||||
}
|
||||
|
||||
fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil {
|
||||
let layout = process.receive_forever(redraw)
|
||||
fn redraw_loop(redraw: Subject(Layout)) -> Nil {
|
||||
process.receive_forever(redraw)
|
||||
|> layout.render
|
||||
|
||||
internal.clear_screen()
|
||||
layout.render(layout, layout.Root)
|
||||
|
||||
redraw_on_update_loop(redraw)
|
||||
redraw_loop(redraw)
|
||||
}
|
||||
|
||||
fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) {
|
||||
|
||||
Reference in New Issue
Block a user