Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Terminal.Gui
Submodule Terminal.Gui updated 199 files
4 changes: 0 additions & 4 deletions smoc.Tests/Fakes/FakeMainWindow.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Smoc.Ui;
using Smoc.Ui.Drawing;
using Smoc.Ui.Models;
using Terminal.Gui.App;

Expand All @@ -13,9 +12,6 @@ public class FakeMainWindow : IMainWindow {
/// <inheritdoc/>
public IApplication? App { get; } = FakeApplication.New();

/// <inheritdoc/>
public ISixelDriver SixelDriver { get; set; } = new FakeSixelDriver();

/// <inheritdoc/>
public Mode CurrentMode { get; set; }

Expand Down
37 changes: 0 additions & 37 deletions smoc.Tests/Fakes/FakeSixelDriver.cs

This file was deleted.

15 changes: 15 additions & 0 deletions smoc.Tests/TestInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Runtime.CompilerServices;

/// <summary>
/// Initializes the test environment.
/// </summary>
internal static class TestInitializer {
/// <summary>
/// Sets up the test environment.
/// </summary>
[ModuleInitializer]
public static void SetupEnvironment() {
// Disable real driver IO to prevent terminal.gui from opening a real terminal.
Environment.SetEnvironmentVariable("DisableRealDriverIO", "1");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
NowPlayingBarTest.OnSongChanged_UpdatesSongDetails_0:
┌╌╌╌╌┐ OnSongChangedUpdatesSongDetails
?? Radiohead volume: 0%
└╌╌╌╌┘ --:-- --:--
OnSongChangedUpdatesSongDetails
?? Radiohead volume: 0%
--:-- --:--



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ NowPlayingViewTest.InitialState_ShowsEmptyNowPlayingInfo_0:


┌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┐
┆ ?? ┆
┆ ┆
┆ ┆
┆ ┆
┆ ┆
┆ ?? ┆
┆ ┆
┆ ┆
┆ ┆
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ NowPlayingViewTest.InitialState_ShowsEmptyNowPlayingInfo_Tall_0:


┌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┐
┆ ?? ┆
┆ ┆
┆ ┆
┆ ┆
Expand All @@ -15,6 +14,7 @@ NowPlayingViewTest.InitialState_ShowsEmptyNowPlayingInfo_Tall_0:
┆ ┆
┆ ┆
┆ ┆
┆ ?? ┆
┆ ┆
┆ ┆
┆ ┆
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ NowPlayingViewTest.InitialState_ShowsEmptyNowPlayingInfo_Wide_0:


┌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┐
┆ ?? ┆
┆ ┆
┆ ┆
┆ ┆
┆ ┆
┆ ?? ┆
┆ ┆
┆ ┆
┆ ┆
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ NowPlayingViewTest.OnPositionChanged_UpdatesPosition_0:


┌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┐
┆ ?? ┆
┆ ┆
┆ ┆
┆ ┆
┆ ┆
┆ ?? ┆
┆ ┆
┆ ┆
┆ ┆
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ NowPlayingViewTest.OnPositionChanged_UpdatesPosition_MultipleTimes_0:


┌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┐
┆ ?? ┆
┆ ┆
┆ ┆
┆ ┆
┆ ┆
┆ ?? ┆
┆ ┆
┆ ┆
┆ ┆
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ NowPlayingViewTest.OnSongChanged_SongIsNull_UpdatesSongDetails_0:


┌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┐
┆ ?? ┆
┆ ┆
┆ ┆
┆ ┆
┆ ┆
┆ ?? ┆
┆ ┆
┆ ┆
┆ ┆
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ NowPlayingViewTest.OnSongChanged_UpdatesSongDetails_0:



┌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┐
??
└╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┘
??

OnSongChangedUpdatesSongDetails
Radiohead
Expand Down
5 changes: 1 addition & 4 deletions smoc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Smoc.Streaming.Subsonic;
using Smoc.Streaming.YouTubeMusic;
using Smoc.Ui;
using Smoc.Ui.Drawing;
using Terminal.Gui;
using Terminal.Gui.App;
using Terminal.Gui.Configuration;
Expand Down Expand Up @@ -62,9 +61,7 @@ public static async Task Main(string[] args) {
application.Mouse.IsMouseDisabled = true;
VimKeyBindings.AddNavigationKeyBindings(application.Keyboard.KeyBindings);
IStreamingClient streamingClient = CreateStreamingClient();
var sixelDriver = new SixelDriver(application);
sixelDriver.Initialize();
using var window = new MainWindow(streamingClient, sixelDriver);
using var window = new MainWindow(streamingClient);
try {
application.Run(window, (e) => {
Logging.Error(e.ToString());
Expand Down
74 changes: 33 additions & 41 deletions smoc/Ui/Components/SixelImageView.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using Terminal.Gui.Drawing;
using Terminal.Gui.ViewBase;
using Terminal.Gui.App;
using Terminal.Gui.Views;
using Color = Terminal.Gui.Drawing.Color;

namespace Smoc.Ui.Components;

/// <summary>
/// A view that renders images using Sixel graphics sequences.
/// </summary>
public sealed class SixelImageView : View {
private readonly IMainWindow _mainWindow;
public sealed class SixelImageView : ImageView {
private Image<Rgba32>? _image;
private SixelToRender? _sixelToRender;
private CancellationTokenSource? _cancellationTokenSource;

/// <summary>
/// Initializes a new instance of the <see cref="SixelImageView"/> class.
/// </summary>
/// <param name="mainWindow">The main window.</param>
/// <param name="image">The initial image to display.</param>
public SixelImageView(IMainWindow mainWindow, Image<Rgba32>? image = null) {
_mainWindow = mainWindow;
public SixelImageView(Image<Rgba32>? image = null) {
_image = image;
}

Expand All @@ -34,7 +31,7 @@ public void ClearImage() {
}

_image = null;
_sixelToRender = null;
Image = null;
SetNeedsDraw();
}

Expand All @@ -44,50 +41,45 @@ public void ClearImage() {
/// <param name="image">The image to display.</param>
public void SetImage(Image<Rgba32> image) {
_image = image;
_sixelToRender = null;
UpdateSixelData();
SetNeedsDraw();
}

protected override void OnFrameChanged(in System.Drawing.Rectangle frame) {
base.OnFrameChanged(frame);
UpdateSixelData();
Comment thread
mrazza marked this conversation as resolved.
SetNeedsDraw();
}

protected override bool OnDrawingContent(DrawContext? context) {
base.OnDrawingContent(context);

if (_sixelToRender is not null) {
_mainWindow.SixelDriver.EnqueueSixel(_sixelToRender);
context?.AddDrawnRectangle(GetRenderableArea());
return true;
}

return false;
}
private void UpdateSixelData() {
_cancellationTokenSource?.Cancel();
_cancellationTokenSource = new CancellationTokenSource();

var token = _cancellationTokenSource.Token;
Task.Run(() => {
Color[,]? data = null;
try {
data = GenerateSixelData();
} catch (Exception ex) {
Logging.Warning($"Failed to render album art: {ex.Message}");
return;
}

private System.Drawing.Rectangle GetRenderableArea() {
var frame = FrameToScreen();
return new(
frame.X + (Margin?.Thickness.Left ?? 0),
frame.Y + (Margin?.Thickness.Top ?? 0),
frame.Width - (Margin?.Thickness.Horizontal ?? 0),
frame.Height - (Margin?.Thickness.Vertical ?? 0));
App?.Invoke(() => {
if (token.IsCancellationRequested) {
return;
}
Image = data;
SetNeedsDraw();
});
}, token);
}

private void UpdateSixelData() {
if (_image is null || !_mainWindow.SixelDriver.IsSupported) {
return;
private Color[,] GenerateSixelData() {
if (_image is null || App?.Driver?.SixelSupport is not { IsSupported: true }) {
throw new InvalidOperationException("Sixel not supported.");
}

var boundsRect = GetRenderableArea();
var resizedImage = _image.Clone(
i => i.Resize(boundsRect.Width * _mainWindow.SixelDriver.Resolution!.Value.Width, boundsRect.Height * _mainWindow.SixelDriver.Resolution!.Value.Height));
_sixelToRender = new SixelToRender() {
SixelData = _mainWindow.SixelDriver.EncodeSixel(ConvertToColorArray(resizedImage)),
ScreenPosition = new System.Drawing.Point(boundsRect.X, boundsRect.Y)
};
var targetSize = FitImageInViewportInPixels(new(_image.Width, _image.Height));
var resizedImage = _image.Clone(i => i.Resize(targetSize.Width, targetSize.Height));
return ConvertToColorArray(resizedImage);
}

private static Color[,] ConvertToColorArray(Image<Rgba32> image) {
Expand Down
Loading
Loading