Coverage Report

Created: 2025-08-28 06:26

/src/serenity/Userland/Libraries/LibLine/KeyCallbackMachine.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2020, the SerenityOS developers.
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <AK/Function.h>
10
#include <AK/HashMap.h>
11
#include <AK/Vector.h>
12
13
namespace Line {
14
15
class Editor;
16
17
struct Key {
18
    enum Modifier : int {
19
        None = 0,
20
        Alt = 1,
21
    };
22
23
    int modifiers { None };
24
    unsigned key { 0 };
25
26
    Key(unsigned c)
27
0
        : modifiers(None)
28
0
        , key(c) {};
29
30
    Key(unsigned c, int modifiers)
31
0
        : modifiers(modifiers)
32
0
        , key(c)
33
0
    {
34
0
    }
35
36
    bool operator==(Key const& other) const
37
0
    {
38
0
        return other.key == key && other.modifiers == modifiers;
39
0
    }
40
};
41
42
struct KeyCallback {
43
    KeyCallback(Function<bool(Editor&)> cb)
44
0
        : callback(move(cb))
45
0
    {
46
0
    }
47
    Function<bool(Editor&)> callback;
48
};
49
50
class KeyCallbackMachine {
51
public:
52
    void register_key_input_callback(Vector<Key>, Function<bool(Editor&)> callback);
53
    void key_pressed(Editor&, Key);
54
    void interrupted(Editor&);
55
0
    bool should_process_last_pressed_key() const { return m_should_process_this_key; }
56
57
private:
58
    HashMap<Vector<Key>, NonnullOwnPtr<KeyCallback>> m_key_callbacks;
59
    Vector<Vector<Key>> m_current_matching_keys;
60
    size_t m_sequence_length { 0 };
61
    bool m_should_process_this_key { true };
62
};
63
64
}
65
66
namespace AK {
67
68
template<>
69
struct Traits<Line::Key> : public DefaultTraits<Line::Key> {
70
0
    static constexpr bool is_trivial() { return true; }
71
0
    static unsigned hash(Line::Key k) { return pair_int_hash(k.key, k.modifiers); }
72
};
73
74
template<>
75
struct Traits<Vector<Line::Key>> : public DefaultTraits<Vector<Line::Key>> {
76
0
    static constexpr bool is_trivial() { return false; }
77
    static unsigned hash(Vector<Line::Key> const& ks)
78
0
    {
79
0
        unsigned h = 0;
80
0
        for (auto& k : ks)
81
0
            h ^= Traits<Line::Key>::hash(k);
82
0
        return h;
83
0
    }
84
};
85
86
}