Files
sbox-public/engine/Sandbox.Tools/Qt/Window.Cookies.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

63 lines
1.8 KiB
C#

using Sandbox;
namespace Editor
{
public partial class Window
{
string _stateCookie;
/// <summary>
/// A unique identifier for this window, to store the window state across sessions using the <see cref="Cookie">Cookie</see> library.
/// </summary>
public string StateCookie
{
get => _stateCookie;
set
{
if ( _stateCookie == value ) return;
_stateCookie = value;
RestoreFromStateCookie();
}
}
/// <summary>
/// Called whenever the window should restore its state via the <see cref="EditorCookie">EditorCookie</see> library,
/// that was previously saved in <see cref="SaveToStateCookie"/>.<br/>
/// You should use <see cref="StateCookie"/> in the cookie name.
/// </summary>
public virtual void RestoreFromStateCookie()
{
if ( string.IsNullOrWhiteSpace( StateCookie ) )
return;
var state = EditorCookie.GetString( $"Window.{StateCookie}.State", null );
var geo = EditorCookie.GetString( $"Window.{StateCookie}.Geometry", null );
if ( geo != null ) RestoreGeometry( geo );
if ( state != null ) RestoreState( state );
}
/// <summary>
/// Called whenever the window should save its state via the <see cref="EditorCookie">EditorCookie</see> library,
/// to be later restored in <see cref="RestoreFromStateCookie"/>. This is useful to carry data across game sessions.<br/>
/// You should use <see cref="StateCookie"/> in the cookie name.
/// </summary>
[Event( "app.exit" )]
public virtual void SaveToStateCookie()
{
if ( string.IsNullOrWhiteSpace( StateCookie ) )
return;
if ( !this.IsValid() )
return;
var state = SaveState();
var geo = SaveGeometry();
EditorCookie.SetString( $"Window.{StateCookie}.State", state );
EditorCookie.SetString( $"Window.{StateCookie}.Geometry", geo );
}
}
}