Skip to content

Commit

Permalink
Eliminate PointerState struct
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Sep 20, 2024
1 parent de0f45c commit f86b561
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 34 deletions.
10 changes: 5 additions & 5 deletions parley/src/editor/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use std::ops::{Deref, DerefMut, Range};
use crate::{style::StyleProperty, FontContext, LayoutContext};
use kurbo::Point;
use winit::{
event::Ime,
event::{Ime, Modifiers},
keyboard::{Key, NamedKey},
};

use super::masonry_types::{Handled, PointerButton, PointerState, TextEvent};
use super::masonry_types::{Handled, PointerButton, TextEvent};

use super::{
offset_for_delete_backwards,
Expand Down Expand Up @@ -89,13 +89,13 @@ impl<T: EditableText> TextEditor<T> {

pub fn pointer_down(
&mut self,
origin: Point,
state: &PointerState,
position: Point,
mods: Modifiers,
button: PointerButton,
) -> bool {
// TODO: If we have a selection and we're hovering over it,
// implement (optional?) click and drag
self.inner.pointer_down(origin, state, button)
self.inner.pointer_down(position, mods, button)
}

pub fn text_event(&mut self, event: &TextEvent) -> Handled {
Expand Down
14 changes: 1 addition & 13 deletions parley/src/editor/masonry_types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use dpi::{LogicalPosition, PhysicalPosition};
use std::collections::HashSet;
use winit::{
event::{Ime, KeyEvent, Modifiers},
event::{Ime, KeyEvent},
keyboard::ModifiersState,
};

Expand Down Expand Up @@ -36,16 +34,6 @@ pub enum PointerButton {
Other,
}

#[derive(Debug, Clone)]
pub struct PointerState {
pub physical_position: PhysicalPosition<f64>,
pub position: LogicalPosition<f64>,
pub buttons: HashSet<PointerButton>,
pub mods: Modifiers,
pub count: u8,
pub focus: bool,
}

/// An enum for specifying whether an event was handled.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Handled {
Expand Down
3 changes: 2 additions & 1 deletion parley/src/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ pub use edit::{EditableText, TextEditor};
mod backspace;
pub use backspace::offset_for_delete_backwards;

mod masonry_types;
pub mod masonry_types;
pub use masonry_types::*;
24 changes: 9 additions & 15 deletions parley/src/editor/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ use crate::{style::StyleProperty, FontContext, LayoutContext};
use kurbo::{Line, Point};
use peniko::Color;
use unicode_segmentation::{GraphemeCursor, UnicodeSegmentation};
use winit::event::Modifiers;
use winit::keyboard::NamedKey;

use super::masonry_types::{Handled, PointerButton, PointerState, TextEvent};
use super::masonry_types::{Handled, PointerButton, TextEvent};

use super::{TextBrush, TextLayout, TextStorage};

Expand Down Expand Up @@ -63,21 +64,17 @@ impl<T: Selectable> TextWithSelection<T> {

pub fn pointer_down(
&mut self,
origin: Point,
state: &PointerState,
position: Point,
mods: Modifiers,
button: PointerButton,
) -> bool {
// TODO: work out which button is the primary button?
if button == PointerButton::Primary {
self.selecting_with_mouse = true;
self.needs_selection_update = true;
// TODO: Much of this juggling seems unnecessary
let position = Point::new(state.position.x, state.position.y) - origin;
let position = self
.layout
.cursor_for_point(Point::new(position.x, position.y));
let position = self.layout.cursor_for_point(position);
tracing::warn!("Got cursor point without getting affinity");
if state.mods.state().shift_key() {
if mods.state().shift_key() {
if let Some(selection) = self.selection.as_mut() {
selection.active = position.insert_point;
selection.active_affinity = Affinity::Downstream;
Expand All @@ -94,19 +91,16 @@ impl<T: Selectable> TextWithSelection<T> {
}
}

pub fn pointer_up(&mut self, _origin: Point, _state: &PointerState, button: PointerButton) {
pub fn pointer_up(&mut self, _position: Point, _mods: Modifiers, button: PointerButton) {
if button == PointerButton::Primary {
self.selecting_with_mouse = false;
}
}

pub fn pointer_move(&mut self, origin: Point, state: &PointerState) -> bool {
pub fn pointer_move(&mut self, position: Point, _mods: Modifiers) -> bool {
if self.selecting_with_mouse {
self.needs_selection_update = true;
let position = Point::new(state.position.x, state.position.y) - origin;
let position = self
.layout
.cursor_for_point(Point::new(position.x, position.y));
let position = self.layout.cursor_for_point(position);
tracing::warn!("Got cursor point without getting affinity");
if let Some(selection) = self.selection.as_mut() {
selection.active = position.insert_point;
Expand Down

0 comments on commit f86b561

Please sign in to comment.