|
| 1 | +(* |
| 2 | + * double_editor.ml |
| 3 | + * ---------- |
| 4 | + * Copyright : (c) 2016, Fabian Bonk <[email protected]> |
| 5 | + * Licence : BSD3 |
| 6 | + * |
| 7 | + * This file is a part of Lambda-Term. |
| 8 | + *) |
| 9 | +open LTerm_geom |
| 10 | + |
| 11 | +let ( >>= ) = Lwt.( >>= ) |
| 12 | + |
| 13 | +(* helper functions *) |
| 14 | +let make_key ?(ctrl = false) ?(meta = false) ?(shift = false) c = |
| 15 | + let code = |
| 16 | + match c with |
| 17 | + | `Char c -> LTerm_key.Char (CamomileLibrary.UChar.of_char c) |
| 18 | + | `Other key -> key in |
| 19 | + { LTerm_key.control = ctrl; meta; shift; code } |
| 20 | + |
| 21 | +let frame widget = |
| 22 | + let frame = new LTerm_widget.frame in |
| 23 | + frame#set widget; |
| 24 | + frame |
| 25 | + |
| 26 | +let main () = |
| 27 | + let waiter, wakener = Lwt.wait () in |
| 28 | + |
| 29 | + let ctrl_c = [make_key ~ctrl:true @@ `Char 'c'] in |
| 30 | + let tab = [make_key @@ `Other LTerm_key.Tab] in |
| 31 | + let quit = [LTerm_edit.Custom (Lwt.wakeup wakener)] in |
| 32 | + |
| 33 | + let vbox = new LTerm_widget.vbox in |
| 34 | + |
| 35 | + let top_editor = new LTerm_edit.edit () in |
| 36 | + let top_frame = frame top_editor in |
| 37 | + |
| 38 | + (* make bottom editor a fixed 10 rows in size *) |
| 39 | + let bottom_editor = new LTerm_edit.edit ~size:{ rows = 10; cols = 1 } () in |
| 40 | + (* changed my mind: make it 5 rows smaller *) |
| 41 | + bottom_editor#set_allocation |
| 42 | + { bottom_editor#allocation with row1 = bottom_editor#allocation.row1 - 5 }; |
| 43 | + let bottom_frame = frame bottom_editor in |
| 44 | + |
| 45 | + vbox#add top_frame; |
| 46 | + (* in versions before PR#42 this would either crash or make the bottom editor unusable *) |
| 47 | + vbox#add ~expand:false bottom_frame; |
| 48 | + |
| 49 | + (* exit on C-c *) |
| 50 | + top_editor#bind ctrl_c quit; |
| 51 | + bottom_editor#bind ctrl_c quit; |
| 52 | + |
| 53 | + let send_key key = |
| 54 | + LTerm_edit.Custom (fun () -> vbox#send_event @@ LTerm_event.Key (make_key key)) in |
| 55 | + |
| 56 | + (* switch editors on Tab *) |
| 57 | + top_editor#bind tab [send_key @@ `Other LTerm_key.Down]; |
| 58 | + bottom_editor#bind tab [send_key @@ `Other LTerm_key.Up]; |
| 59 | + |
| 60 | + let label = new LTerm_widget.label "Press Tab to switch between editors.\nPress C-c to exit." in |
| 61 | + vbox#add ~expand:false label; |
| 62 | + |
| 63 | + Lazy.force LTerm.stdout |
| 64 | + >>= fun term -> |
| 65 | + LTerm.enable_mouse term |
| 66 | + >>= fun () -> |
| 67 | + Lwt.finalize |
| 68 | + (fun () -> LTerm_widget.run term ~save_state:false ~load_resources:false vbox waiter) |
| 69 | + (fun () -> LTerm.disable_mouse term) |
| 70 | + |
| 71 | +let () = Lwt_main.run (main ()) |
0 commit comments