Skip to content

Commit

Permalink
Linux-related fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GrimMaple committed Sep 13, 2024
1 parent 47040da commit da98c19
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 17 deletions.
61 changes: 61 additions & 0 deletions SpeedTool/Platform/Linux/Fonts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Diagnostics;
using System.Runtime.Versioning;

namespace SpeedTool.Platform.Linux;

[SupportedOSPlatform("Linux")]
public sealed class Fonts
{
public static string DefaultFont
{
get
{
if(!loaded)
Load();

var str = fonts.Where(x => x.Contains("DroidSans.ttf")).FirstOrDefault();
return str == null ? "" : str;
}
}

public static string DefaultCJKFont
{
get
{
if(!loaded)
Load();

var str = fonts.Where(x => x.Contains("DroidSansJapanese.ttf")).FirstOrDefault();
return str == null ? "" : str;
}
}

private static void Load()
{
using(var proc = new Process())
{
loaded = true;
proc.StartInfo = new()
{
FileName = "fc-list",
Arguments = ": file",
RedirectStandardOutput = true
};

if(!proc.Start())
return;

fonts = proc.StandardOutput.ReadToEnd()
.Split("\n")
.Where(x => x.Length > 0)
.Select(x => x.Substring(0, x.Length - 2))
.Where(x => x.EndsWith(".ttf"))
.ToArray();

proc.WaitForExit();
}
}

private static string[] fonts = [];
private static bool loaded = false;
}
21 changes: 6 additions & 15 deletions SpeedTool/Platform/Linux/UniversalFileDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ private void DrawWindow()
}
}



ImGui.Text(directory.CurrentPath);

if (ImGui.BeginListBox("", new Vector2(ImGui.GetWindowWidth() * 1f, ImGui.GetWindowHeight() * 0.6f)))
Expand Down Expand Up @@ -94,16 +92,9 @@ private void DrawWindow()
}
ImGui.EndListBox();




ImGui.InputText("", ref fileName, 255);

ImGui.SameLine();


ImGui.SameLine();


if (operationMode == DialogOperation.Open)
{
if (ImGui.Button("Open", new Vector2(ImGui.GetWindowWidth() * 0.3f, ImGui.GetWindowHeight() * 0.05f)))
Expand All @@ -122,15 +113,13 @@ private void DrawWindow()
SaveFile(path => onLoad(path));
}
}

if (ImGui.Button("Cancel", new Vector2(ImGui.GetWindowWidth() * 0.4f, ImGui.GetWindowHeight() * 0.05f)))
{
Close();
}
}



public void OpenFolder(Action<string> onOpen)
{
if (Directory.Exists(selectedDirectory))
Expand Down Expand Up @@ -162,14 +151,16 @@ public void SaveFile(Action<string> onSave)
{
onSave?.Invoke(fullPath);
}

Close();
}

private readonly DialogOperation operationMode;

private string fileName = "";

private Action<string> onLoad;

private string? selectedDirectory = AppContext.BaseDirectory;

private bool isfolderSelected;
Expand Down
7 changes: 6 additions & 1 deletion SpeedTool/Platform/Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace SpeedTool.Platform;
using SpeedTool.Platform.EventsArgs;
using StbImageSharp;
using Silk.NET.Core;
using System.Runtime.InteropServices;
using SpeedTool.Platform.Linux;

public class Window : IDisposable
{
Expand All @@ -35,7 +37,10 @@ public Window(WindowOptions options, Sizes sz)
images = new Images(gl!);
controller = new ImGuiController(gl, window, input, () => {
LoadDefaultFont();
LoadFontEx(Environment.GetFolderPath(Environment.SpecialFolder.Fonts) + "\\segoeui.ttf", Environment.GetFolderPath(Environment.SpecialFolder.Fonts) + "\\meiryo.ttc", 22, "UI");
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
LoadFontEx(Environment.GetFolderPath(Environment.SpecialFolder.Fonts) + "\\segoeui.ttf", Environment.GetFolderPath(Environment.SpecialFolder.Fonts) + "\\meiryo.ttc", 22, "UI");
else if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
LoadFontEx(Fonts.DefaultFont, Fonts.DefaultCJKFont, 22, "UI");
var stream = GetType().Assembly.GetManifestResourceStream(ICON_RESOURCE_NAME)!;
var img = ImageResult.FromStream(stream);
var rawImg = new RawImage(img.Width, img.Height, new(img.Data));
Expand Down
7 changes: 6 additions & 1 deletion SpeedTool/Windows/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using SpeedTool.Global;
using SpeedTool.Global.Definitions;
using SpeedTool.Splits;
using System.Runtime.InteropServices;
using SpeedTool.Platform.Linux;

namespace SpeedTool.Windows;

Expand Down Expand Up @@ -53,7 +55,10 @@ protected override void OnAfterUI(double dt)

protected override void OnLoad()
{
LoadFontEx(Environment.GetFolderPath(Environment.SpecialFolder.Fonts) + "\\segoeui.ttf", Environment.GetFolderPath(Environment.SpecialFolder.Fonts) + "\\meiryo.ttc", 42, "Main");
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
LoadFontEx(Environment.GetFolderPath(Environment.SpecialFolder.Fonts) + "\\segoeui.ttf", Environment.GetFolderPath(Environment.SpecialFolder.Fonts) + "\\meiryo.ttc", 42, "Main");
else if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
LoadFontEx(Fonts.DefaultFont, Fonts.DefaultCJKFont, 42, "Main");
}

protected override void OnUI(double dt)
Expand Down

0 comments on commit da98c19

Please sign in to comment.