Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

treat altgr as Modifier #408

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions examples/context/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use floem::{
keyboard::{Key, ModifiersState, NamedKey},
keyboard::{Key, Modifiers, NamedKey},
peniko::Color,
reactive::{provide_context, use_context},
view::View,
Expand Down Expand Up @@ -47,11 +47,9 @@ fn app_view() -> impl View {
});

let id = view.id();
view.on_key_up(
Key::Named(NamedKey::F11),
ModifiersState::empty(),
move |_| id.inspect(),
)
view.on_key_up(Key::Named(NamedKey::F11), Modifiers::empty(), move |_| {
id.inspect()
})
}

fn main() {
Expand Down
10 changes: 4 additions & 6 deletions examples/counter/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use floem::{
keyboard::{Key, ModifiersState, NamedKey},
keyboard::{Key, Modifiers, NamedKey},
peniko::Color,
reactive::create_signal,
unit::UnitExt,
Expand Down Expand Up @@ -73,11 +73,9 @@ fn app_view() -> impl View {
});

let id = view.id();
view.on_key_up(
Key::Named(NamedKey::F11),
ModifiersState::empty(),
move |_| id.inspect(),
)
view.on_key_up(Key::Named(NamedKey::F11), Modifiers::empty(), move |_| {
id.inspect()
})
}

