KeymapCollection

Struct KeymapCollection 

Source
pub struct KeymapCollection { /* private fields */ }
Expand description

A collection of keymaps, typically loaded from multiple files

Implementations§

Source§

impl KeymapCollection

Source

pub fn new() -> KeymapCollection

Create a new empty keymap collection

Examples found in repository?
examples/editor_demo.rs (line 352)
350fn load_keymaps(cx: &mut App) {
351    // Load keymaps from JSON configuration
352    let mut keymap_collection = KeymapCollection::new();
353
354    let keymap_path = Path::new("examples/demo-keymap.json");
355    let loaded_from_file = if keymap_path.exists() {
356        match keymap_collection.load_file(keymap_path) {
357            Ok(_) => {
358                println!("Loaded keymaps from file: {}", keymap_path.display());
359                true
360            }
361            Err(e) => {
362                eprintln!("Failed to load keymap file: {}", e);
363                false
364            }
365        }
366    } else {
367        false
368    };
369
370    if !loaded_from_file {
371        let demo_keymap = include_str!("demo-keymap.json");
372        keymap_collection
373            .load_json(demo_keymap)
374            .expect("Failed to load embedded demo keymaps");
375        println!("Loaded embedded demo keymaps");
376    }
377
378    let specs = keymap_collection.get_binding_specs();
379
380    let mut bindings = Vec::new();
381
382    for spec in specs {
383        if !spec.action_name.starts_with("editor::") {
384            continue;
385        }
386
387        let action_name = spec
388            .action_name
389            .strip_prefix("editor::")
390            .unwrap_or(&spec.action_name);
391        let context = spec.context.as_deref();
392
393        match action_name {
394            "MoveUp" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveUp, context)),
395            "MoveDown" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveDown, context)),
396            "MoveLeft" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveLeft, context)),
397            "MoveRight" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveRight, context)),
398            "MoveUpWithShift" => {
399                bindings.push(KeyBinding::new(&spec.keystrokes, MoveUpWithShift, context))
400            }
401            "MoveDownWithShift" => bindings.push(KeyBinding::new(
402                &spec.keystrokes,
403                MoveDownWithShift,
404                context,
405            )),
406            "MoveLeftWithShift" => bindings.push(KeyBinding::new(
407                &spec.keystrokes,
408                MoveLeftWithShift,
409                context,
410            )),
411            "MoveRightWithShift" => bindings.push(KeyBinding::new(
412                &spec.keystrokes,
413                MoveRightWithShift,
414                context,
415            )),
416            "Backspace" => bindings.push(KeyBinding::new(&spec.keystrokes, Backspace, context)),
417            "Delete" => bindings.push(KeyBinding::new(&spec.keystrokes, Delete, context)),
418            "InsertNewline" => {
419                bindings.push(KeyBinding::new(&spec.keystrokes, InsertNewline, context))
420            }
421            "SelectAll" => bindings.push(KeyBinding::new(&spec.keystrokes, SelectAll, context)),
422            "Escape" => bindings.push(KeyBinding::new(&spec.keystrokes, Escape, context)),
423            "Copy" => bindings.push(KeyBinding::new(&spec.keystrokes, Copy, context)),
424            "Cut" => bindings.push(KeyBinding::new(&spec.keystrokes, Cut, context)),
425            "Paste" => bindings.push(KeyBinding::new(&spec.keystrokes, Paste, context)),
426            "NextTheme" => bindings.push(KeyBinding::new(&spec.keystrokes, NextTheme, context)),
427            "PreviousTheme" => {
428                bindings.push(KeyBinding::new(&spec.keystrokes, PreviousTheme, context))
429            }
430            "NextLanguage" => {
431                bindings.push(KeyBinding::new(&spec.keystrokes, NextLanguage, context))
432            }
433            "PreviousLanguage" => {
434                bindings.push(KeyBinding::new(&spec.keystrokes, PreviousLanguage, context))
435            }
436            unknown => {
437                eprintln!("Unknown editor action: {}", unknown);
438            }
439        }
440    }
441
442    println!(
443        "Registered {} keybindings from configuration",
444        bindings.len()
445    );
446    cx.bind_keys(bindings);
447}
Source

pub fn load_file<P>(&mut self, path: P) -> Result<(), Error>
where P: AsRef<Path>,

