<commit_before>﻿using Playnite.Commands;
using Playnite.Common;
using Playnite.DesktopApp.Markup;
using Playnite.DesktopApp.ViewModels;
using Playnite.SDK;
using Playnite.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Playnite.DesktopApp.Controls
{
    public class TrayContextMenu : ContextMenu
    {
        private DesktopAppViewModel mainModel;

        static TrayContextMenu()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TrayContextMenu), new FrameworkPropertyMetadata(typeof(TrayContextMenu)));
        }

        public TrayContextMenu()
        {
        }

        public TrayContextMenu(DesktopAppViewModel model)
        {
            mainModel = model;
            mainModel.GamesEditor.PropertyChanged += GamesEditor_PropertyChanged;
            InitializeItems();
        }

        private void GamesEditor_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            InitializeItems();
        }

        private MenuItem AddMenuChild(
            ItemCollection parent,
            string locString,
            RelayCommand command,
            object commandParameter = null,
            object icon = null)
        {
            var item = new MenuItem
            {
                Command = command,
                CommandParameter = commandParameter
            };

            if (locString.StartsWith("LOC"))
            {
                item.SetResourceReference(MenuItem.HeaderProperty, locString);
            }
            else
            {
                item.Header = locString;
            }

            if (icon != null)
            {
                item.Icon = icon;
            }

            parent.Add(item);
            return item;
        }

        private void InitializeItems()
        {
            Items.Clear();
            foreach (var game in mainModel.GamesEditor.LastGames)
            {
                object icon = null;
                if (!game.Icon.IsNullOrEmpty())
                {
                    var path = mainModel.Database.GetFullFilePath(game.Icon);
                    if (File.Exists(path))
                    {
                        icon = Images.GetImageFromFile(path);
                    }
                }

                if (icon == null)
                {
                    icon = ResourceProvider.GetResource("DefaultGameIcon");
                }

                AddMenuChild(Items, game.Name, mainModel.StartGameCommand, game, icon);
            }
            
            Items.Add(new Separator());
            AddMenuChild(Items, "LOCOpenPlaynite", mainModel.ShowWindowCommand);

            var openClientItem = AddMenuChild(Items, "LOCMenuOpenClient", null);
            foreach (var tool in mainModel.ThirdPartyTools)
            {
                var item = new MenuItem
                {
                    Header = tool.Name,
                    Command = mainModel.ThirdPartyToolOpenCommand,
                    CommandParameter = tool
                };

                if (tool.Client?.Icon != null && File.Exists(tool.Client.Icon))
                {
                    item.Icon = Images.GetImageFromFile(tool.Client.Icon);
                }

                openClientItem.Items.Add(item);<commit_msg>Last played items in try don't have correct icon set<commit_after>﻿using Playnite.Commands;
using Playnite.Common;
using Playnite.DesktopApp.Markup;
using Playnite.DesktopApp.ViewModels;
using Playnite.SDK;
using Playnite.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Playnite.DesktopApp.Controls
{
    public class TrayContextMenu : ContextMenu
    {
        private DesktopAppViewModel mainModel;

        static TrayContextMenu()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TrayContextMenu), new FrameworkPropertyMetadata(typeof(TrayContextMenu)));
        }

        public TrayContextMenu()
        {
        }

        public TrayContextMenu(DesktopAppViewModel model)
        {
            mainModel = model;
            mainModel.GamesEditor.PropertyChanged += GamesEditor_PropertyChanged;
            InitializeItems();
        }

        private void GamesEditor_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(GamesEditor.LastGames))
            {
                InitializeItems();
            }
        }

        private MenuItem AddMenuChild(
            ItemCollection parent,
            string locString,
            RelayCommand command,
            object commandParameter = null,
            object icon = null)
        {
            var item = new MenuItem
            {
                Command = command,
                CommandParameter = commandParameter
            };

            if (locString.StartsWith("LOC"))
            {
                item.SetResourceReference(MenuItem.HeaderProperty, locString);
            }
            else
            {
                item.Header = locString;
            }

            if (icon != null)
            {
                item.Icon = icon;
            }

            parent.Add(item);
            return item;
        }

        private void InitializeItems()
        {
            Items.Clear();
            foreach (var game in mainModel.GamesEditor.LastGames)
            {
                object icon = null;
                if (!game.Icon.IsNullOrEmpty())
                {
                    var path = mainModel.Database.GetFullFilePath(game.Icon);
                    if (File.Exists(path))
                    {
                        icon = Images.GetImageFromFile(path);
                    }
                }

                if (icon == null)
                {
                    var resourceIcon = ResourceProvider.GetResource("DefaultGameIcon") as BitmapImage;
                    var image = new Image() { Source = resourceIcon };
                    RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality);
                    icon = image;
                }

                AddMenuChild(Items, game.Name, mainModel.StartGameCommand, game, icon);
            }
            
            Items.Add(new Separator());
            AddMenuChild(Items, "LOCOpenPlaynite", mainModel.ShowWindowCommand);

            var openClientItem = AddMenuChild(Items, "LOCMenuOpenClient", null);
            foreach (var tool in mainModel.ThirdPartyTools)
            {
                var item = new MenuItem
                {
                    Header = tool.Name,
                    Command = mainModel.ThirdPartyToolOpenCommand,
                    CommandParameter = tool
                };

                if (tool.Client?.Icon != null && File.Exists(tool.Client.Icon))
                {
                    item.Icon = Images.GetImageFromFile(tool.Client.Icon);
                }

                openClientItem.Items.Add(item);
{'fraction_non_alphanumeric': 0.07781097229766432, 'fraction_numerical': 0.0, 'mean_word_length': 1.5317978686833964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4819514', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1716, 'n_words': 352}