fn main() {
Expand Down
10 changes: 4 additions & 6 deletions examples/editor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use floem::{
keyboard::{Key, ModifiersState, NamedKey},
keyboard::{Key, Modifiers, NamedKey},
reactive::RwSignal,
view::View,
views::{
Expand Down Expand Up @@ -67,11 +67,9 @@ fn app_view() -> impl View {
.style(|s| s.size_full().flex_col().items_center().justify_center());

let id = view.id();
view.on_key_up(
Key::Named(NamedKey::F11),
ModifiersState::empty(),
move |_| id.inspect(),
)
view.on_key_up(Key::Named(NamedKey::F11), Modifiers::empty(), move |_| {
id.inspect()
})
}

fn main() {
Expand Down
10 changes: 4 additions & 6 deletions examples/files/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use floem::{
action::{open_file, save_as},
file::{FileDialogOptions, FileSpec},
keyboard::{Key, ModifiersState, NamedKey},
keyboard::{Key, Modifiers, NamedKey},
view::View,
views::{h_stack, Decorators},
widgets::button,
Expand Down Expand Up @@ -88,11 +88,9 @@ fn app_view() -> impl View {
});

let id = view.id();
view.on_key_up(
Key::Named(NamedKey::F11),
ModifiersState::empty(),
move |_| id.inspect(),
)
view.on_key_up(Key::Named(NamedKey::F11), Modifiers::empty(), move |_| {
id.inspect()
})
}

fn main() {
Expand Down
11 changes: 5 additions & 6 deletions examples/syntax-editor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use floem::cosmic_text::{Attrs, AttrsList, Stretch, Style, Weight};
use floem::keyboard::Modifiers;
use floem::peniko::Color;
use floem::reactive::RwSignal;
use floem::views::editor::core::buffer::rope_text::RopeText;
Expand All @@ -8,7 +9,7 @@ use floem::views::editor::text::{default_dark_color, Document, SimpleStylingBuil
use floem::views::editor::EditorStyle;
use floem::{
cosmic_text::FamilyOwned,
keyboard::{Key, ModifiersState, NamedKey},
keyboard::{Key, NamedKey},
view::View,
views::{
editor::{
Expand Down Expand Up @@ -243,11 +244,9 @@ mod tests {
.style(|s| s.size_full().flex_col().items_center().justify_center());

let id = view.id();
view.on_key_up(
Key::Named(NamedKey::F11),
ModifiersState::empty(),
move |_| id.inspect(),
)
view.on_key_up(Key::Named(NamedKey::F11), Modifiers::empty(), move |_| {
id.inspect()
})
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl ApplicationHandle {
}
}
WindowEvent::ModifiersChanged(modifiers) => {
window_handle.modifiers = modifiers.state();
window_handle.modifiers_changed(modifiers.state());
}
WindowEvent::Ime(ime) => {
window_handle.ime(ime);
Expand Down
3 changes: 1 addition & 2 deletions src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,8 +942,7 @@ pub fn capture(window_id: WindowId) {
.style(|s| s.width_full().height_full())
.on_event(EventListener::KeyUp, move |e| {
if let Event::KeyUp(e) = e {
if e.key.logical_key == Key::Named(NamedKey::F11)
&& e.modifiers.shift_key()
if e.key.logical_key == Key::Named(NamedKey::F11) && e.modifiers.shift()
{
id.inspect();
return EventPropagation::Stop;
Expand Down
64 changes: 63 additions & 1 deletion src/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,70 @@
use bitflags::bitflags;
pub use floem_winit::keyboard::{Key, KeyCode, ModifiersState, NamedKey, NativeKey, PhysicalKey};
pub use floem_winit::platform::modifier_supplement::KeyEventExtModifierSupplement;

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct KeyEvent {
pub key: floem_winit::event::KeyEvent,
pub modifiers: ModifiersState,
pub modifiers: Modifiers,
}

bitflags! {
/// Represents the current state of the keyboard modifiers
///
/// Each flag represents a modifier and is set if this modifier is active.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Modifiers: u32 {
/// The "shift" key.
const SHIFT = 0b100;
/// The "control" key.
const CONTROL = 0b100 << 3;
/// The "alt" key.
const ALT = 0b100 << 6;
/// This is the "windows" key on PC and "command" key on Mac.
const META = 0b100 << 9;
/// The "altgr" key.
const ALTGR = 0b100 << 12;
}
}

impl Modifiers {
/// Returns `true` if the shift key is pressed.
pub fn shift(&self) -> bool {
self.intersects(Self::SHIFT)
}
/// Returns `true` if the control key is pressed.
pub fn control(&self) -> bool {
self.intersects(Self::CONTROL)
}
/// Returns `true` if the alt key is pressed.
pub fn alt(&self) -> bool {
self.intersects(Self::ALT)
}
/// Returns `true` if the meta key is pressed.
pub fn meta(&self) -> bool {
self.intersects(Self::META)
}
/// Returns `true` if the altgr key is pressed.
pub fn altgr(&self) -> bool {
self.intersects(Self::ALTGR)
}
}

impl From<ModifiersState> for Modifiers {
fn from(value: ModifiersState) -> Self {
let mut modifiers = Modifiers::empty();
if value.shift_key() {
modifiers.set(Modifiers::SHIFT, true);
}
if value.alt_key() {
modifiers.set(Modifiers::ALT, true);
}
if value.control_key() {
modifiers.set(Modifiers::CONTROL, true);
}
if value.super_key() {
modifiers.set(Modifiers::META, true);
}
modifiers
}
}
10 changes: 6 additions & 4 deletions src/pointer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use floem_winit::{event::MouseButton, keyboard::ModifiersState};
use floem_winit::event::MouseButton;
use kurbo::{Point, Vec2};

use crate::keyboard::Modifiers;

#[derive(Debug, Clone)]
pub struct PointerWheelEvent {
pub pos: Point,
pub delta: Vec2,
pub modifiers: ModifiersState,
pub modifiers: Modifiers,
}

#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
Expand Down Expand Up @@ -57,12 +59,12 @@ impl PointerButton {
pub struct PointerInputEvent {
pub pos: Point,
pub button: PointerButton,
pub modifiers: ModifiersState,
pub modifiers: Modifiers,
pub count: u8,
}

#[derive(Debug, Clone)]
pub struct PointerMoveEvent {
pub pos: Point,
pub modifiers: ModifiersState,
pub modifiers: Modifiers,
}
7 changes: 4 additions & 3 deletions src/views/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
//! The decorator trait is the primary interface for extending the appereance and functionality of ['View']s.

use floem_reactive::{create_effect, create_updater};
use floem_winit::keyboard::{Key, ModifiersState};
use floem_winit::keyboard::Key;
use kurbo::{Point, Rect};

use crate::{
action::{set_window_menu, set_window_title, update_window_scale},
animate::Animation,
event::{Event, EventListener},
keyboard::Modifiers,
menu::Menu,
style::{Style, StyleClass, StyleSelector},
view::View,
Expand Down Expand Up @@ -108,7 +109,7 @@ pub trait Decorators: View + Sized {
fn on_key_down(
mut self,
key: Key,
modifiers: ModifiersState,
modifiers: Modifiers,
action: impl Fn(&Event) + 'static,
) -> Self {
self.view_data_mut().event_handlers.push(Box::new(move |e| {
Expand All @@ -129,7 +130,7 @@ pub trait Decorators: View + Sized {
fn on_key_up(
mut self,
key: Key,
modifiers: ModifiersState,
modifiers: Modifiers,
action: impl Fn(&Event) + 'static,
) -> Self {
self.view_data_mut().event_handlers.push(Box::new(move |e| {
Expand Down
10 changes: 5 additions & 5 deletions src/views/editor/actions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Range;

use crate::keyboard::ModifiersState;
use crate::keyboard::Modifiers;
use floem_editor_core::{
command::{EditCommand, MotionModeCommand, MultiSelectionCommand, ScrollCommand},
cursor::Cursor,
Expand All @@ -19,7 +19,7 @@ pub fn handle_command_default(
action: &dyn CommonAction,
cmd: &Command,
count: Option<usize>,
modifiers: ModifiersState,
modifiers: Modifiers,
) -> CommandExecuted {
match cmd {
Command::Edit(cmd) => handle_edit_command_default(ed, action, cmd),
Expand Down Expand Up @@ -72,14 +72,14 @@ fn handle_move_command_default(
action: &dyn CommonAction,
movement: Movement,
count: Option<usize>,
modifiers: ModifiersState,
modifiers: Modifiers,
) -> CommandExecuted {
// TODO: should we track jump locations?

ed.last_movement.set(movement.clone());

let mut cursor = ed.cursor.get_untracked();
let modify = modifiers.shift_key();
let modify = modifiers.shift();
ed.register.update(|register| {
movement::move_cursor(
ed,
Expand All @@ -101,7 +101,7 @@ fn handle_scroll_command_default(
ed: &Editor,
cmd: &ScrollCommand,
count: Option<usize>,
mods: ModifiersState,
mods: Modifiers,
) -> CommandExecuted {
match cmd {
ScrollCommand::PageUp => {
Expand Down
Loading
Loading