-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathConsolonia.Controls
141 lines (122 loc) · 5.58 KB
/
Consolonia.Controls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Templates;
using Avalonia.Platform;
using Consolonia.Core.Controls;
using Consolonia.Core.Drawing;
using Consolonia.Core.Drawing.PixelBufferImplementation;
using Consolonia.Core.Drawing.PixelBufferImplementation.EgaConsoleColor;
using Consolonia.Core.Infrastructure;
// ReSharper disable UnusedMember.Global //todo: how to disable it for public methods?
// ReSharper disable CheckNamespace
// ReSharper disable MemberCanBePrivate.Global
#nullable enable
namespace Consolonia
{
public static class ApplicationStartup
{
public static void StartConsolonia<TApp>(params string[] args) where TApp : Application, new()
{
StartConsolonia<TApp>(new DefaultNetConsole(), new EgaConsoleColorMode(), args);
}
public static void StartConsolonia<TApp>(IConsole console, IConsoleColorMode consoleColorMode,
params string[] args) where TApp : Application, new()
{
ConsoloniaLifetime lifetime = BuildLifetime<TApp>(console, consoleColorMode, args);
lifetime.Start(args);
}
public static AppBuilder UseStandardConsole(this AppBuilder builder)
{
return builder.UseConsole(new DefaultNetConsole()).UseConsoleColorMode(new EgaConsoleColorMode());
}
public static AppBuilder UseConsole(this AppBuilder builder, IConsole console)
{
return builder.With(console)
.With<IConsoleOutput>(console);
}
public static AppBuilder UseConsoleColorMode(this AppBuilder builder, IConsoleColorMode consoleColorMode)
{
return builder.With(consoleColorMode);
}
public static AppBuilder UseConsolonia(this AppBuilder builder)
{
return builder
.UseStandardRuntimePlatformSubsystem()
.UseWindowingSubsystem(() => new ConsoloniaPlatform().Initialize(), nameof(ConsoloniaPlatform))
.UseRenderingSubsystem(() =>
{
var consoloniaRenderInterface = new ConsoloniaRenderInterface();
AvaloniaLocator.CurrentMutable
.Bind<IPlatformRenderInterface>().ToConstant(consoloniaRenderInterface);
}, nameof(ConsoloniaRenderInterface));
}
public static ConsoloniaLifetime BuildLifetime<TApp>(IConsole console,
IConsoleColorMode consoleColorMode, string[] args)
where TApp : Application, new()
{
AppBuilder consoloniaAppBuilder = AppBuilder.Configure<TApp>()
.UseConsole(console)
.UseConsolonia()
.UseConsoleColorMode(consoleColorMode)
.LogToException();
return CreateLifetime(consoloniaAppBuilder, args);
}
/// <summary>
/// Shuts down the application with the specified exit code.
/// </summary>
/// <param name="lifetime">The application lifetime.</param>
/// <param name="exitCode">The exit code to use.</param>
/// <exception cref="InvalidOperationException">Thrown when the lifetime does not support controlled shutdown.</exception>
public static void Shutdown(this IApplicationLifetime lifetime, int exitCode = 0)
{
if (lifetime is IControlledApplicationLifetime controlledLifetime)
controlledLifetime.Shutdown(exitCode);
else
throw new InvalidOperationException("The lifetime does not support controlled shutdown.");
}
/// <summary>
/// Shuts down the application with the specified exit code.
/// </summary>
/// <param name="lifetime">The application lifetime.</param>
/// <param name="exitCode">The exit code to use.</param>
/// <exception cref="InvalidOperationException">Thrown when the lifetime does not support controlled shutdown.</exception>
public static void TryShutdown(this IApplicationLifetime lifetime, int exitCode = 0)
{
if (lifetime is IControlledApplicationLifetime controlledLifetime)
controlledLifetime.TryShutdown(exitCode);
else
throw new InvalidOperationException("The lifetime does not support controlled shutdown.");
}
private static ConsoloniaLifetime CreateLifetime(AppBuilder builder, string[] args)
{
var lifetime = new ConsoloniaLifetime
{
Args = args
};
builder.SetupWithLifetime(lifetime);
// Application has been instantiated here.
// We need to initialize it
// override AccessText to use ConsoloniaAccessText as default ContentPresenter for unknown data types (aka string)
Application.Current!.DataTemplates.Add(new FuncDataTemplate<object>(
(_, _) =>
{
var result = new ConsoloniaAccessText();
// ReSharper disable AccessToStaticMemberViaDerivedType
result.Bind(TextBlock.TextProperty,
result.GetBindingObservable(Control.DataContextProperty, x => x?.ToString()));
return result;
},
true)
);
return lifetime;
}
public static int StartWithConsoleLifetime(
this AppBuilder builder, string[] args)
{
ConsoloniaLifetime lifetime = CreateLifetime(builder, args);
return lifetime.Start(args);
}
}
}