Load a keymap from a JSON file

Examples found in repository?
examples/editor_demo.rs (line 356)
350fn load_keymaps(cx: &mut App) {
351    // Load keymaps from JSON configuration
352    let mut keymap_collection = KeymapCollection::new();
353
354    let keymap_path = Path::new("examples/demo-keymap.json");
355    let loaded_from_file = if keymap_path.exists() {
356        match keymap_collection.load_file(keymap_path) {
357            Ok(_) => {
358                println!("Loaded keymaps from file: {}", keymap_path.display());
359                true
360            }
361            Err(e) => {
362                eprintln!("Failed to load keymap file: {}", e);
363                false
364            }
365        }
366    } else {
367        false
368    };
369
370    if !loaded_from_file {
371        let demo_keymap = include_str!("demo-keymap.json");
372        keymap_collection
373            .load_json(demo_keymap)
374            .expect("Failed to load embedded demo keymaps");
375        println!("Loaded embedded demo keymaps");
376    }
377
378    let specs = keymap_collection.get_binding_specs();
379
380    let mut bindings = Vec::new();
381
382    for spec in specs {
383        if !spec.action_name.starts_with("editor::") {
384            continue;
385        }
386
387        let action_name = spec
388            .action_name
389            .strip_prefix("editor::")
390            .unwrap_or(&spec.action_name);
391        let context = spec.context.as_deref();
392
393        match action_name {
394            "MoveUp" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveUp, context)),
395            "MoveDown" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveDown, context)),
396            "MoveLeft" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveLeft, context)),
397            "MoveRight" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveRight, context)),
398            "MoveUpWithShift" => {
399                bindings.push(KeyBinding::new(&spec.keystrokes, MoveUpWithShift, context))
400            }
401            "MoveDownWithShift" => bindings.push(KeyBinding::new(
402                &spec.keystrokes,
403                MoveDownWithShift,
404                context,
405            )),
406            "MoveLeftWithShift" => bindings.push(KeyBinding::new(
407                &spec.keystrokes,
408                MoveLeftWithShift,
409                context,
410            )),
411            "MoveRightWithShift" => bindings.push(KeyBinding::new(
412                &spec.keystrokes,
413                MoveRightWithShift,
414                context,
415            )),
416            "Backspace" => bindings.push(KeyBinding::new(&spec.keystrokes, Backspace, context)),
417            "Delete" => bindings.push(KeyBinding::new(&spec.keystrokes, Delete, context)),
418            "InsertNewline" => {
419                bindings.push(KeyBinding::new(&spec.keystrokes, InsertNewline, context))
420            }
421            "SelectAll" => bindings.push(KeyBinding::new(&spec.keystrokes, SelectAll, context)),
422            "Escape" => bindings.push(KeyBinding::new(&spec.keystrokes, Escape, context)),
423            "Copy" => bindings.push(KeyBinding::new(&spec.keystrokes, Copy, context)),
424            "Cut" => bindings.push(KeyBinding::new(&spec.keystrokes, Cut, context)),
425            "Paste" => bindings.push(KeyBinding::new(&spec.keystrokes, Paste, context)),
426            "NextTheme" => bindings.push(KeyBinding::new(&spec.keystrokes, NextTheme, context)),
427            "PreviousTheme" => {
428                bindings.push(KeyBinding::new(&spec.keystrokes, PreviousTheme, context))
429            }
430            "NextLanguage" => {
431                bindings.push(KeyBinding::new(&spec.keystrokes, NextLanguage, context))
432            }
433            "PreviousLanguage" => {
434                bindings.push(KeyBinding::new(&spec.keystrokes, PreviousLanguage, context))
435            }
436            unknown => {
437                eprintln!("Unknown editor action: {}", unknown);
438            }
439        }
440    }
441
442    println!(
443        "Registered {} keybindings from configuration",
444        bindings.len()
445    );
446    cx.bind_keys(bindings);
447}
Source

pub fn load_json(&mut self, json: &str) -> Result<(), Error>

Load keymaps from a JSON string

