Rename Receiver trait to Application
This commit is contained in:
parent
81e791f06b
commit
29917b87cd
|
@ -20,7 +20,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
struct MyProgram {}
|
struct MyProgram {}
|
||||||
|
|
||||||
impl baseview::Receiver for MyProgram {
|
impl baseview::Application for MyProgram {
|
||||||
type AppMessage = ();
|
type AppMessage = ();
|
||||||
|
|
||||||
fn create_context(
|
fn create_context(
|
||||||
|
|
|
@ -33,7 +33,7 @@ pub struct WindowOpenOptions<'a> {
|
||||||
pub parent: Parent,
|
pub parent: Parent,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Receiver {
|
pub trait Application {
|
||||||
type AppMessage;
|
type AppMessage;
|
||||||
|
|
||||||
fn create_context(
|
fn create_context(
|
||||||
|
|
|
@ -6,18 +6,18 @@ use cocoa::appkit::{
|
||||||
use cocoa::base::{nil, NO};
|
use cocoa::base::{nil, NO};
|
||||||
use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString};
|
use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString};
|
||||||
|
|
||||||
use crate::{Event, MouseButtonID, MouseScroll, Receiver, WindowOpenOptions};
|
use crate::{Application, Event, MouseButtonID, MouseScroll, WindowOpenOptions};
|
||||||
|
|
||||||
pub struct Window<R: Receiver> {
|
pub struct Window<A: Application> {
|
||||||
receiver: R,
|
application: A,
|
||||||
app_message_rx: mpsc::Receiver<R::AppMessage>,
|
app_message_rx: mpsc::Receiver<A::AppMessage>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: Receiver> Window<R> {
|
impl<A: Application> Window<A> {
|
||||||
pub fn open(
|
pub fn open(
|
||||||
options: WindowOpenOptions,
|
options: WindowOpenOptions,
|
||||||
receiver: R,
|
application: A,
|
||||||
app_message_rx: mpsc::Receiver<R::AppMessage>,
|
app_message_rx: mpsc::Receiver<A::AppMessage>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
unsafe {
|
unsafe {
|
||||||
let _pool = NSAutoreleasePool::new(nil);
|
let _pool = NSAutoreleasePool::new(nil);
|
||||||
|
@ -50,7 +50,7 @@ impl<R: Receiver> Window<R> {
|
||||||
app.run();
|
app.run();
|
||||||
|
|
||||||
Window {
|
Window {
|
||||||
receiver,
|
application,
|
||||||
app_message_rx,
|
app_message_rx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ use self::winapi::um::winuser::{
|
||||||
use self::winapi::ctypes::c_void;
|
use self::winapi::ctypes::c_void;
|
||||||
use crate::Parent::WithParent;
|
use crate::Parent::WithParent;
|
||||||
use crate::{handle_message, WindowOpenOptions};
|
use crate::{handle_message, WindowOpenOptions};
|
||||||
use crate::{Event, MouseButtonID, MouseScroll, Receiver};
|
use crate::{Application, Event, MouseButtonID, MouseScroll};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
unsafe fn message_box(title: &str, msg: &str) {
|
unsafe fn message_box(title: &str, msg: &str) {
|
||||||
|
@ -113,24 +113,24 @@ unsafe fn unregister_wnd_class(wnd_class: ATOM) {
|
||||||
|
|
||||||
unsafe fn init_gl_context() {}
|
unsafe fn init_gl_context() {}
|
||||||
|
|
||||||
pub struct Window<R: Receiver> {
|
pub struct Window<A: Application> {
|
||||||
pub(crate) hwnd: HWND,
|
pub(crate) hwnd: HWND,
|
||||||
hdc: HDC,
|
hdc: HDC,
|
||||||
gl_context: HGLRC,
|
gl_context: HGLRC,
|
||||||
window_class: ATOM,
|
window_class: ATOM,
|
||||||
receiver: R,
|
application: A,
|
||||||
app_message_rx: mpsc::Receiver<R::AppMessage>,
|
app_message_rx: mpsc::Receiver<A::AppMessage>,
|
||||||
scaling: Option<f64>, // DPI scale, 96.0 is "default".
|
scaling: Option<f64>, // DPI scale, 96.0 is "default".
|
||||||
r: f32,
|
r: f32,
|
||||||
g: f32,
|
g: f32,
|
||||||
b: f32,
|
b: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: Receiver> Window<R> {
|
impl<A: Application> Window<A> {
|
||||||
pub fn open(
|
pub fn open(
|
||||||
options: WindowOpenOptions,
|
options: WindowOpenOptions,
|
||||||
receiver: R,
|
application: A,
|
||||||
app_message_rx: mpsc::Receiver<R::AppMessage>,
|
app_message_rx: mpsc::Receiver<A::AppMessage>,
|
||||||
) {
|
) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut window = Window {
|
let mut window = Window {
|
||||||
|
@ -138,7 +138,7 @@ impl<R: Receiver> Window<R> {
|
||||||
hdc: null_mut(),
|
hdc: null_mut(),
|
||||||
gl_context: null_mut(),
|
gl_context: null_mut(),
|
||||||
window_class: 0,
|
window_class: 0,
|
||||||
receiver,
|
application,
|
||||||
app_message_rx,
|
app_message_rx,
|
||||||
scaling: None,
|
scaling: None,
|
||||||
r: 0.3,
|
r: 0.3,
|
||||||
|
@ -264,7 +264,7 @@ impl<R: Receiver> Window<R> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn close(&self) {
|
pub fn close(&self) {
|
||||||
self.receiver.on_event(Event::WillClose);
|
self.application.on_event(Event::WillClose);
|
||||||
|
|
||||||
// todo: see https://github.com/wrl/rutabaga/blob/f30ff67e157375cafdbafe5fb549f1790443a3a8/src/platform/win/window.c#L402
|
// todo: see https://github.com/wrl/rutabaga/blob/f30ff67e157375cafdbafe5fb549f1790443a3a8/src/platform/win/window.c#L402
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -289,6 +289,6 @@ impl<R: Receiver> Window<R> {
|
||||||
self.r = r;
|
self.r = r;
|
||||||
self.g = g;
|
self.g = g;
|
||||||
|
|
||||||
self.receiver.on_message(Event::CursorMotion(x, y));
|
self.application.on_message(Event::CursorMotion(x, y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,22 +6,24 @@ use ::x11::xlib;
|
||||||
// use xcb::dri2; // needed later
|
// use xcb::dri2; // needed later
|
||||||
|
|
||||||
use super::XcbConnection;
|
use super::XcbConnection;
|
||||||
use crate::{Event, MouseButtonID, MouseScroll, Parent, Receiver, WindowInfo, WindowOpenOptions};
|
use crate::{
|
||||||
|
Application, Event, MouseButtonID, MouseScroll, Parent, WindowInfo, WindowOpenOptions,
|
||||||
|
};
|
||||||
|
|
||||||
use raw_window_handle::RawWindowHandle;
|
use raw_window_handle::RawWindowHandle;
|
||||||
|
|
||||||
pub struct Window<R: Receiver> {
|
pub struct Window<A: Application> {
|
||||||
scaling: Option<f64>, // DPI scale, 96.0 is "default".
|
scaling: Option<f64>, // DPI scale, 96.0 is "default".
|
||||||
xcb_connection: XcbConnection,
|
xcb_connection: XcbConnection,
|
||||||
receiver: R,
|
application: A,
|
||||||
app_message_rx: mpsc::Receiver<R::AppMessage>,
|
app_message_rx: mpsc::Receiver<A::AppMessage>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: Receiver> Window<R> {
|
impl<A: Application> Window<A> {
|
||||||
pub fn open(
|
pub fn open(
|
||||||
options: WindowOpenOptions,
|
options: WindowOpenOptions,
|
||||||
receiver: R,
|
application: A,
|
||||||
app_message_rx: mpsc::Receiver<R::AppMessage>,
|
app_message_rx: mpsc::Receiver<A::AppMessage>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
// Convert the parent to a X11 window ID if we're given one
|
// Convert the parent to a X11 window ID if we're given one
|
||||||
let parent = match options.parent {
|
let parent = match options.parent {
|
||||||
|
@ -134,7 +136,7 @@ impl<R: Receiver> Window<R> {
|
||||||
let mut x11_window = Self {
|
let mut x11_window = Self {
|
||||||
scaling: None,
|
scaling: None,
|
||||||
xcb_connection,
|
xcb_connection,
|
||||||
receiver,
|
application,
|
||||||
app_message_rx,
|
app_message_rx,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -148,7 +150,9 @@ impl<R: Receiver> Window<R> {
|
||||||
dpi: x11_window.scaling,
|
dpi: x11_window.scaling,
|
||||||
};
|
};
|
||||||
|
|
||||||
x11_window.receiver.create_context(raw_handle, &window_info);
|
x11_window
|
||||||
|
.application
|
||||||
|
.create_context(raw_handle, &window_info);
|
||||||
|
|
||||||
x11_window.run_event_loop();
|
x11_window.run_event_loop();
|
||||||
|
|
||||||
|
@ -187,14 +191,14 @@ impl<R: Receiver> Window<R> {
|
||||||
|
|
||||||
match event_type {
|
match event_type {
|
||||||
xcb::EXPOSE => {
|
xcb::EXPOSE => {
|
||||||
self.receiver.on_event(Event::RenderExpose);
|
self.application.on_event(Event::RenderExpose);
|
||||||
}
|
}
|
||||||
xcb::MOTION_NOTIFY => {
|
xcb::MOTION_NOTIFY => {
|
||||||
let event = unsafe { xcb::cast_event::<xcb::MotionNotifyEvent>(&event) };
|
let event = unsafe { xcb::cast_event::<xcb::MotionNotifyEvent>(&event) };
|
||||||
let detail = event.detail();
|
let detail = event.detail();
|
||||||
|
|
||||||
if detail != 4 && detail != 5 {
|
if detail != 4 && detail != 5 {
|
||||||
self.receiver.on_event(Event::CursorMotion(
|
self.application.on_event(Event::CursorMotion(
|
||||||
event.event_x() as i32,
|
event.event_x() as i32,
|
||||||
event.event_y() as i32,
|
event.event_y() as i32,
|
||||||
));
|
));
|
||||||
|
@ -206,20 +210,20 @@ impl<R: Receiver> Window<R> {
|
||||||
|
|
||||||
match detail {
|
match detail {
|
||||||
4 => {
|
4 => {
|
||||||
self.receiver.on_event(Event::MouseScroll(MouseScroll {
|
self.application.on_event(Event::MouseScroll(MouseScroll {
|
||||||
x_delta: 0.0,
|
x_delta: 0.0,
|
||||||
y_delta: 1.0,
|
y_delta: 1.0,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
5 => {
|
5 => {
|
||||||
self.receiver.on_event(Event::MouseScroll(MouseScroll {
|
self.application.on_event(Event::MouseScroll(MouseScroll {
|
||||||
x_delta: 0.0,
|
x_delta: 0.0,
|
||||||
y_delta: -1.0,
|
y_delta: -1.0,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
detail => {
|
detail => {
|
||||||
let button_id = mouse_id(detail);
|
let button_id = mouse_id(detail);
|
||||||
self.receiver.on_event(Event::MouseDown(button_id));
|
self.application.on_event(Event::MouseDown(button_id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -229,20 +233,20 @@ impl<R: Receiver> Window<R> {
|
||||||
|
|
||||||
if detail != 4 && detail != 5 {
|
if detail != 4 && detail != 5 {
|
||||||
let button_id = mouse_id(detail);
|
let button_id = mouse_id(detail);
|
||||||
self.receiver.on_event(Event::MouseUp(button_id));
|
self.application.on_event(Event::MouseUp(button_id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
xcb::KEY_PRESS => {
|
xcb::KEY_PRESS => {
|
||||||
let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) };
|
let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) };
|
||||||
let detail = event.detail();
|
let detail = event.detail();
|
||||||
|
|
||||||
self.receiver.on_event(Event::KeyDown(detail));
|
self.application.on_event(Event::KeyDown(detail));
|
||||||
}
|
}
|
||||||
xcb::KEY_RELEASE => {
|
xcb::KEY_RELEASE => {
|
||||||
let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) };
|
let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) };
|
||||||
let detail = event.detail();
|
let detail = event.detail();
|
||||||
|
|
||||||
self.receiver.on_event(Event::KeyUp(detail));
|
self.application.on_event(Event::KeyUp(detail));
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("Unhandled event type: {:?}", event_type);
|
println!("Unhandled event type: {:?}", event_type);
|
||||||
|
|
Loading…
Reference in a new issue