From bae53d4db6609c1a151a60dcdda9bef5629805c5 Mon Sep 17 00:00:00 2001 From: psyGamer Date: Sat, 5 Oct 2024 23:01:36 +0200 Subject: [PATCH] refactor(Studio): Move entire dialog registration into Studio.RegisterDialog --- Studio/CelesteStudio/Dialog/FindDialog.cs | 5 +-- Studio/CelesteStudio/Dialog/FontDialog.cs | 5 +-- Studio/CelesteStudio/Dialog/GoToDialog.cs | 5 +-- Studio/CelesteStudio/Dialog/HotkeyDialog.cs | 4 +- .../CelesteStudio/Dialog/KeyBindingDialog.cs | 5 +-- .../CelesteStudio/Dialog/NumberInputDialog.cs | 5 +-- Studio/CelesteStudio/Dialog/RecordDialog.cs | 3 -- .../CelesteStudio/Dialog/RenameLabelDialog.cs | 3 -- .../Dialog/SettingsErrorDialog.cs | 5 +-- Studio/CelesteStudio/Dialog/SnippetDialog.cs | 4 +- Studio/CelesteStudio/Dialog/WhatsNewDialog.cs | 5 +-- Studio/CelesteStudio/Editing/GameInfoPanel.cs | 3 +- Studio/CelesteStudio/Studio.cs | 41 ++++++++++++++++++- 13 files changed, 49 insertions(+), 44 deletions(-) diff --git a/Studio/CelesteStudio/Dialog/FindDialog.cs b/Studio/CelesteStudio/Dialog/FindDialog.cs index 2a5d34f2..1fde8c64 100644 --- a/Studio/CelesteStudio/Dialog/FindDialog.cs +++ b/Studio/CelesteStudio/Dialog/FindDialog.cs @@ -44,14 +44,11 @@ private FindDialog(Editor editor, string initialText) { } }, }; - Icon = Assets.AppIcon; - Studio.RegisterDialog(this); KeyDown += HandleKeyDown; textBox.KeyDown += HandleKeyDown; - Load += (_, _) => Studio.Instance.WindowCreationCallback(this); - Shown += (_, _) => Location = Studio.Instance.Location + new Point((Studio.Instance.Width - Width) / 2, (Studio.Instance.Height - Height) / 2); + Studio.RegisterDialog(this); return; diff --git a/Studio/CelesteStudio/Dialog/FontDialog.cs b/Studio/CelesteStudio/Dialog/FontDialog.cs index 6e7780ea..bbac2a59 100644 --- a/Studio/CelesteStudio/Dialog/FontDialog.cs +++ b/Studio/CelesteStudio/Dialog/FontDialog.cs @@ -96,8 +96,6 @@ private FontDialog() { Title = "Font"; Content = CreateDialogContent(); - Icon = Assets.AppIcon; - Studio.RegisterDialog(this); DefaultButton = new Button((_, _) => Close(true)) { Text = "&OK" }; AbortButton = new Button((_, _) => Close(false)) { Text = "&Cancel" }; @@ -123,8 +121,7 @@ private FontDialog() { }); } - Load += (_, _) => Studio.Instance.WindowCreationCallback(this); - Shown += (_, _) => Location = Studio.Instance.Location + new Point((Studio.Instance.Width - Width) / 2, (Studio.Instance.Height - Height) / 2); + Studio.RegisterDialog(this); } private Control CreateDialogContent() { diff --git a/Studio/CelesteStudio/Dialog/GoToDialog.cs b/Studio/CelesteStudio/Dialog/GoToDialog.cs index 5161d2e6..32e71e54 100644 --- a/Studio/CelesteStudio/Dialog/GoToDialog.cs +++ b/Studio/CelesteStudio/Dialog/GoToDialog.cs @@ -59,8 +59,6 @@ private GoToDialog(Document document) { } } }; - Icon = Assets.AppIcon; - Studio.RegisterDialog(this); DefaultButton = new Button((_, _) => Close((int)lineSelector.Value - 1)) { Text = "&Go" }; AbortButton = new Button((_, _) => Close(document.Caret.Row)) { Text = "&Cancel" }; @@ -68,8 +66,7 @@ private GoToDialog(Document document) { PositiveButtons.Add(DefaultButton); NegativeButtons.Add(AbortButton); - Load += (_, _) => Studio.Instance.WindowCreationCallback(this); - Shown += (_, _) => Location = Studio.Instance.Location + new Point((Studio.Instance.Width - Width) / 2, (Studio.Instance.Height - Height) / 2); + Studio.RegisterDialog(this); } public static int Show(Document document) => new GoToDialog(document).ShowModal(); diff --git a/Studio/CelesteStudio/Dialog/HotkeyDialog.cs b/Studio/CelesteStudio/Dialog/HotkeyDialog.cs index ad660af0..0e0c2ef0 100644 --- a/Studio/CelesteStudio/Dialog/HotkeyDialog.cs +++ b/Studio/CelesteStudio/Dialog/HotkeyDialog.cs @@ -25,7 +25,6 @@ private HotkeyDialog(Keys currentHotkey, Dictionary keyBindings new Button((_, _) => Close(Keys.None)) { Text = "Clear" }, } }; - Icon = Assets.AppIcon; Topmost = true; Result = currentHotkey; @@ -103,8 +102,7 @@ or Keys.LeftAlt or Keys.RightAlt } }; - Load += (_, _) => Studio.Instance.WindowCreationCallback(this); - Shown += (_, _) => Location = ParentWindow.Location + new Point((ParentWindow.Width - Width) / 2, (ParentWindow.Height - Height) / 2); + Studio.RegisterDialog(this); } public static Keys Show(Window parent, Keys currentHotkey, Dictionary? keyBindings, List? snippets) { diff --git a/Studio/CelesteStudio/Dialog/KeyBindingDialog.cs b/Studio/CelesteStudio/Dialog/KeyBindingDialog.cs index 1b0507b1..757f822e 100644 --- a/Studio/CelesteStudio/Dialog/KeyBindingDialog.cs +++ b/Studio/CelesteStudio/Dialog/KeyBindingDialog.cs @@ -69,8 +69,6 @@ private KeyBindingDialog() { Height = 500, Content = list, }.FixBorder(); - Icon = Assets.AppIcon; - Studio.RegisterDialog(this); DefaultButton = new Button((_, _) => Close(true)) { Text = "&OK" }; AbortButton = new Button((_, _) => Close(false)) { Text = "&Cancel" }; @@ -78,8 +76,7 @@ private KeyBindingDialog() { PositiveButtons.Add(DefaultButton); NegativeButtons.Add(AbortButton); - Load += (_, _) => Studio.Instance.WindowCreationCallback(this); - Shown += (_, _) => Location = Studio.Instance.Location + new Point((Studio.Instance.Width - Width) / 2, (Studio.Instance.Height - Height) / 2); + Studio.RegisterDialog(this); } public static void Show() { diff --git a/Studio/CelesteStudio/Dialog/NumberInputDialog.cs b/Studio/CelesteStudio/Dialog/NumberInputDialog.cs index 99af9363..436c13be 100644 --- a/Studio/CelesteStudio/Dialog/NumberInputDialog.cs +++ b/Studio/CelesteStudio/Dialog/NumberInputDialog.cs @@ -26,8 +26,6 @@ private NumberInputDialog(string title, T input, T minValue, T maxValue, T step) Padding = 10, Items = { stepper }, }; - Icon = Assets.AppIcon; - Studio.RegisterDialog(this); DefaultButton = new Button((_, _) => Close(T.CreateChecked(stepper.Value))) { Text = "&OK" }; AbortButton = new Button((_, _) => Close(input)) { Text = "&Cancel" }; @@ -35,8 +33,7 @@ private NumberInputDialog(string title, T input, T minValue, T maxValue, T step) PositiveButtons.Add(DefaultButton); NegativeButtons.Add(AbortButton); - Load += (_, _) => Studio.Instance.WindowCreationCallback(this); - Shown += (_, _) => Location = Studio.Instance.Location + new Point((Studio.Instance.Width - Width) / 2, (Studio.Instance.Height - Height) / 2); + Studio.RegisterDialog(this); } public static T Show(string title, T input, T minValue, T maxValue, T step) { diff --git a/Studio/CelesteStudio/Dialog/RecordDialog.cs b/Studio/CelesteStudio/Dialog/RecordDialog.cs index 502abb5d..dd334638 100644 --- a/Studio/CelesteStudio/Dialog/RecordDialog.cs +++ b/Studio/CelesteStudio/Dialog/RecordDialog.cs @@ -19,7 +19,6 @@ private RecordDialog() { Orientation = Orientation.Horizontal, Items = { new Label { Text = "File Name" }, textBox }, }; - Icon = Assets.AppIcon; DefaultButton = new Button((_, _) => Close(true)) { Text = "&Record" }; AbortButton = new Button((_, _) => Close(false)) { Text = "&Cancel" }; @@ -28,8 +27,6 @@ private RecordDialog() { NegativeButtons.Add(AbortButton); Studio.RegisterDialog(this); - Load += (_, _) => Studio.Instance.WindowCreationCallback(this); - Shown += (_, _) => Location = Studio.Instance.Location + new Point((Studio.Instance.Width - Width) / 2, (Studio.Instance.Height - Height) / 2); } public static void Show() { diff --git a/Studio/CelesteStudio/Dialog/RenameLabelDialog.cs b/Studio/CelesteStudio/Dialog/RenameLabelDialog.cs index a735f308..fc10e87d 100644 --- a/Studio/CelesteStudio/Dialog/RenameLabelDialog.cs +++ b/Studio/CelesteStudio/Dialog/RenameLabelDialog.cs @@ -19,7 +19,6 @@ private RenameLabelDialog(string labelName) { Orientation = Orientation.Horizontal, Items = { new Label { Text = "Label Name" }, labelNameBox }, }; - Icon = Assets.AppIcon; DefaultButton = new Button((_, _) => Close(true)) { Text = "&Rename" }; AbortButton = new Button((_, _) => Close(false)) { Text = "&Cancel" }; @@ -28,8 +27,6 @@ private RenameLabelDialog(string labelName) { NegativeButtons.Add(AbortButton); Studio.RegisterDialog(this); - Load += (_, _) => Studio.Instance.WindowCreationCallback(this); - Shown += (_, _) => Location = Studio.Instance.Location + new Point((Studio.Instance.Width - Width) / 2, (Studio.Instance.Height - Height) / 2); } public static string Show(string labelName) { diff --git a/Studio/CelesteStudio/Dialog/SettingsErrorDialog.cs b/Studio/CelesteStudio/Dialog/SettingsErrorDialog.cs index 917c08c6..ef2e5c8b 100644 --- a/Studio/CelesteStudio/Dialog/SettingsErrorDialog.cs +++ b/Studio/CelesteStudio/Dialog/SettingsErrorDialog.cs @@ -68,17 +68,14 @@ Celeste Studio {Studio.Version} }, }, }; - Icon = Assets.AppIcon; - Studio.RegisterDialog(this); Result = SettingsErrorAction.None; Load += (_, _) => { // Need to make theme is applied Settings.OnThemeChanged(); - Studio.Instance.WindowCreationCallback(this); }; - Shown += (_, _) => Location = new Point((int)((Screen.WorkingArea.Width - Width) / 2), (int)((Screen.WorkingArea.Height - Height) / 2)); + Studio.RegisterDialog(this); } protected override void OnClosing(CancelEventArgs e) { diff --git a/Studio/CelesteStudio/Dialog/SnippetDialog.cs b/Studio/CelesteStudio/Dialog/SnippetDialog.cs index 05209abb..3a091dd3 100644 --- a/Studio/CelesteStudio/Dialog/SnippetDialog.cs +++ b/Studio/CelesteStudio/Dialog/SnippetDialog.cs @@ -51,7 +51,6 @@ private SnippetDialog() { }.FixBorder() } }; - Icon = Assets.AppIcon; DefaultButton = new Button((_, _) => Close(true)) { Text = "&OK" }; AbortButton = new Button((_, _) => Close(false)) { Text = "&Cancel" }; @@ -59,8 +58,7 @@ private SnippetDialog() { PositiveButtons.Add(DefaultButton); NegativeButtons.Add(AbortButton); - Load += (_, _) => Studio.Instance.WindowCreationCallback(this); - Shown += (_, _) => Location = Studio.Instance.Location + new Point((Studio.Instance.Width - Width) / 2, (Studio.Instance.Height - Height) / 2); + Studio.RegisterDialog(this); } private void GenerateListEntries(ICollection items) { diff --git a/Studio/CelesteStudio/Dialog/WhatsNewDialog.cs b/Studio/CelesteStudio/Dialog/WhatsNewDialog.cs index a1f6c10c..22a5e39f 100644 --- a/Studio/CelesteStudio/Dialog/WhatsNewDialog.cs +++ b/Studio/CelesteStudio/Dialog/WhatsNewDialog.cs @@ -58,11 +58,8 @@ Control GenerateContent() { } Title = title; - Icon = Assets.AppIcon; - Studio.RegisterDialog(this); - Load += (_, _) => Studio.Instance.WindowCreationCallback(this); - Shown += (_, _) => Location = new Point((int)((Screen.WorkingArea.Width - Width) / 2), (int)((Screen.WorkingArea.Height - Height) / 2)); + Studio.RegisterDialog(this); } public static void Show(string title, string markdown) => new WhatsNewDialog(title, markdown).ShowModal(); diff --git a/Studio/CelesteStudio/Editing/GameInfoPanel.cs b/Studio/CelesteStudio/Editing/GameInfoPanel.cs index 5551b9e9..c1ea1386 100644 --- a/Studio/CelesteStudio/Editing/GameInfoPanel.cs +++ b/Studio/CelesteStudio/Editing/GameInfoPanel.cs @@ -470,7 +470,6 @@ public GameInfoPopout() { }; Title = "Game Info"; - Icon = Assets.AppIcon; MinimumSize = new Size(300, 100); Padding = 10; @@ -484,7 +483,7 @@ public GameInfoPopout() { gameInfo.AvailableWidth = ClientSize.Width - Padding.Left - Padding.Right; }; - Load += (_, _) => Studio.Instance.WindowCreationCallback(this); + Studio.RegisterWindow(this, centerWindow: false); Shown += (_, _) => { Size = Settings.Instance.GameInfoPopoutSize; if (!Settings.Instance.GameInfoPopoutLocation.IsZero) { diff --git a/Studio/CelesteStudio/Studio.cs b/Studio/CelesteStudio/Studio.cs index e644078e..a3870686 100644 --- a/Studio/CelesteStudio/Studio.cs +++ b/Studio/CelesteStudio/Studio.cs @@ -228,8 +228,45 @@ public Studio(string[] args, Action windowCreationCallback) { CommunicationWrapper.Start(); } - // Dialogs should always be focused over the editor / tools, but not other OS windows - public static void RegisterDialog(Eto.Forms.Dialog dialog) { + /// Properly registers a window + public static void RegisterWindow(Window window, Window? parent = null, bool centerWindow = true) { + parent ??= window.ParentWindow; + + window.Icon = Assets.AppIcon; + window.ShowInTaskbar = true; + + // Apply theming on WPF + window.Load += (_, _) => Instance.WindowCreationCallback(window); + + if (centerWindow) { + window.Shown += (_, _) => { + // Center on parent + var location = parent.Location + new Point((parent.Width - window.Width) / 2, (parent.Height - window.Height) / 2); + + // Clamp to screen + var screen = Screen.FromRectangle(new RectangleF(location, window.Size)); + if (location.X < screen.WorkingArea.Left) { + location = location with { X = (int)screen.WorkingArea.Left }; + } else if (location.X + window.Width > screen.WorkingArea.Right) { + location = location with { X = (int)screen.WorkingArea.Right - window.Width }; + } + if (location.Y < screen.WorkingArea.Top) { + location = location with { Y = (int)screen.WorkingArea.Top }; + } else if (location.Y + window.Height > screen.WorkingArea.Bottom) { + location = location with { Y = (int)screen.WorkingArea.Bottom - window.Height }; + } + + window.Location = location; + }; + } + } + + /// Properly registers a dialog window + public static void RegisterDialog(Eto.Forms.Dialog dialog, Window? parent = null, bool centerWindow = true) { + RegisterWindow(dialog); + + // Dialogs should always be focused over the editor / tools, but not other OS windows + // For some reason macOS can just revive dialogs from the dead // Thankfully Topmost already behaves how we want on macOS if (Eto.Platform.Instance.IsMac) {