timelapsenow/TimelapseExtensions/ClockExtension/ClockDialog.cs

154 lines
5.6 KiB
C#
Raw Permalink Normal View History

2022-05-21 15:37:43 +00:00
using System.Collections.ObjectModel;
using Eto.Forms;
using SixLabors.Fonts;
namespace ClockExtension
{
public class ClockDialog : Dialog
{
public ClockDialog(ClockData data,FontCollection collection,string extPath)
{
Button changeColor = new Button {Text="Change Color"};
changeColor.Click+=(sender,e)=>{
using(var ccd = new ColorDialog())
{
ccd.Color = data.ForeColor;
ccd.ColorChanged+=(sender,e)=>{
data.ForeColor=ccd.Color;
};
ccd.ShowDialog(this);
}
};
Button saveBtn = new Button {Text="Save"};
saveBtn.Click +=(sender,e)=>{
data.Save();
this.Close();
};
CheckBox enabled=new CheckBox{Text="Enable Clock"};
enabled.Checked = data.EnableClock;
enabled.CheckedChanged+=(sender,e)=>{
data.EnableClock=enabled.Checked.GetValueOrDefault();
};
NumericStepper fontSize=new NumericStepper{MinValue=6,MaxValue=65535,Value = data.FontSize,DecimalPlaces=2};
fontSize.ValueChanged+= (sender,e)=>{
data.FontSize = (float)fontSize.Value;
};
NumericStepper x=new NumericStepper {MinValue=0,MaxValue=65535,Value=data.X};
x.ValueChanged +=(sender,e)=>{
data.X=(int)x.Value;
};
NumericStepper y=new NumericStepper {MinValue=0,MaxValue=65535,Value=data.Y};
y.ValueChanged +=(sender,e)=>{
data.Y=(int)y.Value;
};
GroupBox gbox=new GroupBox{Text="Clock Position"};
DynamicLayout lytg=new DynamicLayout();
lytg.BeginVertical();
lytg.BeginHorizontal();
lytg.Add(x,true);
lytg.Add(y,true);
lytg.EndHorizontal();
lytg.EndVertical();
gbox.Content=lytg;
ObservableCollection<string> fonts = new ObservableCollection<string>();
Title="Clock Settings";
TextBox timeFormat=new TextBox();
timeFormat.PlaceholderText="yyyy/MM/dd HH:mm:ss";
timeFormat.Text=data.DateFormat;
timeFormat.TextChanged+=(sender,e)=>{
if(string.IsNullOrWhiteSpace(timeFormat.Text))
{
data.DateFormat=timeFormat.PlaceholderText;
}else{
data.DateFormat=timeFormat.Text;
}
};
DropDown down = new DropDown();
down.Tag = data;
down.DataStore = fonts;
Button browseFont=new Button {Text="Add Font"};
browseFont.Click +=(sender,e)=>{
using(var ofd=new OpenFileDialog())
{
ofd.MultiSelect=true;
ofd.Filters.Add(new FileFilter("True Type Font",".ttf"));
if(ofd.ShowDialog(this)==DialogResult.Ok)
{
foreach(var fname in ofd.Filenames){
string fontDir = Path.Combine(extPath,"Fonts");
Directory.CreateDirectory(fontDir);
string fontPath=Path.Combine(fontDir,Path.GetFileName(fname));
File.Copy(fname,fontPath);
collection.Add(fontPath);
}
}
}
DropDownRefresh(collection,fonts,down);
};
DynamicLayout lyt=new DynamicLayout();
lyt.BeginVertical();
lyt.BeginHorizontal();
lyt.Add(enabled);
lyt.EndHorizontal();
lyt.EndBeginVertical();
lyt.BeginHorizontal();
lyt.Add(down,true);
DropDownRefresh(collection,fonts,down);
if(data.FontIndex < fonts.Count)
{
down.SelectedIndex=data.FontIndex;
}else{
down.SelectedIndex=0;
}
lyt.Add(fontSize);
lyt.Add(browseFont);
lyt.EndHorizontal();
lyt.EndBeginVertical();
lyt.BeginHorizontal();
lyt.Add(timeFormat,true);
lyt.EndHorizontal();
lyt.EndBeginVertical();
lyt.BeginHorizontal();
lyt.Add(gbox,true);
lyt.EndHorizontal();
lyt.EndBeginVertical();
lyt.BeginHorizontal();
lyt.Add(changeColor,true);
lyt.Add(saveBtn,true);
lyt.EndHorizontal();
lyt.EndVertical();
this.Content=lyt;
this.Resizable=true;
}
public void SelectFont(object? sender,EventArgs e)
{
if(sender ==null)
{
return;
}
var dd=(DropDown)sender;
var coll=(ObservableCollection<string>)dd.DataStore;
var selectedFont=(ClockData)dd.Tag;
if(dd.SelectedIndex > -1){
selectedFont.FontName = coll[dd.SelectedIndex];
selectedFont.FontIndex=dd.SelectedIndex;
}
}
public void DropDownRefresh(FontCollection collection,ObservableCollection<string> fonts,DropDown dd)
{
dd.SelectedIndexChanged-=SelectFont;
fonts.Clear();
foreach(var font in collection.Families)
{
fonts.Add(font.Name);
}
dd.SelectedIndexChanged+=SelectFont;
}
}
}