跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

ChartsItemTypeList.cs

206 行 · 6.2 KB历史下载
1using Core.Librarys.Image;
2using System;
3using System.Collections.Generic;
4using System.Diagnostics;
5using System.IO;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Windows;
10using System.Windows.Controls;
11using System.Windows.Data;
12using System.Windows.Documents;
13using System.Windows.Input;
14using System.Windows.Media;
15using System.Windows.Media.Imaging;
16using System.Windows.Navigation;
17using System.Windows.Shapes;
18using UI.Controls.Charts.Model;
19
20namespace UI.Controls.Charts
21{
22 public class ChartsItemTypeList : Control
23 {
24 #region ChartsType 样式类型
25 /// <summary>
26 /// 样式类型
27 /// </summary>
28 public ChartsDataModel Data
29 {
30 get { return (ChartsDataModel)GetValue(DataProperty); }
31 set { SetValue(DataProperty, value); }
32 }
33 public static readonly DependencyProperty DataProperty =
34 DependencyProperty.Register("Data",
35 typeof(ChartsDataModel),
36 typeof(ChartsItemTypeList));
37
38
39 #endregion
40
41 #region MaxValue 最大值
42 /// <summary>
43 /// 样式类型
44 /// </summary>
45 public double MaxValue
46 {
47 get { return (double)GetValue(MaxValueProperty); }
48 set { SetValue(MaxValueProperty, value); }
49 }
50 public static readonly DependencyProperty MaxValueProperty =
51 DependencyProperty.Register("MaxValue",
52 typeof(double),
53 typeof(ChartsItemTypeList));
54
55
56 #endregion
57
58 #region IsLoading 是否正在加载中
59 /// <summary>
60 /// 是否正在加载中
61 /// </summary>
62 public bool IsLoading
63 {
64 get { return (bool)GetValue(IsLoadingProperty); }
65 set { SetValue(IsLoadingProperty, value); }
66 }
67 public static readonly DependencyProperty IsLoadingProperty =
68 DependencyProperty.Register("IsLoading",
69 typeof(bool),
70 typeof(ChartsItemTypeList));
71
72
73 #endregion
74
75 #region IsShowBadge 是否显示徽章
76 /// <summary>
77 /// 是否显示徽章
78 /// </summary>
79 public bool IsShowBadge
80 {
81 get { return (bool)GetValue(IsShowBadgeProperty); }
82 set { SetValue(IsShowBadgeProperty, value); }
83 }
84 public static readonly DependencyProperty IsShowBadgeProperty =
85 DependencyProperty.Register("IsShowBadge",
86 typeof(bool),
87 typeof(ChartsItemTypeList));
88
89
90 #endregion
91
92 #region 图标大小
93 /// <summary>
94 /// 图标大小
95 /// </summary>
96 public double IconSize
97 {
98 get { return (double)GetValue(IconSizeProperty); }
99 set { SetValue(IconSizeProperty, value); }
100 }
101 public static readonly DependencyProperty IconSizeProperty =
102 DependencyProperty.Register("IconSize",
103 typeof(double),
104 typeof(ChartsItemTypeList), new PropertyMetadata((double)25));
105
106
107 #endregion
108 private TextBlock NameTextObj, ValueTextObj;
109 private Rectangle ValueBlockObj;
110 private StackPanel ValueContainer;
111 //private Grid ValueContainer;
112 private Image IconObj;
113 private bool isRendering = false;
114 private bool IsAddEvent = false;
115 public ChartsItemTypeList()
116 {
117 DefaultStyleKey = typeof(ChartsItemTypeList);
118 Unloaded += ChartsItemTypeList_Unloaded;
119 }
120
121 private void ChartsItemTypeList_Unloaded(object sender, RoutedEventArgs e)
122 {
123 Unloaded -= ChartsItemTypeList_Unloaded;
124 Loaded -= ChartsItemTypeList_Loaded;
125 }
126
127 public override void OnApplyTemplate()
128 {
129 base.OnApplyTemplate();
130 NameTextObj = GetTemplateChild("NameTextObj") as TextBlock;
131 ValueTextObj = GetTemplateChild("ValueTextObj") as TextBlock;
132 ValueBlockObj = GetTemplateChild("ValueBlockObj") as Rectangle;
133 //ValueContainer = GetTemplateChild("ValueContainer") as Grid;
134 ValueContainer = GetTemplateChild("ValueContainer") as StackPanel;
135
136 IconObj = GetTemplateChild("IconObj") as Image;
137
138 if (!IsAddEvent)
139 {
140 Loaded += ChartsItemTypeList_Loaded;
141 IsAddEvent = true;
142 }
143
144 var parent = Parent as FrameworkElement;
145 parent.SizeChanged += Parent_SizeChanged;
146 }
147
148 private void Parent_SizeChanged(object sender, SizeChangedEventArgs e)
149 {
150 UpdateValueBlockWidth();
151 }
152
153 private void ChartsItemTypeList_Loaded(object sender, RoutedEventArgs e)
154 {
155 Render();
156 }
157
158 private void Render()
159 {
160 if (isRendering || Data == null)
161 {
162 return;
163 }
164 isRendering = true;
165 //NameTextObj.Text = Data.Name;
166 //对部分程序未获取程序名的程序使用路径中名字作为程序名
167 //if (Data.Name.Trim() == "" && Data.PopupText.Trim() != "")
168 //{
169 // FileInfo fi = new FileInfo(Data.PopupText);
170 // NameTextObj.Text = fi.Name.Replace(fi.Extension, "");
171 //}
172
173 ValueTextObj.Text = Data.Tag;
174 IconObj.Source = Imager.Load(Data.Icon);
175
176 ValueTextObj.SizeChanged += (e, c) =>
177 {
178 if (MaxValue <= 0)
179 {
180 return;
181 }
182 UpdateValueBlockWidth();
183 };
184 }
185
186 public void UpdateValueBlockWidth()
187 {
188 if (Data == null || !IsLoaded)
189 {
190 return;
191 }
192 ValueBlockObj.Width = (Data.Value / MaxValue) * (ValueContainer.ActualWidth * 0.95 - ValueTextObj.ActualWidth);
193 }
194
195 protected override void OnMouseEnter(MouseEventArgs e)
196 {
197 base.OnMouseEnter(e);
198 VisualStateManager.GoToState(this, "MouseOver", true);
199 }
200 protected override void OnMouseLeave(MouseEventArgs e)
201 {
202 base.OnMouseLeave(e);
203 VisualStateManager.GoToState(this, "NormalState", true);
204 }
205 }
206}