Fix main window stealing focus after dropping queue videos

This commit is contained in:
N00MKRAD
2025-02-06 22:09:06 +01:00
parent 05cb41d818
commit 3cb7b155ba
4 changed files with 66 additions and 34 deletions

View File

@@ -285,29 +285,48 @@ namespace Flowframes
return filePath.IsConcatFile() ? $"{rateStr}-safe 0 -f concat " : ""; return filePath.IsConcatFile() ? $"{rateStr}-safe 0 -f concat " : "";
} }
public static string Get(this Dictionary<string, string> dict, string key, bool returnKeyInsteadOfEmptyString = false, bool ignoreCase = false) public static string Get<TKey>(this Dictionary<TKey, string> dict, string key, bool returnKeyInsteadOfEmptyString = false, bool ignoreCase = false)
{ {
if (key == null) if (key == null)
key = ""; key = "";
// Safely handle a null dictionary
for (int i = 0; i < (dict == null ? 0 : dict.Count); i++) for (int i = 0; i < (dict == null ? 0 : dict.Count); i++)
{ {
var elementKey = dict.ElementAt(i).Key;
var elementValue = dict.ElementAt(i).Value;
// Convert the dictionary key to string (handling null)
string dictKeyString = (elementKey == null ? "" : elementKey.ToString());
if (ignoreCase) if (ignoreCase)
{ {
if (key.Lower() == dict.ElementAt(i).Key.Lower()) if (key.Lower() == dictKeyString.Lower())
return dict.ElementAt(i).Value; return elementValue;
} }
else else
{ {
if (key == dict.ElementAt(i).Key) if (key == dictKeyString)
return dict.ElementAt(i).Value; return elementValue;
} }
} }
if (returnKeyInsteadOfEmptyString) return returnKeyInsteadOfEmptyString ? key : "";
return key; }
else
return ""; /// <summary> Get a value from a dictionary, returns <paramref name="fallback"/> if not found. For strings, the default fallback is an empty string instead of null. </summary>
public static TValue Get<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue fallback = default)
{
// For string values, use empty string instead of null as default value
if (typeof(TValue) == typeof(string) && fallback != null)
{
fallback = (TValue)(object)string.Empty;
}
if (dictionary == null)
return fallback;
return dictionary.TryGetValue(key, out var value) ? value : fallback;
} }
public static void FillFromEnum<TEnum>(this ComboBox comboBox, Dictionary<string, string> stringMap = null, int defaultIndex = -1, List<TEnum> exclusionList = null, bool useKeyNames = false) where TEnum : Enum public static void FillFromEnum<TEnum>(this ComboBox comboBox, Dictionary<string, string> stringMap = null, int defaultIndex = -1, List<TEnum> exclusionList = null, bool useKeyNames = false) where TEnum : Enum

View File

