mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-12-03 02:05:18 +01:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9583117eea | |||
| 121481af00 | |||
| e1800a20b9 |
@@ -0,0 +1,155 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.VisualTree;
|
||||
using StarterNG.Controls;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
/// <summary>Windows under gamescope, which gives them no title bar, no close button and no maximizing.</summary>
|
||||
[Collection(AvaloniaControls.Name)]
|
||||
public class GamescopeWindowsTests
|
||||
{
|
||||
static GamescopeWindowsTests()
|
||||
{
|
||||
HeadlessDrawing.Ensure();
|
||||
DialogKeyboard.Install();
|
||||
GamescopeWindows.Install(false, () => "Zamknij");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("gamescope", null, true)]
|
||||
[InlineData("Gamescope", null, true)]
|
||||
[InlineData("KDE", "gamescope-0", true)]
|
||||
[InlineData(null, "gamescope-0", true)]
|
||||
[InlineData("KDE", null, false)]
|
||||
[InlineData("KDE", "", false)]
|
||||
[InlineData(null, null, false)]
|
||||
public void Gamescope_is_told_by_its_session_or_by_its_display(string? desktop, string? display, bool expected)
|
||||
{
|
||||
var variables = new Dictionary<string, string?>
|
||||
{
|
||||
["XDG_CURRENT_DESKTOP"] = desktop,
|
||||
["GAMESCOPE_WAYLAND_DISPLAY"] = display,
|
||||
// Big Picture on a desktop, whose window manager still draws the bars.
|
||||
["SteamGamepadUI"] = "1"
|
||||
};
|
||||
|
||||
Assert.Equal(expected, GamescopeWindows.IsGamescope(name => variables.GetValueOrDefault(name)));
|
||||
}
|
||||
|
||||
private static (Window Owner, Window Window) Open(Control content, bool gamescope, bool escapeCloses = true)
|
||||
{
|
||||
GamescopeWindows.Active = gamescope;
|
||||
var owner = new Window();
|
||||
owner.Show();
|
||||
var window = new Window { Title = "Taśmy", Width = 300, Height = 200, Content = content };
|
||||
DialogKeyboard.SetEscapeCloses(window, escapeCloses);
|
||||
window.Show(owner);
|
||||
// Headless a window says it opened before it is given its owner; it says so again once it has one.
|
||||
Assert.Same(owner, window.Owner);
|
||||
window.RaiseEvent(new RoutedEventArgs(Window.WindowOpenedEvent));
|
||||
Controls.Settle();
|
||||
return (owner, window);
|
||||
}
|
||||
|
||||
private static Button? CloseButton(Window window) =>
|
||||
window.GetVisualDescendants().OfType<Button>().FirstOrDefault(b => Equals(b.Content, "Zamknij"));
|
||||
|
||||
[Fact]
|
||||
public void Under_gamescope_a_window_over_another_gets_its_title_and_a_close_button()
|
||||
{
|
||||
var content = new TextBlock { Text = "zawartość" };
|
||||
var (owner, window) = Open(content, gamescope: true);
|
||||
try
|
||||
{
|
||||
bool closed = false;
|
||||
window.Closed += (_, _) => closed = true;
|
||||
|
||||
Assert.Contains(window.GetVisualDescendants().OfType<TextBlock>(), t => t.Text == "Taśmy");
|
||||
Assert.Same(content, window.GetVisualDescendants().OfType<TextBlock>().First(t => t.Text == "zawartość"));
|
||||
var close = CloseButton(window);
|
||||
Assert.NotNull(close);
|
||||
|
||||
close.RaiseEvent(Controls.Click());
|
||||
Assert.True(closed);
|
||||
}
|
||||
finally
|
||||
{
|
||||
GamescopeWindows.Active = false;
|
||||
owner.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Under_gamescope_esc_closes_even_a_window_that_stays_open_beside_the_starter()
|
||||
{
|
||||
var (owner, window) = Open(new TextBox(), gamescope: true, escapeCloses: false);
|
||||
try
|
||||
{
|
||||
bool closed = false;
|
||||
window.Closed += (_, _) => closed = true;
|
||||
|
||||
Assert.True(Controls.Press(window, Key.Escape));
|
||||
Assert.True(closed);
|
||||
}
|
||||
finally
|
||||
{
|
||||
GamescopeWindows.Active = false;
|
||||
owner.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_window_shows_what_it_had_first_and_the_bar_after_it_in_tab_order()
|
||||
{
|
||||
var first = new Button { Content = "Pierwszy" };
|
||||
var (owner, window) = Open(new StackPanel { Children = { first } }, gamescope: true);
|
||||
try
|
||||
{
|
||||
var grid = Assert.IsType<Grid>(window.Content);
|
||||
Assert.IsType<StackPanel>(grid.Children[0]);
|
||||
Assert.True(first.IsFocused);
|
||||
}
|
||||
finally
|
||||
{
|
||||
GamescopeWindows.Active = false;
|
||||
owner.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_window_that_says_twice_it_opened_gets_one_bar()
|
||||
{
|
||||
var (owner, window) = Open(new TextBlock { Text = "zawartość" }, gamescope: true);
|
||||
try
|
||||
{
|
||||
window.RaiseEvent(new RoutedEventArgs(Window.WindowOpenedEvent));
|
||||
Controls.Settle();
|
||||
|
||||
Assert.Single(window.GetVisualDescendants().OfType<Button>(), b => Equals(b.Content, "Zamknij"));
|
||||
}
|
||||
finally
|
||||
{
|
||||
GamescopeWindows.Active = false;
|
||||
owner.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Elsewhere_a_window_keeps_what_it_shows_and_esc_keeps_its_choice()
|
||||
{
|
||||
var content = new TextBox();
|
||||
var (owner, window) = Open(content, gamescope: false, escapeCloses: false);
|
||||
try
|
||||
{
|
||||
Assert.Same(content, window.Content);
|
||||
Assert.Null(CloseButton(window));
|
||||
Assert.False(Controls.Press(window, Key.Escape));
|
||||
}
|
||||
finally
|
||||
{
|
||||
owner.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,6 +79,20 @@ public class TrainsetDisplayTests
|
||||
Assert.True(TrainsetDisplay.StartableIn(decor, Mode(advanced: true)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_standard_mode_changes_the_crew_but_not_in_the_simulators_own_consists()
|
||||
{
|
||||
var standard = Mode(advanced: false);
|
||||
var decor = new Trainset("trainset a tor 0 0 //$decor\nnode -1 0 v1 dynamic d s m 0 headdriver 3 0 enddynamic\nendtrainset");
|
||||
|
||||
Assert.True(TrainsetDisplay.CrewEditableIn(Consist(), standard));
|
||||
Assert.True(TrainsetDisplay.CrewEditableIn(Consist(driver: "nobody"), standard));
|
||||
Assert.True(TrainsetDisplay.CrewEditableIn(null, standard));
|
||||
Assert.False(TrainsetDisplay.CrewEditableIn(Consist(description: "-ai"), standard));
|
||||
Assert.False(TrainsetDisplay.CrewEditableIn(decor, standard));
|
||||
Assert.True(TrainsetDisplay.CrewEditableIn(Consist(description: "-ai"), Mode(advanced: true)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_filters_work_in_the_advanced_mode()
|
||||
{
|
||||
|
||||
@@ -28,6 +28,8 @@ public partial class App : Avalonia.Application
|
||||
// Before any control exists: the names are given as controls are filled in.
|
||||
ScreenReaderNames.Install();
|
||||
DialogKeyboard.Install();
|
||||
GamescopeWindows.Install(GamescopeWindows.IsGamescope(AppServices.Current.Environment.GetVariable),
|
||||
() => Loc["Close"]);
|
||||
|
||||
AvaloniaXamlLoader.Load(this);
|
||||
|
||||
|
||||
@@ -41,7 +41,9 @@ public static class DialogKeyboard
|
||||
// button or a window of its own that handles the key keep it.
|
||||
private static void OnEscape(Window window, KeyEventArgs e)
|
||||
{
|
||||
if (e.Handled || e.Key != Key.Escape || e.KeyModifiers != KeyModifiers.None || !GetEscapeCloses(window))
|
||||
// Under gamescope a window has no other way out than Esc and the bar it is given there.
|
||||
if (e.Handled || e.Key != Key.Escape || e.KeyModifiers != KeyModifiers.None ||
|
||||
!(GetEscapeCloses(window) || GamescopeWindows.Active))
|
||||
return;
|
||||
|
||||
window.Close();
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
using System;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Layout;
|
||||
using Avalonia.Media;
|
||||
|
||||
namespace StarterNG.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// The starter's windows under gamescope, SteamOS's game mode (or gamescope started by
|
||||
/// hand): it shows one window at a time over the whole screen and is no window manager.
|
||||
/// No window gets a title bar or a close button from it, and a request to maximize is
|
||||
/// not answered, so gamescope stretches a window of its first size over the screen.
|
||||
/// Here each window over another gets a bar with its title and a close button, Esc closes
|
||||
/// it whatever it says otherwise, and every window takes the size of the screen itself.
|
||||
/// </summary>
|
||||
public static class GamescopeWindows
|
||||
{
|
||||
private static readonly AttachedProperty<bool> FittedProperty =
|
||||
AvaloniaProperty.RegisterAttached<Window, bool>("GamescopeFitted", typeof(GamescopeWindows));
|
||||
|
||||
private static readonly IBrush BarLine = new SolidColorBrush(Color.FromArgb(0x40, 0x80, 0x80, 0x80));
|
||||
|
||||
private static Func<string> _closeLabel = () => "Close";
|
||||
private static bool _installed;
|
||||
|
||||
/// <summary>
|
||||
/// XDG_CURRENT_DESKTOP is gamescope in SteamOS's game mode and in the sessions made
|
||||
/// after it; GAMESCOPE_WAYLAND_DISPLAY is set by any gamescope for what it starts,
|
||||
/// one started by hand on a desktop too. Steam's Big Picture on a desktop is neither:
|
||||
/// its window manager still draws the bars.
|
||||
/// </summary>
|
||||
public static bool IsGamescope(Func<string, string?> variable) =>
|
||||
string.Equals(variable("XDG_CURRENT_DESKTOP"), "gamescope", StringComparison.OrdinalIgnoreCase)
|
||||
|| !string.IsNullOrEmpty(variable("GAMESCOPE_WAYLAND_DISPLAY"));
|
||||
|
||||
/// <summary>Whether the starter runs under gamescope; set once by <see cref="Install"/>.</summary>
|
||||
public static bool Active { get; set; }
|
||||
|
||||
public static void Install(bool active, Func<string> closeLabel)
|
||||
{
|
||||
Active = active;
|
||||
_closeLabel = closeLabel;
|
||||
if (_installed)
|
||||
return;
|
||||
|
||||
_installed = true;
|
||||
Window.WindowOpenedEvent.AddClassHandler<Window>((window, _) => OnOpened(window));
|
||||
}
|
||||
|
||||
private static void OnOpened(Window window)
|
||||
{
|
||||
// The main window has a bar of its own and fills the screen from its constructor;
|
||||
// the splash goes by itself. A window may say twice that it opened.
|
||||
if (!Active || window.Owner is null || window.GetValue(FittedProperty))
|
||||
return;
|
||||
|
||||
window.SetValue(FittedProperty, true);
|
||||
AddTitleBar(window);
|
||||
FillScreen(window);
|
||||
}
|
||||
|
||||
/// <summary>The window's title and a close button above what it shows, after it in Tab order.</summary>
|
||||
public static void AddTitleBar(Window window)
|
||||
{
|
||||
if (window.Content is not Control content)
|
||||
return;
|
||||
|
||||
// Over the whole screen a window sized to what it shows keeps it in the middle.
|
||||
if (window.SizeToContent != SizeToContent.Manual)
|
||||
{
|
||||
content.HorizontalAlignment = HorizontalAlignment.Center;
|
||||
content.VerticalAlignment = VerticalAlignment.Center;
|
||||
}
|
||||
|
||||
var title = new TextBlock
|
||||
{
|
||||
VerticalAlignment = VerticalAlignment.Center,
|
||||
Margin = new Thickness(12, 0),
|
||||
FontWeight = FontWeight.SemiBold,
|
||||
TextTrimming = TextTrimming.CharacterEllipsis
|
||||
};
|
||||
title[!TextBlock.TextProperty] = window[!Window.TitleProperty];
|
||||
|
||||
var close = new Button
|
||||
{
|
||||
Content = _closeLabel(),
|
||||
VerticalAlignment = VerticalAlignment.Center,
|
||||
Margin = new Thickness(8, 4)
|
||||
};
|
||||
close.Classes.Add("Flat");
|
||||
close.Click += (_, _) => window.Close();
|
||||
|
||||
var bar = new DockPanel
|
||||
{
|
||||
Background = Brushes.Transparent,
|
||||
Children = { close, title }
|
||||
};
|
||||
DockPanel.SetDock(close, Dock.Right);
|
||||
|
||||
var line = new Border
|
||||
{
|
||||
BorderBrush = BarLine,
|
||||
BorderThickness = new Thickness(0, 0, 0, 1),
|
||||
Child = bar
|
||||
};
|
||||
|
||||
window.Content = null;
|
||||
Grid.SetRow(content, 1);
|
||||
Grid.SetRow(line, 0);
|
||||
// What the window shows first, so Tab and the first focus reach it before the bar.
|
||||
window.Content = new Grid
|
||||
{
|
||||
RowDefinitions = new RowDefinitions("Auto,*"),
|
||||
Children = { content, line }
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>The size and place of the screen's working area, which gamescope then shows as it is.</summary>
|
||||
public static void FillScreen(Window window)
|
||||
{
|
||||
var screen = window.Screens.ScreenFromWindow(window)
|
||||
?? window.Screens.ScreenFromPoint(window.Position)
|
||||
?? window.Screens.Primary;
|
||||
if (screen is null)
|
||||
return;
|
||||
|
||||
var area = screen.WorkingArea;
|
||||
double width = area.Width / screen.Scaling;
|
||||
double height = area.Height / screen.Scaling;
|
||||
window.SizeToContent = SizeToContent.Manual;
|
||||
window.MinWidth = Math.Min(window.MinWidth, width);
|
||||
window.MinHeight = Math.Min(window.MinHeight, height);
|
||||
window.MaxWidth = Math.Max(window.MaxWidth, width);
|
||||
window.MaxHeight = Math.Max(window.MaxHeight, height);
|
||||
window.Position = area.Position;
|
||||
window.Width = width;
|
||||
window.Height = height;
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,14 @@ public static class TrainsetDisplay
|
||||
public static bool StartableIn(Trainset trainset, SimulatorSettings settings) =>
|
||||
settings.AdvancedSettings || (!IsAi(trainset) && !trainset.Decor);
|
||||
|
||||
/// <summary>
|
||||
/// Whether the crew of the consist being edited may be changed: in the standard mode
|
||||
/// only in one the player may start in - the simulator's own consists and the
|
||||
/// decorations keep the crew the scenario gave them.
|
||||
/// </summary>
|
||||
public static bool CrewEditableIn(Trainset? trainset, SimulatorSettings settings) =>
|
||||
trainset is null || StartableIn(trainset, settings);
|
||||
|
||||
/// <summary>The consist's row in those lists: what it is made of, or the track of one with nothing on it.</summary>
|
||||
public static string ListLabel(Trainset trainset) =>
|
||||
CompositionText(trainset) ?? string.Format(App.Loc["EmptyTrainset"], trainset.Track ?? "");
|
||||
|
||||
@@ -57,6 +57,9 @@ public partial class MainWindow : Window
|
||||
ExtendClientAreaTitleBarHeightHint = TitleBarHeight;
|
||||
|
||||
FitToScreen();
|
||||
// Gamescope does not maximize a window: it would stretch the first size over the screen.
|
||||
if (GamescopeWindows.Active)
|
||||
GamescopeWindows.FillScreen(this);
|
||||
// Ahead of the layout a new size starts: changed during it, the bar keeps the old width.
|
||||
Resized += (_, e) => FitTopBar(e.ClientSize.Width);
|
||||
Opened += (_, _) => FitTopBar(ClientSize.Width);
|
||||
@@ -432,7 +435,7 @@ public partial class MainWindow : Window
|
||||
private bool wantsMaximized()
|
||||
{
|
||||
// make sure the window fills the whole screen on gamescope sessions
|
||||
if (Environment.GetEnvironmentVariable("XDG_CURRENT_DESKTOP") == "gamescope")
|
||||
if (GamescopeWindows.Active)
|
||||
return true;
|
||||
|
||||
// catch being launched via Steam Big Picture mode as well
|
||||
|
||||
@@ -69,7 +69,7 @@ public partial class Depot : UserControl
|
||||
if (Design.IsDesignMode)
|
||||
return;
|
||||
|
||||
// The standard mode lists other consists and leaves the crew alone.
|
||||
// The standard mode lists other consists and leaves the crew of the simulator's own alone.
|
||||
Settings.AdvancedModeChanged += () =>
|
||||
{
|
||||
PopulateSceneryConsists();
|
||||
|
||||
@@ -49,6 +49,9 @@ public sealed class VehicleCards
|
||||
|
||||
public Dynamic? PressedCar { get; set; }
|
||||
|
||||
private bool CrewEditable =>
|
||||
TrainsetDisplay.CrewEditableIn(_consist.EditingTrainset, AppServices.Current.Settings);
|
||||
|
||||
public VehicleCards(Panel consistStack, VehicleCatalog db, Consist consist,
|
||||
Cargo cargo, MiniTextures minis, Cursor hand,
|
||||
Action redraw, Action refreshDetails, Action<Dynamic> selectInBrowser,
|
||||
@@ -117,8 +120,8 @@ public sealed class VehicleCards
|
||||
HorizontalAlignment = HorizontalAlignment.Center,
|
||||
Margin = new Thickness(0, 1, 0, 2)
|
||||
};
|
||||
// Who sits in the cab - the player, the simulator, a passenger or nobody - is for the advanced mode.
|
||||
if (AppServices.Current.Settings.AdvancedSettings)
|
||||
// Who sits in the cab; in the standard mode not in the simulator's own consists.
|
||||
if (CrewEditable)
|
||||
controls.Children.Add(DriverButton(item));
|
||||
controls.Children.Add(FlipButton(item));
|
||||
|
||||
@@ -695,7 +698,7 @@ public sealed class VehicleCards
|
||||
else if (_consist.CanJoin(item, index))
|
||||
menu.AddItem(App.Loc["TipJoin"], () => _consist.Join(item));
|
||||
|
||||
if (AppServices.Current.Settings.AdvancedSettings)
|
||||
if (CrewEditable)
|
||||
{
|
||||
var crew = new MenuItem { Header = App.Loc["CrewLabel"] };
|
||||
foreach (var (type, key) in CrewChoices.All)
|
||||
|
||||
@@ -280,8 +280,8 @@ public sealed class VehicleDetails
|
||||
};
|
||||
driver.MinWidth = 0;
|
||||
driver.HorizontalAlignment = HorizontalAlignment.Stretch;
|
||||
// Shown but not changed in the standard mode: who drives is for the advanced one.
|
||||
driver.IsEnabled = AppServices.Current.Settings.AdvancedSettings;
|
||||
// Shown but not changed in the standard mode in the simulator's own consists.
|
||||
driver.IsEnabled = TrainsetDisplay.CrewEditableIn(_consist.EditingTrainset, AppServices.Current.Settings);
|
||||
|
||||
var rows = new StackPanel();
|
||||
rows.Children.Add(Row(App.Loc["CrewLabel"], driver));
|
||||
|
||||
Reference in New Issue
Block a user