introduce WindowContext for manipulating the window in a callback

This commit is contained in:
Wez Furlong committed 2019-09-15 19:09:08 -07:00
1 parent e6265c93a4
commit dff8e1e652
4 files changed
+90 -20

No files matched your search

+21 -5
View File
@@ -3,6 +3,7 @@ use failure::Fallible;
struct MyWindow {
allow_close: bool,
cursor_pos: (u16, u16),
}
impl Drop for MyWindow {
@@ -40,7 +41,16 @@ impl WindowCallbacks for MyWindow {
0,
200,
120,
Color::rgb(0xff, 0xff, 0xff),
Color::rgb(0xff, 0x80, 0xff),
Operator::Over,
);
context.draw_line(
0,
0,
self.cursor_pos.0 as isize,
self.cursor_pos.1 as isize,
Color::rgb(0xff, 0xff, 0x80),
Operator::Over,
);
}
@@ -49,14 +59,17 @@ impl WindowCallbacks for MyWindow {
eprintln!("resize {:?}", dims);
}
fn key_event(&mut self, key: &KeyEvent) -> bool {
fn key_event(&mut self, key: &KeyEvent, ctx: &mut WindowContext) -> bool {
eprintln!("{:?}", key);
ctx.set_cursor(Some(MouseCursor::Text));
false
}
fn mouse_event(&mut self, event: &MouseEvent) -> Option<MouseCursor> {
fn mouse_event(&mut self, event: &MouseEvent, ctx: &mut WindowContext) {
eprintln!("{:?}", event);
Some(MouseCursor::Text)
self.cursor_pos = (event.x, event.y);
ctx.invalidate();
ctx.set_cursor(Some(MouseCursor::Arrow));
}
}
@@ -68,7 +81,10 @@ fn main() -> Fallible<()> {
"the title",
800,
600,
Box::new(MyWindow { allow_close: false }),
Box::new(MyWindow {
allow_close: false,
cursor_pos: (100, 200),
}),
)?;
win.show();
+26 -3
View File
@@ -123,11 +123,34 @@ pub trait WindowCallbacks {
/// If your window didn't handle the event, you must return false.
/// This is particularly important for eg: ALT keys on windows,
/// otherwise standard key assignments may not function in your window.
fn key_event(&mut self, key: &KeyEvent) -> bool {
fn key_event(&mut self, key: &KeyEvent, context: &mut WindowContext) -> bool {
false
}
fn mouse_event(&mut self, event: &MouseEvent) -> Option<MouseCursor> {
Some(MouseCursor::Arrow)
fn mouse_event(&mut self, event: &MouseEvent, context: &mut WindowContext) {
context.set_cursor(Some(MouseCursor::Arrow));
}
}
/// The WindowContext is used to buffer up requests to operate on the window
/// that might arise during processing of events within that window.
/// The requests will be dispatched shortly after returning from the
/// WindowCallbacks function(s).
#[derive(Default)]
pub struct WindowContext {
cursor: Option<Option<MouseCursor>>,
invalidate: bool,
}
impl WindowContext {
/// Change the cursor
pub fn set_cursor(&mut self, cursor: Option<MouseCursor>) {
self.cursor = Some(cursor);
}
/// Invalidate the window so that the entire client area will
/// be repainted shortly
pub fn invalidate(&mut self) {
self.invalidate = true;
}
}
+26 -7
View File
@@ -4,7 +4,7 @@ use crate::bitmaps::*;
use crate::color::Color;
use crate::{
Dimensions, KeyCode, KeyEvent, Modifiers, MouseButtons, MouseCursor, MouseEvent,
MouseEventKind, MousePress, Operator, PaintContext, WindowCallbacks,
MouseEventKind, MousePress, Operator, PaintContext, WindowCallbacks, WindowContext,
};
use failure::Fallible;
use std::io::Error as IoError;
@@ -417,8 +417,16 @@ fn apply_mouse_cursor(cursor: Option<MouseCursor>) {
}
}
fn do_mouse_event(inner: &mut WindowInner, event: &MouseEvent) {
apply_mouse_cursor(inner.callbacks.mouse_event(event))
fn apply_context(inner: &mut WindowInner, mut context: WindowContext) {
if let Some(cursor) = context.cursor.take() {
apply_mouse_cursor(cursor);
}
if context.invalidate {
unsafe {
InvalidateRect(inner.hwnd, null(), 1);
}
}
}
unsafe fn mouse_button(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option<LRESULT> {
@@ -443,8 +451,10 @@ unsafe fn mouse_button(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) ->
mouse_buttons,
modifiers,
};
let mut ctx = WindowContext::default();
let mut inner = inner.lock().unwrap();
do_mouse_event(&mut inner, &event);
inner.callbacks.mouse_event(&event, &mut ctx);
apply_context(&mut inner, ctx);
Some(0)
} else {
None
@@ -462,8 +472,11 @@ unsafe fn mouse_move(hwnd: HWND, _msg: UINT, wparam: WPARAM, lparam: LPARAM) ->
mouse_buttons,
modifiers,
};
let mut ctx = WindowContext::default();
let mut inner = inner.lock().unwrap();
do_mouse_event(&mut inner, &event);
inner.callbacks.mouse_event(&event, &mut ctx);
apply_context(&mut inner, ctx);
Some(0)
} else {
None
@@ -486,8 +499,10 @@ unsafe fn mouse_wheel(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) ->
mouse_buttons,
modifiers,
};
let mut ctx = WindowContext::default();
let mut inner = inner.lock().unwrap();
do_mouse_event(&mut inner, &event);
inner.callbacks.mouse_event(&event, &mut ctx);
apply_context(&mut inner, ctx);
Some(0)
} else {
None
@@ -652,7 +667,11 @@ unsafe fn key(hwnd: HWND, _msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option<
repeat_count: repeat,
key_is_down: !releasing,
};
if inner.callbacks.key_event(&key) {
let mut ctx = WindowContext::default();
let handled = inner.callbacks.key_event(&key, &mut ctx);
apply_context(&mut inner, ctx);
if handled {
return Some(0);
}
}
+17 -5
View File
@@ -2,7 +2,7 @@ use super::*;
use crate::bitmaps::*;
use crate::{
Color, Dimensions, KeyEvent, MouseButtons, MouseCursor, MouseEvent, MouseEventKind, MousePress,
Operator, PaintContext, WindowCallbacks,
Operator, PaintContext, WindowCallbacks, WindowContext,
};
use failure::Fallible;
use std::collections::VecDeque;
@@ -243,9 +243,19 @@ impl WindowInner {
self.expose.push_back(expose);
}
fn apply_context(&mut self, mut ctx: WindowContext) {
if let Some(cursor) = ctx.cursor.take() {
self.set_cursor(cursor);
}
if ctx.invalidate {
self.paint_all = true;
}
}
fn do_mouse_event(&mut self, event: &MouseEvent) -> Fallible<()> {
let cursor = self.callbacks.mouse_event(&event);
self.set_cursor(cursor)?;
let mut ctx = WindowContext::default();
self.callbacks.mouse_event(&event, &mut ctx);
self.apply_context(ctx);
Ok(())
}
@@ -256,7 +266,7 @@ impl WindowInner {
let id_no = match cursor.unwrap_or(MouseCursor::Arrow) {
// `/usr/include/X11/cursorfont.h`
MouseCursor::Arrow => 2,
MouseCursor::Arrow => 132,
MouseCursor::Hand => 58,
MouseCursor::Text => 152,
};
@@ -314,7 +324,9 @@ impl WindowInner {
repeat_count: 1,
key_is_down: r == xcb::KEY_PRESS,
};
self.callbacks.key_event(&key);
let mut ctx = WindowContext::default();
self.callbacks.key_event(&key, &mut ctx);
self.apply_context(ctx);
}
}