efl/src/bindings/mono/efl_mono/efl_csharp_application.cs

209 lines
6.3 KiB
C#
Raw Normal View History

/*
* Copyright 2019 by its authors. See AUTHORS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Runtime.InteropServices;
using System.Threading;
using static Efl.UnsafeNativeMethods;
namespace Efl
{
namespace Csharp
{
/// <summary>The components to be initialized.
/// <para>Since EFL 1.23.</para>
/// </summary>
public enum Components
{
///<summary>Basic components: Eina, Eo, Ecore, Evas and DBus.
/// <para>Since EFL 1.23.</para>
/// </summary>
Basic,
///<summary>The same components of <see cref="Efl.Csharp.Components.Basic"/>
/// and the Elementary widget toolkit.
/// <para>Since EFL 1.23.</para>
/// </summary>
Ui,
}
/// <summary>
/// This represents the entry point for the EFL framework
/// You can use this class to implement the 4 abstract methods which will then be called accordingly
/// All subsystems of efl are booted up correctly when the abstract methods of this class are called.
/// <para>Since EFL 1.23.</para>
/// </summary>
/// <remarks>
/// Calls to efl outside those efl-callbacks or outside the mainloop are not allowed and will lead to issues
/// </remarks>
///
/// <example>
/// UserApp is the class that implements the Application abstract
/// <code>
/// public static void Main()
/// {
/// UserApp editor = new UserApp();
/// editor.Launch(editor);
/// }
/// </code>
/// </example>
public abstract class Application
{
//the initializied components
private static Components initComponent;
//what follows are 3 private functions to boot up the internals of efl
private static void Init(Efl.Csharp.Components component)
{
Eina.Config.Init();
Efl.Eo.Config.Init();
ecore_init();
ecore_init_ex(0, IntPtr.Zero);
evas_init();
eldbus.Config.Init();
if (component == Components.Ui)
{
// TODO Support elm command line arguments
#if WIN32 // Not a native define, we define it in our build system
// Ecore_Win32 uses OleInitialize, which requires single thread apartments
if (System.Threading.Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
{
throw new InvalidOperationException("UI Applications require STAThreadAttribute in Main()");
}
#endif
elm_init(0, IntPtr.Zero);
Efl.Ui.Win.ExitOnAllWindowsClosed = new Eina.Value(0);
}
initComponent = component;
}
private static void Shutdown()
{
// Try to cleanup everything before actually shutting down.
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
if (initComponent == Components.Ui)
{
elm_shutdown();
}
eldbus.Config.Shutdown();
evas_shutdown();
ecore_shutdown();
Efl.Eo.Config.Shutdown();
Eina.Config.Shutdown();
}
/// <summary>
/// Called when the application is started. Arguments from the command line are passed here.
/// <para>Since EFL 1.23.</para>
/// </summary>
/// <param name="args">Arguments passed from command line.</param>
csharp: fix Eina_Stringshare support in containers for manual and generated API Summary: Both C strings and eina stringshares are bound as regular strings in EFL#, as working directly with these types would demand unnecessary hassle from the user viewpoint. But for eina containers this distinction is important, and since C# generics do not provide a convenient way of dealing with the same type requiring a different management based on some other condition (at least not without compromising the usability for other types), we added a simple `System.String` wrapper named `Eina.Stringshare` that works as a placeholder for signaling this distinction. Working with this class should be transparent in most use cases because it converts to and from `System.String` implicitly. It also implements equality/inequality methods for easier comparison with strings and other stringshare objects. Add new methods and a new container element trait for dealing specifically with `Eina_Stringshare` elements. Adapt eolian_mono to identify and generate the proper placeholder in methods that require stringshare containers. Remove some direct uses of DllImport-ed functions in favor of more flexible manual binding methods. Move `Eina.Stringshare` DllImport directives to a static class named `NativeMethods`, in accordance with the code design warning CA1060. Also add a TODO comment to move all other DllImport directives to this class. Change parameter of the method `Efl.Csharp.Application.OnInitialize` from `Eina.Array<System.String>` to `string[]`. This will make this API more similar with the default C# way of receiving command line arguments. Add tests for containers storing stringshare elements. Reviewers: felipealmeida, lauromoura, segfaultxavi, bu5hm4n Reviewed By: lauromoura Subscribers: cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D9178
2019-06-28 06:40:52 -07:00
protected abstract void OnInitialize(string[] args);
/// <summary>
/// Arguments are passed here, Additional calls to this function may occure,
/// but then the initialization flag is set to false.
/// <para>Since EFL 1.23.</para>
/// </summary>
/// <remarks>
/// When Initialize is true then OnInitialize is also called
/// </remarks>
/// <param name="args">Argsuments passed from command line</param>
protected virtual void OnArguments(Efl.LoopArguments args)
{
}
/// <summary>
/// Called when the application is not going to be displayed, or is not used by a user for some time.
/// <para>Since EFL 1.23.</para>
/// </summary>
protected virtual void OnPause()
{
}
/// <summary>
/// Called before an application is used again after a call to OnPause().
/// <para>Since EFL 1.23.</para>
/// </summary>
protected virtual void OnResume()
{
}
/// <summary>
/// Called before starting the shutdown of the application.
/// <para>Since EFL 1.23.</para>
/// </summary>
protected virtual void OnTerminate()
{
}
/// <summary>
/// This function initializices everything in EFL and runs your application.
/// This call will result in a call to OnInitialize(), which you application should override.
/// <para>Since EFL 1.23.</para>
/// </summary>
/// <param name="components">The <see cref="Efl.Csharp.Components" /> to run the application.</param>
public void Launch(Efl.Csharp.Components components = Components.Ui)
{
Init(components);
Efl.App app = Efl.App.AppMain;
csharp: fix Eina_Stringshare support in containers for manual and generated API Summary: Both C strings and eina stringshares are bound as regular strings in EFL#, as working directly with these types would demand unnecessary hassle from the user viewpoint. But for eina containers this distinction is important, and since C# generics do not provide a convenient way of dealing with the same type requiring a different management based on some other condition (at least not without compromising the usability for other types), we added a simple `System.String` wrapper named `Eina.Stringshare` that works as a placeholder for signaling this distinction. Working with this class should be transparent in most use cases because it converts to and from `System.String` implicitly. It also implements equality/inequality methods for easier comparison with strings and other stringshare objects. Add new methods and a new container element trait for dealing specifically with `Eina_Stringshare` elements. Adapt eolian_mono to identify and generate the proper placeholder in methods that require stringshare containers. Remove some direct uses of DllImport-ed functions in favor of more flexible manual binding methods. Move `Eina.Stringshare` DllImport directives to a static class named `NativeMethods`, in accordance with the code design warning CA1060. Also add a TODO comment to move all other DllImport directives to this class. Change parameter of the method `Efl.Csharp.Application.OnInitialize` from `Eina.Array<System.String>` to `string[]`. This will make this API more similar with the default C# way of receiving command line arguments. Add tests for containers storing stringshare elements. Reviewers: felipealmeida, lauromoura, segfaultxavi, bu5hm4n Reviewed By: lauromoura Subscribers: cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D9178
2019-06-28 06:40:52 -07:00
var command_line = new Eina.Array<Eina.Stringshare>();
command_line.Append(Array.ConvertAll(Environment.GetCommandLineArgs(), s => (Eina.Stringshare)s));
#if EFL_BETA
app.SetCommandArray(command_line);
#endif
app.ArgumentsEvent += (object sender, LoopArgumentsEventArgs evt) =>
{
if (evt.arg.Initialization)
{
csharp: fix Eina_Stringshare support in containers for manual and generated API Summary: Both C strings and eina stringshares are bound as regular strings in EFL#, as working directly with these types would demand unnecessary hassle from the user viewpoint. But for eina containers this distinction is important, and since C# generics do not provide a convenient way of dealing with the same type requiring a different management based on some other condition (at least not without compromising the usability for other types), we added a simple `System.String` wrapper named `Eina.Stringshare` that works as a placeholder for signaling this distinction. Working with this class should be transparent in most use cases because it converts to and from `System.String` implicitly. It also implements equality/inequality methods for easier comparison with strings and other stringshare objects. Add new methods and a new container element trait for dealing specifically with `Eina_Stringshare` elements. Adapt eolian_mono to identify and generate the proper placeholder in methods that require stringshare containers. Remove some direct uses of DllImport-ed functions in favor of more flexible manual binding methods. Move `Eina.Stringshare` DllImport directives to a static class named `NativeMethods`, in accordance with the code design warning CA1060. Also add a TODO comment to move all other DllImport directives to this class. Change parameter of the method `Efl.Csharp.Application.OnInitialize` from `Eina.Array<System.String>` to `string[]`. This will make this API more similar with the default C# way of receiving command line arguments. Add tests for containers storing stringshare elements. Reviewers: felipealmeida, lauromoura, segfaultxavi, bu5hm4n Reviewed By: lauromoura Subscribers: cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D9178
2019-06-28 06:40:52 -07:00
var evtArgv = evt.arg.Argv;
int n = evtArgv.Length;
var argv = new string[n];
for (int i = 0; i < n; ++i)
{
argv[i] = evtArgv[i];
}
OnInitialize(argv);
}
OnArguments(evt.arg);
};
app.PauseEvent += (object sender, EventArgs e) =>
{
OnPause();
};
app.ResumeEvent += (object sender, EventArgs e) =>
{
OnResume();
};
app.TerminateEvent += (object sender, EventArgs e) =>
{
OnTerminate();
};
app.Begin();
Shutdown();
}
}
}
}