changed to using Point<I>
instead of tuples or slices
This commit is contained in:
parent
db17c9dbfe
commit
a85d8d0df5
|
@ -128,11 +128,49 @@ impl<Window> KeyEvent<Window> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
|
||||
pub struct Point<I>
|
||||
where
|
||||
I: Copy + Clone + PartialEq + PartialOrd,
|
||||
{
|
||||
pub x: I,
|
||||
pub y: I,
|
||||
}
|
||||
impl<I> From<(I, I)> for Point<I>
|
||||
where
|
||||
I: Copy + Clone + PartialEq + PartialOrd,
|
||||
{
|
||||
fn from(value: (I, I)) -> Self {
|
||||
Self::from_tuple(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Point<I>
|
||||
where
|
||||
I: Copy + Clone + PartialEq + PartialOrd,
|
||||
{
|
||||
pub fn new(x: I, y: I) -> Self {
|
||||
Self { x, y }
|
||||
}
|
||||
|
||||
pub fn from_tuple(tuple: (I, I)) -> Self {
|
||||
Self {
|
||||
x: tuple.0,
|
||||
y: tuple.1,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_tuple(&self) -> (I, I) {
|
||||
(self.x, self.y)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ButtonEvent<Window> {
|
||||
pub window: Window,
|
||||
pub state: KeyState,
|
||||
pub keycode: MouseButton,
|
||||
pub cursor_position: Point<i32>,
|
||||
pub modifierstate: ModifierState,
|
||||
}
|
||||
|
||||
|
@ -141,12 +179,14 @@ impl<Window> ButtonEvent<Window> {
|
|||
window: Window,
|
||||
state: KeyState,
|
||||
keycode: MouseButton,
|
||||
cursor_position: Point<i32>,
|
||||
modifierstate: ModifierState,
|
||||
) -> Self {
|
||||
Self {
|
||||
window,
|
||||
state,
|
||||
keycode,
|
||||
cursor_position,
|
||||
modifierstate,
|
||||
}
|
||||
}
|
||||
|
@ -154,12 +194,12 @@ impl<Window> ButtonEvent<Window> {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct MotionEvent<Window> {
|
||||
pub position: [i32; 2],
|
||||
pub position: Point<i32>,
|
||||
pub window: Window,
|
||||
}
|
||||
|
||||
impl<Window> MotionEvent<Window> {
|
||||
pub fn new(position: [i32; 2], window: Window) -> Self {
|
||||
pub fn new(position: Point<i32>, window: Window) -> Self {
|
||||
Self { position, window }
|
||||
}
|
||||
}
|
||||
|
@ -193,12 +233,12 @@ impl<Window> DestroyEvent<Window> {
|
|||
#[derive(Debug)]
|
||||
pub struct CreateEvent<Window> {
|
||||
pub window: Window,
|
||||
pub position: [i32; 2],
|
||||
pub size: [i32; 2],
|
||||
pub position: Point<i32>,
|
||||
pub size: Point<i32>,
|
||||
}
|
||||
|
||||
impl<Window> CreateEvent<Window> {
|
||||
pub fn new(window: Window, position: [i32; 2], size: [i32; 2]) -> Self {
|
||||
pub fn new(window: Window, position: Point<i32>, size: Point<i32>) -> Self {
|
||||
Self {
|
||||
window,
|
||||
position,
|
||||
|
@ -210,12 +250,12 @@ impl<Window> CreateEvent<Window> {
|
|||
#[derive(Debug)]
|
||||
pub struct ConfigureEvent<Window> {
|
||||
pub window: Window,
|
||||
pub position: [i32; 2],
|
||||
pub size: [i32; 2],
|
||||
pub position: Point<i32>,
|
||||
pub size: Point<i32>,
|
||||
}
|
||||
|
||||
impl<Window> ConfigureEvent<Window> {
|
||||
pub fn new(window: Window, position: [i32; 2], size: [i32; 2]) -> Self {
|
||||
pub fn new(window: Window, position: Point<i32>, size: Point<i32>) -> Self {
|
||||
Self {
|
||||
window,
|
||||
position,
|
||||
|
|
|
@ -303,8 +303,8 @@ impl TryFrom<XEvent> for XLibWindowEvent {
|
|||
let ev = unsafe { &event.configure_request };
|
||||
Ok(Self::ConfigureEvent(ConfigureEvent {
|
||||
window: ev.window,
|
||||
position: [ev.x, ev.y],
|
||||
size: [ev.width, ev.height],
|
||||
position: (ev.x, ev.y).into(),
|
||||
size: (ev.width, ev.height).into(),
|
||||
}))
|
||||
}
|
||||
xlib::EnterNotify => {
|
||||
|
@ -315,6 +315,8 @@ impl TryFrom<XEvent> for XLibWindowEvent {
|
|||
let ev = unsafe { &event.destroy_window };
|
||||
Ok(Self::DestroyEvent(DestroyEvent { window: ev.window }))
|
||||
}
|
||||
// both ButtonPress and ButtonRelease use the XButtonEvent structure, aliased as either
|
||||
// XButtonReleasedEvent or XButtonPressedEvent
|
||||
xlib::ButtonPress | xlib::ButtonRelease => {
|
||||
let ev = unsafe { &event.button };
|
||||
let keycode = xev_to_mouse_button(ev).unwrap();
|
||||
|
@ -327,9 +329,10 @@ impl TryFrom<XEvent> for XLibWindowEvent {
|
|||
let modifierstate = ModifierState::empty();
|
||||
|
||||
Ok(Self::ButtonEvent(ButtonEvent::new(
|
||||
ev.window,
|
||||
ev.subwindow,
|
||||
state,
|
||||
keycode,
|
||||
(ev.x, ev.y).into(),
|
||||
modifierstate,
|
||||
)))
|
||||
}
|
||||
|
|
91
src/state.rs
91
src/state.rs
|
@ -2,10 +2,7 @@ use std::rc::Rc;
|
|||
|
||||
use log::{error, info};
|
||||
|
||||
use x11::xlib::{
|
||||
self, Window, XButtonPressedEvent, XButtonReleasedEvent, XEvent, XKeyEvent,
|
||||
XMotionEvent,
|
||||
};
|
||||
use x11::xlib::{self, Window, XEvent, XKeyEvent, XMotionEvent};
|
||||
use xlib::{
|
||||
XConfigureRequestEvent, XCrossingEvent, XDestroyWindowEvent,
|
||||
XMapRequestEvent, XUnmapEvent,
|
||||
|
@ -15,7 +12,8 @@ use crate::{
|
|||
backends::{
|
||||
keycodes::{MouseButton, VirtualKeyCode},
|
||||
window_event::{
|
||||
ButtonEvent, KeyBind, KeyEvent, ModifierKey, ModifierState,
|
||||
ButtonEvent, KeyBind, KeyEvent, KeyState, ModifierKey,
|
||||
ModifierState,
|
||||
},
|
||||
xlib::XLib,
|
||||
WindowServerBackend,
|
||||
|
@ -661,18 +659,18 @@ where
|
|||
}
|
||||
|
||||
/// ensure event.subwindow refers to a valid client.
|
||||
fn start_move_resize_window(&mut self, event: &XButtonPressedEvent) {
|
||||
let window = event.subwindow;
|
||||
fn start_move_resize_window(&mut self, event: &ButtonEvent<B::Window>) {
|
||||
let window = event.window; // xev.subwindow
|
||||
|
||||
match event.button {
|
||||
1 => {
|
||||
match event.keycode {
|
||||
MouseButton::Left => {
|
||||
if self.clients.set_floating(&window) {
|
||||
self.arrange_clients();
|
||||
}
|
||||
|
||||
self.move_resize_window = MoveResizeInfo::Move(MoveInfoInner {
|
||||
window,
|
||||
starting_cursor_pos: (event.x, event.y),
|
||||
starting_cursor_pos: event.cursor_position.as_tuple(),
|
||||
starting_window_pos: self
|
||||
.clients
|
||||
.get(&window)
|
||||
|
@ -680,7 +678,7 @@ where
|
|||
.position,
|
||||
});
|
||||
}
|
||||
3 => {
|
||||
MouseButton::Right => {
|
||||
if self.clients.set_floating(&window) {
|
||||
self.arrange_clients();
|
||||
}
|
||||
|
@ -709,13 +707,17 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn end_move_resize_window(&mut self, event: &XButtonReleasedEvent) {
|
||||
if event.button == 1 || event.button == 3 {
|
||||
self.move_resize_window = MoveResizeInfo::None;
|
||||
}
|
||||
if event.button == 3 {
|
||||
// TODO fix backend cursor api
|
||||
//self.xlib.release_cursor();
|
||||
fn end_move_resize_window(&mut self, event: &ButtonEvent<B::Window>) {
|
||||
match event.keycode {
|
||||
MouseButton::Left => {
|
||||
self.move_resize_window = MoveResizeInfo::None;
|
||||
}
|
||||
MouseButton::Right => {
|
||||
self.move_resize_window = MoveResizeInfo::None;
|
||||
// TODO fix backend cursor api
|
||||
//self.xlib.release_cursor();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -759,36 +761,39 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn button_press(&mut self, event: &ButtonEvent<B::Window>) {
|
||||
self.focus_client(&event.window, true);
|
||||
fn button_event(&mut self, event: &ButtonEvent<B::Window>) {
|
||||
match event.state {
|
||||
KeyState::Pressed => {
|
||||
self.focus_client(&event.window, true);
|
||||
|
||||
match event.keycode {
|
||||
MouseButton::Left | MouseButton::Right => {
|
||||
match self.move_resize_window {
|
||||
MoveResizeInfo::None
|
||||
if ModifierState::from([self.config.mod_key])
|
||||
.eq_ignore_lock(&event.modifierstate)
|
||||
&& self.clients.contains(&event.window) =>
|
||||
{
|
||||
//self.start_move_resize_window(event)
|
||||
match event.keycode {
|
||||
MouseButton::Left | MouseButton::Right => {
|
||||
match self.move_resize_window {
|
||||
MoveResizeInfo::None
|
||||
if ModifierState::from([self
|
||||
.config
|
||||
.mod_key])
|
||||
.eq_ignore_lock(&event.modifierstate)
|
||||
&& self.clients.contains(&event.window) =>
|
||||
{
|
||||
self.start_move_resize_window(event)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
MouseButton::Middle => {
|
||||
self.clients.toggle_floating(&event.window);
|
||||
self.arrange_clients();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
MouseButton::Middle => {
|
||||
self.clients.toggle_floating(&event.window);
|
||||
self.arrange_clients();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn button_release(&mut self, event: &XButtonReleasedEvent) {
|
||||
match self.move_resize_window {
|
||||
MoveResizeInfo::None => {}
|
||||
_ => {
|
||||
self.end_move_resize_window(event);
|
||||
}
|
||||
KeyState::Released => match self.move_resize_window {
|
||||
MoveResizeInfo::None => {}
|
||||
_ => {
|
||||
self.end_move_resize_window(event);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue