Skip to content

Commit

Permalink
- Add chat notification back to AutoUpdate (#2146)
Browse files Browse the repository at this point in the history
- ImRaii UI elements in AutoUpdate tab
- Fix scoping in IconButton
  • Loading branch information
Infiziert90 authored Dec 19, 2024
1 parent f7ef68d commit 2e6cb6e
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 114 deletions.
19 changes: 12 additions & 7 deletions Dalamud/Configuration/Internal/DalamudConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ internal sealed class DalamudConfiguration : IInternalDisposableService
/// Gets or sets a value indicating whether or not docking should be globally enabled in ImGui.
/// </summary>
public bool IsDocking { get; set; }

/// <summary>
/// Gets or sets a value indicating whether or not plugin user interfaces should trigger sound effects.
/// This setting is effected by the in-game "System Sounds" option and volume.
Expand Down Expand Up @@ -484,10 +484,15 @@ public string EffectiveLanguage
public AutoUpdateBehavior? AutoUpdateBehavior { get; set; } = null;

/// <summary>
/// Gets or sets a value indicating whether or not users should be notified regularly about pending updates.
/// Gets or sets a value indicating whether users should be notified regularly about pending updates.
/// </summary>
public bool CheckPeriodicallyForUpdates { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether users should be notified about updates in chat.
/// </summary>
public bool SendUpdateNotificationToChat { get; set; } = false;

/// <summary>
/// Load a configuration from the provided path.
/// </summary>
Expand All @@ -504,7 +509,7 @@ public static DalamudConfiguration Load(string path, ReliableFileStorage fs)
{
deserialized =
JsonConvert.DeserializeObject<DalamudConfiguration>(text, SerializerSettings);

// If this reads as null, the file was empty, that's no good
if (deserialized == null)
throw new Exception("Read config was null.");
Expand All @@ -530,7 +535,7 @@ public static DalamudConfiguration Load(string path, ReliableFileStorage fs)
{
Log.Error(e, "Failed to set defaults for DalamudConfiguration");
}

return deserialized;
}

Expand All @@ -549,7 +554,7 @@ public void ForceSave()
{
this.Save();
}

/// <inheritdoc/>
void IInternalDisposableService.DisposeService()
{
Expand Down Expand Up @@ -595,14 +600,14 @@ private void SetDefaults()
this.ReduceMotions = winAnimEnabled == 0;
}
}

// Migrate old auto-update setting to new auto-update behavior
this.AutoUpdateBehavior ??= this.AutoUpdatePlugins
? Plugin.Internal.AutoUpdate.AutoUpdateBehavior.UpdateAll
: Plugin.Internal.AutoUpdate.AutoUpdateBehavior.OnlyNotify;
#pragma warning restore CS0618
}

private void Save()
{
ThreadSafety.AssertMainThread();
Expand Down
13 changes: 6 additions & 7 deletions Dalamud/Interface/Components/ImGuiComponents.IconButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,14 @@ public static bool IconButtonWithText(FontAwesomeIcon icon, string text, Vector4
/// <returns>Width.</returns>
public static float GetIconButtonWithTextWidth(FontAwesomeIcon icon, string text)
{
Vector2 iconSize;
using (ImRaii.PushFont(UiBuilder.IconFont))
{
var iconSize = ImGui.CalcTextSize(icon.ToIconString());

var textSize = ImGui.CalcTextSize(text);

var iconPadding = 3 * ImGuiHelpers.GlobalScale;

return iconSize.X + textSize.X + (ImGui.GetStyle().FramePadding.X * 2) + iconPadding;
iconSize = ImGui.CalcTextSize(icon.ToIconString());
}

var textSize = ImGui.CalcTextSize(text);
var iconPadding = 3 * ImGuiHelpers.GlobalScale;
return iconSize.X + textSize.X + (ImGui.GetStyle().FramePadding.X * 2) + iconPadding;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ internal static uint DrawPluginPicker(string id, ref string pickerSearch, Action
var pm = Service<PluginManager>.GetNullable();
if (pm == null)
return 0;

var addPluginToProfilePopupId = ImGui.GetID(id);
using var popup = ImRaii.Popup(id);

if (popup.Success)
{
var width = ImGuiHelpers.GlobalScale * 300;

ImGui.SetNextItemWidth(width);
ImGui.InputTextWithHint("###pluginPickerSearch", Locs.SearchHint, ref pickerSearch, 255);

var currentSearchString = pickerSearch;
if (ImGui.BeginListBox("###pluginPicker", new Vector2(width, width - 80)))

using var listBox = ImRaii.ListBox("###pluginPicker", new Vector2(width, width - 80));
if (listBox.Success)
{
// TODO: Plugin searching should be abstracted... installer and this should use the same search
var plugins = pm.InstalledPlugins.Where(
Expand All @@ -53,19 +55,15 @@ internal static uint DrawPluginPicker(string id, ref string pickerSearch, Action
currentSearchString,
StringComparison.InvariantCultureIgnoreCase)))
.Where(pluginFiltered ?? (_ => true));

foreach (var plugin in plugins)
{
using var disabled2 =
ImRaii.Disabled(pluginDisabled(plugin));

using var disabled2 = ImRaii.Disabled(pluginDisabled(plugin));
if (ImGui.Selectable($"{plugin.Manifest.Name}{(plugin is LocalDevPlugin ? "(dev plugin)" : string.Empty)}###selector{plugin.Manifest.InternalName}"))
{
onClicked(plugin);
}
}

ImGui.EndListBox();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ public class SettingsTabAutoUpdates : SettingsTab
{
private AutoUpdateBehavior behavior;
private bool checkPeriodically;
private bool chatNotification;
private string pickerSearch = string.Empty;
private List<AutoUpdatePreference> autoUpdatePreferences = [];
public override SettingsEntry[] Entries { get; } = Array.Empty<SettingsEntry>();

public override SettingsEntry[] Entries { get; } = [];

public override string Title => Loc.Localize("DalamudSettingsAutoUpdates", "Auto-Updates");

Expand All @@ -36,15 +37,15 @@ public override void Draw()
"Dalamud can update your plugins automatically, making sure that you always " +
"have the newest features and bug fixes. You can choose when and how auto-updates are run here."));
ImGuiHelpers.ScaledDummy(2);

ImGuiHelpers.SafeTextColoredWrapped(ImGuiColors.DalamudGrey, Loc.Localize("DalamudSettingsAutoUpdateDisclaimer1",
"You can always update your plugins manually by clicking the update button in the plugin list. " +
"You can also opt into updates for specific plugins by right-clicking them and selecting \"Always auto-update\"."));
ImGuiHelpers.SafeTextColoredWrapped(ImGuiColors.DalamudGrey, Loc.Localize("DalamudSettingsAutoUpdateDisclaimer2",
"Dalamud will only notify you about updates while you are idle."));

ImGuiHelpers.ScaledDummy(8);

ImGuiHelpers.SafeTextColoredWrapped(ImGuiColors.DalamudWhite, Loc.Localize("DalamudSettingsAutoUpdateBehavior",
"When the game starts..."));
var behaviorInt = (int)this.behavior;
Expand All @@ -62,46 +63,47 @@ public override void Draw()
"These updates are not reviewed by the Dalamud team and may contain malicious code.");
ImGuiHelpers.SafeTextColoredWrapped(ImGuiColors.DalamudOrange, warning);
}

ImGuiHelpers.ScaledDummy(8);


ImGui.Checkbox(Loc.Localize("DalamudSettingsAutoUpdateChatMessage", "Show notification about updates available in chat"), ref this.chatNotification);
ImGui.Checkbox(Loc.Localize("DalamudSettingsAutoUpdatePeriodically", "Periodically check for new updates while playing"), ref this.checkPeriodically);
ImGuiHelpers.SafeTextColoredWrapped(ImGuiColors.DalamudGrey, Loc.Localize("DalamudSettingsAutoUpdatePeriodicallyHint",
"Plugins won't update automatically after startup, you will only receive a notification while you are not actively playing."));

ImGuiHelpers.ScaledDummy(5);
ImGui.Separator();
ImGuiHelpers.ScaledDummy(5);

ImGuiHelpers.SafeTextColoredWrapped(ImGuiColors.DalamudWhite, Loc.Localize("DalamudSettingsAutoUpdateOptedIn",
"Per-plugin overrides"));

ImGuiHelpers.SafeTextColoredWrapped(ImGuiColors.DalamudWhite, Loc.Localize("DalamudSettingsAutoUpdateOverrideHint",
"Here, you can choose to receive or not to receive updates for specific plugins. " +
"This will override the settings above for the selected plugins."));

if (this.autoUpdatePreferences.Count == 0)
{
ImGuiHelpers.ScaledDummy(20);

using (ImRaii.PushColor(ImGuiCol.Text, ImGuiColors.DalamudGrey))
{
ImGuiHelpers.CenteredText(Loc.Localize("DalamudSettingsAutoUpdateOptedInHint2",
"You don't have auto-update rules for any plugins."));
}

ImGuiHelpers.ScaledDummy(2);
}
else
{
ImGuiHelpers.ScaledDummy(5);

var pic = Service<PluginImageCache>.Get();

var windowSize = ImGui.GetWindowSize();
var pluginLineHeight = 32 * ImGuiHelpers.GlobalScale;
Guid? wantRemovePluginGuid = null;

foreach (var preference in this.autoUpdatePreferences)
{
var pmPlugin = Service<PluginManager>.Get().InstalledPlugins
Expand All @@ -120,11 +122,12 @@ public override void Draw()
if (pmPlugin.IsDev)
{
ImGui.SetCursorPos(cursorBeforeIcon);
ImGui.PushStyleVar(ImGuiStyleVar.Alpha, 0.7f);
ImGui.Image(pic.DevPluginIcon.ImGuiHandle, new Vector2(pluginLineHeight));
ImGui.PopStyleVar();
using (ImRaii.PushStyle(ImGuiStyleVar.Alpha, 0.7f))
{
ImGui.Image(pic.DevPluginIcon.ImGuiHandle, new Vector2(pluginLineHeight));
}
}

ImGui.SameLine();

var text = $"{pmPlugin.Name}{(pmPlugin.IsDev ? " (dev plugin" : string.Empty)}";
Expand All @@ -147,7 +150,7 @@ public override void Draw()

ImGui.SetCursorPosY(ImGui.GetCursorPosY() + (pluginLineHeight / 2) - (textHeight.Y / 2));
ImGui.TextUnformatted(text);

ImGui.SetCursorPos(before);
}

Expand All @@ -166,19 +169,18 @@ string OptKindToString(AutoUpdatePreference.OptKind kind)
}

ImGui.SetNextItemWidth(ImGuiHelpers.GlobalScale * 250);
if (ImGui.BeginCombo(
$"###autoUpdateBehavior{preference.WorkingPluginId}",
OptKindToString(preference.Kind)))
using (var combo = ImRaii.Combo($"###autoUpdateBehavior{preference.WorkingPluginId}", OptKindToString(preference.Kind)))
{
foreach (var kind in Enum.GetValues<AutoUpdatePreference.OptKind>())
if (combo.Success)
{
if (ImGui.Selectable(OptKindToString(kind)))
foreach (var kind in Enum.GetValues<AutoUpdatePreference.OptKind>())
{
preference.Kind = kind;
if (ImGui.Selectable(OptKindToString(kind)))
{
preference.Kind = kind;
}
}
}

ImGui.EndCombo();
}

ImGui.SameLine();
Expand All @@ -193,7 +195,7 @@ string OptKindToString(AutoUpdatePreference.OptKind kind)
if (ImGui.IsItemHovered())
ImGui.SetTooltip(Loc.Localize("DalamudSettingsAutoUpdateOptInRemove", "Remove this override"));
}

if (wantRemovePluginGuid != null)
{
this.autoUpdatePreferences.RemoveAll(x => x.WorkingPluginId == wantRemovePluginGuid);
Expand All @@ -205,19 +207,19 @@ void OnPluginPicked(LocalPlugin plugin)
var id = plugin.EffectiveWorkingPluginId;
if (id == Guid.Empty)
throw new InvalidOperationException("Plugin ID is empty.");

this.autoUpdatePreferences.Add(new AutoUpdatePreference(id));
}

bool IsPluginDisabled(LocalPlugin plugin)
=> this.autoUpdatePreferences.Any(x => x.WorkingPluginId == plugin.EffectiveWorkingPluginId);

bool IsPluginFiltered(LocalPlugin plugin)
=> !plugin.IsDev;

var pickerId = DalamudComponents.DrawPluginPicker(
"###autoUpdatePicker", ref this.pickerSearch, OnPluginPicked, IsPluginDisabled, IsPluginFiltered);

const FontAwesomeIcon addButtonIcon = FontAwesomeIcon.Plus;
var addButtonText = Loc.Localize("DalamudSettingsAutoUpdateOptInAdd", "Add new override");
ImGuiHelpers.CenterCursorFor(ImGuiComponents.GetIconButtonWithTextWidth(addButtonIcon, addButtonText));
Expand All @@ -235,20 +237,22 @@ public override void Load()
var configuration = Service<DalamudConfiguration>.Get();

this.behavior = configuration.AutoUpdateBehavior ?? AutoUpdateBehavior.None;
this.chatNotification = configuration.SendUpdateNotificationToChat;
this.checkPeriodically = configuration.CheckPeriodicallyForUpdates;
this.autoUpdatePreferences = configuration.PluginAutoUpdatePreferences;

base.Load();
}

public override void Save()
{
var configuration = Service<DalamudConfiguration>.Get();

configuration.AutoUpdateBehavior = this.behavior;
configuration.SendUpdateNotificationToChat = this.chatNotification;
configuration.CheckPeriodicallyForUpdates = this.checkPeriodically;
configuration.PluginAutoUpdatePreferences = this.autoUpdatePreferences;

base.Save();
}
}
Loading

0 comments on commit 2e6cb6e

Please sign in to comment.