@@ -7,6 +7,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using Flowframes.Data; using Flowframes.Data;
using static Flowframes.Ui.ControlExtensions;
namespace Flowframes.Forms namespace Flowframes.Forms
{ {
@@ -25,7 +26,7 @@ namespace Flowframes.Forms
RefreshGui(); RefreshGui();
} }
public void RefreshGui () public void RefreshGui()
{ {
taskList.Items.Clear(); taskList.Items.Clear();
@@ -34,22 +35,22 @@ namespace Flowframes.Forms
InterpSettings entry = Program.batchQueue.ElementAt(i); InterpSettings entry = Program.batchQueue.ElementAt(i);
string outFormat = Strings.OutputFormat.Get(entry.outSettings.Format.ToString()); string outFormat = Strings.OutputFormat.Get(entry.outSettings.Format.ToString());
string inPath = string.IsNullOrWhiteSpace(entry.inPath) ? "No Path" : Path.GetFileName(entry.inPath).Trunc(40); string inPath = string.IsNullOrWhiteSpace(entry.inPath) ? "No Path" : Path.GetFileName(entry.inPath).Trunc(40);
string str = $"#{i+1}: {inPath} - {entry.inFps.Float} FPS => {entry.interpFactor}x {entry.ai.NameShort} ({entry.model.Name}) => {outFormat}"; string str = $"#{i + 1}: {inPath} - {entry.inFps.Float} FPS => {entry.interpFactor}x {entry.ai.NameShort} ({entry.model.Name}) => {outFormat}";
taskList.Items.Add(str); taskList.Items.Add(str);
} }
} }
private void RefreshIndex () private void RefreshIndex()
{ {
for(int i = 0; i < taskList.Items.Count; i++) for (int i = 0; i < taskList.Items.Count; i++)
{ {
string[] split = taskList.Items[i].ToString().Split(':'); string[] split = taskList.Items[i].ToString().Split(':');
split[0] = $"#{i+1}"; split[0] = $"#{i + 1}";
taskList.Items[i] = string.Join(":", split); taskList.Items[i] = string.Join(":", split);
} }
} }
public void SetWorking (bool working) public void SetWorking(bool working)
{ {
runBtn.Enabled = !working; runBtn.Enabled = !working;
addToQueue.Enabled = !working; addToQueue.Enabled = !working;
@@ -119,7 +120,7 @@ namespace Flowframes.Forms
Queue<InterpSettings> temp = new Queue<InterpSettings>(); Queue<InterpSettings> temp = new Queue<InterpSettings>();
for(int i = 0; i < Program.batchQueue.Count; i++) for (int i = 0; i < Program.batchQueue.Count; i++)
{ {
if (i != taskList.SelectedIndex) if (i != taskList.SelectedIndex)
temp.Enqueue(Program.batchQueue.ElementAt(i)); temp.Enqueue(Program.batchQueue.ElementAt(i));
@@ -146,7 +147,15 @@ namespace Flowframes.Forms
await LoadDroppedPaths(droppedPaths); await LoadDroppedPaths(droppedPaths);
} }
public async Task LoadDroppedPaths (string[] droppedPaths, bool start = false) public async Task LoadDroppedPaths(string[] droppedPaths, bool start = false)
{
if (droppedPaths == null || droppedPaths.Length < 1)
return;
var prevOpacity = Program.mainForm.Opacity;
Program.mainForm.Invoke(() => Program.mainForm.Opacity = 0f);
try
{ {
foreach (string path in droppedPaths) foreach (string path in droppedPaths)
{ {
@@ -156,7 +165,7 @@ namespace Flowframes.Forms
current.inFpsDetected = await IoUtils.GetFpsFolderOrVideo(path); current.inFpsDetected = await IoUtils.GetFpsFolderOrVideo(path);
current.inFps = current.inFpsDetected; current.inFps = current.inFpsDetected;
if(current.inFps.Float <= 0) if (current.inFps.Float <= 0)
current.inFps = InterpolateUtils.AskForFramerate(new DirectoryInfo(path).Name, false); current.inFps = InterpolateUtils.AskForFramerate(new DirectoryInfo(path).Name, false);
current.outFps = current.inFps * current.interpFactor; current.outFps = current.inFps * current.interpFactor;
@@ -164,6 +173,14 @@ namespace Flowframes.Forms
Program.batchQueue.Enqueue(current); Program.batchQueue.Enqueue(current);
RefreshGui(); RefreshGui();
} }
}
catch (Exception ex)
{
Logger.Log($"BatchForm: Error while loading dropped paths: {ex.Message}", true);
}
Program.mainForm.Invoke(() => Program.mainForm.Opacity = prevOpacity);
BringToFront();
if (start) if (start)
runBtn_Click(null, null); runBtn_Click(null, null);

View File

@@ -1,5 +1,4 @@
using Flowframes; using Flowframes.Media;
using Flowframes.Media;
using Flowframes.Data; using Flowframes.Data;
using Flowframes.IO; using Flowframes.IO;
using Flowframes.Magick; using Flowframes.Magick;
@@ -17,7 +16,6 @@ using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using Padding = Flowframes.Data.Padding; using Padding = Flowframes.Data.Padding;
using Utils = Flowframes.Main.InterpolateUtils; using Utils = Flowframes.Main.InterpolateUtils;
using System.Drawing.Imaging;
namespace Flowframes namespace Flowframes
{ {

View File

@@ -1,8 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Flowframes.MiscUtils namespace Flowframes.MiscUtils
{ {