Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 157418d

Browse files
committedDec 19, 2018
Moved out cursor loading
1 parent b492564 commit 157418d

File tree

2 files changed

+85
-81
lines changed

2 files changed

+85
-81
lines changed
 

‎src/platform_impl/macos/util.rs

+82-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use core_graphics::display::CGDisplay;
1212
use objc::runtime::{BOOL, Class, Object, Sel, YES};
1313

1414
pub use util::*;
15-
use dpi::LogicalSize;
15+
use {dpi::LogicalSize, window::MouseCursor};
1616
use platform_impl::platform::{dispatch::*, ffi, window::SharedState};
1717

1818
pub const EMPTY_RANGE: ffi::NSRange = ffi::NSRange {
@@ -402,6 +402,87 @@ pub unsafe fn create_input_context(view: id) -> IdRef {
402402
IdRef::new(input_context)
403403
}
404404

405+
pub enum CursorType {
406+
Native(&'static str),
407+
Undocumented(&'static str),
408+
WebKit(&'static str),
409+
}
410+
411+
impl From<MouseCursor> for CursorType {
412+
fn from(cursor: MouseCursor) -> Self {
413+
match cursor {
414+
MouseCursor::Arrow | MouseCursor::Default => CursorType::Native("arrowCursor"),
415+
MouseCursor::Hand => CursorType::Native("pointingHandCursor"),
416+
MouseCursor::Grabbing | MouseCursor::Grab => CursorType::Native("closedHandCursor"),
417+
MouseCursor::Text => CursorType::Native("IBeamCursor"),
418+
MouseCursor::VerticalText => CursorType::Native("IBeamCursorForVerticalLayout"),
419+
MouseCursor::Copy => CursorType::Native("dragCopyCursor"),
420+
MouseCursor::Alias => CursorType::Native("dragLinkCursor"),
421+
MouseCursor::NotAllowed | MouseCursor::NoDrop => CursorType::Native("operationNotAllowedCursor"),
422+
MouseCursor::ContextMenu => CursorType::Native("contextualMenuCursor"),
423+
MouseCursor::Crosshair => CursorType::Native("crosshairCursor"),
424+
MouseCursor::EResize => CursorType::Native("resizeRightCursor"),
425+
MouseCursor::NResize => CursorType::Native("resizeUpCursor"),
426+
MouseCursor::WResize => CursorType::Native("resizeLeftCursor"),
427+
MouseCursor::SResize => CursorType::Native("resizeDownCursor"),
428+
MouseCursor::EwResize | MouseCursor::ColResize => CursorType::Native("resizeLeftRightCursor"),
429+
MouseCursor::NsResize | MouseCursor::RowResize => CursorType::Native("resizeUpDownCursor"),
430+
431+
// Undocumented cursors: https://stackoverflow.com/a/46635398/5435443
432+
MouseCursor::Help => CursorType::Undocumented("_helpCursor"),
433+
MouseCursor::ZoomIn => CursorType::Undocumented("_zoomInCursor"),
434+
MouseCursor::ZoomOut => CursorType::Undocumented("_zoomOutCursor"),
435+
MouseCursor::NeResize => CursorType::Undocumented("_windowResizeNorthEastCursor"),
436+
MouseCursor::NwResize => CursorType::Undocumented("_windowResizeNorthWestCursor"),
437+
MouseCursor::SeResize => CursorType::Undocumented("_windowResizeSouthEastCursor"),
438+
MouseCursor::SwResize => CursorType::Undocumented("_windowResizeSouthWestCursor"),
439+
MouseCursor::NeswResize => CursorType::Undocumented("_windowResizeNorthEastSouthWestCursor"),
440+
MouseCursor::NwseResize => CursorType::Undocumented("_windowResizeNorthWestSouthEastCursor"),
441+
442+
// While these are available, the former just loads a white arrow,
443+
// and the latter loads an ugly deflated beachball!
444+
// MouseCursor::Move => CursorType::Undocumented("_moveCursor"),
445+
// MouseCursor::Wait => CursorType::Undocumented("_waitCursor"),
446+
447+
// An even more undocumented cursor...
448+
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=522349
449+
// This is the wrong semantics for `Wait`, but it's the same as
450+
// what's used in Safari and Chrome.
451+
MouseCursor::Wait | MouseCursor::Progress => CursorType::Undocumented("busyButClickableCursor"),
452+
453+
// For the rest, we can just snatch the cursors from WebKit...
454+
// They fit the style of the native cursors, and will seem
455+
// completely standard to macOS users.
456+
// https://stackoverflow.com/a/21786835/5435443
457+
MouseCursor::Move | MouseCursor::AllScroll => CursorType::WebKit("move"),
458+
MouseCursor::Cell => CursorType::WebKit("cell"),
459+
}
460+
}
461+
}
462+
463+
impl CursorType {
464+
pub unsafe fn load(self) -> id {
465+
match self {
466+
CursorType::Native(cursor_name) => {
467+
let sel = Sel::register(cursor_name);
468+
msg_send![class!(NSCursor), performSelector:sel]
469+
},
470+
CursorType::Undocumented(cursor_name) => {
471+
let class = class!(NSCursor);
472+
let sel = Sel::register(cursor_name);
473+
let sel = if msg_send![class, respondsToSelector:sel] {
474+
sel
475+
} else {
476+
warn!("Cursor `{}` appears to be invalid", cursor_name);
477+
sel!(arrowCursor)
478+
};
479+
msg_send![class, performSelector:sel]
480+
},
481+
CursorType::WebKit(cursor_name) => load_webkit_cursor(cursor_name),
482+
}
483+
}
484+
}
485+
405486
// Note that loading `busybutclickable` with this code won't animate the frames;
406487
// instead you'll just get them all in a column.
407488
pub unsafe fn load_webkit_cursor(cursor_name: &str) -> id {

‎src/platform_impl/macos/window.rs

+3-80
Original file line numberDiff line numberDiff line change
@@ -477,86 +477,9 @@ impl UnownedWindow {
477477
}
478478

479479
pub fn set_cursor(&self, cursor: MouseCursor) {
480-
enum CursorType {
481-
Native(&'static str),
482-
Undocumented(&'static str),
483-
WebKit(&'static str),
484-
}
485-
486-
let cursor_name = match cursor {
487-
MouseCursor::Arrow | MouseCursor::Default => CursorType::Native("arrowCursor"),
488-
MouseCursor::Hand => CursorType::Native("pointingHandCursor"),
489-
MouseCursor::Grabbing | MouseCursor::Grab => CursorType::Native("closedHandCursor"),
490-
MouseCursor::Text => CursorType::Native("IBeamCursor"),
491-
MouseCursor::VerticalText => CursorType::Native("IBeamCursorForVerticalLayout"),
492-
MouseCursor::Copy => CursorType::Native("dragCopyCursor"),
493-
MouseCursor::Alias => CursorType::Native("dragLinkCursor"),
494-
MouseCursor::NotAllowed | MouseCursor::NoDrop => CursorType::Native("operationNotAllowedCursor"),
495-
MouseCursor::ContextMenu => CursorType::Native("contextualMenuCursor"),
496-
MouseCursor::Crosshair => CursorType::Native("crosshairCursor"),
497-
MouseCursor::EResize => CursorType::Native("resizeRightCursor"),
498-
MouseCursor::NResize => CursorType::Native("resizeUpCursor"),
499-
MouseCursor::WResize => CursorType::Native("resizeLeftCursor"),
500-
MouseCursor::SResize => CursorType::Native("resizeDownCursor"),
501-
MouseCursor::EwResize | MouseCursor::ColResize => CursorType::Native("resizeLeftRightCursor"),
502-
MouseCursor::NsResize | MouseCursor::RowResize => CursorType::Native("resizeUpDownCursor"),
503-
504-
// Undocumented cursors: https://stackoverflow.com/a/46635398/5435443
505-
MouseCursor::Help => CursorType::Undocumented("_helpCursor"),
506-
MouseCursor::ZoomIn => CursorType::Undocumented("_zoomInCursor"),
507-
MouseCursor::ZoomOut => CursorType::Undocumented("_zoomOutCursor"),
508-
MouseCursor::NeResize => CursorType::Undocumented("_windowResizeNorthEastCursor"),
509-
MouseCursor::NwResize => CursorType::Undocumented("_windowResizeNorthWestCursor"),
510-
MouseCursor::SeResize => CursorType::Undocumented("_windowResizeSouthEastCursor"),
511-
MouseCursor::SwResize => CursorType::Undocumented("_windowResizeSouthWestCursor"),
512-
MouseCursor::NeswResize => CursorType::Undocumented("_windowResizeNorthEastSouthWestCursor"),
513-
MouseCursor::NwseResize => CursorType::Undocumented("_windowResizeNorthWestSouthEastCursor"),
514-
515-
// While these are available, the former just loads a white arrow,
516-
// and the latter loads an ugly deflated beachball!
517-
// MouseCursor::Move => CursorType::Undocumented("_moveCursor"),
518-
// MouseCursor::Wait => CursorType::Undocumented("_waitCursor"),
519-
520-
// An even more undocumented cursor...
521-
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=522349
522-
// This is the wrong semantics for `Wait`, but it's the same as
523-
// what's used in Safari and Chrome.
524-
MouseCursor::Wait | MouseCursor::Progress => CursorType::Undocumented("busyButClickableCursor"),
525-
526-
// For the rest, we can just snatch the cursors from WebKit...
527-
// They fit the style of the native cursors, and will seem
528-
// completely standard to macOS users.
529-
// https://stackoverflow.com/a/21786835/5435443
530-
MouseCursor::Move | MouseCursor::AllScroll => CursorType::WebKit("move"),
531-
MouseCursor::Cell => CursorType::WebKit("cell"),
532-
};
533-
534-
let class = class!(NSCursor);
535-
match cursor_name {
536-
CursorType::Native(cursor_name) => {
537-
let sel = Sel::register(cursor_name);
538-
unsafe {
539-
let cursor: id = msg_send![class, performSelector:sel];
540-
let _: () = msg_send![cursor, set];
541-
}
542-
},
543-
CursorType::Undocumented(cursor_name) => {
544-
let sel = Sel::register(cursor_name);
545-
unsafe {
546-
let sel = if msg_send![class, respondsToSelector:sel] {
547-
sel
548-
} else {
549-
warn!("Cursor `{}` appears to be invalid", cursor_name);
550-
sel!(arrowCursor)
551-
};
552-
let cursor: id = msg_send![class, performSelector:sel];
553-
let _: () = msg_send![cursor, set];
554-
}
555-
},
556-
CursorType::WebKit(cursor_name) => unsafe {
557-
let cursor = util::load_webkit_cursor(cursor_name);
558-
let _: () = msg_send![cursor, set];
559-
},
480+
unsafe {
481+
let cursor = util::CursorType::from(cursor).load();
482+
let _: () = msg_send![cursor, set];
560483
}
561484
}
562485

0 commit comments

Comments
 (0)
Please sign in to comment.