Files
GeekDesk/MainWindow.xaml.cs

275 lines
8.1 KiB
C#
Raw Normal View History

2021-04-13 15:26:19 +08:00
using DraggAnimatedPanelExample;
using GalaSoft.MvvmLight;
using GeekDesk.Util;
using GeekDesk.ViewModel;
using System;
2021-04-12 13:46:05 +08:00
using System.Collections.Generic;
2021-04-13 15:26:19 +08:00
using System.IO;
2021-04-12 13:46:05 +08:00
using System.Windows;
2021-04-13 15:26:19 +08:00
using System.Windows.Controls;
2021-04-12 13:46:05 +08:00
using System.Windows.Input;
using System.Windows.Media.Imaging;
namespace GeekDesk
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
///
public partial class MainWindow : Window
{
2021-04-13 15:26:19 +08:00
private static AppData appData = CommonCode.GetAppData();
2021-04-12 13:46:05 +08:00
public MainWindow()
{
InitializeComponent();
2021-04-13 15:26:19 +08:00
loadData();
List<string> menuList = new List<string>();
Dictionary<string, List<IconInfo>> iconMap = new Dictionary<string, List<IconInfo>>();
2021-04-12 13:46:05 +08:00
//this.DataContext = mainModel;
//menu.Items = mainModel;
//System.Diagnostics.Process.Start(@"D:\SoftWare\WeGame\wegame.exe");
this.Loaded += Window_Loaded;
this.SizeChanged += MainWindow_Resize;
}
2021-04-13 15:26:19 +08:00
private void loadData()
{
this.DataContext = appData;
appData.MenuList.Add("Test1");
this.Width = appData.AppConfig.WindowWidth;
this.Height = appData.AppConfig.WindowHeight;
List<IconInfo> iconList;
if (appData.IconMap.ContainsKey("1"))
{
iconList = appData.IconMap["1"];
}
else
{
iconList = new List<IconInfo>();
appData.IconMap.Add("1", iconList);
}
icons.ItemsSource = iconList;
}
2021-04-12 13:46:05 +08:00
DelegateCommand<int[]> _swap;
public DelegateCommand<int[]> SwapCommand
{
get
{
if (_swap == null)
_swap = new DelegateCommand<int[]>(
(indexes) =>
{
int fromS = indexes[0];
int to = indexes[1];
2021-04-13 15:26:19 +08:00
var elementSource = icons.Items[to];
var dragged = icons.Items[fromS];
2021-04-12 13:46:05 +08:00
if (fromS > to)
{
2021-04-13 15:26:19 +08:00
icons.Items.Remove(dragged);
icons.Items.Insert(to, dragged);
2021-04-12 13:46:05 +08:00
}
else
{
2021-04-13 15:26:19 +08:00
icons.Items.Remove(dragged);
icons.Items.Insert(to, dragged);
2021-04-12 13:46:05 +08:00
}
}
);
return _swap;
}
}
DelegateCommand<int[]> _swap2;
public DelegateCommand<int[]> SwapCommand2
{
get
{
if (_swap2 == null)
_swap2 = new DelegateCommand<int[]>(
(indexes) =>
{
int fromS = indexes[0];
int to = indexes[1];
2021-04-13 15:26:19 +08:00
var elementSource = menus.Items[to];
var dragged = menus.Items[fromS];
2021-04-12 13:46:05 +08:00
if (fromS > to)
{
2021-04-13 15:26:19 +08:00
menus.Items.Remove(dragged);
menus.Items.Insert(to, dragged);
2021-04-12 13:46:05 +08:00
}
else
{
2021-04-13 15:26:19 +08:00
menus.Items.Remove(dragged);
menus.Items.Insert(to, dragged);
2021-04-12 13:46:05 +08:00
}
}
);
return _swap2;
}
}
private void Wrap_Drop(object sender, DragEventArgs e)
{
Array dropObject = (System.Array)e.Data.GetData(DataFormats.FileDrop);
if (dropObject == null) return;
2021-04-13 15:26:19 +08:00
foreach (object obj in dropObject)
2021-04-12 13:46:05 +08:00
{
2021-04-13 15:26:19 +08:00
string path = (string)obj;
if (File.Exists(path))
{
// 文件
BitmapImage bi = FileIcon.GetBitmapImage(path);
IconInfo iconInfo = new IconInfo();
iconInfo.Path = path;
iconInfo.BitmapImage = bi;
iconInfo.Name = Path.GetFileNameWithoutExtension(path);
List<IconInfo> iconList;
if (appData.IconMap.ContainsKey("1"))
{
iconList = appData.IconMap["1"];
}
else
{
iconList = new List<IconInfo>();
appData.IconMap.Add("1", iconList);
}
iconList.Add(iconInfo);
icons.ItemsSource = iconList;
CommonCode.SaveAppData(appData);
2021-04-12 13:46:05 +08:00
2021-04-13 15:26:19 +08:00
}
else if (Directory.Exists(path))
{
//文件夹
}
2021-04-12 13:46:05 +08:00
}
2021-04-13 15:26:19 +08:00
icons.Items.Refresh();
2021-04-12 13:46:05 +08:00
}
//菜单点击事件
private void menuClick(object sender, MouseButtonEventArgs e)
{
}
/// <summary>
/// 图标点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataClick(object sender, MouseButtonEventArgs e)
{
2021-04-13 15:26:19 +08:00
IconInfo icon = (IconInfo)((StackPanel)sender).Tag;
System.Diagnostics.Process.Start(icon.Path);
icon.Count++;
CommonCode.SaveAppData(appData);
2021-04-12 13:46:05 +08:00
}
/// <summary>
/// data选中事件 设置不可选中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void data_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
2021-04-13 15:26:19 +08:00
if (icons.SelectedIndex != -1) icons.SelectedIndex = -1;
2021-04-12 13:46:05 +08:00
}
#region Window_Loaded
void Window_Loaded(object sender, RoutedEventArgs e)
{
2021-04-13 15:26:19 +08:00
//this.menus.Items.Add(new ViewModel.Menu() { menu = "test1" });
//this.menus.Items.Add(new ViewModel.Menu() { menu = "test2" });
//this.menus.Items.Add(new ViewModel.Menu() { menu = "test3" });
2021-04-12 13:46:05 +08:00
}
#endregion // Window_Loaded
2021-04-13 15:26:19 +08:00
//#region Window_Closing
//void Window_Closing(object sender, CancelEventArgs e)
//{
// Rect rect = this.RestoreBounds;
// AppConfig config = this.DataContext as AppConfig;
// config.WindowWidth = rect.Width;
// config.WindowHeight = rect.Height;
// CommonCode.SaveAppConfig(config);
//}
//#endregion // Window_Closing
2021-04-12 13:46:05 +08:00
void MainWindow_Resize(object sender, System.EventArgs e)
{
if (this.DataContext != null)
{
2021-04-13 15:26:19 +08:00
AppData appData = this.DataContext as AppData;
appData.AppConfig.WindowWidth = this.Width;
appData.AppConfig.WindowHeight = this.Height;
CommonCode.SaveAppData(appData);
2021-04-12 13:46:05 +08:00
}
}
private void leftCard_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
}
private void deleteMenu(object sender, RoutedEventArgs e)
{
//if (data.SelectedIndex == -1)
//{
// return;
//}
ViewModel.Menu pojo = (ViewModel.Menu)((ContextMenu)((MenuItem)sender).Parent).DataContext;
string menuTitle = pojo.menu;
int index = 0;
2021-04-13 15:26:19 +08:00
foreach (object obj in menus.Items)
2021-04-12 13:46:05 +08:00
{
string test = ((ViewModel.Menu)obj).menu;
if (test == menuTitle)
{
2021-04-13 15:26:19 +08:00
menus.Items.RemoveAt(index);
menus.Items.Refresh();
2021-04-12 13:46:05 +08:00
return;
}
index++;
}
}
2021-04-13 15:26:19 +08:00
2021-04-12 13:46:05 +08:00
}
public class MainModel : ViewModelBase
{
public List<ViewModel.Menu> MenuList { get; set; }
2021-04-13 15:26:19 +08:00
public List<ViewModel.IconInfo> DataList { get; set; }
2021-04-12 13:46:05 +08:00
}
}