fix margins

This commit is contained in:
cqc
2024-05-15 15:01:09 -05:00
parent 51dd25deee
commit 4ce218978b

78
ogui.ml
View File

@ -592,63 +592,9 @@ let rec nth_tl n = function
| hd :: tl -> if n > 0 then nth_tl (n - 1) tl else hd :: tl | hd :: tl -> if n > 0 then nth_tl (n - 1) tl else hd :: tl
| [] -> [] | [] -> []
let _ =
assert (List.equal Int.equal (nth_tl 2 [ 0; 1; 2; 3 ]) [ 2; 3 ]);
assert (List.equal Int.equal (nth_tl 3 [ 0; 1; 2 ]) []);
assert (List.equal Int.equal (nth_tl 0 [ 0; 1 ]) [ 0; 1 ])
module Style = struct
open Gg
type text_style = unit
type spacing = {
item_spacing : Gg.size2;
window_margin : Margin.t;
indent : Gg.size1;
interact_size : Gg.size2;
slider_width : Gg.size1;
text_edit_width : Gg.size1;
icon_width : Gg.size1;
icon_width_inner : Gg.size1;
icon_spacing : Gg.size1;
}
type t = {
override_text_style : text_style option;
override_font : TextLayout.font_selection option;
wrap : bool option;
spacing : spacing;
(*interaction: Interaction.t; *)
animation_time : float;
}
let default =
{
override_text_style = None;
override_font = None;
wrap = None;
spacing =
{
item_spacing = Size2.v 10. 10.;
window_margin =
Margin.{ (symmetric 5. 5.) with bottom = 0. };
indent = 5.;
slider_width = 5.;
text_edit_width = 500.;
icon_width = 40.;
icon_width_inner = 35.;
icon_spacing = 50.;
interact_size = P2.v 500. 500.;
};
animation_time = 0.1;
}
end
module Ui = struct module Ui = struct
type t = { type t = {
mutable rect : Gg.box2; mutable rect : Gg.box2;
style : Style.t;
enabled : bool; enabled : bool;
gv : Gv.t; gv : Gv.t;
glfw_window : GLFW.window option; glfw_window : GLFW.window option;
@ -658,12 +604,10 @@ module Ui = struct
and action = Custom of (unit -> unit Lwt.t) and action = Custom of (unit -> unit Lwt.t)
let id = ref 0 let id = ref 0
let spacing ui = ui.style.spacing
let window gv ?(window : GLFW.window option) rect : t = let window gv ?(window : GLFW.window option) rect : t =
{ {
rect; rect;
style = Style.default;
enabled = true; enabled = true;
gv; gv;
glfw_window = window; glfw_window = window;
@ -1053,7 +997,7 @@ module TextEdit = struct
formatter = None; formatter = None;
password = false; password = false;
frame = true; frame = true;
margin = Margin.symmetric 4.0 2.0; margin = Margin.symmetric 4.0 4.0;
multiline = true; multiline = true;
interactive = true; interactive = true;
desired_width = None; desired_width = None;
@ -1173,7 +1117,7 @@ module Painter = struct
Option.value ~default:(Gv.Text.metrics t).line_height Option.value ~default:(Gv.Text.metrics t).line_height
g.line_height g.line_height
in in
let max_rows = Int.of_float (Box2.h rect /. line_height) + 1 in let max_rows = Int.of_float (Box2.h rect /. line_height) in
te.rows <- max_rows; te.rows <- max_rows;
let lines = Gv.Text.make_empty_rows max_rows in let lines = Gv.Text.make_empty_rows max_rows in
TextBuffer.contents g.text >>= fun contents -> TextBuffer.contents g.text >>= fun contents ->
@ -1211,10 +1155,6 @@ module Painter = struct
contents) contents)
.advance .advance
in in
let line_height =
Option.value ~default:(Gv.Text.metrics t).line_height
sec.format.line_height
in
draw_box t draw_box t
~box: ~box:
(Box2.v (Box2.v
@ -1228,14 +1168,16 @@ module Painter = struct
V2.v V2.v
(Gv.Text.text_w t ~x:(P2.x cur') ~y:(P2.y cur) ~start (Gv.Text.text_w t ~x:(P2.x cur') ~y:(P2.y cur) ~start
~end_ contents) ~end_ contents)
Float.(max (P2.y cur +. line_height) (P2.y cur'))) (P2.y cur'))
P2.(v (Box2.minx rect) (y cur)) P2.(v (Box2.minx rect) (y cur))
sections sections
|> fun cur'' -> |> fun cur'' ->
(V2.(v (max (x cur) (x cur'')) (y cur'')), row.next)) ( V2.(v (max (x cur) (x cur'')) (y cur'' +. line_height)),
row.next ))
(Box2.o rect, te.scroll) (Box2.o rect, te.scroll)
(Seq.take row_count (Array.to_seq lines)) (Seq.take row_count (Array.to_seq lines))
|> fst |> fst
|> (fun cur''' -> V2.(cur''' - v 0. line_height))
|> Box2.(of_pts (o rect)) |> Box2.(of_pts (o rect))
|> Lwt.return |> Lwt.return
@ -1267,7 +1209,13 @@ module Painter = struct
|> text_layout ui.gv box' t) |> text_layout ui.gv box' t)
| _ -> Lwt.return box) | _ -> Lwt.return box)
>>= fun r -> >>= fun r ->
let r' = Margin.outer frame.style.margin r in let r' =
Box2.add_pt r
V2.(
Box2.max r
+ v frame.style.margin.right frame.style.margin.bottom)
|> Margin.outer frame.style.margin
in
draw_box ui.gv ~box:r' ~style:frame.style; draw_box ui.gv ~box:r' ~style:frame.style;
Lwt.return r' Lwt.return r'