Skip to content

Commit

Permalink
refactor(Studio): Move entire dialog registration into Studio.Registe…
Browse files Browse the repository at this point in the history
…rDialog
  • Loading branch information
psyGamer committed Oct 5, 2024
1 parent f72a7ee commit bae53d4
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 44 deletions.
5 changes: 1 addition & 4 deletions Studio/CelesteStudio/Dialog/FindDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 1 addition & 4 deletions Studio/CelesteStudio/Dialog/FontDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
Expand All @@ -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() {
Expand Down
5 changes: 1 addition & 4 deletions Studio/CelesteStudio/Dialog/GoToDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,14 @@ 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" };

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();
Expand Down
4 changes: 1 addition & 3 deletions Studio/CelesteStudio/Dialog/HotkeyDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ private HotkeyDialog(Keys currentHotkey, Dictionary<MenuEntry, Keys> keyBindings
new Button((_, _) => Close(Keys.None)) { Text = "Clear" },
}
};
Icon = Assets.AppIcon;
Topmost = true;

Result = currentHotkey;
Expand Down Expand Up @@ -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<MenuEntry, Keys>? keyBindings, List<Snippet>? snippets) {
Expand Down
5 changes: 1 addition & 4 deletions Studio/CelesteStudio/Dialog/KeyBindingDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,14 @@ 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" };

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() {
Expand Down
5 changes: 1 addition & 4 deletions Studio/CelesteStudio/Dialog/NumberInputDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@ 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" };

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) {
Expand Down
3 changes: 0 additions & 3 deletions Studio/CelesteStudio/Dialog/RecordDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
Expand All @@ -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() {
Expand Down
3 changes: 0 additions & 3 deletions Studio/CelesteStudio/Dialog/RenameLabelDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
Expand All @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions Studio/CelesteStudio/Dialog/SettingsErrorDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 1 addition & 3 deletions Studio/CelesteStudio/Dialog/SnippetDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,14 @@ private SnippetDialog() {
}.FixBorder()
}
};
Icon = Assets.AppIcon;

DefaultButton = new Button((_, _) => Close(true)) { Text = "&OK" };
AbortButton = new Button((_, _) => Close(false)) { Text = "&Cancel" };

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<StackLayoutItem> items) {
Expand Down
5 changes: 1 addition & 4 deletions Studio/CelesteStudio/Dialog/WhatsNewDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions Studio/CelesteStudio/Editing/GameInfoPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ public GameInfoPopout() {
};

Title = "Game Info";
Icon = Assets.AppIcon;
MinimumSize = new Size(300, 100);

Padding = 10;
Expand All @@ -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) {
Expand Down
41 changes: 39 additions & 2 deletions Studio/CelesteStudio/Studio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,45 @@ public Studio(string[] args, Action<Window> 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) {
Expand Down

0 comments on commit bae53d4

Please sign in to comment.