Examples found in repository?
examples/editor_demo.rs (line 373)
350fn load_keymaps(cx: &mut App) {
351    // Load keymaps from JSON configuration
352    let mut keymap_collection = KeymapCollection::new();
353
354    let keymap_path = Path::new("examples/demo-keymap.json");
355    let loaded_from_file = if keymap_path.exists() {
356        match keymap_collection.load_file(keymap_path) {
357            Ok(_) => {
358                println!("Loaded keymaps from file: {}", keymap_path.display());
359                true
360            }
361            Err(e) => {
362                eprintln!("Failed to load keymap file: {}", e);
363                false
364            }
365        }
366    } else {
367        false
368    };
369
370    if !loaded_from_file {
371        let demo_keymap = include_str!("demo-keymap.json");
372        keymap_collection
373            .load_json(demo_keymap)
374            .expect("Failed to load embedded demo keymaps");
375        println!("Loaded embedded demo keymaps");
376    }
377
378    let specs = keymap_collection.get_binding_specs();
379
380    let mut bindings = Vec::new();
381
382    for spec in specs {
383        if !spec.action_name.starts_with("editor::") {
384            continue;
385        }
386
387        let action_name = spec
388            .action_name
389            .strip_prefix("editor::")
390            .unwrap_or(&spec.action_name);
391        let context = spec.context.as_deref();
392
393        match action_name {
394            "MoveUp" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveUp, context)),
395            "MoveDown" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveDown, context)),
396            "MoveLeft" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveLeft, context)),
397            "MoveRight" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveRight, context)),
398            "MoveUpWithShift" => {
399                bindings.push(KeyBinding::new(&spec.keystrokes, MoveUpWithShift, context))
400            }
401            "MoveDownWithShift" => bindings.push(KeyBinding::new(
402                &spec.keystrokes,
403                MoveDownWithShift,
404                context,
405            )),
406            "MoveLeftWithShift" => bindings.push(KeyBinding::new(
407                &spec.keystrokes,
408                MoveLeftWithShift,
409                context,
410            )),
411            "MoveRightWithShift" => bindings.push(KeyBinding::new(
412                &spec.keystrokes,
413                MoveRightWithShift,
414                context,
415            )),
416            "Backspace" => bindings.push(KeyBinding::new(&spec.keystrokes, Backspace, context)),
417            "Delete" => bindings.push(KeyBinding::new(&spec.keystrokes, Delete, context)),
418            "InsertNewline" => {
419                bindings.push(KeyBinding::new(&spec.keystrokes, InsertNewline, context))
420            }
421            "SelectAll" => bindings.push(KeyBinding::new(&spec.keystrokes, SelectAll, context)),
422            "Escape" => bindings.push(KeyBinding::new(&spec.keystrokes, Escape, context)),
423            "Copy" => bindings.push(KeyBinding::new(&spec.keystrokes, Copy, context)),
424            "Cut" => bindings.push(KeyBinding::new(&spec.keystrokes, Cut, context)),
425            "Paste" => bindings.push(KeyBinding::new(&spec.keystrokes, Paste, context)),
426            "NextTheme" => bindings.push(KeyBinding::new(&spec.keystrokes, NextTheme, context)),
427            "PreviousTheme" => {
428                bindings.push(KeyBinding::new(&spec.keystrokes, PreviousTheme, context))
429            }
430            "NextLanguage" => {
431                bindings.push(KeyBinding::new(&spec.keystrokes, NextLanguage, context))
432            }
433            "PreviousLanguage" => {
434                bindings.push(KeyBinding::new(&spec.keystrokes, PreviousLanguage, context))
435            }
436            unknown => {
437                eprintln!("Unknown editor action: {}", unknown);
438            }
439        }
440    }
441
442    println!(
443        "Registered {} keybindings from configuration",
444        bindings.len()
445    );
446    cx.bind_keys(bindings);
447}
Source

pub fn load_defaults(&mut self) -> Result<(), Error>

Load default keymaps

Source

pub fn get_binding_specs(&self) -> Vec<BindingSpec>

Get all key binding specifications from this collection

Returns a list of binding specifications that can be used to create actual GPUI key bindings with concrete action types.

