pub struct KeymapCollection { /* private fields */ }Expand description
A collection of keymaps, typically loaded from multiple files
Implementations§
Source§impl KeymapCollection
impl KeymapCollection
Sourcepub fn new() -> KeymapCollection
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}Sourcepub fn load_file<P>(&mut self, path: P) -> Result<(), Error>
pub fn load_file<P>(&mut self, path: P) -> Result<(), Error>
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}Sourcepub fn load_json(&mut self, json: &str) -> Result<(), Error>
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}Sourcepub fn load_defaults(&mut self) -> Result<(), Error>
pub fn load_defaults(&mut self) -> Result<(), Error>
Load default keymaps
Sourcepub fn get_binding_specs(&self) -> Vec<BindingSpec>
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}Sourcepub fn find_bindings_for_action(&self, action_name: &str) -> Vec<BindingSpec>
pub fn find_bindings_for_action(&self, action_name: &str) -> Vec<BindingSpec>
Find all bindings for a given action
Trait Implementations§
Source§impl Debug for KeymapCollection
impl Debug for KeymapCollection
Source§impl Default for KeymapCollection
impl Default for KeymapCollection
Source§fn default() -> KeymapCollection
fn default() -> KeymapCollection
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for KeymapCollection
impl RefUnwindSafe for KeymapCollection
impl Send for KeymapCollection
impl Sync for KeymapCollection
impl Unpin for KeymapCollection
impl UnwindSafe for KeymapCollection
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
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>
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)
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)
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
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
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().