Implemented shortcut metadata (not handling). Added command metadata docs template.

This commit is contained in:
Samuel Guerra 2021-06-23 01:10:40 -03:00
parent f055972829
commit 1aca0ce515
6 changed files with 116 additions and 14 deletions

View File

@ -116,7 +116,7 @@ pub mod text_box {
fn main() {
App::default().run(|ctx| {
CopyCommand::shortcuts().set(ctx.vars, shortcut![Ctrl+C]);
CopyCommand::shortcuts().set(ctx.vars, shortcut!(Ctrl+C));
})
}

View File

@ -52,7 +52,7 @@ macro_rules! command {
}
/// Schedule an event update if the command is enabled.
/// `parameter` is an optional value for the command handler.
/// The `parameter` is an optional value for the command handler.
#[inline]
#[allow(unused)]
pub fn notify(self, events: &mut $crate::event::Events, parameter: Option<std::rc::Rc<dyn std::any::Any>>) {
@ -256,8 +256,10 @@ state_key! {
impl<C: Command> CommandNameExt for C {
fn name(self) -> RcVar<Text> {
self.with_meta(|m| {
let entry = m.entry::<CommandNameKey>();
let var = entry.or_insert_with(|| var_from(type_name::<C>()));
let var = m.entry::<CommandNameKey>().or_insert_with(|| {
let name = type_name::<C>();
var_from(name.strip_suffix("Command").unwrap_or(name))
});
var.clone()
})
}
@ -284,17 +286,12 @@ state_key! {
}
impl<C: Command> CommandInfoExt for C {
fn info(self) -> RcVar<Text> {
self.with_meta(|m| {
let entry = m.entry::<CommandInfoKey>();
let var = entry.or_insert_with(|| var_from(""));
var.clone()
})
self.with_meta(|m| m.entry::<CommandInfoKey>().or_insert_with(|| var_from("")).clone())
}
fn init_info(self, info: impl Into<Text>) -> Self {
self.with_meta(|m| {
let entry = m.entry::<CommandInfoKey>();
entry.or_insert_with(|| var(info.into()));
m.entry::<CommandInfoKey>().or_insert_with(|| var(info.into()));
});
self
}

View File

@ -2,12 +2,14 @@
use super::{service::Service, units::LayoutPoint, WidgetId};
use crate::app::*;
use crate::command::Command;
use crate::context::*;
use crate::event::*;
use crate::focus::Focus;
use crate::keyboard::*;
use crate::mouse::*;
use crate::render::*;
use crate::var::RcVar;
use crate::window::{WindowEvent, WindowId, Windows};
use std::num::NonZeroU32;
use std::{
@ -1190,3 +1192,30 @@ impl HeadlessAppGestureExt for HeadlessApp {
}
}
}
/// Adds the [`shortcut`](Self::shortcut) metadata.
///
/// If a command has a shortcut the [`GestureManager`] will invoke the command when the shortcut is pressed
/// and the command is enabled.
pub trait CommandShortcutExt {
/// Gets a read-write variable that is zero-or-more shortcuts that invoke the command.
fn shortcut(self) -> RcVar<Shortcuts>;
/// Sets the initial shortcuts.
fn init_shortcut(self, shortcut: impl Into<Shortcuts>) -> Self;
}
impl<C: Command> CommandShortcutExt for C {
fn shortcut(self) -> RcVar<Shortcuts> {
self.with_meta(|m| m.entry::<CommandShortcutKey>().or_default().clone())
}
fn init_shortcut(self, shortcut: impl Into<Shortcuts>) -> Self {
self.with_meta(|m| {
m.entry::<CommandShortcutKey>().or_insert_with(|| RcVar::new(shortcut.into()));
});
self
}
}
state_key! {
struct CommandShortcutKey: RcVar<Shortcuts>;
}

View File

@ -79,6 +79,11 @@ impl<T: VarValue> Var<T> for OwnedVar<T> {
self
}
}
impl<T: VarValue + Default> Default for OwnedVar<T> {
fn default() -> Self {
OwnedVar(T::default())
}
}
impl<T: VarValue> IntoVar<T> for OwnedVar<T> {
type Var = Self;

View File

@ -174,6 +174,11 @@ impl<T: VarValue> Clone for RcVar<T> {
RcVar(Rc::clone(&self.0))
}
}
impl<T: VarValue + Default> Default for RcVar<T> {
fn default() -> Self {
var(T::default())
}
}
/// New [`RcVar`].
#[inline]

View File

@ -14,20 +14,86 @@
//! commands when the shortcut is pressed.
use crate::core::command::*;
use crate::core::gesture::{shortcut, CommandShortcutExt};
command! {
/// Represents the clipboard **cut** action.
///
/// # Metadata
///
/// This command initializes with the following metadata:
///
/// | metadata | value |
/// |--------------|-------------------------------------------------------|
/// | [`name`] | "Cut" |
/// | [`info`] | "Remove the selection and place it in the clipboard." |
/// | [`shortcut`] | `CTRL+X`, `SHIFT+Delete` |
///
/// [`name`]: CommandNameExt
/// [`info`]: CommandInfoExt
/// [`shortcut`]: CommandShortcutExt
pub CutCommand
.init_name("Cut")
.init_info("Remove the selection and place it in the clipboard.");
.init_info("Remove the selection and place it in the clipboard.")
.init_shortcut([shortcut!(CTRL+X), shortcut!(SHIFT+Delete)]);
/// Represents the clipboard **copy** action.
///
/// # Metadata
///
/// This command initializes with the following metadata:
///
/// | metadata | value |
/// |--------------|-------------------------------------------------------|
/// | [`name`] | "Copy" |
/// | [`info`] | "Place a copy of the selection in the clipboard." |
/// | [`shortcut`] | `CTRL+C`, `CTRL+Insert` |
///
/// [`name`]: CommandNameExt
/// [`info`]: CommandInfoExt
/// [`shortcut`]: CommandShortcutExt
pub CopyCommand
.init_name("Copy")
.init_info("Place a copy of the selection in the clipboard.");
.init_info("Place a copy of the selection in the clipboard.")
.init_shortcut([shortcut!(CTRL+C), shortcut!(CTRL+Insert)]);
/// Represents the clipboard **paste** action.
///
/// # Metadata
///
/// This command initializes with the following metadata:
///
/// | metadata | value |
/// |--------------|-------------------------------------------------------|
/// | [`name`] | "Paste" |
/// | [`info`] | "Insert content from the clipboard." |
/// | [`shortcut`] | `CTRL+V`, `SHIFT+Insert` |
///
/// [`name`]: CommandNameExt
/// [`info`]: CommandInfoExt
/// [`shortcut`]: CommandShortcutExt
pub PasteCommand
.init_name("Paste")
.init_info("Insert content from the clipboard.");
.init_info("Insert content from the clipboard.")
.init_shortcut([shortcut!(CTRL+V), shortcut!(SHIFT+Insert)]);
/// Represents the window close action.
///
/// # Metadata
///
/// This command initializes with the following metadata:
///
/// | metadata | value |
/// |--------------|-------------------------------------------------------|
/// | [`name`] | "Close Window" |
/// | [`info`] | "Close the current window." |
/// | [`shortcut`] | `ALT+F4`, `CTRL+W` |
///
/// [`name`]: CommandNameExt
/// [`info`]: CommandInfoExt
/// [`shortcut`]: CommandShortcutExt
pub CloseWindowCommand
.init_name("Close Window")
.init_info("Close the current window.")
.init_shortcut([shortcut!(ALT+F4), shortcut!(CTRL+W)]);
}