Skip to content

Commit

Permalink
Core: better ApplicationId for MauiGtkApplication (#24)
Browse files Browse the repository at this point in the history
Improve algorithm for ApplicationId property in
MauiGtkApplication class. Filter out invalid chars. For Name
property, which is used as last element in ApplicationId, make
default value be executable's filename.
  • Loading branch information
webwarrior-ws authored and knocte committed May 16, 2024
1 parent bc8eb6b commit d92687b
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/Core/src/Platform/Gtk/MauiGtkApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,42 @@
using Microsoft.Maui.LifecycleEvents;
using Gtk;
using Microsoft.Maui.Dispatching;
using System.Text.RegularExpressions;

namespace Microsoft.Maui
{
public abstract class MauiGtkApplication : IPlatformApplication
{
protected abstract MauiApp CreateMauiApp();

static readonly Regex InvalidGtkApplicationIdElementCharRegex = new Regex("[^A-Za-z0-9_\\-]");

// https://docs.gtk.org/gio/type_func.Application.id_is_valid.html
// TODO: find a better algo for id
public virtual string ApplicationId => $"{typeof(MauiGtkApplication).Namespace}.{nameof(MauiGtkApplication)}.{Name}".PadRight(255, ' ').Substring(0, 255).Trim();
public virtual string ApplicationId
{
get
{
var name = InvalidGtkApplicationIdElementCharRegex.Replace(Name!, "_");
if (name.Length == 0 || (name[0] >= '0' || name[0] <= '9'))
name = "_" + name;
return $"{typeof(MauiGtkApplication).Namespace}.{nameof(MauiGtkApplication)}.{name}".PadRight(255, ' ').Substring(0, 255).Trim();
}
}

string? _name;

// https://docs.gtk.org/gio/type_func.Application.id_is_valid.html
public string? Name
{
get => _name ??= $"A{Guid.NewGuid()}";
get
{
if (_name is null)
{
var exeName = Environment.GetCommandLineArgs()[0];
_name = string.IsNullOrEmpty(exeName) ? "Unknown" : System.IO.Path.GetFileName(exeName);
}
return _name;
}
set { _name = value; }
}

Expand Down

0 comments on commit d92687b

Please sign in to comment.