99using System . Runtime . ExceptionServices ;
1010using System . Runtime . InteropServices ;
1111using System . Threading ;
12- using System . Threading . Tasks ;
1312using OpenTK . Core ;
1413using OpenTK . Mathematics ;
1514using OpenTK . Windowing . Common ;
1615using OpenTK . Windowing . Common . Input ;
1716using OpenTK . Windowing . GraphicsLibraryFramework ;
1817
18+ using Monitor = OpenTK . Windowing . GraphicsLibraryFramework . Monitor ;
19+
1920namespace OpenTK . Windowing . Desktop
2021{
2122 /// <summary>
@@ -275,31 +276,19 @@ public string Title
275276 /// </summary>
276277 public IGLFWGraphicsContext Context { get ; }
277278
278- private MonitorHandle _currentMonitor ;
279-
280279 /// <summary>
281- /// Gets or sets the current <see cref="MonitorHandle"/>.
280+ /// Gets the current <see cref="MonitorInfo"/> of the monitor that the window is currently on.
281+ /// To make the window fullscreen use <see cref="MakeFullscreen(MonitorHandle, int?, int?, int?)"/> or <see cref="WindowState"/>.
282282 /// </summary>
283- public unsafe MonitorHandle CurrentMonitor
283+ /// <seealso cref="MakeFullscreen(MonitorHandle, int?, int?, int?)"/>
284+ /// <seealso cref="WindowState"/>
285+ public unsafe MonitorInfo CurrentMonitor
284286 {
285- get => _currentMonitor ;
287+ get => Monitors . GetMonitorFromWindow ( WindowPtr ) ;
286288
289+ [ Obsolete ( "Use MakeFullscreen to set the current monitor. Setting this property does nothing anymore." ) ]
287290 set
288291 {
289- GraphicsLibraryFramework . Monitor * monitor = value . ToUnsafePtr < GraphicsLibraryFramework . Monitor > ( ) ;
290- VideoMode * mode = GLFW . GetVideoMode ( monitor ) ;
291- Vector2i location = ClientLocation ;
292- Vector2i size = ClientSize ;
293- GLFW . SetWindowMonitor (
294- WindowPtr ,
295- monitor ,
296- location . X ,
297- location . Y ,
298- size . X ,
299- size . Y ,
300- mode ->RefreshRate ) ;
301-
302- _currentMonitor = value ;
303292 }
304293 }
305294
@@ -373,13 +362,13 @@ public unsafe WindowState WindowState
373362 // Set the new window state before any potential callback is called,
374363 // so that the new state is available in for example OnResize.
375364 // - Noggin_bops 2023-09-25
376- var previousWindowState = _windowState ;
365+ WindowState previousWindowState = _windowState ;
377366 _windowState = value ;
378367
379368 if ( previousWindowState == WindowState . Fullscreen && value != WindowState . Fullscreen )
380369 {
381370 // We are going from fullscreen to something else.
382- GLFW . SetWindowMonitor ( WindowPtr , null , _cachedWindowLocation . X , _cachedWindowLocation . Y , _cachedWindowClientSize . X , _cachedWindowClientSize . Y , 0 ) ;
371+ GLFW . SetWindowMonitor ( WindowPtr , null , _cachedWindowLocation . X , _cachedWindowLocation . Y , _cachedWindowClientSize . X , _cachedWindowClientSize . Y , GLFW . DontCare ) ;
383372 }
384373
385374 switch ( value )
@@ -397,10 +386,13 @@ public unsafe WindowState WindowState
397386 break ;
398387
399388 case WindowState . Fullscreen :
400- _cachedWindowClientSize = ClientSize ;
401- _cachedWindowLocation = ClientLocation ;
402- var monitor = CurrentMonitor . ToUnsafePtr < GraphicsLibraryFramework . Monitor > ( ) ;
403- var modePtr = GLFW . GetVideoMode ( monitor ) ;
389+ if ( previousWindowState != WindowState . Fullscreen )
390+ {
391+ _cachedWindowClientSize = ClientSize ;
392+ _cachedWindowLocation = ClientLocation ;
393+ }
394+ Monitor * monitor = CurrentMonitor . Handle . ToUnsafePtr < Monitor > ( ) ;
395+ VideoMode * modePtr = GLFW . GetVideoMode ( monitor ) ;
404396 GLFW . SetWindowMonitor ( WindowPtr , monitor , 0 , 0 , modePtr ->Width , modePtr ->Height , modePtr ->RefreshRate ) ;
405397 break ;
406398 }
@@ -418,7 +410,7 @@ public unsafe WindowBorder WindowBorder
418410
419411 set
420412 {
421- GLFW . GetVersion ( out var major , out var minor , out _ ) ;
413+ GLFW . GetVersion ( out int major , out int minor , out _ ) ;
422414
423415 // It isn't possible to implement this in versions of GLFW older than 3.3,
424416 // as SetWindowAttrib didn't exist before then.
@@ -685,7 +677,7 @@ public MouseCursor Cursor
685677
686678 unsafe
687679 {
688- var oldCursor = _glfwCursor ;
680+ Cursor * oldCursor = _glfwCursor ;
689681 _glfwCursor = null ;
690682
691683 // Create the new GLFW cursor
@@ -694,7 +686,7 @@ public MouseCursor Cursor
694686 // User provided mouse cursor.
695687 fixed ( byte * ptr = value . Data )
696688 {
697- var cursorImg = new GraphicsLibraryFramework . Image ( value . Width , value . Height , ptr ) ;
689+ GraphicsLibraryFramework . Image cursorImg = new GraphicsLibraryFramework . Image ( value . Width , value . Height , ptr ) ;
698690 _glfwCursor = GLFW . CreateCursor ( cursorImg , value . X , value . Y ) ;
699691 }
700692 }
@@ -811,8 +803,6 @@ public unsafe NativeWindow(NativeWindowSettings settings)
811803
812804 _title = settings . Title ;
813805
814- _currentMonitor = settings . CurrentMonitor ;
815-
816806 switch ( settings . WindowBorder )
817807 {
818808 case WindowBorder . Hidden :
@@ -828,7 +818,7 @@ public unsafe NativeWindow(NativeWindowSettings settings)
828818 break ;
829819 }
830820
831- var isOpenGl = false ;
821+ bool isOpenGl = false ;
832822 API = settings . API ;
833823 switch ( settings . API )
834824 {
@@ -2130,5 +2120,29 @@ public void CenterWindow(Vector2i newSize)
21302120 // Actually move the window.
21312121 ClientRectangle = new Box2i ( x , y , x + newSize . X , y + newSize . Y ) ;
21322122 }
2123+
2124+ /// <summary>
2125+ /// Make the window fullscreen with the specified resolution and refresh rate.
2126+ /// This function is meant to provide greater control than <see cref="WindowState"/> and <see cref="CurrentMonitor"/> when making the window fullscreen.
2127+ /// </summary>
2128+ /// <param name="monitor">The monitor on which to make the window fullscreen.</param>
2129+ /// <param name="horizontalResolution">The horizontal resolution to switch the screen to, or <see langword="null"/> to use the monitor resolution.</param>
2130+ /// <param name="verticalResolution">The vertical resoltion to switch the screen to, or <see langword="null"/> to use the monitor resolution.</param>
2131+ /// <param name="refreshRate">The refresh rate to use, or <see langword="null"/> to not change refresh rate.</param>
2132+ /// <seealso cref="CurrentMonitor"/>
2133+ /// <seealso cref="WindowState"/>
2134+ public unsafe void MakeFullscreen ( MonitorHandle monitor , int ? horizontalResolution = null , int ? verticalResolution = null , int ? refreshRate = null )
2135+ {
2136+ if ( _windowState != WindowState . Fullscreen )
2137+ {
2138+ _cachedWindowClientSize = ClientSize ;
2139+ _cachedWindowLocation = ClientLocation ;
2140+ }
2141+ _windowState = WindowState . Fullscreen ;
2142+ Monitor * monitorPtr = monitor . ToUnsafePtr < Monitor > ( ) ;
2143+ GLFW . GetMonitorPos ( monitorPtr , out int x , out int y ) ;
2144+ VideoMode * mode = GLFW . GetVideoMode ( monitorPtr ) ;
2145+ GLFW . SetWindowMonitor ( WindowPtr , monitorPtr , x , y , horizontalResolution ?? mode ->Width , verticalResolution ?? mode ->Height , refreshRate ?? - 1 ) ;
2146+ }
21332147 }
21342148}
0 commit comments