fix cursor visibility in OSX

This commit is contained in:
2019-03-11 18:39:11 +01:00
parent c111495cae
commit 5bef9c7286
7 changed files with 43 additions and 23 deletions

View File

@@ -45,6 +45,14 @@ std::mutex task_mutex;
{ {
[[NSApplication sharedApplication] terminate:nil]; [[NSApplication sharedApplication] terminate:nil];
} }
- (void)show_cursor:(bool)visible
{
if (cursor_visible == visible)
return;
cursor_visible = visible;
if (cursor_inside)
visible ? [NSCursor unhide] : [NSCursor hide];
}
- (std::string)pick_file:(NSArray<NSString*>*)types - (std::string)pick_file:(NSArray<NSString*>*)types
{ {
NSOpenPanel *panel = [NSOpenPanel openPanel]; NSOpenPanel *panel = [NSOpenPanel openPanel];
@@ -107,6 +115,8 @@ std::mutex task_mutex;
airdrop_service = [NSSharingService sharingServiceNamed:NSSharingServiceNameSendViaAirDrop]; airdrop_service = [NSSharingService sharingServiceNamed:NSSharingServiceNameSendViaAirDrop];
airdrop_service.delegate = self; airdrop_service.delegate = self;
gl_ready = false; gl_ready = false;
cursor_visible = CGCursorIsVisible();
cursor_inside = false;
NSOpenGLPixelFormatAttribute attrs[] = NSOpenGLPixelFormatAttribute attrs[] =
{ {
NSOpenGLPFADoubleBuffer, NSOpenGLPFADoubleBuffer,
@@ -125,7 +135,7 @@ std::mutex task_mutex;
[self setPixelFormat:pf]; [self setPixelFormat:pf];
[self setOpenGLContext:context]; [self setOpenGLContext:context];
self->trackingArea = [[NSTrackingArea alloc] initWithRect:frameRect trackingArea = [[NSTrackingArea alloc] initWithRect:frameRect
options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow ) options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow )
owner:self userInfo:nil]; owner:self userInfo:nil];
[self addTrackingArea:trackingArea]; [self addTrackingArea:trackingArea];
@@ -268,9 +278,9 @@ static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTime
} }
- (void)updateTrackingAreas - (void)updateTrackingAreas
{ {
if(self->trackingArea != nil) if(trackingArea != nil)
[self removeTrackingArea:self->trackingArea]; [self removeTrackingArea:trackingArea];
self->trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds]
options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow) options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow)
owner:self userInfo:nil]; owner:self userInfo:nil];
[self addTrackingArea:trackingArea]; [self addTrackingArea:trackingArea];
@@ -389,14 +399,15 @@ static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTime
} }
-(void)mouseExited:(NSEvent *)event -(void)mouseExited:(NSEvent *)event
{ {
self->cursor_visible = CGCursorIsVisible(); if (!cursor_visible)
if (!self->cursor_visible)
[NSCursor unhide]; [NSCursor unhide];
cursor_inside = false;
} }
-(void)mouseEntered:(NSEvent *)event -(void)mouseEntered:(NSEvent *)event
{ {
if (!self->cursor_visible) if (!cursor_visible)
[NSCursor hide]; [NSCursor hide];
cursor_inside = true;
} }
- (void)scrollWheel:(NSEvent *)theEvent - (void)scrollWheel:(NSEvent *)theEvent
{ {

View File

@@ -17,6 +17,7 @@
_CGLContextObject* cgl; _CGLContextObject* cgl;
bool gl_ready; bool gl_ready;
bool cursor_visible; bool cursor_visible;
bool cursor_inside;
NSTrackingArea* trackingArea; NSTrackingArea* trackingArea;
@public NSString* file2open; @public NSString* file2open;
} }
@@ -29,4 +30,5 @@
- (std::string)pick_dir; - (std::string)pick_dir;
- (void)share_file:(NSString*)file_path; - (void)share_file:(NSString*)file_path;
- (void)hockeyapp_crash; - (void)hockeyapp_crash;
- (void)show_cursor:(bool)visible;
@end @end

View File

@@ -43,8 +43,7 @@ void App::show_cursor()
#ifdef _WIN32 #ifdef _WIN32
win32_show_cursor(true); win32_show_cursor(true);
#elif __OSX__ #elif __OSX__
if (!CGCursorIsVisible()) [osx_view show_cursor:true];
[NSCursor unhide];
#endif #endif
} }
@@ -53,8 +52,7 @@ void App::hide_cursor()
#ifdef _WIN32 #ifdef _WIN32
win32_show_cursor(false); win32_show_cursor(false);
#elif __OSX__ #elif __OSX__
if (CGCursorIsVisible()) [osx_view show_cursor:false];
[NSCursor hide];
#endif #endif
} }

View File

