Skip to content

Commit

Permalink
feat: add open in heliosphere button
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-is-cute committed Mar 22, 2024
1 parent 870ff8d commit b791767
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 25 deletions.
60 changes: 35 additions & 25 deletions Ui/PenumbraWindowIntegration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,40 @@ internal void PreSettingsTabBarDraw(string directory, float width, float titleWi
return;
}

if (pkg.CoverImage is { } img) {
var maxHeight = width * this.Plugin.Config.Penumbra.ImageSize;
if (pkg.CoverImage is not { } img) {
return;
}

ImGuiHelper.ImageFullWidth(img, maxHeight, true);
var maxHeight = width * this.Plugin.Config.Penumbra.ImageSize;

if (ImGui.IsItemHovered()) {
ImGui.BeginTooltip();
using var endTooltip = new OnDispose(ImGui.EndTooltip);
ImGuiHelper.ImageFullWidth(img, maxHeight, true);

ImGui.TextUnformatted("Click to open this image. Hold right-click to zoom.");
}
if (ImGui.IsItemHovered()) {
ImGui.BeginTooltip();
using var endTooltip = new OnDispose(ImGui.EndTooltip);

if (ImGui.IsItemClicked(ImGuiMouseButton.Left)) {
Process.Start(new ProcessStartInfo(pkg.CoverImagePath) {
UseShellExecute = true,
});
}

if (ImGui.IsItemHovered() && ImGui.IsMouseDown(ImGuiMouseButton.Right)) {
var winSize = ImGuiHelpers.MainViewport.WorkSize;
var imgSize = new Vector2(img.Width, img.Height);
ImGui.TextUnformatted("Click to open this image. Hold right-click to zoom.");
}

if (imgSize.X > winSize.X || imgSize.Y > winSize.Y) {
var ratio = Math.Min(winSize.X / img.Width, winSize.Y / img.Height);
imgSize *= ratio;
}
if (ImGui.IsItemClicked(ImGuiMouseButton.Left)) {
Process.Start(new ProcessStartInfo(pkg.CoverImagePath) {
UseShellExecute = true,
});
}

var min = new Vector2(winSize.X / 2 - imgSize.X / 2, winSize.Y / 2 - imgSize.Y / 2);
var max = new Vector2(winSize.X / 2 + imgSize.X / 2, winSize.Y / 2 + imgSize.Y / 2);
if (ImGui.IsItemHovered() && ImGui.IsMouseDown(ImGuiMouseButton.Right)) {
var winSize = ImGuiHelpers.MainViewport.WorkSize;
var imgSize = new Vector2(img.Width, img.Height);

ImGui.GetForegroundDrawList().AddImage(img.ImGuiHandle, min, max);
if (imgSize.X > winSize.X || imgSize.Y > winSize.Y) {
var ratio = Math.Min(winSize.X / img.Width, winSize.Y / img.Height);
imgSize *= ratio;
}

var min = new Vector2(winSize.X / 2 - imgSize.X / 2, winSize.Y / 2 - imgSize.Y / 2);
var max = new Vector2(winSize.X / 2 + imgSize.X / 2, winSize.Y / 2 + imgSize.Y / 2);

ImGui.GetForegroundDrawList().AddImage(img.ImGuiHandle, min, max);
}
}

Expand All @@ -87,12 +89,20 @@ internal void PostEnabledDraw(string directory) {
return;
}

ImGui.Spacing();

var cursor = ImGui.GetCursorPos();
Widget.BeginFramedGroup("Heliosphere");
using (new OnDispose(() => Widget.EndFramedGroup())) {
if (ImGui.Button("Download updates")) {
if (ImGuiHelper.WideButton("Check for and download updates")) {
meta.DownloadUpdates(this.Plugin);
}

if (ImGuiHelper.WideButton("Open in Heliosphere")) {
this.Plugin.PluginUi.ForceOpen = PluginUi.Tab.Manager;
this.Plugin.PluginUi.ForceOpenVariant = meta.VariantId;
this.Plugin.PluginUi.Visible = true;
}
}

var afterCursor = ImGui.GetCursorPos();
Expand Down
1 change: 1 addition & 0 deletions Ui/PluginUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal enum Tab {

internal bool Visible;
internal Tab? ForceOpen;
internal Guid? ForceOpenVariant;

private Guard<List<IDrawable>> ToDraw { get; } = new([]);
private List<IDrawable> ToDispose { get; } = [];
Expand Down
15 changes: 15 additions & 0 deletions Ui/Tabs/Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal class Manager : IDisposable {
private bool _downloadingUpdates;
private bool _checkingForUpdates;
private string _filter = string.Empty;
private bool _forced;

internal Manager(Plugin plugin) {
this.Plugin = plugin;
Expand Down Expand Up @@ -85,6 +86,11 @@ internal void Draw() {
}

ImGui.EndTabItem();

if (this._forced) {
this._forced = false;
this.Ui.ForceOpenVariant = null;
}
}

private async Task GetInfo() {
Expand Down Expand Up @@ -193,10 +199,19 @@ private void DrawPackageList(Dictionary<Guid, IVariantInfo> allInfo) {
(float) scanned / toScan,
$"Scanning - {scanned} / {toScan}"
);
} else {
this._forced = true;
}

var lower = this._filter.ToLowerInvariant();
foreach (var (pkgId, pkg) in this.Plugin.State.InstalledNoBlock.OrderBy(entry => entry.Value.Name)) {
if (this.Ui.ForceOpenVariant is { } forceVariant && pkg.Variants.Any(v => v.VariantId == forceVariant)) {
this._selected = pkgId;
this._selectedVariant = forceVariant;

ImGui.SetScrollHereY(0.5f);
}

if (!pkg.Name.ToLowerInvariant().Contains(lower)) {
continue;
}
Expand Down
10 changes: 10 additions & 0 deletions Util/ImGuiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ internal static bool CentredWideButton(string label) {
return ImGui.Button(label, new Vector2(buttonWidth, 0));
}

internal static bool WideButton(string label) {
var avail = ImGui.GetContentRegionAvail().X;
var textSize = ImGui.CalcTextSize(label).X;

var buttonSizeBase = Math.Max(avail / 2, textSize);
var buttonWidth = buttonSizeBase + ImGui.GetStyle().FramePadding.X * 2;

return ImGui.Button(label, new Vector2(buttonWidth, 0));
}

internal static void FullWidthProgressBar(float ratio, string? overlay = null) {
if (overlay == null) {
ImGui.ProgressBar(
Expand Down

0 comments on commit b791767

Please sign in to comment.