PowerToys/Wox/ViewModel/ResultItemViewModel.cs

191 lines
4.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wox.Core.Plugin;
using Wox.Core.Resource;
using Wox.Infrastructure;
using Wox.Infrastructure.Hotkey;
using Wox.Plugin;
using Wox.Storage;
namespace Wox.ViewModel
{
public class ResultItemViewModel : BaseViewModel
{
#region Private Fields
private Result _result;
private bool _isSelected;
#endregion
#region Constructor
public ResultItemViewModel(Result result)
{
if(null!= result)
{
this._result = result;
2016-02-12 14:21:12 +08:00
this.OpenResultCommand = new RelayCommand((parameter) => {
bool hideWindow = result.Action(new ActionContext
{
SpecialKeyState = GlobalHotkey.Instance.CheckModifiers()
});
if (hideWindow)
{
App.API.HideApp();
UserSelectedRecordStorage.Instance.Add(this._result);
QueryHistoryStorage.Instance.Add(this._result.OriginQuery.RawQuery);
}
});
this.OpenResultActionPanelCommand = new RelayCommand((parameter) =>
{
var actions = PluginManager.GetContextMenusForPlugin(result);
var pluginMetaData = PluginManager.GetPluginForId(result.PluginID).Metadata;
actions.ForEach(o =>
{
o.PluginDirectory = pluginMetaData.PluginDirectory;
o.PluginID = result.PluginID;
o.OriginQuery = result.OriginQuery;
});
actions.Add(GetTopMostContextMenu(result));
App.API.ShowContextMenu(pluginMetaData, actions);
});
}
}
#endregion
#region ViewModel Properties
public string Title
{
get
{
return this._result.Title;
}
}
public string SubTitle
{
get
{
return this._result.SubTitle;
}
}
public string FullIcoPath
{
get
{
return this._result.FullIcoPath;
}
}
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
OnPropertyChanged("IsSelected");
}
}
public RelayCommand OpenResultCommand
{
get;
set;
}
public RelayCommand OpenResultActionPanelCommand
{
get;
set;
}
#endregion
#region Properties
public Result RawResult
{
get
{
return this._result;
}
}
#endregion
#region Private Methods
private Result GetTopMostContextMenu(Result result)
{
if (TopMostRecordStorage.Instance.IsTopMost(result))
{
return new Result(InternationalizationManager.Instance.GetTranslation("cancelTopMostInThisQuery"), "Images\\down.png")
{
PluginDirectory = WoxDirectroy.Executable,
Action = _ =>
{
TopMostRecordStorage.Instance.Remove(result);
App.API.ShowMsg("Succeed", "", "");
return false;
}
};
}
else
{
return new Result(InternationalizationManager.Instance.GetTranslation("setAsTopMostInThisQuery"), "Images\\up.png")
{
PluginDirectory = WoxDirectroy.Executable,
Action = _ =>
{
TopMostRecordStorage.Instance.AddOrUpdate(result);
App.API.ShowMsg("Succeed", "", "");
return false;
}
};
}
}
#endregion
2016-02-18 22:53:41 +08:00
public override bool Equals(object obj)
{
ResultItemViewModel r = obj as ResultItemViewModel;
if (r != null)
{
return _result.Equals(r.RawResult);
2016-02-18 22:53:41 +08:00
}
return false;
}
public override int GetHashCode()
{
return _result.GetHashCode();
}
public override string ToString()
{
return _result.ToString();
2016-02-18 22:53:41 +08:00
}
}
}