@@ -115,10 +115,13 @@ std::wstring BinaryStreamReader::rwstring(size_t len)
// std::swap(ptr[i * 2], ptr[i * 2 + 1]); // std::swap(ptr[i * 2], ptr[i * 2 + 1]);
// right trim trailing zeroes // right trim trailing zeroes
auto wptr = (uint16_t*)ptr; if (len > 0)
for (int i = len - 1; i >= 0; i--) {
if (wptr[i] == 0) len--; auto wptr = (uint16_t*)ptr;
for (size_t i = len - 1; i >= 0 && len > 0; i--)
if (wptr[i] == 0) len--;
}
// wide to UTF-16le // wide to UTF-16le
std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10ffff>> converter; std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10ffff>> converter;
return converter.from_bytes(ptr, ptr + len * 2); return converter.from_bytes(ptr, ptr + len * 2);
@@ -257,7 +260,7 @@ void BinaryStreamWriter::wdbl(double v)
void BinaryStreamWriter::wstring(std::string s) void BinaryStreamWriter::wstring(std::string s)
{ {
wu32(s.size()); wu32((int)s.size());
write(s.data(), s.size()); write(s.data(), s.size());
} }
@@ -268,7 +271,7 @@ void BinaryStreamWriter::wstring_raw(std::string s)
void BinaryStreamWriter::wwstring(std::wstring s) void BinaryStreamWriter::wwstring(std::wstring s)
{ {
wu32(s.size()); wu32((int)s.size());
std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10ffff>> converter; std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10ffff>> converter;
wstring_raw(converter.to_bytes(s)); wstring_raw(converter.to_bytes(s));
} }
@@ -287,13 +290,13 @@ void BinaryStreamWriter::wpascal(std::string s)
void BinaryStreamWriter::wraw(std::vector<uint8_t> raw) void BinaryStreamWriter::wraw(std::vector<uint8_t> raw)
{ {
wu32(raw.size()); wu32((int)raw.size());
write(raw.data(), raw.size()); write(raw.data(), raw.size());
} }
void BinaryStreamWriter::wrle(std::vector<uint8_t> data) void BinaryStreamWriter::wrle(std::vector<uint8_t> data)
{ {
int idx = 0; size_t idx = 0;
auto ptr = data.data(); auto ptr = data.data();
// find the next sequence and return the position // find the next sequence and return the position

View File

@@ -104,11 +104,13 @@ kEventResult Node::on_event(Event* e)
MouseEvent e2 = *me; MouseEvent e2 = *me;
e2.m_type = kEventType::MouseUnfocus; e2.m_type = kEventType::MouseUnfocus;
child_mouse_focus->handle_event(&e2); child_mouse_focus->handle_event(&e2);
child_mouse_focus->m_mouse_focus = false;
} }
MouseEvent e2 = *me; MouseEvent e2 = *me;
e2.m_type = kEventType::MouseFocus; e2.m_type = kEventType::MouseFocus;
current_mouse_capture->handle_event(&e2); current_mouse_capture->handle_event(&e2);
child_mouse_focus = current_mouse_capture; child_mouse_focus = current_mouse_capture;
child_mouse_focus->m_mouse_focus = true;
} }
return current_mouse_capture->on_event(e); return current_mouse_capture->on_event(e);
} }
@@ -140,11 +142,13 @@ kEventResult Node::on_event(Event* e)
MouseEvent e2 = *me; MouseEvent e2 = *me;
e2.m_type = kEventType::MouseUnfocus; e2.m_type = kEventType::MouseUnfocus;
child_mouse_focus->handle_event(&e2); child_mouse_focus->handle_event(&e2);
child_mouse_focus->m_mouse_focus = false;
} }
MouseEvent e2 = *me; MouseEvent e2 = *me;
e2.m_type = kEventType::MouseFocus; e2.m_type = kEventType::MouseFocus;
(*it)->handle_event(&e2); (*it)->handle_event(&e2);
child_mouse_focus = it->get(); child_mouse_focus = it->get();
child_mouse_focus->m_mouse_focus = true;
} }
ret = kEventResult::Consumed; ret = kEventResult::Consumed;
break; break;

View File

@@ -110,6 +110,7 @@ public:
glm::mat4 m_proj; glm::mat4 m_proj;
glm::mat4 m_mvp; glm::mat4 m_mvp;
bool m_mouse_focus = false;
bool m_mouse_inside = false; bool m_mouse_inside = false;
bool m_flood_events = false; bool m_flood_events = false;
bool m_force_mouse_capture = false; bool m_force_mouse_capture = false;

View File

@@ -466,8 +466,9 @@ kEventResult NodeCanvas::handle_event(Event* e)
App::I.show_cursor(); App::I.show_cursor();
break; break;
case kEventType::MouseFocus: case kEventType::MouseFocus:
m_canvas->m_current_mode == kCanvasMode::Draw || (m_canvas->m_current_mode == kCanvasMode::Draw ||
m_canvas->m_current_mode == kCanvasMode::Erase ? m_canvas->m_current_mode == kCanvasMode::Erase) &&
!App::I.keys[(int)kKey::KeyAlt] ?
App::I.hide_cursor() : App::I.show_cursor(); App::I.hide_cursor() : App::I.show_cursor();
break; break;
case kEventType::KeyDown: case kEventType::KeyDown:
@@ -476,11 +477,11 @@ kEventResult NodeCanvas::handle_event(Event* e)
if (ke->m_key == kKey::AndroidBack) if (ke->m_key == kKey::AndroidBack)
if (!ActionManager::empty()) if (!ActionManager::empty())
ActionManager::undo(); ActionManager::undo();
if (ke->m_key == kKey::KeyAlt) if (ke->m_key == kKey::KeyAlt && m_mouse_focus)
App::I.show_cursor(); App::I.show_cursor();
break; break;
case kEventType::KeyUp: case kEventType::KeyUp:
if (ke->m_key == kKey::KeyAlt) if (ke->m_key == kKey::KeyAlt && m_mouse_focus)
m_canvas->m_current_mode == kCanvasMode::Draw || m_canvas->m_current_mode == kCanvasMode::Draw ||
m_canvas->m_current_mode == kCanvasMode::Erase ? m_canvas->m_current_mode == kCanvasMode::Erase ?
App::I.hide_cursor() : App::I.show_cursor(); App::I.hide_cursor() : App::I.show_cursor();