Skip to content

Commit

Permalink
imp - Re-made screensaver selection as interactive TUI
Browse files Browse the repository at this point in the history
---

We've used Terminaux's interactive TUI feature instead of the selection style to make using this feature easier than before.

---

Type: imp
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Feb 26, 2024
1 parent 667036d commit 47409ae
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 25 deletions.
78 changes: 78 additions & 0 deletions public/Nitrocid/Misc/Interactives/ScreensaverTui.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// Nitrocid KS Copyright (C) 2018-2024 Aptivi
//
// This file is part of Nitrocid KS
//
// Nitrocid KS is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Nitrocid KS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using Nitrocid.Languages;
using Nitrocid.Misc.Screensaver;
using Terminaux.Inputs.Interactive;
using Terminaux.Reader;
using Textify.General;

namespace Nitrocid.Misc.Interactives
{
internal class ScreensaverTui : BaseInteractiveTui<string>, IInteractiveTui<string>
{
public override List<InteractiveTuiBinding> Bindings { get; set; } =
[
// Operations
new InteractiveTuiBinding("Preview", ConsoleKey.Enter,
(saver, _) => PressAndBailHelper((string)saver))
];

/// <inheritdoc/>
public override IEnumerable<string> PrimaryDataSource =>
ScreensaverManager.GetScreensaverNames();

/// <inheritdoc/>
public override string GetInfoFromItem(string item)
{
// Get an instance of the screensaver to grab its info from
var screensaver = ScreensaverManager.GetScreensaver(item);

// Generate the rendered text
string name = screensaver.ScreensaverName;
bool flashing = screensaver.ScreensaverContainsFlashingImages;

// Render them to the second pane
return
Translate.DoTranslation("Screensaver name") + $": {name}" + CharManager.NewLine +
Translate.DoTranslation("Screensaver contains flashing images") + $": {flashing}";
;
}

/// <inheritdoc/>
public override void RenderStatus(string item) =>
InteractiveTuiStatus.Status = item;

/// <inheritdoc/>
public override string GetEntryFromItem(string item) =>
item;

private static void PressAndBailHelper(string saver)
{
ScreensaverManager.ShowSavers(saver);
if (ScreensaverManager.inSaver)
{
TermReader.ReadKey();
ScreensaverDisplayer.BailFromScreensaver();
}
}
}
}
22 changes: 22 additions & 0 deletions public/Nitrocid/Misc/Screensaver/ScreensaverManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ public static string[] GetScreensaverNames()
return [.. savers];
}

/// <summary>
/// Gets the screensaver instance
/// </summary>
/// <param name="saver">Saver name</param>
/// <returns>An instance of <see cref="BaseScreensaver"/> that represents the selected screensaver</returns>
public static BaseScreensaver GetScreensaver(string saver)
{
// Check to see if there is a screensaver with this name
if (!IsScreensaverRegistered(saver))
{
DebugWriter.WriteDebug(DebugLevel.W, "{0} is not found.", saver);
throw new KernelException(KernelExceptionType.NoSuchScreensaver, Translate.DoTranslation("Screensaver {0} not found in database. Check the name and try again."), saver);
}

// Now, get the screensaver instance
var BaseSaver =
AddonSavers.TryGetValue(saver, out BaseScreensaver @base) ? @base :
CustomSavers.TryGetValue(saver, out BaseScreensaver customBase) ? customBase :
Screensavers[saver];
return BaseSaver;
}

/// <summary>
/// Shows the screensaver
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion public/Nitrocid/Nitrocid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<NitrocidModAPIVersionMajor>3.0.25</NitrocidModAPIVersionMajor>

<!-- Increment NitrocidModAPIVersionChangeset every time there is a breaking change or an API addition in the N-KS API. -->
<NitrocidModAPIVersionChangeset>433</NitrocidModAPIVersionChangeset>
<NitrocidModAPIVersionChangeset>434</NitrocidModAPIVersionChangeset>

<!-- To be installed to the file version -->
<NitrocidModAPIVersion>$(NitrocidModAPIVersionMajor).$(NitrocidModAPIVersionChangeset)</NitrocidModAPIVersion>
Expand Down
27 changes: 3 additions & 24 deletions public/Nitrocid/Shell/Shells/UESH/Commands/SaveScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
using System.Linq;
using Terminaux.Inputs;
using Terminaux.Reader;
using Terminaux.Inputs.Interactive;
using Nitrocid.Misc.Interactives;

namespace Nitrocid.Shell.Shells.UESH.Commands
{
Expand All @@ -42,30 +44,7 @@ public override int Execute(CommandParameters parameters, ref string variableVal
{
bool selectionMode = SwitchManager.ContainsSwitch(parameters.SwitchesList, "-select");
if (selectionMode)
{
// Get the names and build the choices
var saverNames = ScreensaverManager.GetScreensaverNames();
var saverNums = saverNames.Select((_, idx) => $"{idx + 1}").ToArray();
var saverChoices = InputChoiceTools.GetInputChoices(saverNums, saverNames);
var saverExit = new InputChoiceInfo[]
{
new($"{saverNames.Length + 1}", Translate.DoTranslation("Exit"))
};

// Main loop
while (true)
{
// Prompt for screensaver selection
int selected = SelectionStyle.PromptSelection(Translate.DoTranslation("Select a screensaver to showcase"), [.. saverChoices], saverExit);
if (selected == -1 || selected == saverNames.Length + 1)
break;

// Now, showcase the selected screensaver
string name = saverNames[selected - 1];
ScreensaverManager.ShowSavers(name);
PressAndBailHelper();
}
}
InteractiveTuiTools.OpenInteractiveTui(new ScreensaverTui());
else
{
if (parameters.ArgumentsList.Length != 0)
Expand Down

0 comments on commit 47409ae

Please sign in to comment.