[osx, surface] use autoresizeMask to resize surfaces to the parent window's client area

This commit is contained in:
Martin Fouilleul 2023-07-25 17:02:11 +02:00
parent a4ae1cf214
commit e7cce5e36f
3 changed files with 50 additions and 51 deletions

View File

@ -1159,6 +1159,8 @@ void mg_mtl_canvas_resize(mg_mtl_canvas_backend* backend, vec2 size)
backend->outTexture = [backend->surface->device newTextureWithDescriptor:texDesc];
backend->surface->mtlLayer.drawableSize = (CGSize){size.x, size.y};
backend->frameSize = size;
}
}
@ -1179,13 +1181,13 @@ void mg_mtl_canvas_render(mg_canvas_backend* interface,
//NOTE: ensure screen tiles buffer is correct size
mg_mtl_surface* surface = backend->surface;
mp_rect frame = surface->interface.getFrame((mg_surface_data*)surface);
vec2 frameSize = surface->interface.getSize((mg_surface_data*)surface);
f32 scale = surface->mtlLayer.contentsScale;
vec2 viewportSize = {frame.w * scale, frame.h * scale};
vec2 viewportSize = {frameSize.x * scale, frameSize.y * scale};
int tileSize = MG_MTL_TILE_SIZE;
int nTilesX = (int)(frame.w * scale + tileSize - 1)/tileSize;
int nTilesY = (int)(frame.h * scale + tileSize - 1)/tileSize;
int nTilesX = (int)(viewportSize.x * scale + tileSize - 1)/tileSize;
int nTilesY = (int)(viewportSize.y * scale + tileSize - 1)/tileSize;
if(viewportSize.x != backend->frameSize.x || viewportSize.y != backend->frameSize.y)
{
@ -1482,10 +1484,10 @@ mg_canvas_backend* mtl_canvas_backend_create(mg_mtl_surface* surface)
backend->blitPipeline = [surface->device newRenderPipelineStateWithDescriptor: pipelineStateDescriptor error:&err];
//NOTE: create textures
mp_rect frame = surface->interface.getFrame((mg_surface_data*)surface);
vec2 size = surface->interface.getSize((mg_surface_data*)surface);
f32 scale = surface->mtlLayer.contentsScale;
backend->frameSize = (vec2){frame.w*scale, frame.h*scale};
backend->frameSize = (vec2){size.x*scale, size.y*scale};
MTLTextureDescriptor* texDesc = [[MTLTextureDescriptor alloc] init];
texDesc.textureType = MTLTextureType2D;
@ -1540,8 +1542,8 @@ mg_canvas_backend* mtl_canvas_backend_create(mg_mtl_surface* surface)
options: bufferOptions];
int tileSize = MG_MTL_TILE_SIZE;
int nTilesX = (int)(frame.w * scale + tileSize - 1)/tileSize;
int nTilesY = (int)(frame.h * scale + tileSize - 1)/tileSize;
int nTilesX = (int)(backend->frameSize.x + tileSize - 1)/tileSize;
int nTilesY = (int)(backend->frameSize.y + tileSize - 1)/tileSize;
backend->screenTilesBuffer = [surface->device newBufferWithLength: nTilesX*nTilesY*sizeof(mg_mtl_screen_tile)
options: bufferOptions];

View File

@ -116,6 +116,7 @@ void mg_mtl_surface_swap_interval(mg_surface_data* interface, int swap)
}
}
/*
void mg_mtl_surface_set_frame(mg_surface_data* interface, mp_rect frame)
{
mg_mtl_surface* surface = (mg_mtl_surface*)interface;
@ -135,6 +136,7 @@ void mg_mtl_surface_set_frame(mg_surface_data* interface, mp_rect frame)
CGSize drawableSize = (CGSize){.width = frame.w * scale.x, .height = frame.h * scale.y};
surface->mtlLayer.drawableSize = drawableSize;
}
*/
//TODO fix that according to real scaling, depending on the monitor settings
@ -162,8 +164,6 @@ mg_surface_data* mg_mtl_surface_create_for_window(mp_window window)
surface->interface.present = mg_mtl_surface_present;
surface->interface.swapInterval = mg_mtl_surface_swap_interval;
surface->interface.setFrame = mg_mtl_surface_set_frame;
@autoreleasepool
{
//-----------------------------------------------------------
@ -203,7 +203,7 @@ mg_surface_data* mg_mtl_surface_create_for_window(mp_window window)
[surface->mtlLayer setOpaque:NO];
surface->mtlLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
[surface->interface.layer.caLayer addSublayer: (CALayer*)surface->mtlLayer];
//-----------------------------------------------------------

View File

@ -663,11 +663,18 @@ void mp_install_keyboard_layout_listener()
mp_event event = {};
event.window = mp_window_handle_from_ptr(mpWindow);
event.type = MP_EVENT_WINDOW_MOVE;
event.frame.rect.x = contentRect.origin.x;
event.frame.rect.y = contentRect.origin.y;
event.frame.rect.w = contentRect.size.width;
event.frame.rect.h = contentRect.size.height;
///////////////////////////////////////////////////
//TODO make it relative to top-left corner!
///////////////////////////////////////////////////
event.move.contents.x = contentRect.origin.x;
event.move.contents.y = contentRect.origin.y;
event.move.contents.w = contentRect.size.width;
event.move.contents.h = contentRect.size.height;
///////////////////////////////////////////////////
//TODO: add window frame!
///////////////////////////////////////////////////
mp_queue_event(&event);
}
@ -680,10 +687,14 @@ void mp_install_keyboard_layout_listener()
mp_event event = {};
event.window = mp_window_handle_from_ptr(mpWindow);
event.type = MP_EVENT_WINDOW_RESIZE;
event.frame.rect.x = contentRect.origin.x;
event.frame.rect.y = contentRect.origin.y;
event.frame.rect.w = contentRect.size.width;
event.frame.rect.h = contentRect.size.height;
///////////////////////////////////////////////////
//TODO make it relative to top-left corner!
///////////////////////////////////////////////////
event.move.contents.x = contentRect.origin.x;
event.move.contents.y = contentRect.origin.y;
event.move.contents.w = contentRect.size.width;
event.move.contents.h = contentRect.size.height;
if(__mpApp.liveResizeCallback)
{
@ -869,11 +880,11 @@ static void mp_process_mouse_button(NSEvent* nsEvent, mp_window_data* window, mp
mp_event event = {};
event.type = MP_EVENT_MOUSE_MOVE;
event.window = mp_window_handle_from_ptr(window);
event.move.x = p.x;
event.move.y = frame.size.height - p.y;
event.move.deltaX = [nsEvent deltaX];
event.move.deltaY = [nsEvent deltaY];
event.move.mods = mp_convert_osx_mods([nsEvent modifierFlags]);
event.mouse.x = p.x;
event.mouse.y = frame.size.height - p.y;
event.mouse.deltaX = [nsEvent deltaX];
event.mouse.deltaY = [nsEvent deltaY];
event.mouse.mods = mp_convert_osx_mods([nsEvent modifierFlags]);
mp_queue_event(&event);
}
@ -885,11 +896,11 @@ static void mp_process_mouse_button(NSEvent* nsEvent, mp_window_data* window, mp
event.type = MP_EVENT_MOUSE_WHEEL;
double factor = [nsEvent hasPreciseScrollingDeltas] ? 0.1 : 1.0;
event.move.x = 0;
event.move.y = 0;
event.move.deltaX = -[nsEvent scrollingDeltaX]*factor;
event.move.deltaY = -[nsEvent scrollingDeltaY]*factor;
event.move.mods = mp_convert_osx_mods([nsEvent modifierFlags]);
event.mouse.x = 0;
event.mouse.y = 0;
event.mouse.deltaX = -[nsEvent scrollingDeltaX]*factor;
event.mouse.deltaY = -[nsEvent scrollingDeltaY]*factor;
event.mouse.mods = mp_convert_osx_mods([nsEvent modifierFlags]);
mp_queue_event(&event);
}
@ -1678,26 +1689,13 @@ vec2 mg_osx_surface_contents_scaling(mg_surface_data* surface)
return(res);
}}
mp_rect mg_osx_surface_get_frame(mg_surface_data* surface)
vec2 mg_osx_surface_get_size(mg_surface_data* surface)
{@autoreleasepool{
CGRect frame = surface->layer.caLayer.frame;
mp_rect res = {frame.origin.x,
frame.origin.y,
frame.size.width,
frame.size.height};
CGRect bounds = surface->layer.caLayer.bounds;
vec2 res = {bounds.size.width, bounds.size.height};
return(res);
}}
void mg_osx_surface_set_frame(mg_surface_data* surface, mp_rect frame)
{@autoreleasepool{
CGRect cgFrame = {{frame.x, frame.y}, {frame.w, frame.h}};
[CATransaction begin];
[CATransaction setDisableActions:YES];
[surface->layer.caLayer setFrame: cgFrame];
[CATransaction commit];
}}
bool mg_osx_surface_get_hidden(mg_surface_data* surface)
{@autoreleasepool{
return([surface->layer.caLayer isHidden]);
@ -1721,8 +1719,7 @@ void mg_surface_init_for_window(mg_surface_data* surface, mp_window_data* window
surface->nativeLayer = mg_osx_surface_native_layer;
surface->contentsScaling = mg_osx_surface_contents_scaling;
surface->getFrame = mg_osx_surface_get_frame;
surface->setFrame = mg_osx_surface_set_frame;
surface->getSize = mg_osx_surface_get_size;
surface->getHidden = mg_osx_surface_get_hidden;
surface->setHidden = mg_osx_surface_set_hidden;
@ -1734,6 +1731,8 @@ void mg_surface_init_for_window(mg_surface_data* surface, mp_window_data* window
surface->layer.caLayer.frame = (CGRect){{0, 0}, size};
surface->layer.caLayer.contentsScale = window->osx.nsView.layer.contentsScale;
surface->layer.caLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
[window->osx.nsView.layer addSublayer: surface->layer.caLayer];
}}
@ -1758,8 +1757,7 @@ void mg_surface_init_remote(mg_surface_data* surface, u32 width, u32 height)
surface->nativeLayer = mg_osx_surface_native_layer;
surface->contentsScaling = mg_osx_surface_contents_scaling;
surface->getFrame = mg_osx_surface_get_frame;
surface->setFrame = mg_osx_surface_set_frame;
surface->getSize = mg_osx_surface_get_size;
surface->getHidden = mg_osx_surface_get_hidden;
surface->setHidden = mg_osx_surface_set_hidden;
surface->remoteID = mg_osx_surface_remote_id;
@ -1786,8 +1784,7 @@ void mg_surface_init_host(mg_surface_data* surface, mp_window_data* window)
surface->api = MG_HOST;
surface->nativeLayer = mg_osx_surface_native_layer;
surface->contentsScaling = mg_osx_surface_contents_scaling;
surface->getFrame = mg_osx_surface_get_frame;
surface->setFrame = mg_osx_surface_set_frame;
surface->getSize = mg_osx_surface_get_size;
surface->getHidden = mg_osx_surface_get_hidden;
surface->setHidden = mg_osx_surface_set_hidden;
surface->hostConnect = mg_osx_surface_host_connect;