From 4ce218978b1d25a5001a795f3382b7dea3bb7d6b Mon Sep 17 00:00:00 2001 From: cqc Date: Wed, 15 May 2024 15:01:09 -0500 Subject: [PATCH] fix margins --- ogui.ml | 78 ++++++++++----------------------------------------------- 1 file changed, 13 insertions(+), 65 deletions(-) diff --git a/ogui.ml b/ogui.ml index ef6a89b..dc119e9 100644 --- a/ogui.ml +++ b/ogui.ml @@ -592,63 +592,9 @@ let rec nth_tl n = function | 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 type t = { mutable rect : Gg.box2; - style : Style.t; enabled : bool; gv : Gv.t; glfw_window : GLFW.window option; @@ -658,12 +604,10 @@ module Ui = struct and action = Custom of (unit -> unit Lwt.t) let id = ref 0 - let spacing ui = ui.style.spacing let window gv ?(window : GLFW.window option) rect : t = { rect; - style = Style.default; enabled = true; gv; glfw_window = window; @@ -1053,7 +997,7 @@ module TextEdit = struct formatter = None; password = false; frame = true; - margin = Margin.symmetric 4.0 2.0; + margin = Margin.symmetric 4.0 4.0; multiline = true; interactive = true; desired_width = None; @@ -1173,7 +1117,7 @@ module Painter = struct Option.value ~default:(Gv.Text.metrics t).line_height g.line_height 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; let lines = Gv.Text.make_empty_rows max_rows in TextBuffer.contents g.text >>= fun contents -> @@ -1211,10 +1155,6 @@ module Painter = struct contents) .advance in - let line_height = - Option.value ~default:(Gv.Text.metrics t).line_height - sec.format.line_height - in draw_box t ~box: (Box2.v @@ -1228,14 +1168,16 @@ module Painter = struct V2.v (Gv.Text.text_w t ~x:(P2.x cur') ~y:(P2.y cur) ~start ~end_ contents) - Float.(max (P2.y cur +. line_height) (P2.y cur'))) + (P2.y cur')) P2.(v (Box2.minx rect) (y cur)) sections |> 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) (Seq.take row_count (Array.to_seq lines)) |> fst + |> (fun cur''' -> V2.(cur''' - v 0. line_height)) |> Box2.(of_pts (o rect)) |> Lwt.return @@ -1267,7 +1209,13 @@ module Painter = struct |> text_layout ui.gv box' t) | _ -> Lwt.return box) >>= 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; Lwt.return r'