OSDN Git Service

63122c71a6ebd36c8c4220c21e3c7aad9dfd103f
[fooeditengine/FooEditEngine.git] / UWP / Test / MainViewModel.cs
1 using System;
2 using System.Collections.ObjectModel;
3 using System.ComponentModel;
4 using System.IO;
5 using System.Linq;
6 using System.Text;
7 using System.Threading.Tasks;
8 using FooEditEngine;
9 using FooEditEngine.UWP;
10 using Windows.Storage;
11
12 namespace Test
13 {
14     class MainViewModel : INotifyPropertyChanged
15     {
16         ObservableCollection<Document> _list = new ObservableCollection<Document>();
17
18         public MainViewModel()
19         {
20         }
21
22         public ObservableCollection<Document> DocumentList
23         {
24             get
25             {
26                 return this._list;
27             }
28         }
29
30         Document _currentDocument;
31         public Document CurrentDocument
32         {
33             get
34             {
35                 return this._currentDocument;
36             }
37             set
38             {
39                 this._currentDocument = value;
40                 this.OnPropertyChanged(this);
41                 if(this.CurrentDocumentChanged != null)
42                     this.CurrentDocumentChanged(this, null);
43             }
44         }
45
46         public event EventHandler CurrentDocumentChanged;
47         
48         public event PropertyChangedEventHandler PropertyChanged;
49
50         public void Initalize()
51         {
52             var complete_collection = new CompleteCollection<ICompleteItem>();
53             CompleteHelper.AddComleteWord(complete_collection, "int");
54             CompleteHelper.AddComleteWord(complete_collection, "float");
55             CompleteHelper.AddComleteWord(complete_collection, "double");
56             CompleteHelper.AddComleteWord(complete_collection, "char");
57             CompleteHelper.AddComleteWord(complete_collection, "byte");
58             CompleteHelper.AddComleteWord(complete_collection, "var");
59             CompleteHelper.AddComleteWord(complete_collection, "short");
60
61             var doc = new Document() { Title = "test1" };
62             doc.AutoComplete = new AutoCompleteBox(doc);
63             doc.AutoComplete.Items = complete_collection;
64             doc.AutoComplete.Enabled = true;
65             doc.ShowLineBreak = true;
66             _list.Add(doc);
67
68             doc = new Document() { Title = "test2" };
69             _list.Add(doc);
70
71             this.CurrentDocument = _list[0];
72         }
73
74         public void AddDocument()
75         {
76             var doc = new Document() { Title = "test" + _list.Count };
77             _list.Add(doc);
78             this.CurrentDocument = _list.Last();
79         }
80
81         public async Task AddDocumentFromFile(IStorageFile file)
82         {
83             if (file != null)
84             {
85                 var doc = new Document() { Title = "test" + _list.Count };
86                 doc.ShowLineBreak = true;
87                 doc.ShowFullSpace = true;
88                 doc.ShowTab = true;
89                 using (var ws = await file.OpenAsync(FileAccessMode.Read))
90                 using (var fs = new StreamReader(ws.AsStream()))
91                 {
92                     await doc.LoadAsync(fs, null);
93                 }
94                 doc.RequestRedraw();
95                 _list.Add(doc);
96                 this.CurrentDocument = _list.Last();
97             }
98         }
99
100         private void OnPropertyChanged(object sender, [System.Runtime.CompilerServices.CallerMemberName] string name = "")
101         {
102             if(this.PropertyChanged != null)
103                 this.PropertyChanged(sender, new PropertyChangedEventArgs(name));
104         }
105     }
106 }