Skip to content

Commit ec4f968

Browse files
authored
Save main screen size in the app preferences. (#166)
1 parent 01b0793 commit ec4f968

File tree

7 files changed

+59
-9
lines changed

7 files changed

+59
-9
lines changed

Yafc.UI/Core/Ui.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ public static void ProcessEvents() {
156156
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESIZED:
157157
window.WindowResize();
158158
break;
159+
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MAXIMIZED:
160+
window.WindowMaximized();
161+
break;
162+
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESTORED:
163+
window.WindowRestored();
164+
break;
159165
default:
160166
Console.WriteLine("Window event of type " + evt.window.windowEvent);
161167
window.Rebuild(); // might be something like "window exposed", better to paint the UI again

Yafc.UI/Core/Window.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ internal int CalculateUnitsToPixels(int display) {
7777
return desiredUnitsToPixels;
7878
}
7979

80-
internal virtual void WindowResize() {
80+
protected internal virtual void WindowResize() {
8181
rootGui.MarkEverythingForRebuild();
8282
rootGui.Rebuild();
8383
}
@@ -223,5 +223,7 @@ private void Build(ImGui gui) {
223223
public virtual void Dispose() => rootGui.Dispose();
224224

225225
internal ImGui.DragOverlay GetDragOverlay() => draggingOverlay ??= new ImGui.DragOverlay(InputSystem);
226+
protected internal virtual void WindowMaximized() { }
227+
protected internal virtual void WindowRestored() { }
226228
}
227229
}

Yafc.UI/Core/WindowMain.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Yafc.UI {
77
// Main window is resizable and hardware-accelerated
88
public abstract class WindowMain : Window {
9-
protected void Create(string title, int display) {
9+
protected void Create(string title, int display, float initialWidth, float initialHeight, bool maximized) {
1010
if (visible) {
1111
return;
1212
}
@@ -17,13 +17,16 @@ protected void Create(string title, int display) {
1717
int minWidth = MathUtils.Round(85f * pixelsPerUnit);
1818
int minHeight = MathUtils.Round(60f * pixelsPerUnit);
1919
// Initial width/height define the initial size of the MainWindow when it is opened.
20-
int initialWidth = MathUtils.Round(85f * pixelsPerUnit);
21-
int initialHeight = MathUtils.Round(60f * pixelsPerUnit);
20+
int initialWidthPixels = Math.Max(minWidth, MathUtils.Round(initialWidth * pixelsPerUnit));
21+
int initialHeightPixels = Math.Max(minHeight, MathUtils.Round(initialHeight * pixelsPerUnit));
22+
SDL.SDL_WindowFlags flags = SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE | (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0 : SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL);
23+
if (maximized) {
24+
flags |= SDL.SDL_WindowFlags.SDL_WINDOW_MAXIMIZED;
25+
}
2226
window = SDL.SDL_CreateWindow(title,
2327
SDL.SDL_WINDOWPOS_CENTERED_DISPLAY(display),
2428
SDL.SDL_WINDOWPOS_CENTERED_DISPLAY(display),
25-
initialWidth, initialHeight,
26-
SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE | (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0 : SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL)
29+
initialWidthPixels, initialHeightPixels, flags
2730
);
2831
SDL.SDL_SetWindowMinimumSize(window, minWidth, minHeight);
2932
WindowResize();
@@ -43,12 +46,19 @@ protected override void OnRepaint() {
4346
base.OnRepaint();
4447
}
4548

46-
internal override void WindowResize() {
49+
protected internal override void WindowResize() {
4750
SDL.SDL_GetWindowSize(window, out int windowWidth, out int windowHeight);
4851
contentSize = new Vector2(windowWidth / pixelsPerUnit, windowHeight / pixelsPerUnit);
4952
base.WindowResize();
5053
}
5154

55+
protected bool IsMaximized {
56+
get {
57+
SDL.SDL_WindowFlags flags = (SDL.SDL_WindowFlags)SDL.SDL_GetWindowFlags(window);
58+
return flags.HasFlag(SDL.SDL_WindowFlags.SDL_WINDOW_MAXIMIZED);
59+
}
60+
}
61+
5262
protected WindowMain(Padding padding) : base(padding) { }
5363
}
5464

Yafc.UI/Core/WindowUtility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected void Create(string? title, float width, Window? parent) {
3535
base.Create();
3636
}
3737

38-
internal override void WindowResize() {
38+
protected internal override void WindowResize() {
3939
(surface as UtilityWindowDrawingSurface)!.OnResize(); // null-forgiving: Assuming WindowResize cannot be called before Create
4040
base.WindowResize();
4141
}

Yafc/Utils/Preferences.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ public void Save() {
4545
public bool darkMode { get; set; }
4646
public string language { get; set; } = "en";
4747
public string? overrideFont { get; set; }
48+
/// <summary>
49+
/// Whether or not the main screen should be created maximized.
50+
/// </summary>
51+
public bool maximizeMainScreen { get; set; }
52+
/// <summary>
53+
/// The initial width of the main screen or the width the main screen will be after being restored, depending on whether it starts restored or maximized.
54+
/// </summary>
55+
public float initialMainScreenWidth { get; set; }
56+
/// <summary>
57+
/// The initial height of the main screen or the height the main screen will be after being restored, depending on whether it starts restored or maximized.
58+
/// </summary>
59+
public float initialMainScreenHeight { get; set; }
4860

4961
public void AddProject(string path, string dataPath, string modsPath, bool expensiveRecipes, bool netProduction) {
5062
recentProjects = recentProjects.Where(x => string.Compare(path, x.path, StringComparison.InvariantCultureIgnoreCase) != 0)

Yafc/Windows/MainScreen.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public MainScreen(int display, Project project) : base(default) {
5555
objectTooltip = new ObjectTooltip();
5656
tabBar = new MainScreenTabBar(this);
5757
allPages = new VirtualScrollList<ProjectPage>(30, new Vector2(0f, 2f), BuildPage, InputSystem, collapsible: true);
58-
Create("Yet Another Factorio Calculator CE v" + YafcLib.version, display);
58+
Create("Yet Another Factorio Calculator CE v" + YafcLib.version, display, Preferences.Instance.initialMainScreenWidth, Preferences.Instance.initialMainScreenHeight, Preferences.Instance.maximizeMainScreen);
5959
SetProject(project);
6060
}
6161

@@ -444,6 +444,25 @@ protected override async void Close() {
444444
ForceClose();
445445
}
446446

447+
protected override void WindowMaximized() {
448+
Preferences.Instance.maximizeMainScreen = true;
449+
Preferences.Instance.Save();
450+
}
451+
452+
protected override void WindowRestored() {
453+
Preferences.Instance.maximizeMainScreen = false;
454+
Preferences.Instance.Save();
455+
}
456+
457+
protected override void WindowResize() {
458+
base.WindowResize();
459+
if (!IsMaximized) {
460+
Preferences.Instance.initialMainScreenWidth = size.X;
461+
Preferences.Instance.initialMainScreenHeight = size.Y;
462+
Preferences.Instance.Save();
463+
}
464+
}
465+
447466
public void ForceClose() {
448467
base.Close();
449468
}

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Date: soon
1616
Features:
1717
- Add several right-click and keyboard shortcuts, notably Enter/Return to close most dialogs.
1818
- Add UI rebuilding itself on resize.
19+
- When opening the main window, use the same size it was when it was last closed.
1920
Bugfixes:
2021
- Fix that some pages couldn't be deleted.
2122
- Fix that returning to the Welcome Screen could break the panels in the main window.

0 commit comments

Comments
 (0)