Skip to content

Commit

Permalink
Rename internal structs for consistency (rust-windowing#2149)
Browse files Browse the repository at this point in the history
Proxy -> EventLoopProxy
Id -> WindowId or DeviceId
WindowTarget -> EventLoopWindowTarget
Handle -> MonitorHandle
Mode -> VideoMode
PlatformSpecificBuilderAttributes -> PlatformSpecificWindowBuilderAttributes
SuperWindowId -> RootWindowId
  • Loading branch information
madsmtm authored Mar 18, 2022
1 parent 85baf79 commit a438091
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 135 deletions.
18 changes: 9 additions & 9 deletions src/platform_impl/macos/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ impl<T> EventLoop<T> {
exit_code
}

pub fn create_proxy(&self) -> Proxy<T> {
Proxy::new(self.window_target.p.sender.clone())
pub fn create_proxy(&self) -> EventLoopProxy<T> {
EventLoopProxy::new(self.window_target.p.sender.clone())
}
}

Expand Down Expand Up @@ -281,28 +281,28 @@ pub fn stop_app_on_panic<F: FnOnce() -> R + UnwindSafe, R>(
}
}

pub struct Proxy<T> {
pub struct EventLoopProxy<T> {
sender: mpsc::Sender<T>,
source: CFRunLoopSourceRef,
}

unsafe impl<T: Send> Send for Proxy<T> {}
unsafe impl<T: Send> Send for EventLoopProxy<T> {}

impl<T> Drop for Proxy<T> {
impl<T> Drop for EventLoopProxy<T> {
fn drop(&mut self) {
unsafe {
CFRelease(self.source as _);
}
}
}

impl<T> Clone for Proxy<T> {
impl<T> Clone for EventLoopProxy<T> {
fn clone(&self) -> Self {
Proxy::new(self.sender.clone())
EventLoopProxy::new(self.sender.clone())
}
}

impl<T> Proxy<T> {
impl<T> EventLoopProxy<T> {
fn new(sender: mpsc::Sender<T>) -> Self {
unsafe {
// just wake up the eventloop
Expand All @@ -318,7 +318,7 @@ impl<T> Proxy<T> {
CFRunLoopAddSource(rl, source, kCFRunLoopCommonModes);
CFRunLoopWakeUp(rl);

Proxy { sender, source }
EventLoopProxy { sender, source }
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/platform_impl/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ use std::{fmt, ops::Deref, sync::Arc};
pub(crate) use self::{
app_delegate::get_aux_state_mut,
event_loop::{
EventLoop, EventLoopWindowTarget, PlatformSpecificEventLoopAttributes,
Proxy as EventLoopProxy,
EventLoop, EventLoopProxy, EventLoopWindowTarget, PlatformSpecificEventLoopAttributes,
},
monitor::{MonitorHandle, VideoMode},
window::{Id as WindowId, PlatformSpecificWindowBuilderAttributes, UnownedWindow},
window::{PlatformSpecificWindowBuilderAttributes, UnownedWindow, WindowId},
};
use crate::{
error::OsError as RootOsError, event::DeviceId as RootDeviceId, window::WindowAttributes,
Expand Down
12 changes: 6 additions & 6 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ use objc::{
};

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id(pub usize);
pub struct WindowId(pub usize);

impl Id {
impl WindowId {
pub const unsafe fn dummy() -> Self {
Id(0)
Self(0)
}
}

// Convert the `cocoa::base::id` associated with a window to a usize to use as a unique identifier
// for the window.
pub fn get_window_id(window_cocoa_id: id) -> Id {
Id(window_cocoa_id as *const Object as _)
pub fn get_window_id(window_cocoa_id: id) -> WindowId {
WindowId(window_cocoa_id as *const Object as _)
}

#[derive(Clone)]
Expand Down Expand Up @@ -485,7 +485,7 @@ impl UnownedWindow {
unsafe { util::set_style_mask_sync(*self.ns_window, *self.ns_view, mask) };
}

pub fn id(&self) -> Id {
pub fn id(&self) -> WindowId {
get_window_id(*self.ns_window)
}

Expand Down
6 changes: 3 additions & 3 deletions src/platform_impl/web/device.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id(pub i32);
pub struct DeviceId(pub i32);

impl Id {
impl DeviceId {
pub const unsafe fn dummy() -> Self {
Id(0)
Self(0)
}
}
20 changes: 10 additions & 10 deletions src/platform_impl/web/event_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ mod runner;
mod state;
mod window_target;

pub use self::proxy::Proxy;
pub use self::window_target::WindowTarget;
pub use self::proxy::EventLoopProxy;
pub use self::window_target::EventLoopWindowTarget;

use super::{backend, device, window};
use crate::event::Event;
use crate::event_loop as root;
use crate::event_loop::{ControlFlow, EventLoopWindowTarget as RootEventLoopWindowTarget};

use std::marker::PhantomData;

pub struct EventLoop<T: 'static> {
elw: root::EventLoopWindowTarget<T>,
elw: RootEventLoopWindowTarget<T>,
}

#[derive(Default, Debug, Copy, Clone, PartialEq, Hash)]
Expand All @@ -22,18 +22,18 @@ pub(crate) struct PlatformSpecificEventLoopAttributes {}
impl<T> EventLoop<T> {
pub(crate) fn new(_: &PlatformSpecificEventLoopAttributes) -> Self {
EventLoop {
elw: root::EventLoopWindowTarget {
p: WindowTarget::new(),
elw: RootEventLoopWindowTarget {
p: EventLoopWindowTarget::new(),
_marker: PhantomData,
},
}
}

pub fn run<F>(self, mut event_handler: F) -> !
where
F: 'static + FnMut(Event<'_, T>, &root::EventLoopWindowTarget<T>, &mut root::ControlFlow),
F: 'static + FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),
{
let target = root::EventLoopWindowTarget {
let target = RootEventLoopWindowTarget {
p: self.elw.p.clone(),
_marker: PhantomData,
};
Expand All @@ -51,11 +51,11 @@ impl<T> EventLoop<T> {
unreachable!();
}

pub fn create_proxy(&self) -> Proxy<T> {
pub fn create_proxy(&self) -> EventLoopProxy<T> {
self.elw.p.proxy()
}

pub fn window_target(&self) -> &root::EventLoopWindowTarget<T> {
pub fn window_target(&self) -> &RootEventLoopWindowTarget<T> {
&self.elw
}
}
10 changes: 5 additions & 5 deletions src/platform_impl/web/event_loop/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use super::runner;
use crate::event::Event;
use crate::event_loop::EventLoopClosed;

pub struct Proxy<T: 'static> {
pub struct EventLoopProxy<T: 'static> {
runner: runner::Shared<T>,
}

impl<T: 'static> Proxy<T> {
impl<T: 'static> EventLoopProxy<T> {
pub fn new(runner: runner::Shared<T>) -> Self {
Proxy { runner }
Self { runner }
}

pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
Expand All @@ -17,9 +17,9 @@ impl<T: 'static> Proxy<T> {
}
}

impl<T: 'static> Clone for Proxy<T> {
impl<T: 'static> Clone for EventLoopProxy<T> {
fn clone(&self) -> Self {
Proxy {
Self {
runner: self.runner.clone(),
}
}
Expand Down
49 changes: 23 additions & 26 deletions src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{super::ScaleChangeArgs, backend, state::State};
use crate::event::{Event, StartCause};
use crate::event_loop as root;
use crate::event_loop::ControlFlow;
use crate::window::WindowId;

use instant::{Duration, Instant};
Expand Down Expand Up @@ -54,11 +54,11 @@ impl<T: 'static> RunnerEnum<T> {

struct Runner<T: 'static> {
state: State,
event_handler: Box<dyn FnMut(Event<'_, T>, &mut root::ControlFlow)>,
event_handler: Box<dyn FnMut(Event<'_, T>, &mut ControlFlow)>,
}

impl<T: 'static> Runner<T> {
pub fn new(event_handler: Box<dyn FnMut(Event<'_, T>, &mut root::ControlFlow)>) -> Self {
pub fn new(event_handler: Box<dyn FnMut(Event<'_, T>, &mut ControlFlow)>) -> Self {
Runner {
state: State::Init,
event_handler,
Expand All @@ -83,14 +83,14 @@ impl<T: 'static> Runner<T> {
})
}

fn handle_single_event(&mut self, event: Event<'_, T>, control: &mut root::ControlFlow) {
let is_closed = matches!(*control, root::ControlFlow::ExitWithCode(_));
fn handle_single_event(&mut self, event: Event<'_, T>, control: &mut ControlFlow) {
let is_closed = matches!(*control, ControlFlow::ExitWithCode(_));

(self.event_handler)(event, control);

// Maintain closed state, even if the callback changes it
if is_closed {
*control = root::ControlFlow::Exit;
*control = ControlFlow::Exit;
}
}
}
Expand Down Expand Up @@ -123,10 +123,7 @@ impl<T: 'static> Shared<T> {
// Set the event callback to use for the event loop runner
// This the event callback is a fairly thin layer over the user-provided callback that closes
// over a RootEventLoopWindowTarget reference
pub fn set_listener(
&self,
event_handler: Box<dyn FnMut(Event<'_, T>, &mut root::ControlFlow)>,
) {
pub fn set_listener(&self, event_handler: Box<dyn FnMut(Event<'_, T>, &mut ControlFlow)>) {
{
let mut runner = self.0.runner.borrow_mut();
assert!(matches!(*runner, RunnerEnum::Pending));
Expand Down Expand Up @@ -245,7 +242,7 @@ impl<T: 'static> Shared<T> {
// Process the destroy-pending windows. This should only be called from
// `run_until_cleared` and `handle_scale_changed`, somewhere between emitting
// `NewEvents` and `MainEventsCleared`.
fn process_destroy_pending_windows(&self, control: &mut root::ControlFlow) {
fn process_destroy_pending_windows(&self, control: &mut ControlFlow) {
while let Some(id) = self.0.destroy_pending.borrow_mut().pop_front() {
self.0
.all_canvases
Expand Down Expand Up @@ -369,7 +366,7 @@ impl<T: 'static> Shared<T> {
}

fn handle_unload(&self) {
self.apply_control_flow(root::ControlFlow::Exit);
self.apply_control_flow(ControlFlow::Exit);
let mut control = self.current_control_flow();
// We don't call `handle_loop_destroyed` here because we don't need to
// perform cleanup when the web browser is going to destroy the page.
Expand All @@ -379,9 +376,9 @@ impl<T: 'static> Shared<T> {
// handle_single_event_sync takes in an event and handles it synchronously.
//
// It should only ever be called from `scale_changed`.
fn handle_single_event_sync(&self, event: Event<'_, T>, control: &mut root::ControlFlow) {
fn handle_single_event_sync(&self, event: Event<'_, T>, control: &mut ControlFlow) {
if self.is_closed() {
*control = root::ControlFlow::Exit;
*control = ControlFlow::Exit;
}
match *self.0.runner.borrow_mut() {
RunnerEnum::Running(ref mut runner) => {
Expand All @@ -394,9 +391,9 @@ impl<T: 'static> Shared<T> {
// handle_event takes in events and either queues them or applies a callback
//
// It should only ever be called from `run_until_cleared` and `scale_changed`.
fn handle_event(&self, event: Event<'static, T>, control: &mut root::ControlFlow) {
fn handle_event(&self, event: Event<'static, T>, control: &mut ControlFlow) {
if self.is_closed() {
*control = root::ControlFlow::Exit;
*control = ControlFlow::Exit;
}
match *self.0.runner.borrow_mut() {
RunnerEnum::Running(ref mut runner) => {
Expand All @@ -409,7 +406,7 @@ impl<T: 'static> Shared<T> {
RunnerEnum::Destroyed => return,
}

let is_closed = matches!(*control, root::ControlFlow::ExitWithCode(_));
let is_closed = matches!(*control, ControlFlow::ExitWithCode(_));

// Don't take events out of the queue if the loop is closed or the runner doesn't exist
// If the runner doesn't exist and this method recurses, it will recurse infinitely
Expand All @@ -425,18 +422,18 @@ impl<T: 'static> Shared<T> {

// Apply the new ControlFlow that has been selected by the user
// Start any necessary timeouts etc
fn apply_control_flow(&self, control_flow: root::ControlFlow) {
fn apply_control_flow(&self, control_flow: ControlFlow) {
let new_state = match control_flow {
root::ControlFlow::Poll => {
ControlFlow::Poll => {
let cloned = self.clone();
State::Poll {
request: backend::AnimationFrameRequest::new(move || cloned.poll()),
}
}
root::ControlFlow::Wait => State::Wait {
ControlFlow::Wait => State::Wait {
start: Instant::now(),
},
root::ControlFlow::WaitUntil(end) => {
ControlFlow::WaitUntil(end) => {
let start = Instant::now();

let delay = if end <= start {
Expand All @@ -456,7 +453,7 @@ impl<T: 'static> Shared<T> {
),
}
}
root::ControlFlow::ExitWithCode(_) => State::Exit,
ControlFlow::ExitWithCode(_) => State::Exit,
};

match *self.0.runner.borrow_mut() {
Expand All @@ -467,7 +464,7 @@ impl<T: 'static> Shared<T> {
}
}

fn handle_loop_destroyed(&self, control: &mut root::ControlFlow) {
fn handle_loop_destroyed(&self, control: &mut ControlFlow) {
self.handle_event(Event::LoopDestroyed, control);
let all_canvases = std::mem::take(&mut *self.0.all_canvases.borrow_mut());
*self.0.scale_change_detector.borrow_mut() = None;
Expand Down Expand Up @@ -510,11 +507,11 @@ impl<T: 'static> Shared<T> {
}

// Get the current control flow state
fn current_control_flow(&self) -> root::ControlFlow {
fn current_control_flow(&self) -> ControlFlow {
match *self.0.runner.borrow() {
RunnerEnum::Running(ref runner) => runner.state.control_flow(),
RunnerEnum::Pending => root::ControlFlow::Poll,
RunnerEnum::Destroyed => root::ControlFlow::Exit,
RunnerEnum::Pending => ControlFlow::Poll,
RunnerEnum::Destroyed => ControlFlow::Exit,
}
}
}
Loading

0 comments on commit a438091

Please sign in to comment.