use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
use super::{KeyboardEvent, MouseButton};
use crate::{
app::{Accelerator, SyntheticModifiers},
core::NodeId,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum NavigationCommand {
Back,
Forward,
}
const APPLE: bool = cfg!(any(target_os = "macos", target_os = "ios"));
impl NavigationCommand {
pub fn of_key(event: &KeyboardEvent) -> Option<Self> {
Self::of_key_on(event, APPLE)
}
pub fn of_key_on(event: &KeyboardEvent, apple: bool) -> Option<Self> {
let named = |name: &str| event.key == name || event.code == name;
if !event.ctrl && !event.alt && !event.meta {
if named("BrowserBack") {
return Some(Self::Back);
}
if named("BrowserForward") {
return Some(Self::Forward);
}
}
if event.shift {
return None;
}
if apple {
if !event.meta || event.ctrl || event.alt {
return None;
}
return match event.key.as_str() {
"[" => Some(Self::Back),
"]" => Some(Self::Forward),
key if key.is_ascii() && !key.is_empty() && key != "Dead" => None,
_ => match event.code.as_str() {
"BracketLeft" => Some(Self::Back),
"BracketRight" => Some(Self::Forward),
_ => None,
},
};
}
if !event.alt || event.ctrl || event.meta {
return None;
}
if named("ArrowLeft") {
Some(Self::Back)
} else if named("ArrowRight") {
Some(Self::Forward)
} else {
None
}
}
pub fn of_button(button: MouseButton) -> Option<Self> {
match button {
MouseButton::Back => Some(Self::Back),
MouseButton::Forward => Some(Self::Forward),
_ => None,
}
}
pub fn accelerator(self) -> Accelerator {
Self::accelerator_on(self, APPLE)
}
pub(crate) fn accelerator_on(self, apple: bool) -> Accelerator {
if apple {
let meta = SyntheticModifiers::default().meta();
match self {
Self::Back => Accelerator::new("[", meta),
Self::Forward => Accelerator::new("]", meta),
}
} else {
let alt = SyntheticModifiers::default().alt();
match self {
Self::Back => Accelerator::new("ArrowLeft", alt),
Self::Forward => Accelerator::new("ArrowRight", alt),
}
}
}
pub fn reversed(self) -> Self {
match self {
Self::Back => Self::Forward,
Self::Forward => Self::Back,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum NavigationSource {
Key,
Mouse,
Swipe,
Edge,
Browser,
App,
}
pub(crate) type HistoryMover = Arc<dyn Fn(NavigationCommand, NavigationSource) -> bool + Send + Sync>;
#[derive(Clone)]
pub struct HistoryEvent {
command: NavigationCommand,
source: NavigationSource,
pub target_id: NodeId,
pub origin_id: NodeId,
passed: Arc<AtomicBool>,
}
impl HistoryEvent {
pub(crate) fn new(command: NavigationCommand, source: NavigationSource, origin_id: NodeId) -> Self {
Self {
command,
source,
target_id: origin_id,
origin_id,
passed: Arc::new(AtomicBool::new(false)),
}
}
pub(crate) fn at(&self, target_id: NodeId) -> Self {
Self {
command: self.command,
source: self.source,
target_id,
origin_id: self.origin_id,
passed: Arc::clone(&self.passed),
}
}
pub fn command(&self) -> NavigationCommand {
self.command
}
pub fn source(&self) -> NavigationSource {
self.source
}
pub fn pass_on(&self) {
self.passed.store(true, Ordering::Relaxed);
}
pub fn passed_on(&self) -> bool {
self.passed.load(Ordering::Relaxed)
}
pub(crate) fn reset(&self) {
self.passed.store(false, Ordering::Relaxed);
}
}
impl std::fmt::Debug for HistoryEvent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HistoryEvent")
.field("command", &self.command)
.field("source", &self.source)
.field("target_id", &self.target_id)
.field("origin_id", &self.origin_id)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn key(key: &str, code: &str, shift: bool, ctrl: bool, alt: bool, meta: bool) -> KeyboardEvent {
KeyboardEvent::new(key, code, shift, ctrl, alt, meta, NodeId::UNASSIGNED)
}
#[test]
fn elsewhere_alt_and_an_arrow() {
let of = |event: &KeyboardEvent| NavigationCommand::of_key_on(event, false);
use NavigationCommand::*;
assert_eq!(
of(&key("ArrowLeft", "ArrowLeft", false, false, true, false)),
Some(Back)
);
assert_eq!(
of(&key("ArrowRight", "ArrowRight", false, false, true, false)),
Some(Forward)
);
assert_eq!(
of(&key("ArrowLeft", "ArrowLeft", false, false, false, false)),
None,
"an arrow alone"
);
assert_eq!(
of(&key("ArrowLeft", "ArrowLeft", false, true, true, false)),
None,
"Ctrl+Alt"
);
assert_eq!(
of(&key("ArrowLeft", "ArrowLeft", true, false, true, false)),
None,
"Shift+Alt"
);
assert_eq!(
of(&key("[", "BracketLeft", false, false, false, true)),
None,
"Meta+[ is Apple's"
);
}
#[test]
fn apple_command_and_a_bracket() {
let of = |event: &KeyboardEvent| NavigationCommand::of_key_on(event, true);
use NavigationCommand::*;
assert_eq!(of(&key("[", "BracketLeft", false, false, false, true)), Some(Back));
assert_eq!(of(&key("]", "BracketRight", false, false, false, true)), Some(Forward));
assert_eq!(of(&key("х", "BracketLeft", false, false, false, true)), Some(Back));
assert_eq!(of(&key("ї", "BracketRight", false, false, false, true)), Some(Forward));
assert_eq!(
of(&key("{", "BracketLeft", true, false, false, true)),
None,
"Cmd+Shift+[ is the tab before"
);
assert_eq!(
of(&key("ArrowLeft", "ArrowLeft", false, false, true, false)),
None,
"Option+← moves by a word on a Mac"
);
assert_eq!(of(&key("[", "BracketLeft", false, true, false, false)), None, "Ctrl+[");
assert_eq!(
of(&key("^", "BracketLeft", false, false, false, true)),
None,
"a key that says another sign of its own"
);
}
#[test]
fn a_key_of_its_own_on_every_platform() {
for apple in [false, true] {
let of = |event: &KeyboardEvent| NavigationCommand::of_key_on(event, apple);
assert_eq!(
of(&key("BrowserBack", "", false, false, false, false)),
Some(NavigationCommand::Back),
"Android's back, as winit names it"
);
assert_eq!(
of(&key("", "BrowserForward", false, false, false, false)),
Some(NavigationCommand::Forward)
);
assert_eq!(of(&key("BrowserBack", "", false, true, false, false)), None);
}
}
#[test]
fn the_accelerator_is_a_key_of_the_command() {
for apple in [false, true] {
for command in [NavigationCommand::Back, NavigationCommand::Forward] {
let accelerator = command.accelerator_on(apple);
let m = accelerator.modifiers;
let event = KeyboardEvent::new(
accelerator.key.to_string(),
String::new(),
m.shift,
m.ctrl,
m.alt,
m.meta,
NodeId::UNASSIGNED,
);
assert_eq!(NavigationCommand::of_key_on(&event, apple), Some(command));
}
}
}
#[test]
fn the_side_buttons() {
assert_eq!(
NavigationCommand::of_button(MouseButton::Back),
Some(NavigationCommand::Back)
);
assert_eq!(
NavigationCommand::of_button(MouseButton::Forward),
Some(NavigationCommand::Forward)
);
assert_eq!(NavigationCommand::of_button(MouseButton::Left), None);
assert_eq!(NavigationCommand::of_button(MouseButton::Other(8)), None);
}
}