Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/platform/macos/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::dpi::Size;
use crate::platform::macos::view::BaseviewView;
use crate::platform::{PlatformError, PlatformHandle, WindowSharedState};
use crate::platform::{Result, WindowWaker};
use crate::platform::{PlatformResult, WindowWaker};
use crate::wrappers::appkit::{View, ViewRef};
use crate::*;
use dispatch2::MainThreadBound;
Expand Down Expand Up @@ -69,7 +69,7 @@ impl WindowContext {
view.isEqual(Some(&*first_responder))
}

pub fn focus(&self) -> Result<()> {
pub fn focus(&self) -> PlatformResult<()> {
let Some(view) = self.view.load() else { return Ok(()) };

autoreleasepool(|_| {
Expand All @@ -81,7 +81,7 @@ impl WindowContext {
Ok(())
}

pub fn resize(&self, size: Size) -> Result<()> {
pub fn resize(&self, size: Size) -> PlatformResult<()> {
if self.state.closed.get() {
return Ok(());
}
Expand All @@ -98,7 +98,7 @@ impl WindowContext {
Ok(())
}

pub fn set_mouse_cursor(&self, cursor: MouseCursor) -> Result<()> {
pub fn set_mouse_cursor(&self, cursor: MouseCursor) -> PlatformResult<()> {
let Some(view) = self.view.load() else { return Ok(()) };
let Some(view) = view.inner_ref() else { return Ok(()) };

Expand Down Expand Up @@ -137,7 +137,7 @@ impl WindowContext {
}

#[inline]
pub fn create_timer(&self, duration: Duration) -> Result<super::TimerHandle> {
pub fn create_timer(&self, duration: Duration) -> PlatformResult<super::TimerHandle> {
let Some(view_retained) = self.view.load() else {
return Err(PlatformError::InvalidWindowContext);
};
Expand Down
8 changes: 4 additions & 4 deletions src/platform/macos/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct PlatformGlContext {
impl PlatformGlContext {
pub(crate) fn create(
parent_view: &NSView, config: GlConfig, marker: MainThreadMarker,
) -> Result<PlatformGlContext> {
) -> PlatformResult<PlatformGlContext> {
let version = if config.version < (3, 2) && config.profile == Profile::Compatibility {
NSOpenGLProfileVersionLegacy
} else if config.version == (3, 2) && config.profile == Profile::Core {
Expand Down Expand Up @@ -136,14 +136,14 @@ impl PlatformGlContext {
Ok(PlatformGlContext { view, context, gl_bundle })
}

pub unsafe fn make_current(&self) -> Result<()> {
pub unsafe fn make_current(&self) -> PlatformResult<()> {
autoreleasepool(|_| {
self.context.makeCurrentContext();
});
Ok(())
}

pub unsafe fn make_not_current(&self) -> Result<()> {
pub unsafe fn make_not_current(&self) -> PlatformResult<()> {
autoreleasepool(|_| {
NSOpenGLContext::clearCurrentContext();
});
Expand Down Expand Up @@ -173,7 +173,7 @@ impl PlatformGlContext {
autoreleasepool(|_| self.gl_bundle.function_pointer_for_name(Some(&symbol_name)))
}

pub fn swap_buffers(&self) -> Result<()> {
pub fn swap_buffers(&self) -> PlatformResult<()> {
autoreleasepool(|_| {
self.context.flushBuffer();
});
Expand Down
6 changes: 2 additions & 4 deletions src/platform/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use timer::TimerHandle;
pub use waker::WindowWaker;
pub use window::*;

pub(crate) type Result<T> = std::result::Result<T, PlatformError>;
pub(crate) type PlatformResult<T> = Result<T, PlatformError>;

#[cfg(feature = "opengl")]
pub mod gl;
Expand Down Expand Up @@ -82,9 +82,7 @@ pub struct ParentWindowHandle {
}

impl ParentWindowHandle {
pub fn extract(
window: &impl HasWindowHandle,
) -> core::result::Result<Self, ParentWindowHandleError> {
pub fn extract(window: &impl HasWindowHandle) -> Result<Self, ParentWindowHandleError> {
let view = extract_raw_window_handle(window.window_handle()?)?;

let mtm = view.mtm();
Expand Down
2 changes: 1 addition & 1 deletion src/platform/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl BaseviewView {
pub fn new(
init: WindowInitializer, parenting: ViewParentingType, final_size: LogicalSize<f64>,
mtm: MainThreadMarker,
) -> Result<(Retained<View<Self>>, Rc<WindowSharedState>)> {
) -> PlatformResult<(Retained<View<Self>>, Rc<WindowSharedState>)> {
let view_rect =
NSRect::new(NSPoint::ZERO, NSSize::new(final_size.width, final_size.height));

Expand Down
35 changes: 12 additions & 23 deletions src/platform/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::cell::Cell;
use std::rc::Rc;

use crate::platform::macos::view::{BaseviewView, ViewParentingType};
use crate::platform::Result;
use crate::platform::PlatformResult;
use crate::platform::{ParentWindowHandle, WindowWaker};
use crate::utils::SizingStrategy;
use crate::wrappers::appkit::{create_window, View};
Expand All @@ -33,7 +33,7 @@ impl Drop for WindowHandle {
}

impl WindowHandle {
pub fn create_window(mut init: WindowInitializer) -> Result<Self> {
pub fn create_window(mut init: WindowInitializer) -> PlatformResult<Self> {
autoreleasepool(|_| {
let Some(mtm) = MainThreadMarker::new() else {
panic!("macOS: Windows can only be created on the main thread!")
Expand All @@ -52,7 +52,7 @@ impl WindowHandle {

fn create_window_parented(
init: WindowInitializer, parent_view: Retained<NSView>, mtm: MainThreadMarker,
) -> Result<Self> {
) -> PlatformResult<Self> {
let parenting =
ViewParentingType::Parented { parent_view: Weak::from_retained(&parent_view) };

Expand All @@ -67,7 +67,7 @@ impl WindowHandle {

pub fn create_window_standalone(
init: WindowInitializer, mtm: MainThreadMarker,
) -> Result<Self> {
) -> PlatformResult<Self> {
let window = create_window_with_options(&init.settings, mtm);
window.setAcceptsMouseMovedEvents(true);

Expand All @@ -81,7 +81,7 @@ impl WindowHandle {
Ok(Self { mtm, state, view: Weak::from_retained(&view), _window: Some(window) })
}

pub fn run_until_closed(self) -> Result<()> {
pub fn run_until_closed(self) -> PlatformResult<()> {
let Some(view) = self.view.load() else { return Ok(()) };
let Some(view) = view.inner_ref() else { return Ok(()) };

Expand All @@ -103,18 +103,6 @@ impl WindowHandle {
self.state.closed.get()
}

pub fn is_resizable(&self) -> bool {
self.state.sizing_strategy.is_resizable()
}

pub fn min_size(&self) -> Option<Size> {
self.state.sizing_strategy.min_size()
}

pub fn max_size(&self) -> Option<Size> {
self.state.sizing_strategy.max_size()
}

#[inline]
pub fn handle_main_thread_callback(&self) {
// No-op
Expand All @@ -124,7 +112,7 @@ impl WindowHandle {
WindowSize::from_logical(self.state.size.get(), self.state.scale_factor.get())
}

pub fn resize(&self, size: Size) -> Result<()> {
pub fn resize(&self, size: Size) -> PlatformResult<()> {
let Some(view) = self.view.load() else { return Ok(()) };
let Some(view) = view.inner_ref() else { return Ok(()) };

Expand All @@ -135,12 +123,12 @@ impl WindowHandle {
Ok(())
}

pub fn suggest_scale_factor(&self, _scale_factor: f64) -> Result<()> {
pub fn suggest_scale_factor(&self, _scale_factor: f64) -> PlatformResult<()> {
// This does not do anything on macOS: all coordinates are already logical
Ok(())
}

pub fn set_parent(&self, new_parent: ParentWindowHandle) -> Result<()> {
pub fn set_parent(&self, new_parent: ParentWindowHandle) -> PlatformResult<()> {
let Some(view) = self.view.load() else { return Ok(()) };
let Some(view) = view.inner_ref() else { return Ok(()) };

Expand All @@ -151,7 +139,7 @@ impl WindowHandle {
Ok(())
}

pub fn show(&self) -> Result<()> {
pub fn show(&self) -> PlatformResult<()> {
let Some(view) = self.view.load() else { return Ok(()) };
let Some(view) = view.inner_ref() else { return Ok(()) };

Expand All @@ -161,7 +149,7 @@ impl WindowHandle {
Ok(())
}

pub fn hide(&self) -> Result<()> {
pub fn hide(&self) -> PlatformResult<()> {
let Some(view) = self.view.load() else { return Ok(()) };
let Some(view) = view.inner_ref() else { return Ok(()) };

Expand All @@ -171,11 +159,12 @@ impl WindowHandle {
Ok(())
}

#[inline]
pub fn sizing_strategy(&self) -> SizingStrategy {
self.state.sizing_strategy
}

pub fn request_poll(&self) -> Result<()> {
pub fn request_poll(&self) -> PlatformResult<()> {
let Some(view) = self.view.load() else { return Ok(()) };
let Some(view) = view.inner_ref() else { return Ok(()) };

Expand Down
18 changes: 18 additions & 0 deletions src/platform/win/cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::platform::BaseviewWindow;
use crate::wrappers::win32::cursor::SystemCursor;
use crate::wrappers::win32::window::WmCursor;

impl WmCursor for BaseviewWindow {
fn on_set_cursor(&self, mouse_in_window: bool) -> bool {
if mouse_in_window {
// Here we need to set the cursor back to what the state says, since it can have changed when outside the window
if let Ok(cursor) = SystemCursor::load(self.window_state.cursor_icon.get()) {
cursor.set()
}
true
} else {
// Cursor is being changed by some other window, e.g. when having mouse on the borders to resize it
false
}
}
}
36 changes: 36 additions & 0 deletions src/platform/win/draw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::platform::win::window_state::RedrawTimer;
use crate::platform::BaseviewWindow;
use crate::wrappers::win32::window::{HWnd, WmDraw};
use crate::wrappers::win32::{TimerId, TimerType};
use crate::DamageArea;

impl WmDraw for BaseviewWindow {
fn on_timer(&self, window: HWnd, timer_id: TimerId) -> bool {
if timer_id != RedrawTimer::ID {
return false;
}

if !self.window_state.redraw_timer.is_running() {
return false;
};

window.invalidate_window();
true
}

fn on_paint(&self, window: HWnd) {
if let Some(rect) = window.get_update_rect() {
self.notify_damage(DamageArea::Rect(rect.into()));
self.handle_draw();
window.validate_rect(rect);
}
}

fn on_redraw_requested(&self) {
self.window_state.request_redraw();
}

fn on_poll_requested(&self) {
self.handle_poll();
}
}
2 changes: 1 addition & 1 deletion src/platform/win/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::HandlerError;
use dpi::PhysicalSize;
use std::fmt::Display;

pub type Result<T> = std::result::Result<T, PlatformError>;
pub type PlatformResult<T> = Result<T, PlatformError>;

#[derive(Debug)]
pub enum PlatformError {
Expand Down
30 changes: 30 additions & 0 deletions src/platform/win/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ use std::collections::{HashMap, HashSet};
use std::mem;
use std::ops::RangeInclusive;

use crate::platform::BaseviewWindow;
use crate::wrappers::win32::window::{HWnd, WmKeyboard};
use crate::{Event, WindowEvent};
use keyboard_types::{Code, Key, KeyState, KeyboardEvent, Location, Modifiers, NamedKey};
use windows_sys::Win32::{
Foundation::{HWND, LPARAM, WPARAM},
Expand Down Expand Up @@ -695,3 +698,30 @@ impl KeyboardState {
}
}
}

impl WmKeyboard for BaseviewWindow {
fn on_key_event(&self, window: HWnd, msg: u32, wparam: WPARAM, lparam: LPARAM) -> bool {
let opt_event = unsafe {
self.window_state.keyboard_state.borrow_mut().process_message(
window.as_raw(),
msg,
wparam,
lparam,
)
};

if let Some(event) = opt_event {
self.handle_event(Event::Keyboard(event));
}

msg != WM_SYSKEYDOWN
}

fn on_focus(&self) {
self.handle_event(Event::Window(WindowEvent::Focused));
}

fn on_unfocus(&self) {
self.handle_event(Event::Window(WindowEvent::Unfocused));
}
}
Loading
Loading