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