Examples found in repository?
examples/editor_demo.rs (line 378)
350fn load_keymaps(cx: &mut App) {
351    // Load keymaps from JSON configuration
352    let mut keymap_collection = KeymapCollection::new();
353
354    let keymap_path = Path::new("examples/demo-keymap.json");
355    let loaded_from_file = if keymap_path.exists() {
356        match keymap_collection.load_file(keymap_path) {
357            Ok(_) => {
358                println!("Loaded keymaps from file: {}", keymap_path.display());
359                true
360            }
361            Err(e) => {
362                eprintln!("Failed to load keymap file: {}", e);
363                false
364            }
365        }
366    } else {
367        false
368    };
369
370    if !loaded_from_file {
371        let demo_keymap = include_str!("demo-keymap.json");
372        keymap_collection
373            .load_json(demo_keymap)
374            .expect("Failed to load embedded demo keymaps");
375        println!("Loaded embedded demo keymaps");
376    }
377
378    let specs = keymap_collection.get_binding_specs();
379
380    let mut bindings = Vec::new();
381
382    for spec in specs {
383        if !spec.action_name.starts_with("editor::") {
384            continue;
385        }
386
387        let action_name = spec
388            .action_name
389            .strip_prefix("editor::")
390            .unwrap_or(&spec.action_name);
391        let context = spec.context.as_deref();
392
393        match action_name {
394            "MoveUp" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveUp, context)),
395            "MoveDown" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveDown, context)),
396            "MoveLeft" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveLeft, context)),
397            "MoveRight" => bindings.push(KeyBinding::new(&spec.keystrokes, MoveRight, context)),
398            "MoveUpWithShift" => {
399                bindings.push(KeyBinding::new(&spec.keystrokes, MoveUpWithShift, context))
400            }
401            "MoveDownWithShift" => bindings.push(KeyBinding::new(
402                &spec.keystrokes,
403                MoveDownWithShift,
404                context,
405            )),
406            "MoveLeftWithShift" => bindings.push(KeyBinding::new(
407                &spec.keystrokes,
408                MoveLeftWithShift,
409                context,
410            )),
411            "MoveRightWithShift" => bindings.push(KeyBinding::new(
412                &spec.keystrokes,
413                MoveRightWithShift,
414                context,
415            )),
416            "Backspace" => bindings.push(KeyBinding::new(&spec.keystrokes, Backspace, context)),
417            "Delete" => bindings.push(KeyBinding::new(&spec.keystrokes, Delete, context)),
418            "InsertNewline" => {
419                bindings.push(KeyBinding::new(&spec.keystrokes, InsertNewline, context))
420            }
421            "SelectAll" => bindings.push(KeyBinding::new(&spec.keystrokes, SelectAll, context)),
422            "Escape" => bindings.push(KeyBinding::new(&spec.keystrokes, Escape, context)),
423            "Copy" => bindings.push(KeyBinding::new(&spec.keystrokes, Copy, context)),
424            "Cut" => bindings.push(KeyBinding::new(&spec.keystrokes, Cut, context)),
425            "Paste" => bindings.push(KeyBinding::new(&spec.keystrokes, Paste, context)),
426            "NextTheme" => bindings.push(KeyBinding::new(&spec.keystrokes, NextTheme, context)),
427            "PreviousTheme" => {
428                bindings.push(KeyBinding::new(&spec.keystrokes, PreviousTheme, context))
429            }
430            "NextLanguage" => {
431                bindings.push(KeyBinding::new(&spec.keystrokes, NextLanguage, context))
432            }
433            "PreviousLanguage" => {
434                bindings.push(KeyBinding::new(&spec.keystrokes, PreviousLanguage, context))
435            }
436            unknown => {
437                eprintln!("Unknown editor action: {}", unknown);
438            }
439        }
440    }
441
442    println!(
443        "Registered {} keybindings from configuration",
444        bindings.len()
445    );
446    cx.bind_keys(bindings);
447}
Source

pub fn keymaps(&self) -> &[Keymap]

Get all keymaps in this collection

Source

pub fn add(&mut self, keymap: Keymap)

Add a keymap to this collection

Source

pub fn clear(&mut self)

Clear all keymaps from this collection

Source

pub fn find_bindings_for_action(&self, action_name: &str) -> Vec<BindingSpec>

Find all bindings for a given action

Source

pub fn find_action( &self, keystrokes: &str, context: Option<&str>, ) -> Option<&str>

Find the action for a given keystroke in a context

Trait Implementations§

Source§

impl Debug for KeymapCollection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for KeymapCollection

Source§

fn default() -> KeymapCollection

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> NoneValue for T
where T: Default,

Source§

type NoneType = T

Source§

fn null_value() -> T

The none-equivalent value.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more