Create jump and macos_command methods in keyboard::Modifiers
This commit is contained in:
parent
8cfa8149f5
commit
3312dc8080
3 changed files with 32 additions and 70 deletions
|
|
@ -84,4 +84,28 @@ impl Modifiers {
|
||||||
|
|
||||||
is_pressed
|
is_pressed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns true if the "jump key" is pressed in the [`Modifiers`].
|
||||||
|
///
|
||||||
|
/// The "jump key" is the modifier key used to widen text motions. It is the `Alt`
|
||||||
|
/// key in macOS and the `Ctrl` key in other platforms.
|
||||||
|
pub fn jump(self) -> bool {
|
||||||
|
if cfg!(target_os = "macos") {
|
||||||
|
self.alt()
|
||||||
|
} else {
|
||||||
|
self.control()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns true if the "command key" is pressed on a macOS device.
|
||||||
|
///
|
||||||
|
/// This is relevant for macOS-specific actions (e.g. `⌘ + ArrowLeft` moves the cursor
|
||||||
|
/// to the beginning of the line).
|
||||||
|
pub fn macos_command(self) -> bool {
|
||||||
|
if cfg!(target_os = "macos") {
|
||||||
|
self.logo()
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -768,9 +768,7 @@ impl Update {
|
||||||
|
|
||||||
if let keyboard::Key::Named(named_key) = key.as_ref() {
|
if let keyboard::Key::Named(named_key) = key.as_ref() {
|
||||||
if let Some(motion) = motion(named_key) {
|
if let Some(motion) = motion(named_key) {
|
||||||
let motion = if platform::is_macos_command_pressed(
|
let motion = if modifiers.macos_command() {
|
||||||
modifiers,
|
|
||||||
) {
|
|
||||||
match motion {
|
match motion {
|
||||||
Motion::Left => Motion::Home,
|
Motion::Left => Motion::Home,
|
||||||
Motion::Right => Motion::End,
|
Motion::Right => Motion::End,
|
||||||
|
|
@ -780,9 +778,7 @@ impl Update {
|
||||||
motion
|
motion
|
||||||
};
|
};
|
||||||
|
|
||||||
let motion = if platform::is_jump_modifier_pressed(
|
let motion = if modifiers.jump() {
|
||||||
modifiers,
|
|
||||||
) {
|
|
||||||
motion.widen()
|
motion.widen()
|
||||||
} else {
|
} else {
|
||||||
motion
|
motion
|
||||||
|
|
@ -819,31 +815,6 @@ fn motion(key: key::Named) -> Option<Motion> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod platform {
|
|
||||||
use crate::core::keyboard;
|
|
||||||
|
|
||||||
pub fn is_jump_modifier_pressed(modifiers: keyboard::Modifiers) -> bool {
|
|
||||||
if cfg!(target_os = "macos") {
|
|
||||||
modifiers.alt()
|
|
||||||
} else {
|
|
||||||
modifiers.control()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether the command key is pressed on a macOS device.
|
|
||||||
///
|
|
||||||
/// This is relevant for actions like ⌘ + ArrowLeft to move to the beginning of the
|
|
||||||
/// line where the equivalent behavior for `modifiers.command()` is instead a jump on
|
|
||||||
/// other platforms.
|
|
||||||
pub fn is_macos_command_pressed(modifiers: keyboard::Modifiers) -> bool {
|
|
||||||
if cfg!(target_os = "macos") {
|
|
||||||
modifiers.logo()
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The possible status of a [`TextEditor`].
|
/// The possible status of a [`TextEditor`].
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum Status {
|
pub enum Status {
|
||||||
|
|
|
||||||
|
|
@ -826,7 +826,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
keyboard::Key::Named(key::Named::Backspace) => {
|
keyboard::Key::Named(key::Named::Backspace) => {
|
||||||
if platform::is_jump_modifier_pressed(modifiers)
|
if modifiers.jump()
|
||||||
&& state.cursor.selection(&self.value).is_none()
|
&& state.cursor.selection(&self.value).is_none()
|
||||||
{
|
{
|
||||||
if self.is_secure {
|
if self.is_secure {
|
||||||
|
|
@ -850,7 +850,7 @@ where
|
||||||
update_cache(state, &self.value);
|
update_cache(state, &self.value);
|
||||||
}
|
}
|
||||||
keyboard::Key::Named(key::Named::Delete) => {
|
keyboard::Key::Named(key::Named::Delete) => {
|
||||||
if platform::is_jump_modifier_pressed(modifiers)
|
if modifiers.jump()
|
||||||
&& state.cursor.selection(&self.value).is_none()
|
&& state.cursor.selection(&self.value).is_none()
|
||||||
{
|
{
|
||||||
if self.is_secure {
|
if self.is_secure {
|
||||||
|
|
@ -897,9 +897,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
keyboard::Key::Named(key::Named::ArrowLeft)
|
keyboard::Key::Named(key::Named::ArrowLeft)
|
||||||
if platform::is_macos_command_pressed(
|
if modifiers.macos_command() =>
|
||||||
modifiers,
|
|
||||||
) =>
|
|
||||||
{
|
{
|
||||||
if modifiers.shift() {
|
if modifiers.shift() {
|
||||||
state.cursor.select_range(
|
state.cursor.select_range(
|
||||||
|
|
@ -911,9 +909,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
keyboard::Key::Named(key::Named::ArrowRight)
|
keyboard::Key::Named(key::Named::ArrowRight)
|
||||||
if platform::is_macos_command_pressed(
|
if modifiers.macos_command() =>
|
||||||
modifiers,
|
|
||||||
) =>
|
|
||||||
{
|
{
|
||||||
if modifiers.shift() {
|
if modifiers.shift() {
|
||||||
state.cursor.select_range(
|
state.cursor.select_range(
|
||||||
|
|
@ -925,9 +921,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
keyboard::Key::Named(key::Named::ArrowLeft) => {
|
keyboard::Key::Named(key::Named::ArrowLeft) => {
|
||||||
if platform::is_jump_modifier_pressed(modifiers)
|
if modifiers.jump() && !self.is_secure {
|
||||||
&& !self.is_secure
|
|
||||||
{
|
|
||||||
if modifiers.shift() {
|
if modifiers.shift() {
|
||||||
state
|
state
|
||||||
.cursor
|
.cursor
|
||||||
|
|
@ -944,9 +938,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
keyboard::Key::Named(key::Named::ArrowRight) => {
|
keyboard::Key::Named(key::Named::ArrowRight) => {
|
||||||
if platform::is_jump_modifier_pressed(modifiers)
|
if modifiers.jump() && !self.is_secure {
|
||||||
&& !self.is_secure
|
|
||||||
{
|
|
||||||
if modifiers.shift() {
|
if modifiers.shift() {
|
||||||
state
|
state
|
||||||
.cursor
|
.cursor
|
||||||
|
|
@ -1309,31 +1301,6 @@ impl<P: text::Paragraph> operation::TextInput for State<P> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod platform {
|
|
||||||
use crate::core::keyboard;
|
|
||||||
|
|
||||||
pub fn is_jump_modifier_pressed(modifiers: keyboard::Modifiers) -> bool {
|
|
||||||
if cfg!(target_os = "macos") {
|
|
||||||
modifiers.alt()
|
|
||||||
} else {
|
|
||||||
modifiers.control()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether the command key is pressed on a macOS device.
|
|
||||||
///
|
|
||||||
/// This is relevant for actions like ⌘ + ArrowLeft to move to the beginning of the
|
|
||||||
/// line where the equivalent behavior for `modifiers.command()` is instead a jump on
|
|
||||||
/// other platforms.
|
|
||||||
pub fn is_macos_command_pressed(modifiers: keyboard::Modifiers) -> bool {
|
|
||||||
if cfg!(target_os = "macos") {
|
|
||||||
modifiers.logo()
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn offset<P: text::Paragraph>(
|
fn offset<P: text::Paragraph>(
|
||||||
text_bounds: Rectangle,
|
text_bounds: Rectangle,
|
||||||
value: &Value,
|
value: &Value,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue