mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 06:22:46 +00:00 
			
		
		
		
	 1682f0b760
			
		
	
	
		1682f0b760
		
	
	
	
	
		
			
			SPDX License Identifiers are a more compact / standardized way of representing file license information. See: https://spdx.dev/resources/use/#identifiers This was done with the `ambr` search and replace tool. ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
		
			
				
	
	
		
			92 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			92 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2020, the SerenityOS developers.
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #pragma once
 | |
| 
 | |
| #include <AK/Function.h>
 | |
| #include <AK/HashMap.h>
 | |
| #include <AK/String.h>
 | |
| #include <AK/Vector.h>
 | |
| 
 | |
| namespace Line {
 | |
| 
 | |
| class Editor;
 | |
| 
 | |
| struct Key {
 | |
|     enum Modifier : int {
 | |
|         None = 0,
 | |
|         Alt = 1,
 | |
|     };
 | |
| 
 | |
|     int modifiers { None };
 | |
|     unsigned key { 0 };
 | |
| 
 | |
|     Key(unsigned c)
 | |
|         : modifiers(None)
 | |
|         , key(c) {};
 | |
| 
 | |
|     Key(unsigned c, int modifiers)
 | |
|         : modifiers(modifiers)
 | |
|         , key(c)
 | |
|     {
 | |
|     }
 | |
| 
 | |
|     bool operator==(const Key& other) const
 | |
|     {
 | |
|         return other.key == key && other.modifiers == modifiers;
 | |
|     }
 | |
| 
 | |
|     bool operator!=(const Key& other) const
 | |
|     {
 | |
|         return !(*this == other);
 | |
|     }
 | |
| };
 | |
| 
 | |
| struct KeyCallback {
 | |
|     KeyCallback(Function<bool(Editor&)> cb)
 | |
|         : callback(move(cb))
 | |
|     {
 | |
|     }
 | |
|     Function<bool(Editor&)> callback;
 | |
| };
 | |
| 
 | |
| class KeyCallbackMachine {
 | |
| public:
 | |
|     void register_key_input_callback(Vector<Key>, Function<bool(Editor&)> callback);
 | |
|     void key_pressed(Editor&, Key);
 | |
|     void interrupted(Editor&);
 | |
|     bool should_process_last_pressed_key() const { return m_should_process_this_key; }
 | |
| 
 | |
| private:
 | |
|     HashMap<Vector<Key>, NonnullOwnPtr<KeyCallback>> m_key_callbacks;
 | |
|     Vector<Vector<Key>> m_current_matching_keys;
 | |
|     size_t m_sequence_length { 0 };
 | |
|     bool m_should_process_this_key { true };
 | |
| };
 | |
| 
 | |
| }
 | |
| 
 | |
| namespace AK {
 | |
| 
 | |
| template<>
 | |
| struct Traits<Line::Key> : public GenericTraits<Line::Key> {
 | |
|     static constexpr bool is_trivial() { return true; }
 | |
|     static unsigned hash(Line::Key k) { return pair_int_hash(k.key, k.modifiers); }
 | |
| };
 | |
| 
 | |
| template<>
 | |
| struct Traits<Vector<Line::Key>> : public GenericTraits<Vector<Line::Key>> {
 | |
|     static constexpr bool is_trivial() { return false; }
 | |
|     static unsigned hash(const Vector<Line::Key>& ks)
 | |
|     {
 | |
|         unsigned h = 0;
 | |
|         for (auto& k : ks)
 | |
|             h ^= Traits<Line::Key>::hash(k);
 | |
|         return h;
 | |
|     }
 | |
| };
 | |
| 
 | |
| }
 |