Compare commits
6 Commits
layout
...
1732b12fbe
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1732b12fbe | ||
|
|
50c053a42a | ||
|
|
2332710235 | ||
|
|
18c4793872 | ||
|
|
4752ce418b | ||
|
|
df9160b932 |
@@ -146,6 +146,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)
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
import gleam/dict
|
import gleam/dict
|
||||||
|
import gleam/float
|
||||||
|
import gleam/int
|
||||||
import gleam/list
|
import gleam/list
|
||||||
|
import gleam/string
|
||||||
import gleam/string_tree.{type StringTree}
|
import gleam/string_tree.{type StringTree}
|
||||||
|
|
||||||
|
import musicplayer/logging/logging
|
||||||
import musicplayer/ui/internal
|
import musicplayer/ui/internal
|
||||||
|
|
||||||
pub type Layout {
|
pub type Layout {
|
||||||
@@ -9,33 +13,109 @@ pub type Layout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub type Section {
|
pub type Section {
|
||||||
|
Section(String)
|
||||||
|
|
||||||
Root
|
Root
|
||||||
Header
|
Header
|
||||||
Search
|
Search
|
||||||
PlaybackTime
|
PlaybackTime
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pub type Section {
|
||||||
|
// Root
|
||||||
|
// Header
|
||||||
|
// Search
|
||||||
|
// PlaybackTime
|
||||||
|
// Test
|
||||||
|
|
||||||
|
// Row1
|
||||||
|
// A
|
||||||
|
// B
|
||||||
|
|
||||||
|
// Row2
|
||||||
|
// C
|
||||||
|
// D
|
||||||
|
// }
|
||||||
|
|
||||||
|
pub type NodeType {
|
||||||
|
Container
|
||||||
|
Row
|
||||||
|
Cell
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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(
|
||||||
|
t: NodeType,
|
||||||
|
content: String,
|
||||||
|
width_percent: Int,
|
||||||
|
height_percent: Int,
|
||||||
|
children: List(Section),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new() -> Layout {
|
pub fn new() -> Layout {
|
||||||
let nodes =
|
let nodes =
|
||||||
dict.from_list([
|
dict.from_list([
|
||||||
#(
|
#(
|
||||||
Root,
|
Section("Root"),
|
||||||
Node(content: "", x: 0, y: 0, children: [
|
Node(
|
||||||
Header,
|
t: Container,
|
||||||
Search,
|
content: "container",
|
||||||
PlaybackTime,
|
width_percent: 100,
|
||||||
]),
|
height_percent: 100,
|
||||||
|
children: [
|
||||||
|
Section("Row1"),
|
||||||
|
Section("Row2"),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
#(
|
||||||
|
Section("Row1"),
|
||||||
|
Node(
|
||||||
|
t: Row,
|
||||||
|
content: "row 1",
|
||||||
|
width_percent: 100,
|
||||||
|
height_percent: 50,
|
||||||
|
children: [
|
||||||
|
Section("A"),
|
||||||
|
Section("A"),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
#(
|
||||||
|
Section("A"),
|
||||||
|
Node(
|
||||||
|
t: Cell,
|
||||||
|
content: "cell 1",
|
||||||
|
width_percent: 50,
|
||||||
|
height_percent: 100,
|
||||||
|
children: [],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
#(
|
||||||
|
Section("B"),
|
||||||
|
Node(
|
||||||
|
t: Cell,
|
||||||
|
content: "cell 2",
|
||||||
|
width_percent: 50,
|
||||||
|
height_percent: 100,
|
||||||
|
children: [],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
#(
|
||||||
|
Section("Row2"),
|
||||||
|
Node(
|
||||||
|
t: Row,
|
||||||
|
content: "row 1",
|
||||||
|
width_percent: 100,
|
||||||
|
height_percent: 50,
|
||||||
|
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(width: 0, height: 0, nodes: nodes)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_section(
|
pub fn update_section(
|
||||||
@@ -61,30 +141,139 @@ 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(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
|
||||||
|
|
||||||
string_tree.new()
|
string_tree.new()
|
||||||
|> render_loop(layout, from, _)
|
|> render_loop(
|
||||||
|
layout,
|
||||||
|
container_width,
|
||||||
|
container_height,
|
||||||
|
container_top_left_x,
|
||||||
|
container_top_left_y,
|
||||||
|
0,
|
||||||
|
Section("Root"),
|
||||||
|
_,
|
||||||
|
)
|
||||||
|> string_tree.to_string
|
|> string_tree.to_string
|
||||||
|> internal.print
|
|> internal.print
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_loop(
|
pub fn render_loop(
|
||||||
layout: Layout,
|
layout: Layout,
|
||||||
|
container_width: Float,
|
||||||
|
container_height: Float,
|
||||||
|
container_top_left_x: Int,
|
||||||
|
container_top_left_y: Int,
|
||||||
|
index: Int,
|
||||||
from: Section,
|
from: Section,
|
||||||
into: StringTree,
|
tree: StringTree,
|
||||||
) -> StringTree {
|
) -> StringTree {
|
||||||
|
let margin = 2.0
|
||||||
|
|
||||||
case dict.get(layout.nodes, from) {
|
case dict.get(layout.nodes, from) {
|
||||||
Error(_) -> into
|
Error(_) -> tree
|
||||||
Ok(node) -> {
|
Ok(node) -> {
|
||||||
let parent =
|
let final_tree =
|
||||||
|
list.index_map(node.children, fn(child, i) { #(i, child) })
|
||||||
|
|> list.fold(tree, fn(updated_tree: StringTree, ic: #(Int, Section)) {
|
||||||
|
let #(i, child) = ic
|
||||||
|
|
||||||
|
let cw =
|
||||||
|
container_width
|
||||||
|
*. { int.to_float(node.width_percent) /. 100.0 }
|
||||||
|
-. margin
|
||||||
|
|> float.floor
|
||||||
|
|
||||||
|
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, i, child, updated_tree)
|
||||||
|
})
|
||||||
|
|
||||||
|
logging.log("section: " <> string.inspect(from))
|
||||||
|
logging.log("section type: " <> string.inspect(node.t))
|
||||||
|
logging.log("index: " <> string.inspect(index))
|
||||||
|
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("section width: " <> int.to_string(width))
|
||||||
|
logging.log("section height: " <> int.to_string(height))
|
||||||
|
|
||||||
|
let #(cx, cy) = case node.t {
|
||||||
|
Container -> #(container_top_left_x, container_top_left_y)
|
||||||
|
Row -> #(
|
||||||
|
container_top_left_x,
|
||||||
|
container_top_left_y + { index * height },
|
||||||
|
)
|
||||||
|
Cell -> #(
|
||||||
|
container_top_left_x + { index * width },
|
||||||
|
container_top_left_y,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
logging.log("cx: " <> int.to_string(cx))
|
||||||
|
logging.log("cy: " <> int.to_string(cy))
|
||||||
|
|
||||||
|
final_tree
|
||||||
|
|> string_tree.append(draw_box(cx, cy, width, height))
|
||||||
|
// Box heading
|
||||||
|
|> string_tree.append(internal.chars_at(node.content, cx, cy))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_box(x: Int, y: Int, width: Int, height: Int) -> String {
|
||||||
|
let box_tree = string_tree.new()
|
||||||
|
let box_chars = #("┌", "┐", "└", "┘", "─", "│")
|
||||||
|
let #(tl, tr, bl, br, h, v) = box_chars
|
||||||
|
|
||||||
|
let box_tree =
|
||||||
string_tree.append(
|
string_tree.append(
|
||||||
into,
|
box_tree,
|
||||||
internal.chars_at(node.content, node.x, node.y),
|
internal.chars_at(tl <> string.repeat(h, width - 2) <> tr, x, y),
|
||||||
)
|
)
|
||||||
|
|
||||||
list.fold(node.children, parent, fn(into_acc, child) {
|
let box_trees =
|
||||||
render_loop(layout, child, into_acc)
|
list.range(1, height - 2)
|
||||||
|
|> list.map(fn(row) {
|
||||||
|
box_tree
|
||||||
|
|> string_tree.append(internal.chars_at(v, x, y + row))
|
||||||
|
|> string_tree.append(internal.chars_at(v, x + width - 1, y + row))
|
||||||
})
|
})
|
||||||
}
|
|
||||||
}
|
string_tree.append(
|
||||||
|
string_tree.concat(box_trees),
|
||||||
|
internal.chars_at(
|
||||||
|
bl <> string.repeat(h, width - 2) <> br,
|
||||||
|
x,
|
||||||
|
y + height - 1,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|> string_tree.to_string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import gleam/int
|
|||||||
import gleam/list
|
import gleam/list
|
||||||
import gleam/otp/actor
|
import gleam/otp/actor
|
||||||
import gleam/string
|
import gleam/string
|
||||||
|
import gleam/string_tree.{type StringTree}
|
||||||
|
|
||||||
import musicplayer/logging/logging
|
import musicplayer/logging/logging
|
||||||
import musicplayer/ui/control.{type Control}
|
import musicplayer/ui/control.{type Control}
|
||||||
@@ -38,7 +39,7 @@ pub fn new() -> Result(Subject(Control), String) {
|
|||||||
internal.clear_screen()
|
internal.clear_screen()
|
||||||
internal.hide_cursor()
|
internal.hide_cursor()
|
||||||
|
|
||||||
redraw_on_update_loop(redraw)
|
redraw_loop(redraw)
|
||||||
})
|
})
|
||||||
|
|
||||||
Ok(ui)
|
Ok(ui)
|
||||||
@@ -63,21 +64,18 @@ fn handle_message(
|
|||||||
|> string.append("ui - updating dimensions: ", _)
|
|> string.append("ui - updating dimensions: ", _)
|
||||||
|> logging.log
|
|> logging.log
|
||||||
|
|
||||||
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) -> {
|
||||||
@@ -88,13 +86,11 @@ fn handle_message(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil {
|
fn redraw_loop(redraw: Subject(Layout)) -> Nil {
|
||||||
let layout = process.receive_forever(redraw)
|
process.receive_forever(redraw)
|
||||||
|
|> layout.render
|
||||||
|
|
||||||
internal.clear_screen()
|
redraw_loop(redraw)
|
||||||
layout.render(layout, layout.Root)
|
|
||||||
|
|
||||||
redraw_on_update_loop(redraw)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) {
|
fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) {
|
||||||
@@ -108,3 +104,28 @@ fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) {
|
|||||||
process.sleep(interval_ms)
|
process.sleep(interval_ms)
|
||||||
update_dimensions_on_interval(ui, interval_ms)
|
update_dimensions_on_interval(ui, interval_ms)
|
||||||
}
|
}
|
||||||
|
// fn render_layout(layout: Layout, from: Section) -> Nil {
|
||||||
|
// string_tree.new()
|
||||||
|
// |> render_layout_loop(layout, from, _)
|
||||||
|
// |> string_tree.to_string
|
||||||
|
// |> ui_internal.print
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fn render_layout_loop(
|
||||||
|
// layout: Layout,
|
||||||
|
// from: Section,
|
||||||
|
// tree: StringTree,
|
||||||
|
// ) -> StringTree {
|
||||||
|
// case dict.get(layout.nodes, from) {
|
||||||
|
// Error(_) -> tree
|
||||||
|
// Ok(node) -> {
|
||||||
|
// let acc_after_children =
|
||||||
|
// list.fold(node.children, tree, fn(current_acc, child_id) {
|
||||||
|
// render_layout_loop(layout, child_id, current_acc)
|
||||||
|
// })
|
||||||
|
|
||||||
|
// acc_after_children
|
||||||
|
// |> string_tree.append(ui_internal.chars_at(node.content, node.x, node.y))
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|||||||
87
test/musicplayer/ui/layout_test.gleam
Normal file
87
test/musicplayer/ui/layout_test.gleam
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
// import gleam/dict
|
||||||
|
// import gleam/int
|
||||||
|
// import gleeunit
|
||||||
|
|
||||||
|
// import musicplayer/ui/layout.{type Node, Layout, Section}
|
||||||
|
|
||||||
|
// pub fn main() -> Nil {
|
||||||
|
// gleeunit.main()
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn foo_test() {
|
||||||
|
// let expected = ""
|
||||||
|
|
||||||
|
// let layout =
|
||||||
|
// Layout(
|
||||||
|
// width: 80,
|
||||||
|
// height: 20,
|
||||||
|
// nodes:,
|
||||||
|
// dict.from_list([
|
||||||
|
// #(
|
||||||
|
// Section("Root"),
|
||||||
|
// Node(
|
||||||
|
// t: Container,
|
||||||
|
// content: "container",
|
||||||
|
// width_percent: 100,
|
||||||
|
// height_percent: 100,
|
||||||
|
// children: [
|
||||||
|
// Section("Row1"),
|
||||||
|
// Section("Row2"),
|
||||||
|
// ],
|
||||||
|
// ),
|
||||||
|
// ),
|
||||||
|
// #(
|
||||||
|
// Section("Row1"),
|
||||||
|
// Node(
|
||||||
|
// t: Row,
|
||||||
|
// content: "row 1",
|
||||||
|
// width_percent: 100,
|
||||||
|
// height_percent: 50,
|
||||||
|
// children: [
|
||||||
|
// Section("A"),
|
||||||
|
// Section("B"),
|
||||||
|
// ],
|
||||||
|
// ),
|
||||||
|
// ),
|
||||||
|
// #(
|
||||||
|
// Section("A"),
|
||||||
|
// Node(
|
||||||
|
// t: Cell,
|
||||||
|
// content: "cell 1",
|
||||||
|
// width_percent: 50,
|
||||||
|
// height_percent: 100,
|
||||||
|
// children: [],
|
||||||
|
// ),
|
||||||
|
// ),
|
||||||
|
// #(
|
||||||
|
// B,
|
||||||
|
// Node(
|
||||||
|
// t: Cell,
|
||||||
|
// content: "cell 2",
|
||||||
|
// width_percent: 50,
|
||||||
|
// height_percent: 100,
|
||||||
|
// children: [],
|
||||||
|
// ),
|
||||||
|
// ),
|
||||||
|
// #(
|
||||||
|
// Section("Row2"),
|
||||||
|
// Node(
|
||||||
|
// t: Row,
|
||||||
|
// content: "row 1",
|
||||||
|
// width_percent: 100,
|
||||||
|
// height_percent: 50,
|
||||||
|
// children: [],
|
||||||
|
// ),
|
||||||
|
// ),
|
||||||
|
// ]),
|
||||||
|
// )
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
// let assert Ok(data) =
|
||||||
|
// layout.render_loop(layout, container_width, container_height)
|
||||||
|
// assert data == 123.456789
|
||||||
|
// }
|
||||||
Reference in New Issue
Block a user