Files
GeekDesk/MainWindow.xaml.cs

360 lines
12 KiB
C#
Raw Normal View History

2021-04-13 15:26:19 +08:00
using DraggAnimatedPanelExample;
using GeekDesk.Util;
using GeekDesk.ViewModel;
using System;
2021-05-08 17:27:41 +08:00
using System.Collections;
2021-04-12 13:46:05 +08:00
using System.Collections.Generic;
2021-05-08 17:27:41 +08:00
using System.Collections.ObjectModel;
using System.ComponentModel;
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-05-08 17:27:41 +08:00
private AppData appData = CommonCode.GetAppDataByFile();
private int menuSelectIndexTemp = -1;
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;
2021-05-08 17:27:41 +08:00
//menus.ItemsSource = appData.MenuList;
appData.MenuList.Add(new MenuInfo() { MenuName = "test1", MenuId = "1", MenuEdit = (int)Visibility.Collapsed });
2021-04-13 15:26:19 +08:00
this.Width = appData.AppConfig.WindowWidth;
this.Height = appData.AppConfig.WindowHeight;
2021-05-08 17:27:41 +08:00
ObservableCollection<IconInfo> iconList;
2021-04-13 15:26:19 +08:00
if (appData.IconMap.ContainsKey("1"))
{
iconList = appData.IconMap["1"];
}
else
{
2021-05-08 17:27:41 +08:00
iconList = new ObservableCollection<IconInfo>();
2021-04-13 15:26:19 +08:00
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;
2021-05-08 17:27:41 +08:00
2021-04-12 13:46:05 +08:00
public DelegateCommand<int[]> SwapCommand2
{
get
{
if (_swap2 == null)
_swap2 = new DelegateCommand<int[]>(
(indexes) =>
{
int fromS = indexes[0];
int to = indexes[1];
2021-05-08 17:27:41 +08:00
ObservableCollection<MenuInfo> menuList = appData.MenuList;
var elementSource = menuList[to];
var dragged = menuList[fromS];
2021-04-12 13:46:05 +08:00
if (fromS > to)
{
2021-05-08 17:27:41 +08:00
menuList.Remove(dragged);
menuList.Insert(to, dragged);
2021-04-12 13:46:05 +08:00
}
else
{
2021-05-08 17:27:41 +08:00
menuList.Remove(dragged);
menuList.Insert(to, dragged);
2021-04-12 13:46:05 +08:00
}
2021-05-08 17:27:41 +08:00
appData.MenuList = menuList;
//menus.Items.Refresh();
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);
2021-05-08 17:27:41 +08:00
ObservableCollection<IconInfo> iconList;
2021-04-13 15:26:19 +08:00
if (appData.IconMap.ContainsKey("1"))
{
iconList = appData.IconMap["1"];
}
else
{
2021-05-08 17:27:41 +08:00
iconList = new ObservableCollection<IconInfo>();
2021-04-13 15:26:19 +08:00
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)
{
}
2021-05-08 17:27:41 +08:00
/// <summary>
/// 删除菜单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DeleteMenu(object sender, RoutedEventArgs e)
{
MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
appData.MenuList.Remove(menuInfo);
CommonCode.SaveAppData(appData);
}
private void StackPanel_MouseMove(object sender, MouseEventArgs e)
2021-04-12 13:46:05 +08:00
{
2021-05-08 17:27:41 +08:00
UIElementCollection childs = ((StackPanel)sender).Children;
IEnumerator iEnumerator = childs.GetEnumerator();
//((Image)iEnumerator.Current).Style;
}
/// <summary>
/// 重命名菜单 将textbox 设置为可见
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RenameMenu(object sender, RoutedEventArgs e)
{
MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
menuInfo.MenuEdit = (int)Visibility.Visible;
}
/// <summary>
/// 编辑菜单失焦或者敲下Enter键时保存修改后的菜单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LostFocusOrEnterDown(object sender, EventArgs e)
{
TextBox menuBox = null;
if (e.GetType() == typeof(KeyEventArgs))
2021-04-12 13:46:05 +08:00
{
2021-05-08 17:27:41 +08:00
KeyEventArgs eKey = e as KeyEventArgs;
if (eKey.Key == Key.Enter)
2021-04-12 13:46:05 +08:00
{
2021-05-08 17:27:41 +08:00
menuBox = ((TextBox)sender);
2021-04-12 13:46:05 +08:00
}
2021-05-08 17:27:41 +08:00
} else if(e.GetType() == typeof(RoutedEventArgs))
{
menuBox = ((TextBox)sender);
2021-04-12 13:46:05 +08:00
}
2021-05-08 17:27:41 +08:00
if (menuBox != null)
{
MenuInfo menuInfo = menuBox.Tag as MenuInfo;
string text = menuBox.Text;
menuInfo.MenuName = text;
menuInfo.MenuEdit = (int)Visibility.Collapsed;
CommonCode.SaveAppData(appData);
}
2021-04-12 13:46:05 +08:00
}
2021-05-08 17:27:41 +08:00
/// <summary>
/// 当修改菜单元素可见时 设置全选并获得焦点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuEditWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
{
TextBox box = sender as TextBox;
if (box.Visibility == Visibility.Visible)
{
Keyboard.Focus(box);
box.SelectAll();
}
}
2021-04-13 15:26:19 +08:00
2021-05-08 17:27:41 +08:00
/// <summary>
/// 当修改菜单元素可见时 设置原菜单为不可见 并且不可选中
/// 修改菜单元素不可见时 原菜单可见 并 选中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
{
TextBlock tb = sender as TextBlock;
if (tb.Visibility == Visibility.Collapsed)
{
if (menus.SelectedIndex != -1)
{
menuSelectIndexTemp = menus.SelectedIndex;
menus.SelectedIndex = -1;
} else
{
menus.SelectedIndex = menuSelectIndexTemp;
}
}
}
2021-04-12 13:46:05 +08:00
2021-05-08 17:27:41 +08:00
/// <summary>
/// 新建菜单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CreateMenu(object sender, RoutedEventArgs e)
{
appData.MenuList.Add(new MenuInfo() { MenuEdit = (int)Visibility.Collapsed, MenuId = "zz", MenuName = "NewGouop" });
menus.SelectedIndex = appData.MenuList.Count - 1;
//appData.MenuList[appData.MenuList.Count - 1].MenuEdit = (int)Visibility.Visible;
CommonCode.SaveAppData(appData);
}
2021-04-12 13:46:05 +08:00
}
}