package miaou-core

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file focus_chain.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(*****************************************************************************)
(*                                                                           *)
(* SPDX-License-Identifier: MIT                                              *)
(* Copyright (c) 2025 Nomadic Labs <contact@nomadic-labs.com>                *)
(*                                                                           *)
(*****************************************************************************)

type t = {active : int; total : int}

let create ~total = {active = 0; total = max 0 total}

let with_total t total =
  let total = max 0 total in
  let active = if total = 0 then 0 else min t.active (total - 1) in
  {active; total}

let current t = if t.total = 0 then None else Some t.active

let move t dir =
  match t.total with
  | 0 -> (t, `Bubble)
  | total ->
      let next =
        match dir with
        | `Next -> (t.active + 1) mod total
        | `Prev -> (t.active - 1 + total) mod total
      in
      ({t with active = next}, `Handled)

let handle_key t ~key =
  match key with
  | "Tab" -> move t `Next
  | "S-Tab" | "BackTab" -> move t `Prev
  | _ -> (t, `Bubble)